generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathCustomPermissionGroup.ts
118 lines (94 loc) · 2.89 KB
/
CustomPermissionGroup.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import BigNumber from 'bignumber.js';
import { Context, PermissionGroup, setGroupPermissions } from '~/internal';
import { GroupPermissions, ProcedureMethod, SetGroupPermissionsParams } from '~/types';
import {
assetToMeshAssetId,
bigNumberToU32,
extrinsicPermissionsToTransactionPermissions,
transactionPermissionsToTxGroups,
u32ToBigNumber,
} from '~/utils/conversion';
import { createProcedureMethod, toHumanReadable } from '~/utils/internal';
export interface HumanReadable {
id: string;
assetId: string;
}
export interface UniqueIdentifiers {
id: BigNumber;
assetId: string;
}
/**
* Represents a group of custom permissions for an Asset
*/
export class CustomPermissionGroup extends PermissionGroup {
/**
* @hidden
* Check if a value is of type {@link UniqueIdentifiers}
*/
public static override isUniqueIdentifiers(identifier: unknown): identifier is UniqueIdentifiers {
const { id, assetId } = identifier as UniqueIdentifiers;
return id instanceof BigNumber && typeof assetId === 'string';
}
public id: BigNumber;
/**
* @hidden
*/
public constructor(identifiers: UniqueIdentifiers, context: Context) {
super(identifiers, context);
const { id } = identifiers;
this.id = id;
this.setPermissions = createProcedureMethod(
{ getProcedureAndArgs: args => [setGroupPermissions, { group: this, ...args }] },
context
);
}
/**
* Modify the group's permissions
*/
public setPermissions: ProcedureMethod<SetGroupPermissionsParams, void>;
/**
* Retrieve the list of permissions and transaction groups associated with this Permission Group
*/
public async getPermissions(): Promise<GroupPermissions> {
const {
context: {
polymeshApi: {
query: { externalAgents },
},
},
context,
asset,
id,
} = this;
const rawAssetId = assetToMeshAssetId(asset, context);
const rawAgId = bigNumberToU32(id, context);
const rawGroupPermissions = await externalAgents.groupPermissions(rawAssetId, rawAgId);
const transactions = extrinsicPermissionsToTransactionPermissions(rawGroupPermissions.unwrap());
const transactionGroups = transactionPermissionsToTxGroups(transactions);
return {
transactions,
transactionGroups,
};
}
/**
* Determine whether this Custom Permission Group exists on chain
*/
public async exists(): Promise<boolean> {
const { asset, id, context } = this;
const rawAssetId = assetToMeshAssetId(asset, context);
const currentId = await context.polymeshApi.query.externalAgents.agIdSequence(rawAssetId);
// 1 <= id <= currentId
return u32ToBigNumber(currentId).gte(id) && id.gte(1);
}
/**
* Return the Group's static data
*/
public toHuman(): HumanReadable {
const { id, asset } = this;
return toHumanReadable({
id,
ticker: asset,
assetId: asset,
});
}
}