generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Identities.ts
264 lines (235 loc) · 10.2 KB
/
Identities.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
import { createPortfolioTransformer } from '~/api/entities/Venue';
import {
allowIdentityToCreatePortfolios,
attestPrimaryKeyRotation,
AuthorizationRequest,
ChildIdentity,
Context,
createChildIdentity,
createPortfolios,
Identity,
NumberedPortfolio,
registerIdentity,
revokeIdentityToCreatePortfolios,
rotatePrimaryKey,
rotatePrimaryKeyToSecondary,
} from '~/internal';
import {
AllowIdentityToCreatePortfoliosParams,
AttestPrimaryKeyRotationParams,
CreateChildIdentityParams,
ProcedureMethod,
RegisterIdentityParams,
RevokeIdentityToCreatePortfoliosParams,
RotatePrimaryKeyParams,
RotatePrimaryKeyToSecondaryParams,
} from '~/types';
import { identityIdToString } from '~/utils/conversion';
import { asIdentity, assertIdentityExists, createProcedureMethod } from '~/utils/internal';
/**
* Handles all Identity related functionality
*/
export class Identities {
private context: Context;
/**
* @hidden
*/
constructor(context: Context) {
this.context = context;
this.registerIdentity = createProcedureMethod(
{ getProcedureAndArgs: args => [registerIdentity, args] },
context
);
this.attestPrimaryKeyRotation = createProcedureMethod(
{ getProcedureAndArgs: args => [attestPrimaryKeyRotation, args] },
context
);
this.rotatePrimaryKey = createProcedureMethod(
{ getProcedureAndArgs: args => [rotatePrimaryKey, args] },
context
);
this.rotatePrimaryKeyToSecondary = createProcedureMethod(
{ getProcedureAndArgs: args => [rotatePrimaryKeyToSecondary, args] },
context
);
this.createPortfolio = createProcedureMethod(
{
getProcedureAndArgs: args => [
createPortfolios,
{
portfolios: [{ name: args.name, ownerDid: args.ownerDid }],
},
],
transformer: createPortfolioTransformer,
},
context
);
this.createPortfolios = createProcedureMethod(
{
getProcedureAndArgs: args => [
createPortfolios,
{ portfolios: args.names.map(name => ({ name, ownerDid: args.ownerDid })) },
],
},
context
);
this.createChild = createProcedureMethod(
{
getProcedureAndArgs: args => [createChildIdentity, args],
},
context
);
this.allowIdentityToCreatePortfolios = createProcedureMethod(
{ getProcedureAndArgs: args => [allowIdentityToCreatePortfolios, args] },
context
);
this.revokeIdentityToCreatePortfolios = createProcedureMethod(
{ getProcedureAndArgs: args => [revokeIdentityToCreatePortfolios, args] },
context
);
}
/**
* Register an Identity, possibly with a CDD claim
*
* @note the transaction signer must be a CDD provider
* @note this may create {@link api/entities/AuthorizationRequest!AuthorizationRequest | Authorization Requests} which have 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}
*
* @note required role:
* - Customer Due Diligence Provider
*/
public registerIdentity: ProcedureMethod<RegisterIdentityParams, Identity>;
/**
* Get CDD Provider's attestation to change primary key
*
* @note the transaction signer must be a CDD provider
* @note this creates an {@link api/entities/AuthorizationRequest!AuthorizationRequest | Authorization Requests} which have to be accepted by the `targetAccount` along with the authorization for `RotatingPrimaryKey`.
* 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}
*
* @note required role:
* - Customer Due Diligence Provider
*/
public attestPrimaryKeyRotation: ProcedureMethod<
AttestPrimaryKeyRotationParams,
AuthorizationRequest
>;
/**
* Creates an Authorization to rotate primary key of the signing Identity by the `targetAccount`
*
* @note this creates an {@link api/entities/AuthorizationRequest!AuthorizationRequest | Authorization Requests} which have to be accepted by the `targetAccount` along with the optional CDD authorization generated by CDD provider attesting the rotation of primary key
* 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 rotatePrimaryKey: ProcedureMethod<RotatePrimaryKeyParams, AuthorizationRequest>;
/**
* Creates an Authorization to rotate primary key of the signing Identity to an existing secondary key identified by the `targetAccount`
*
* @note the given `targetAccount` must be an existing secondaryKey or unlinked to any other Identity
* @note this creates an {@link api/entities/AuthorizationRequest!AuthorizationRequest | Authorization Requests} which have to be accepted by the `targetAccount` along with the optional CDD authorization generated by CDD provider attesting the rotation of primary key
* 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}
* @throws if the given `targetAccount` is linked with another Identity
* @throws if the given `targetAccount` is already the primary key of the signing Identity
* @throws if the given `targetAccount` already has a pending invitation to become the primary key of the signing Identity
*/
public rotatePrimaryKeyToSecondary: ProcedureMethod<
RotatePrimaryKeyToSecondaryParams,
AuthorizationRequest
>;
/**
* Create a new Portfolio under the ownership of the signing Identity
* @note the `ownerDid` is optional. If provided portfolios will be created as Custody Portfolios under the `ownerDid`
*/
public createPortfolio: ProcedureMethod<
{ name: string; ownerDid?: string },
NumberedPortfolio[],
NumberedPortfolio
>;
/**
* Creates a set of new Portfolios under the ownership of the signing Identity
* @note the `ownerDid` is optional. If provided portfolios will be created as Custody Portfolios under the `ownerDid`
*/
public createPortfolios: ProcedureMethod<
{ names: string[]; ownerDid?: string },
NumberedPortfolio[]
>;
/**
* Create an Identity instance from a DID
*
* @throws if there is no Identity with the passed DID
*/
public getIdentity(args: { did: string }): Promise<Identity> {
return this.context.getIdentity(args.did);
}
/**
* Create a ChildIdentity instance from a DID
*
* @throws if there is no ChildIdentity with the passed DID
*/
public getChildIdentity(args: { did: string }): Promise<ChildIdentity> {
return this.context.getChildIdentity(args.did);
}
/**
* Return whether the supplied Identity/DID exists
*/
public isIdentityValid(args: { identity: Identity | string }): Promise<boolean> {
return asIdentity(args.identity, this.context).exists();
}
/**
* Creates a child identity and makes the `secondaryKey` as the primary key of the child identity
*
* @note the given `secondaryKey` is removed as secondary key from the signing Identity
*
* @throws if
* - the transaction signer is not the primary account of which the `secondaryKey` is a secondary key
* - the `secondaryKey` can't be unlinked (can happen when it's part of a multisig with some balance)
* - the signing account is not a primary key
* - the signing Identity is already a child of some other identity
*/
public createChild: ProcedureMethod<CreateChildIdentityParams, ChildIdentity>;
/**
* Gives permission to the Identity to create Portfolios on behalf of the signing Identity
*
* @throws if
* - the provided Identity already has permissions to create portfolios for signing Identity
* - the provided Identity does not exist
*/
public allowIdentityToCreatePortfolios: ProcedureMethod<
AllowIdentityToCreatePortfoliosParams,
void
>;
/**
* Revokes permission from the Identity to create Portfolios on behalf of the signing Identity
*
* @throws if
* - the provided Identity already does not have permissions to create portfolios for signing Identity
* - the provided Identity does not exist
*/
public revokeIdentityToCreatePortfolios: ProcedureMethod<
RevokeIdentityToCreatePortfoliosParams,
void
>;
/**
* Returns a list of allowed custodian did(s) for Identity
* @throws if
* - the provided Identity does not exist
*/
public async getAllowedCustodians(did: string | Identity): Promise<string[]> {
const {
context: {
polymeshApi: { query },
},
} = this;
const identity = asIdentity(did, this.context);
await assertIdentityExists(identity);
const custodians = await query.portfolio.allowedCustodians.entries(did.toString());
return custodians.map(([storageKey]) => {
const {
args: [, custodianIdentityId],
} = storageKey;
return identityIdToString(custodianIdentityId);
});
}
}