generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomClaims.ts
84 lines (64 loc) · 2.19 KB
/
customClaims.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { BigNumber, Polymesh } from '@polymeshassociation/polymesh-sdk';
import {
ClaimTarget,
ClaimType,
FungibleAsset,
ScopeType,
} from '@polymeshassociation/polymesh-sdk/types';
import assert from 'node:assert';
import { awaitMiddlewareSynced, randomString } from '~/util';
export const manageCustomClaims = async (
sdk: Polymesh,
targetDid: string,
asset: FungibleAsset
): Promise<void> => {
const identity = await sdk.getSigningIdentity();
assert(identity);
const { account: signingAccount } = await identity.getPrimaryAccount();
const name = randomString(12);
// Prepare and run the add claim transaction
const registerCustomClaimTypeTx = await sdk.claims.registerCustomClaimType(
{ name },
{ signingAccount }
);
const customClaimTypeId = await registerCustomClaimTypeTx.run();
await awaitMiddlewareSynced(registerCustomClaimTypeTx, sdk);
const registeredCustomClaimTypes = await sdk.claims.getAllCustomClaimTypes();
assert(
registeredCustomClaimTypes.count?.isGreaterThanOrEqualTo(1),
'There should be at least one registered CustomClaimType'
);
assert(registerCustomClaimTypeTx.isSuccess, 'Should register a CustomClaimType');
assert(BigNumber.isBigNumber(customClaimTypeId), 'CustomClaimTypeId should be BigNumber');
const customClaimTypeById = await sdk.claims.getCustomClaimTypeById(customClaimTypeId);
assert(
customClaimTypeById?.name === name,
'Retrieved CustomClaimType name should equal the one provided'
);
const customClaim: Omit<ClaimTarget, 'expiry'> = {
target: targetDid,
claim: {
type: ClaimType.Custom,
customClaimTypeId,
scope: {
type: ScopeType.Asset,
value: asset.id,
},
},
};
const addClaimTx = await sdk.claims.addClaims(
{
claims: [customClaim],
},
{ signingAccount }
);
await addClaimTx.run();
assert(addClaimTx.isSuccess, 'Should be able to add a custom claim');
await awaitMiddlewareSynced(addClaimTx, sdk);
const revokeClaimTx = await sdk.claims.revokeClaims(
{ claims: [customClaim] },
{ signingAccount }
);
await revokeClaimTx.run();
assert(revokeClaimTx.isSuccess, 'Should be able to revoke a custom claim');
};