generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Claims.ts
434 lines (382 loc) · 12.1 KB
/
Claims.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
import BigNumber from 'bignumber.js';
import { filter, flatten, isEqual, uniqBy, uniqWith } from 'lodash';
import { Context, Identity, modifyClaims } from '~/internal';
import { ClaimTypeEnum } from '~/middleware/enums';
import { claimsGroupingQuery, claimsQuery } from '~/middleware/queries';
import { ClaimsGroupBy, ClaimsOrderBy, Query } from '~/middleware/types';
import {
CddClaim,
ClaimData,
ClaimOperation,
ClaimScope,
ClaimType,
IdentityWithClaims,
ModifyClaimsParams,
ProcedureMethod,
ResultSet,
Scope,
ScopedClaim,
ScopeType,
} from '~/types';
import { Ensured } from '~/types/utils';
import { DEFAULT_GQL_PAGE_SIZE } from '~/utils/constants';
import {
scopeToMiddlewareScope,
signerToString,
toIdentityWithClaimsArray,
} from '~/utils/conversion';
import { calculateNextKey, createProcedureMethod, getDid, removePadding } from '~/utils/internal';
/**
* Handles all Claims related functionality
*/
export class Claims {
private context: Context;
/**
* @hidden
*/
constructor(context: Context) {
this.context = context;
this.addClaims = createProcedureMethod<
Omit<ModifyClaimsParams, 'operation'>,
ModifyClaimsParams,
void
>(
{
getProcedureAndArgs: args => [
modifyClaims,
{
...args,
operation: ClaimOperation.Add,
} as ModifyClaimsParams,
],
},
context
);
this.editClaims = createProcedureMethod<
Omit<ModifyClaimsParams, 'operation'>,
ModifyClaimsParams,
void
>(
{
getProcedureAndArgs: args => [
modifyClaims,
{
...args,
operation: ClaimOperation.Edit,
} as ModifyClaimsParams,
],
},
context
);
this.revokeClaims = createProcedureMethod<
Omit<ModifyClaimsParams, 'operation'>,
ModifyClaimsParams,
void
>(
{
getProcedureAndArgs: args => [
modifyClaims,
{
...args,
operation: ClaimOperation.Revoke,
} as ModifyClaimsParams,
],
},
context
);
}
/**
* Add claims to Identities
*
* @note required roles:
* - Customer Due Diligence Provider: if there is at least one CDD claim in the arguments
*/
public addClaims: ProcedureMethod<Pick<ModifyClaimsParams, 'claims'>, void>;
/**
* Edit claims associated to Identities (only the expiry date can be modified)
*
* @note required roles:
* - Customer Due Diligence Provider: if there is at least one CDD claim in the arguments
*/
public editClaims: ProcedureMethod<Pick<ModifyClaimsParams, 'claims'>, void>;
/**
* Revoke claims from Identities
*
* @note required roles:
* - Customer Due Diligence Provider: if there is at least one CDD claim in the arguments
*/
public revokeClaims: ProcedureMethod<Pick<ModifyClaimsParams, 'claims'>, void>;
/**
* Retrieve all claims issued by an Identity
*
* @param opts.target - Identity (optional, defaults to the signing Identity)
* @param opts.includeExpired - whether to include expired claims. Defaults to true
*
* @note supports pagination
* @note uses the middlewareV2
*/
public async getIssuedClaims(
opts: {
target?: string | Identity;
includeExpired?: boolean;
size?: BigNumber;
start?: BigNumber;
} = {}
): Promise<ResultSet<ClaimData>> {
const { context } = this;
const { target, includeExpired = true, size, start } = opts;
const did = await getDid(target, context);
return context.getIdentityClaimsFromMiddleware({
trustedClaimIssuers: [did],
includeExpired,
size,
start,
});
}
/**
* Retrieve a list of Identities with claims associated to them. Can be filtered using parameters
*
* @param opts.targets - Identities (or Identity IDs) for which to fetch targeting claims. Defaults to all targets
* @param opts.trustedClaimIssuers - Identity IDs of claim issuers. Defaults to all claim issuers
* @param opts.scope - scope of the claims to fetch. Defaults to any scope
* @param opts.claimTypes - types of the claims to fetch. Defaults to any type
* @param opts.includeExpired - whether to include expired claims. Defaults to true
* @param opts.size - page size
* @param opts.start - page offset
*
* @note supports pagination
* @note uses the middleware V2
*/
public async getIdentitiesWithClaims(
opts: {
targets?: (string | Identity)[];
trustedClaimIssuers?: (string | Identity)[];
scope?: Scope;
claimTypes?: ClaimType[];
includeExpired?: boolean;
size?: BigNumber;
start?: BigNumber;
} = {}
): Promise<ResultSet<IdentityWithClaims>> {
const { context } = this;
const {
targets,
trustedClaimIssuers,
scope,
claimTypes,
includeExpired = true,
size = new BigNumber(DEFAULT_GQL_PAGE_SIZE),
start = new BigNumber(0),
} = opts;
let targetIssuers;
const filters = {
scope: scope ? scopeToMiddlewareScope(scope, false) : undefined,
trustedClaimIssuers: trustedClaimIssuers?.map(trustedClaimIssuer =>
signerToString(trustedClaimIssuer)
),
claimTypes: claimTypes?.map(ct => ClaimTypeEnum[ct]),
includeExpired,
};
if (!targets) {
const {
data: {
claims: { groupedAggregates: groupedTargets },
},
} = await context.queryMiddleware<Ensured<Query, 'claims'>>(
claimsGroupingQuery({
...filters,
})
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
targetIssuers = flatten(groupedTargets!.map(groupedTarget => groupedTarget.keys!));
} else {
targetIssuers = targets.map(target => signerToString(target));
}
// note: pagination count is based on the target issuers and not the claims count
const count = new BigNumber(targetIssuers.length);
// tooling-gql does pagination based on sorted target issuers, hence the explicit `sort()` function (as graphql doesn't sort the final data)
targetIssuers.sort();
targetIssuers = targetIssuers.slice(start.toNumber(), size.plus(start).toNumber());
const {
data: {
claims: { nodes },
},
} = await context.queryMiddleware<Ensured<Query, 'claims'>>(
claimsQuery({
dids: targetIssuers,
...filters,
})
);
const data = toIdentityWithClaimsArray(nodes, context, 'targetId');
const next = calculateNextKey(count, data.length, start);
return {
data,
next,
count,
};
}
/**
* Retrieve all scopes in which claims have been made for the target Identity.
* If the scope is an asset DID, the corresponding ticker is returned as well
*
* @param opts.target - Identity for which to fetch claim scopes (optional, defaults to the signing Identity)
*/
public async getClaimScopes(opts: { target?: string | Identity } = {}): Promise<ClaimScope[]> {
const { context } = this;
const { target } = opts;
const did = await getDid(target, context);
const identityClaimsFromChain = await context.getIdentityClaimsFromChain({
targets: [did],
claimTypes: [
ClaimType.Accredited,
ClaimType.Affiliate,
ClaimType.Blocked,
ClaimType.BuyLockup,
ClaimType.Exempted,
ClaimType.Jurisdiction,
ClaimType.KnowYourCustomer,
ClaimType.SellLockup,
],
trustedClaimIssuers: undefined,
includeExpired: true,
});
const claimScopeList = identityClaimsFromChain.map(({ claim }) => {
// only Scoped Claims were fetched so this assertion is reasonable
const {
scope: { type, value },
} = claim as ScopedClaim;
let ticker: string | undefined;
if (type === ScopeType.Ticker) {
ticker = removePadding(value);
}
return {
scope: { type, value: ticker ?? value },
ticker,
};
});
return uniqWith(claimScopeList, isEqual);
}
/**
* Retrieve the list of CDD claims for a target Identity
*
* @param opts.target - Identity for which to fetch CDD claims (optional, defaults to the signing Identity)
* @param opts.includeExpired - whether to include expired claims. Defaults to true
*/
public async getCddClaims(
opts: {
target?: string | Identity;
includeExpired?: boolean;
} = {}
): Promise<ClaimData<CddClaim>[]> {
const { context } = this;
const { target, includeExpired = true } = opts;
const did = await getDid(target, context);
return context.getIdentityClaimsFromChain({
targets: [did],
claimTypes: [ClaimType.CustomerDueDiligence],
includeExpired,
}) as Promise<ClaimData<CddClaim>[]>;
}
/**
* @hidden
*/
private async getClaimsFromChain(
context: Context,
did: string,
trustedClaimIssuers: (string | Identity)[] | undefined,
includeExpired: boolean
): Promise<ResultSet<IdentityWithClaims>> {
const identityClaimsFromChain = await context.getIdentityClaimsFromChain({
targets: [did],
trustedClaimIssuers: trustedClaimIssuers?.map(signerToString),
includeExpired,
});
const issuers = uniqBy(
identityClaimsFromChain.map(i => i.issuer),
identity => identity.did
);
const identitiesWithClaims = issuers.map(identity => ({
identity,
claims: filter(identityClaimsFromChain, ({ issuer }) => issuer.isEqual(identity)),
}));
return {
data: identitiesWithClaims,
next: null,
count: new BigNumber(identitiesWithClaims.length),
};
}
/**
* Retrieve all claims issued about an Identity, grouped by claim issuer
*
* @param opts.target - Identity for which to fetch targeting claims (optional, defaults to the signing Identity)
* @param opts.includeExpired - whether to include expired claims. Defaults to true
*
* @note supports pagination
* @note uses the middlewareV2 (optional)
*/
public async getTargetingClaims(
opts: {
target?: string | Identity;
scope?: Scope;
trustedClaimIssuers?: (string | Identity)[];
includeExpired?: boolean;
size?: BigNumber;
start?: BigNumber;
} = {}
): Promise<ResultSet<IdentityWithClaims>> {
const { context } = this;
const {
target,
trustedClaimIssuers,
scope,
includeExpired = true,
size = new BigNumber(DEFAULT_GQL_PAGE_SIZE),
start = new BigNumber(0),
} = opts;
const did = await getDid(target, context);
const isMiddlewareAvailable = await context.isMiddlewareAvailable();
if (isMiddlewareAvailable) {
const filters = {
dids: [did],
scope: scope ? scopeToMiddlewareScope(scope, false) : undefined,
includeExpired,
};
let claimIssuers;
if (!trustedClaimIssuers) {
const {
data: {
claims: { groupedAggregates: groupedIssuers },
},
} = await context.queryMiddleware<Ensured<Query, 'claims'>>(
claimsGroupingQuery(filters, ClaimsOrderBy.IssuerIdAsc, ClaimsGroupBy.IssuerId)
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
claimIssuers = flatten(groupedIssuers!.map(groupedAggregate => groupedAggregate.keys!));
} else {
claimIssuers = trustedClaimIssuers.map(signerToString);
}
// note: pagination count is based on the claim issuers and not the claims count
const count = new BigNumber(claimIssuers.length);
claimIssuers.sort();
claimIssuers = claimIssuers.slice(start.toNumber(), size.plus(start).toNumber());
const {
data: {
claims: { nodes },
},
} = await context.queryMiddleware<Ensured<Query, 'claims'>>(
claimsQuery({
trustedClaimIssuers: claimIssuers,
...filters,
})
);
const data = toIdentityWithClaimsArray(nodes, context, 'issuerId');
const next = calculateNextKey(count, data.length, start);
return {
data,
next,
count,
};
}
return this.getClaimsFromChain(context, did, trustedClaimIssuers, includeExpired);
}
}