generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
AccountManagement.ts
308 lines (279 loc) · 11 KB
/
AccountManagement.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import { MultiSig } from '~/api/entities/Account/MultiSig';
import {
acceptPrimaryKeyRotation,
Account,
AuthorizationRequest,
Context,
createMultiSigAccount,
inviteAccount,
leaveIdentity,
modifySignerPermissions,
modifySignerPermissionsStorage,
removeSecondaryAccounts,
subsidizeAccount,
Subsidy,
toggleFreezeSecondaryAccounts,
} from '~/internal';
import {
AcceptPrimaryKeyRotationParams,
AccountBalance,
CreateMultiSigParams,
InviteAccountParams,
ModifySignerPermissionsParams,
NoArgsProcedureMethod,
PermissionType,
ProcedureMethod,
RemoveSecondaryAccountsParams,
SubCallback,
SubsidizeAccountParams,
UnsubCallback,
} from '~/types';
import { stringToAccountId } from '~/utils/conversion';
import { asAccount, assertAddressValid, createProcedureMethod } from '~/utils/internal';
/**
* Handles functionality related to Account Management
*/
export class AccountManagement {
private context: Context;
/**
* @hidden
*/
constructor(context: Context) {
this.context = context;
this.leaveIdentity = createProcedureMethod(
{ getProcedureAndArgs: () => [leaveIdentity, undefined], voidArgs: true },
context
);
this.removeSecondaryAccounts = createProcedureMethod(
{
getProcedureAndArgs: args => [removeSecondaryAccounts, { ...args }],
},
context
);
this.revokePermissions = createProcedureMethod<
{ secondaryAccounts: (string | Account)[] },
ModifySignerPermissionsParams,
void,
modifySignerPermissionsStorage
>(
{
getProcedureAndArgs: args => {
const { secondaryAccounts } = args;
const accounts = secondaryAccounts.map(account => {
return {
account,
permissions: {
tokens: { type: PermissionType.Include, values: [] },
transactions: { type: PermissionType.Include, values: [] },
portfolios: { type: PermissionType.Include, values: [] },
},
};
});
return [modifySignerPermissions, { secondaryAccounts: accounts }];
},
},
context
);
this.modifyPermissions = createProcedureMethod(
{ getProcedureAndArgs: args => [modifySignerPermissions, { ...args }] },
context
);
this.inviteAccount = createProcedureMethod(
{ getProcedureAndArgs: args => [inviteAccount, { ...args }] },
context
);
this.freezeSecondaryAccounts = createProcedureMethod(
{
getProcedureAndArgs: () => [toggleFreezeSecondaryAccounts, { freeze: true }],
voidArgs: true,
},
context
);
this.unfreezeSecondaryAccounts = createProcedureMethod(
{
getProcedureAndArgs: () => [toggleFreezeSecondaryAccounts, { freeze: false }],
voidArgs: true,
},
context
);
this.subsidizeAccount = createProcedureMethod(
{ getProcedureAndArgs: args => [subsidizeAccount, { ...args }] },
context
);
this.createMultiSigAccount = createProcedureMethod(
{ getProcedureAndArgs: args => [createMultiSigAccount, args] },
context
);
this.acceptPrimaryKey = createProcedureMethod(
{ getProcedureAndArgs: args => [acceptPrimaryKeyRotation, args] },
context
);
}
/**
* Disassociate the signing Account from its Identity. This operation can only be done if the signing Account is a secondary Account
*/
public leaveIdentity: NoArgsProcedureMethod<void>;
/**
* Remove a list of secondary Accounts associated with the signing Identity
*/
public removeSecondaryAccounts: ProcedureMethod<RemoveSecondaryAccountsParams, void>;
/**
* Revoke all permissions of a list of secondary Accounts associated with the signing Identity
*
* @throws if the signing Account is not the primary Account of the Identity whose secondary Account permissions are being revoked
*/
public revokePermissions: ProcedureMethod<{ secondaryAccounts: (string | Account)[] }, void>;
/**
* Modify all permissions of a list of secondary Accounts associated with the signing Identity
*
* @throws if the signing Account is not the primary Account of the Identity whose secondary Account permissions are being modified
*/
public modifyPermissions: ProcedureMethod<ModifySignerPermissionsParams, void>;
/**
* Send an invitation to an Account to join the signing Identity as a secondary Account
*
* @note this will create an {@link api/entities/AuthorizationRequest!AuthorizationRequest | Authorization Request} which has to be accepted by the `targetAccount`.
* An {@link api/entities/Account!Account} or {@link api/entities/Identity!Identity} can fetch its pending Authorization Requests by calling {@link api/entities/common/namespaces/Authorizations!Authorizations.getReceived | authorizations.getReceived}.
* Also, an Account or Identity can directly fetch the details of an Authorization Request by calling {@link api/entities/common/namespaces/Authorizations!Authorizations.getOne | authorizations.getOne}
*/
public inviteAccount: ProcedureMethod<InviteAccountParams, AuthorizationRequest>;
/**
* Freeze all of the secondary Accounts in the signing Identity. This means revoking their permission to perform any operation on the blockchain and freezing their funds until the Accounts are unfrozen via {@link unfreezeSecondaryAccounts}
*/
public freezeSecondaryAccounts: NoArgsProcedureMethod<void>;
/**
* Unfreeze all of the secondary Accounts in the signing Identity. This will restore their permissions as they were before being frozen
*/
public unfreezeSecondaryAccounts: NoArgsProcedureMethod<void>;
/**
* Send an Authorization Request to an Account to subsidize its transaction fees
*
* @note this will create an {@link api/entities/AuthorizationRequest!AuthorizationRequest | Authorization Request} which has to be accepted by the `beneficiary` Account.
* An {@link api/entities/Account!Account} or {@link api/entities/Identity!Identity} can fetch its pending Authorization Requests by calling {@link api/entities/common/namespaces/Authorizations!Authorizations.getReceived | authorizations.getReceived}.
* Also, an Account or Identity can directly fetch the details of an Authorization Request by calling {@link api/entities/common/namespaces/Authorizations!Authorizations.getOne | authorizations.getOne}
*/
public subsidizeAccount: ProcedureMethod<SubsidizeAccountParams, AuthorizationRequest>;
/**
* Create a MultiSig Account
*
* @note this will create an {@link api/entities/AuthorizationRequest!AuthorizationRequest | Authorization Request} for each signing Account which will have to be accepted before they can approve transactions. None of the signing Accounts can be associated with an Identity when accepting the Authorization
* An {@link api/entities/Account!Account} or {@link api/entities/Identity!Identity} can fetch its pending Authorization Requests by calling {@link api/entities/common/namespaces/Authorizations!Authorizations.getReceived | authorizations.getReceived}.
* Also, an Account or Identity can directly fetch the details of an Authorization Request by calling {@link api/entities/common/namespaces/Authorizations!Authorizations.getOne | authorizations.getOne}
*/
public createMultiSigAccount: ProcedureMethod<CreateMultiSigParams, MultiSig>;
/**
* Get the free/locked POLYX balance of an Account
*
* @param args.account - defaults to the signing Account
*
* @note can be subscribed to
*/
public getAccountBalance(args?: { account: string | Account }): Promise<AccountBalance>;
public getAccountBalance(callback: SubCallback<AccountBalance>): Promise<UnsubCallback>;
public getAccountBalance(
args: { account: string | Account },
callback: SubCallback<AccountBalance>
): Promise<UnsubCallback>;
// eslint-disable-next-line require-jsdoc
public getAccountBalance(
args?: { account: string | Account } | SubCallback<AccountBalance>,
callback?: SubCallback<AccountBalance>
): Promise<AccountBalance | UnsubCallback> {
const { context } = this;
let account: string | Account | undefined;
let cb: SubCallback<AccountBalance> | undefined = callback;
switch (typeof args) {
case 'undefined': {
break;
}
case 'function': {
cb = args;
break;
}
default: {
({ account } = args);
break;
}
}
if (!account) {
account = context.getSigningAccount();
} else {
account = asAccount(account, context);
}
if (cb) {
return account.getBalance(cb);
}
return account.getBalance();
}
/**
* Return an Account instance from an address. If the Account has multiSig signers, the returned value will be a {@link api/entities/Account/MultiSig!MultiSig} instance
*/
public async getAccount(args: { address: string }): Promise<Account | MultiSig> {
const {
context,
context: {
polymeshApi: {
query: { multiSig },
},
},
} = this;
const { address } = args;
const rawAddress = stringToAccountId(address, context);
const rawSigners = await multiSig.multiSigSigners.entries(rawAddress);
if (rawSigners.length > 0) {
return new MultiSig(args, context);
}
return new Account(args, context);
}
/**
* Return the signing Account, or null if no signing Account has been set
*/
public getSigningAccount(): Account | null {
try {
return this.context.getSigningAccount();
} catch (err) {
return null;
}
}
/**
* Return a list that contains all the signing Accounts associated to the SDK instance's Signing Manager
*
* @throws — if there is no Signing Manager attached to the SDK
*/
public async getSigningAccounts(): Promise<Account[]> {
return this.context.getSigningAccounts();
}
/**
* Return an Subsidy instance for a pair of beneficiary and subsidizer Account
*/
public getSubsidy(args: {
beneficiary: string | Account;
subsidizer: string | Account;
}): Subsidy {
const { context } = this;
const { beneficiary, subsidizer } = args;
const { address: beneficiaryAddress } = asAccount(beneficiary, context);
const { address: subsidizerAddress } = asAccount(subsidizer, context);
return new Subsidy({ beneficiary: beneficiaryAddress, subsidizer: subsidizerAddress }, context);
}
/**
* Returns `true` @param args.address is a valid ss58 address for the connected network
*/
public isValidAddress(args: { address: string }): boolean {
try {
assertAddressValid(args.address, this.context.ss58Format);
} catch (error) {
return false;
}
return true;
}
/**
* Accepts the authorization to become the new primary key of the issuing identity.
*
* If a CDD service provider approved this change (or this is not required), primary key of the Identity is updated.
*
* @note The caller (new primary key) must be either a secondary key of the issuing identity, or
* unlinked to any identity.
*/
public acceptPrimaryKey: ProcedureMethod<AcceptPrimaryKeyRotationParams, void>;
}