generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Claims.ts
298 lines (264 loc) · 8.28 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
import { Identity } from '~/api/entities';
import { modifyClaims, ModifyClaimsParams } from '~/api/procedures';
import { Context, TransactionQueue } from '~/base';
import {
didsWithClaims,
issuerDidsWithClaimsByTarget,
scopesByIdentity,
} from '~/middleware/queries';
import { ClaimTypeEnum, Query } from '~/middleware/types';
import {
ClaimData,
ClaimScope,
ClaimType,
Ensured,
IdentityWithClaims,
ResultSet,
Scope,
} from '~/types';
import { ClaimOperation } from '~/types/internal';
import {
middlewareScopeToScope,
scopeToMiddlewareScope,
signerToString,
toIdentityWithClaimsArray,
} from '~/utils/conversion';
import { calculateNextKey, getDid, removePadding } from '~/utils/internal';
/**
* Handles all Claims related functionality
*/
export class Claims {
private context: Context;
/**
* @hidden
*/
constructor(context: Context) {
this.context = context;
}
/**
* Add claims to Identities
*
* @param args.claims - array of claims to be added
*/
public addClaims(args: Omit<ModifyClaimsParams, 'operation'>): Promise<TransactionQueue<void>> {
return modifyClaims.prepare({ ...args, operation: ClaimOperation.Add }, this.context);
}
/**
* Edit claims associated to Identities (only the expiry date can be modified)
*
* * @param args.claims - array of claims to be edited
*/
public editClaims(args: Omit<ModifyClaimsParams, 'operation'>): Promise<TransactionQueue<void>> {
return modifyClaims.prepare({ ...args, operation: ClaimOperation.Edit }, this.context);
}
/**
* Revoke claims from Identities
*
* @param args.claims - array of claims to be revoked
*/
public revokeClaims(
args: Omit<ModifyClaimsParams, 'operation'>
): Promise<TransactionQueue<void>> {
return modifyClaims.prepare({ ...args, operation: ClaimOperation.Revoke }, this.context);
}
/**
* Retrieve all claims issued by an Identity
*
* @param opts.target - identity (optional, defaults to the current Identity)
* @param opts.includeExpired - whether to include expired claims. Defaults to true
*
* @note supports pagination
* @note uses the middleware
*/
public async getIssuedClaims(
opts: {
target?: string | Identity;
includeExpired?: boolean;
size?: number;
start?: number;
} = {}
): Promise<ResultSet<ClaimData>> {
const { context } = this;
const { target, includeExpired = true, size, start } = opts;
const did = await getDid(target, context);
const result = await context.issuedClaims({
trustedClaimIssuers: [did],
includeExpired,
size,
start,
});
return result;
}
/**
* 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 claims (targets). 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
*/
public async getIdentitiesWithClaims(
opts: {
targets?: (string | Identity)[];
trustedClaimIssuers?: (string | Identity)[];
scope?: Scope;
claimTypes?: ClaimType[];
includeExpired?: boolean;
size?: number;
start?: number;
} = {}
): Promise<ResultSet<IdentityWithClaims>> {
const { context } = this;
const {
targets,
trustedClaimIssuers,
scope,
claimTypes,
includeExpired = true,
size,
start,
} = opts;
const result = await context.queryMiddleware<Ensured<Query, 'didsWithClaims'>>(
didsWithClaims({
dids: targets?.map(target => signerToString(target)),
scope: scope ? scopeToMiddlewareScope(scope) : undefined,
trustedClaimIssuers: trustedClaimIssuers?.map(trustedClaimIssuer =>
signerToString(trustedClaimIssuer)
),
claimTypes: claimTypes?.map(ct => ClaimTypeEnum[ct]),
includeExpired,
count: size,
skip: start,
})
);
const {
data: {
didsWithClaims: { items: didsWithClaimsList, totalCount: count },
},
} = result;
const data = toIdentityWithClaimsArray(didsWithClaimsList, context);
const next = calculateNextKey(count, size, 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 current Identity)
*
* @note a null scope means the Identity has scopeless claims (like CDD for example)
* @note uses the middleware
*/
public async getClaimScopes(opts: { target?: string | Identity } = {}): Promise<ClaimScope[]> {
const { context } = this;
const { target } = opts;
const did = await getDid(target, context);
const {
data: { scopesByIdentity: scopes },
} = await context.queryMiddleware<Ensured<Query, 'scopesByIdentity'>>(
scopesByIdentity({ did })
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return scopes.map(({ scope, ticker: symbol }) => {
let ticker: string | undefined;
if (symbol) {
ticker = removePadding(symbol);
}
return {
scope: scope ? middlewareScopeToScope(scope) : null,
ticker,
};
});
}
/**
* Retrieve the list of CDD claims for a target Identity
*
* @param opts.target - identity for which to fetch CDD claims (optional, defaults to the current Identity)
* @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
*/
public async getCddClaims(
opts: {
target?: string | Identity;
includeExpired?: boolean;
size?: number;
start?: number;
} = {}
): Promise<ResultSet<ClaimData>> {
const { context } = this;
const { target, includeExpired = true, size, start } = opts;
const did = await getDid(target, context);
const result = await context.issuedClaims({
targets: [did],
claimTypes: [ClaimType.CustomerDueDiligence],
includeExpired,
size,
start,
});
return result;
}
/**
* 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 current Identity)
* @param opts.includeExpired - whether to include expired claims. Defaults to true
*
* @note supports pagination
* @note uses the middleware
*/
public async getTargetingClaims(
opts: {
target?: string | Identity;
scope?: Scope;
trustedClaimIssuers?: (string | Identity)[];
includeExpired?: boolean;
size?: number;
start?: number;
} = {}
): Promise<ResultSet<IdentityWithClaims>> {
const { context } = this;
const { target, trustedClaimIssuers, scope, includeExpired = true, size, start } = opts;
const did = await getDid(target, context);
const result = await context.queryMiddleware<Ensured<Query, 'issuerDidsWithClaimsByTarget'>>(
issuerDidsWithClaimsByTarget({
target: did,
scope: scope ? scopeToMiddlewareScope(scope) : undefined,
trustedClaimIssuers: trustedClaimIssuers?.map(trustedClaimIssuer =>
signerToString(trustedClaimIssuer)
),
includeExpired,
count: size,
skip: start,
})
);
const {
data: {
issuerDidsWithClaimsByTarget: {
items: issuerDidsWithClaimsByTargetList,
totalCount: count,
},
},
} = result;
const data = toIdentityWithClaimsArray(issuerDidsWithClaimsByTargetList, context);
const next = calculateNextKey(count, size, start);
return {
data,
next,
count,
};
}
}