Skip to content

Commit 9a27ac5

Browse files
feat: 🎸 Add support for filtering custom claim types
This adds support to filter targeting claims, issued claims etc for custom type claims as well. BREAKING CHANGE: 🧨 Type of attribute `claimTypes` (present as filtering option in getters in number of methods in Claims namespace ) has been changed from `ClaimType` to `ClaimTypeInput`
1 parent 4201d21 commit 9a27ac5

File tree

9 files changed

+219
-56
lines changed

9 files changed

+219
-56
lines changed

src/api/client/Claims.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import {
1515
claimsQuery,
1616
customClaimTypeQuery,
1717
} from '~/middleware/queries/claims';
18-
import { ClaimsOrderBy, ClaimTypeEnum, Query } from '~/middleware/types';
18+
import { ClaimsOrderBy, Query } from '~/middleware/types';
1919
import {
2020
CddClaim,
2121
ClaimData,
2222
ClaimOperation,
2323
ClaimScope,
24-
ClaimType,
24+
ClaimTypeInput,
2525
CustomClaim,
2626
CustomClaimType,
2727
CustomClaimTypeWithDid,
@@ -42,6 +42,7 @@ import { DEFAULT_GQL_PAGE_SIZE } from '~/utils/constants';
4242
import {
4343
bigNumberToU32,
4444
bytesToString,
45+
claimTypeInputToMiddlewareClaimTypeDetails,
4546
identityIdToString,
4647
meshClaimToClaim,
4748
momentToDate,
@@ -197,7 +198,7 @@ export class Claims {
197198
targets?: (string | Identity)[];
198199
trustedClaimIssuers?: (string | Identity)[];
199200
scope?: Scope;
200-
claimTypes?: ClaimType[];
201+
claimTypes?: ClaimTypeInput[];
201202
includeExpired?: boolean;
202203
size?: BigNumber;
203204
start?: BigNumber;
@@ -222,8 +223,8 @@ export class Claims {
222223
trustedClaimIssuers: trustedClaimIssuers?.map(trustedClaimIssuer =>
223224
signerToString(trustedClaimIssuer)
224225
),
225-
claimTypes: Array.isArray(claimTypes) ? claimTypes.map(ct => ClaimTypeEnum[ct]) : undefined,
226226
includeExpired,
227+
...claimTypeInputToMiddlewareClaimTypeDetails(claimTypes),
227228
};
228229

229230
if (!targets) {
@@ -293,7 +294,6 @@ export class Claims {
293294
if (isMiddlewareAvailable) {
294295
const { data, count, next } = await this.getIdentitiesWithClaims({
295296
targets: [did],
296-
claimTypes: [ClaimType.Custom],
297297
includeExpired: false,
298298
size,
299299
start: new BigNumber(0),
@@ -331,7 +331,6 @@ export class Claims {
331331
promises.push(
332332
this.getIdentitiesWithClaims({
333333
targets: [did],
334-
claimTypes: [ClaimType.Custom],
335334
includeExpired: false,
336335
start,
337336
size,
@@ -355,16 +354,6 @@ export class Claims {
355354

356355
const identityClaimsFromChain = await context.getIdentityClaimsFromChain({
357356
targets: [did],
358-
claimTypes: [
359-
ClaimType.Accredited,
360-
ClaimType.Affiliate,
361-
ClaimType.Blocked,
362-
ClaimType.BuyLockup,
363-
ClaimType.Exempted,
364-
ClaimType.Jurisdiction,
365-
ClaimType.KnowYourCustomer,
366-
ClaimType.SellLockup,
367-
],
368357
includeExpired: true,
369358
});
370359

src/api/client/__tests__/Claims.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ describe('Claims Class', () => {
109109
jest.useRealTimers();
110110
});
111111

112-
it('should return a list of Identities with claims associated to them', async () => {
112+
it('should return a list2 of Identities with claims associated to them', async () => {
113113
const targetDid = 'someTargetDid';
114114
const issuerDid = 'someIssuerDid';
115115
const date = 1589816265000;
@@ -171,6 +171,7 @@ describe('Claims Class', () => {
171171
trustedClaimIssuers: [issuerDid],
172172
claimTypes: [ClaimTypeEnum.CustomerDueDiligence],
173173
includeExpired: false,
174+
scope: undefined,
174175
}),
175176
{
176177
claims: claimsQueryResponse,
@@ -583,7 +584,6 @@ describe('Claims Class', () => {
583584
when(getIdentitiesWithClaimsSpy)
584585
.calledWith({
585586
targets: [target],
586-
claimTypes: [ClaimType.Custom],
587587
includeExpired: false,
588588
start: new BigNumber(0),
589589
size: new BigNumber(DEFAULT_GQL_PAGE_SIZE),
@@ -655,7 +655,6 @@ describe('Claims Class', () => {
655655
when(getIdentitiesWithClaimsSpy)
656656
.calledWith({
657657
targets: [target],
658-
claimTypes: [ClaimType.Custom],
659658
includeExpired: false,
660659
start: next,
661660
size: new BigNumber(DEFAULT_GQL_PAGE_SIZE),

src/api/entities/types.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,11 @@ export interface ClaimScope {
313313
assetId?: string | undefined;
314314
}
315315

316-
export type TrustedFor =
317-
| Exclude<ClaimType, ClaimType.Custom>
318-
| { type: ClaimType.Custom; customClaimTypeId: BigNumber };
316+
export type CustomClaimWithTypeId = { type: ClaimType.Custom; customClaimTypeId: BigNumber };
317+
318+
export type TrustedFor = Exclude<ClaimType, ClaimType.Custom> | CustomClaimWithTypeId;
319+
320+
export type ClaimTypeInput = TrustedFor;
319321

320322
/**
321323
* @param IsDefault - whether the Identity is a default trusted claim issuer for an asset or just

src/base/Context.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@ import {
3434
import { claimsQuery } from '~/middleware/queries/claims';
3535
import { heartbeatQuery, metadataQuery } from '~/middleware/queries/common';
3636
import { polyxTransactionsQuery } from '~/middleware/queries/polyxTransactions';
37-
import { ClaimTypeEnum, Query } from '~/middleware/types';
37+
import { Query } from '~/middleware/types';
3838
import {
3939
AccountBalance,
4040
ClaimData,
4141
ClaimType,
42+
ClaimTypeInput,
4243
CorporateActionParams,
44+
CustomClaimWithTypeId,
4345
DistributionWithDetails,
4446
ErrorCode,
4547
MiddlewareMetadata,
@@ -67,6 +69,7 @@ import {
6769
balanceToBigNumber,
6870
bigNumberToU32,
6971
boolToBoolean,
72+
claimTypeInputToMiddlewareClaimTypeDetails,
7073
claimTypeToMeshClaimType,
7174
corporateActionIdentifierToCaId,
7275
distributionToDividendDistributionParams,
@@ -868,7 +871,7 @@ export class Context {
868871
*/
869872
public async getIdentityClaimsFromChain(args: {
870873
targets: (string | Identity)[];
871-
claimTypes?: ClaimType[] | undefined;
874+
claimTypes?: ClaimTypeInput[] | undefined;
872875
trustedClaimIssuers?: (string | Identity)[] | undefined;
873876
includeExpired: boolean;
874877
}): Promise<ClaimData[]> {
@@ -878,15 +881,27 @@ export class Context {
878881
},
879882
} = this;
880883

881-
const {
882-
targets,
883-
claimTypes = Object.values(ClaimType),
884-
trustedClaimIssuers,
885-
includeExpired,
886-
} = args;
884+
const { targets, claimTypes, trustedClaimIssuers, includeExpired } = args;
885+
886+
let claimTypesInput: ClaimTypeInput[];
887+
888+
if (claimTypes) {
889+
claimTypesInput = claimTypes;
890+
} else {
891+
const customClaimTypes = await identity.customClaims.entries();
892+
claimTypesInput = [
893+
...Object.values(ClaimType).filter(claimType => claimType !== ClaimType.Custom),
894+
...customClaimTypes.map(entry => {
895+
return {
896+
type: ClaimType.Custom,
897+
customClaimTypeId: u32ToBigNumber(entry[0].args[0]),
898+
} as CustomClaimWithTypeId;
899+
}),
900+
];
901+
}
887902

888903
const claim1stKeys = targets.flatMap(target =>
889-
claimTypes.map(claimType => {
904+
claimTypesInput.map(claimType => {
890905
return {
891906
target: signerToString(target),
892907
claimType: claimTypeToMeshClaimType(claimType, this),
@@ -939,7 +954,7 @@ export class Context {
939954
public async getIdentityClaimsFromMiddleware(args: {
940955
targets?: (string | Identity)[] | undefined;
941956
trustedClaimIssuers?: (string | Identity)[] | undefined;
942-
claimTypes?: ClaimType[] | undefined;
957+
claimTypes?: ClaimTypeInput[] | undefined;
943958
includeExpired?: boolean | undefined;
944959
size?: BigNumber | undefined;
945960
start?: BigNumber | undefined;
@@ -965,7 +980,7 @@ export class Context {
965980
trustedClaimIssuers: trustedClaimIssuers?.map(trustedClaimIssuer =>
966981
signerToString(trustedClaimIssuer)
967982
),
968-
claimTypes: claimTypes?.map(ct => ClaimTypeEnum[ct]),
983+
...claimTypeInputToMiddlewareClaimTypeDetails(claimTypes),
969984
includeExpired,
970985
},
971986
size,
@@ -1004,7 +1019,7 @@ export class Context {
10041019
opts: {
10051020
targets?: (string | Identity)[];
10061021
trustedClaimIssuers?: (string | Identity)[];
1007-
claimTypes?: ClaimType[];
1022+
claimTypes?: ClaimTypeInput[];
10081023
includeExpired?: boolean;
10091024
size?: BigNumber;
10101025
start?: BigNumber;

src/base/__tests__/Context.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,8 @@ describe('Context class', () => {
12601260
const cddId = 'someCddId';
12611261
const date = 1589816265000;
12621262
const customerDueDiligenceType = ClaimTypeEnum.CustomerDueDiligence;
1263+
const customClaimType = ClaimTypeEnum.Custom;
1264+
const customClaimTypeId = new BigNumber(1);
12631265
const claim = {
12641266
target: expect.objectContaining({ did: targetDid }),
12651267
issuer: expect.objectContaining({ did: issuerDid }),
@@ -1279,8 +1281,9 @@ describe('Context class', () => {
12791281
...claim,
12801282
expiry: null,
12811283
claim: {
1282-
type: customerDueDiligenceType,
1283-
id: cddId,
1284+
type: customClaimType,
1285+
customClaimTypeId,
1286+
scope: {},
12841287
},
12851288
},
12861289
];
@@ -1302,7 +1305,9 @@ describe('Context class', () => {
13021305
{
13031306
...commonClaimData,
13041307
expiry: null,
1305-
type: customerDueDiligenceType,
1308+
type: customClaimType,
1309+
customClaimTypeId,
1310+
scope: undefined,
13061311
},
13071312
],
13081313
};
@@ -1313,7 +1318,8 @@ describe('Context class', () => {
13131318
{
13141319
dids: [targetDid],
13151320
trustedClaimIssuers: [targetDid],
1316-
claimTypes: [ClaimTypeEnum.Accredited],
1321+
claimTypes: [ClaimTypeEnum.CustomerDueDiligence],
1322+
customClaimTypeIds: [customClaimTypeId.toString()],
13171323
includeExpired: true,
13181324
},
13191325
new BigNumber(2),
@@ -1327,7 +1333,13 @@ describe('Context class', () => {
13271333
let result = await context.issuedClaims({
13281334
targets: [targetDid],
13291335
trustedClaimIssuers: [targetDid],
1330-
claimTypes: [ClaimType.Accredited],
1336+
claimTypes: [
1337+
ClaimType.CustomerDueDiligence,
1338+
{
1339+
type: ClaimType.Custom,
1340+
customClaimTypeId,
1341+
},
1342+
],
13311343
includeExpired: true,
13321344
size: new BigNumber(2),
13331345
start: new BigNumber(0),
@@ -1494,6 +1506,17 @@ describe('Context class', () => {
14941506

14951507
expect(result.data.length).toEqual(0);
14961508

1509+
const customClaimTypeId = new BigNumber(1);
1510+
1511+
dsMockUtils.createQueryMock('identity', 'customClaims', {
1512+
entries: [
1513+
tuple(
1514+
[dsMockUtils.createMockU32(customClaimTypeId), dsMockUtils.createMockOption()],
1515+
dsMockUtils.createMockOption(dsMockUtils.createMockBytes('custom Claim'))
1516+
),
1517+
],
1518+
});
1519+
14971520
result = await context.issuedClaims({
14981521
targets: [targetDid],
14991522
trustedClaimIssuers: [targetDid],

src/middleware/__tests__/queries/claims.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ describe('claimsQuery', () => {
3636
scope: { type: ClaimScopeTypeEnum.Asset, value: '0x12341234123412341234123412341234' },
3737
trustedClaimIssuers: ['someTrustedClaim'],
3838
claimTypes: [ClaimTypeEnum.Accredited],
39+
customClaimTypeIds: ['123', '456'],
3940
includeExpired: true,
4041
size: DEFAULT_GQL_PAGE_SIZE,
4142
start: 0,
@@ -64,11 +65,12 @@ describe('claimsQuery', () => {
6465
});
6566

6667
it('should not include undefined values in the variables', () => {
67-
const result = claimsQuery(false, { includeExpired: true });
68+
const result = claimsQuery(false, { includeExpired: true, customClaimTypeIds: ['123', '456'] });
6869
expect(result.variables).toEqual({
6970
includeExpired: true,
7071
size: DEFAULT_GQL_PAGE_SIZE,
7172
start: 0,
73+
customClaimTypeIds: ['123', '456'],
7274
});
7375
});
7476
});

src/middleware/queries/claims.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@ function createClaimsFilters(variables: ClaimsQueryFilter): {
1919
filter: string;
2020
} {
2121
const args = ['$size: Int, $start: Int'];
22-
const filters = ['revokeDate: { isNull: true }'];
23-
const { dids, claimTypes, trustedClaimIssuers, scope, includeExpired } = variables;
22+
let filters = ['revokeDate: { isNull: true }'];
23+
const { dids, claimTypes, trustedClaimIssuers, scope, includeExpired, customClaimTypeIds } =
24+
variables;
2425
if (dids?.length) {
2526
args.push('$dids: [String!]');
2627
filters.push('targetId: { in: $dids }');
2728
}
28-
if (claimTypes) {
29-
args.push('$claimTypes: [ClaimTypeEnum!]!');
30-
filters.push('type: { in: $claimTypes }');
31-
}
3229
if (trustedClaimIssuers?.length) {
3330
args.push('$trustedClaimIssuers: [String!]');
3431
filters.push('issuerId: { in: $trustedClaimIssuers }');
@@ -39,10 +36,25 @@ function createClaimsFilters(variables: ClaimsQueryFilter): {
3936
}
4037
if (!includeExpired) {
4138
args.push('$expiryTimestamp: BigFloat');
42-
filters.push(
43-
'or: [{ filterExpiry: { lessThan: $expiryTimestamp } }, { expiry: { isNull: true } }]'
44-
);
39+
filters.push('filterExpiry: { lessThan: $expiryTimestamp } , expiry: { isNull: true }');
4540
}
41+
42+
if (claimTypes && !customClaimTypeIds) {
43+
args.push('$claimTypes: [ClaimTypeEnum!]!');
44+
filters.push('type: { in: $claimTypes }');
45+
}
46+
if (!claimTypes && customClaimTypeIds) {
47+
args.push('$customClaimTypeIds: [String!]!');
48+
filters.push('customClaimTypeId: { in: $customClaimTypeIds }');
49+
}
50+
if (claimTypes && customClaimTypeIds) {
51+
filters = [
52+
`or: [ { ${filters.join()}, type: { in: $claimTypes } }, { ${filters.join()}, customClaimTypeId: { in: $customClaimTypeIds } } ]`,
53+
];
54+
args.push('$claimTypes: [ClaimTypeEnum!]!');
55+
args.push('$customClaimTypeIds: [String!]!');
56+
}
57+
4658
return {
4759
args: `(${args.join()})`,
4860
filter: `filter: { ${filters.join()} },`,
@@ -56,6 +68,7 @@ export interface ClaimsQueryFilter {
5668
claimTypes?: ClaimTypeEnum[] | undefined;
5769
includeExpired?: boolean | undefined;
5870
expiryTimestamp?: number | undefined;
71+
customClaimTypeIds?: string[] | undefined;
5972
}
6073
/**
6174
* @hidden

0 commit comments

Comments
 (0)