Skip to content

Commit

Permalink
feat: getCddClaims
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Aug 12, 2020
1 parent 2aa4a44 commit 75cbc54
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 153 deletions.
52 changes: 6 additions & 46 deletions src/Polymesh.ts
Expand Up @@ -547,58 +547,18 @@ export class Polymesh {
start?: number;
} = {}
): Promise<ResultSet<ClaimData>> {
const {
context,
context: { middlewareApi },
} = this;
const { context } = this;

const { size, start } = opts;
const { did } = context.getCurrentIdentity();

let result: ApolloQueryResult<Ensured<Query, 'didsWithClaims'>>;
try {
result = await middlewareApi.query<Ensured<Query, 'didsWithClaims'>>(
didsWithClaims({
trustedClaimIssuers: [did],
count: size,
skip: start,
})
);
} catch (e) {
throw new PolymeshError({
code: ErrorCode.FatalError,
message: `Error in middleware query: ${e.message}`,
});
}

const {
data: {
didsWithClaims: { items: didsWithClaimsList, totalCount: count },
},
} = result;
const data: ClaimData[] = [];

didsWithClaimsList.forEach(({ claims }) => {
claims.forEach(
({ targetDID, issuer, issuance_date: issuanceDate, expiry, type, jurisdiction, scope }) => {
data.push({
target: new Identity({ did: targetDID }, context),
issuer: new Identity({ did: issuer }, context),
issuedAt: new Date(issuanceDate),
expiry: expiry ? new Date(expiry) : null,
claim: createClaim(type, jurisdiction, scope),
});
}
);
const result = await context.issuedClaims({
trustedClaimIssuers: [did],
size,
start,
});

const next = calculateNextKey(count, size, start);

return {
data,
next,
count,
};
return result;
}

/**
Expand Down
119 changes: 16 additions & 103 deletions src/__tests__/Polymesh.ts
Expand Up @@ -15,8 +15,10 @@ import { Polymesh } from '~/Polymesh';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import {
AccountBalance,
ClaimData,
ClaimTargets,
ClaimType,
ResultSet,
Signer,
SignerType,
SubCallback,
Expand Down Expand Up @@ -655,121 +657,32 @@ describe('Polymesh Class', () => {
describe('method: getIssuedClaims', () => {
test('should return a list of issued claims', async () => {
const context = dsMockUtils.getContextInstance();
const targetDid = 'someTargetDid';
const issuerDid = 'someIssuerDid';
const date = 1589816265000;
const customerDueDiligenceType = ClaimTypeEnum.CustomerDueDiligence;
const jurisdictionType = ClaimTypeEnum.Jurisdiction;
const exemptedType = ClaimTypeEnum.Exempted;
const claim = {
target: new Identity({ did: targetDid }, context),
issuer: new Identity({ did: issuerDid }, context),
issuedAt: new Date(date),
};
const fakeClaims = [
{
...claim,
expiry: null,
claim: {
type: customerDueDiligenceType,
},
},
{
...claim,
expiry: new Date(date),
claim: {
type: jurisdictionType,
name: 'someJurisdiction',
scope: 'someScope',
},
},
{
...claim,
expiry: null,
claim: {
type: exemptedType,
scope: 'someScope',
},
},
];
/* eslint-disable @typescript-eslint/camelcase */
const commonClaimData = {
targetDID: targetDid,
issuer: issuerDid,
issuance_date: date,
last_update_date: date,
};
const didsWithClaimsQueryResponse: IdentityWithClaimsResult = {
totalCount: 1,
items: [
const issuedClaims: ResultSet<ClaimData> = {
data: [
{
did: targetDid,
claims: [
{
...commonClaimData,
expiry: null,
type: customerDueDiligenceType,
},
{
...commonClaimData,
expiry: date,
type: jurisdictionType,
jurisdiction: 'someJurisdiction',
scope: 'someScope',
},
{
...commonClaimData,
expiry: null,
type: exemptedType,
jurisdiction: null,
scope: 'someScope',
},
],
target: new Identity({ did: 'someDid' }, context),
issuer: new Identity({ did: 'otherDid' }, context),
issuedAt: new Date(),
expiry: null,
claim: { type: ClaimType.NoData },
},
],
next: 1,
count: 1,
};
/* eslint-enabled @typescript-eslint/camelcase */

dsMockUtils.configureMocks({ contextOptions: { withSeed: true } });

const polymesh = await Polymesh.connect({
nodeUrl: 'wss://some.url',
accountUri: '//uri',
middleware: {
link: 'someLink',
key: 'someKey',
dsMockUtils.configureMocks({
contextOptions: {
issuedClaims,
},
});

dsMockUtils.createApolloQueryStub(
didsWithClaims({ trustedClaimIssuers: ['someDid'], count: 30, skip: 1 }),
{
didsWithClaims: didsWithClaimsQueryResponse,
}
);

const result = await polymesh.getIssuedClaims({ start: 1, size: 30 });

expect(result.data).toEqual(fakeClaims);
expect(result.count).toEqual(1);
expect(result.next).toBeNull();
});

test('should throw if the middleware query fails', async () => {
dsMockUtils.configureMocks({ contextOptions: { withSeed: true } });

const polymesh = await Polymesh.connect({
nodeUrl: 'wss://some.url',
accountUri: '//uri',
middleware: {
link: 'someLink',
key: 'someKey',
},
});

dsMockUtils.throwOnMiddlewareQuery();

return expect(polymesh.getIssuedClaims()).rejects.toThrow('Error in middleware query: Error');
const result = await polymesh.getIssuedClaims();
expect(result).toEqual(issuedClaims);
});
});

Expand Down
40 changes: 39 additions & 1 deletion src/api/entities/Identity/__tests__/index.ts
Expand Up @@ -6,7 +6,15 @@ import sinon from 'sinon';
import { Entity } from '~/base';
import { Context } from '~/context';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import { Role, RoleType, TickerOwnerRole, TokenOwnerRole } from '~/types';
import {
ClaimData,
ClaimType,
ResultSet,
Role,
RoleType,
TickerOwnerRole,
TokenOwnerRole,
} from '~/types';
import * as utilsModule from '~/utils';

import { Identity } from '../';
Expand Down Expand Up @@ -304,6 +312,36 @@ describe('Identity class', () => {
});
});

describe('method: getCddClaims', () => {
test('should return a list of cdd claims', async () => {
const did = 'someDid';
const identity = new Identity({ did }, context);

const issuedClaims: ResultSet<ClaimData> = {
data: [
{
target: new Identity({ did }, context),
issuer: new Identity({ did: 'otherDid' }, context),
issuedAt: new Date(),
expiry: null,
claim: { type: ClaimType.CustomerDueDiligence },
},
],
next: 1,
count: 1,
};

dsMockUtils.configureMocks({
contextOptions: {
issuedClaims,
},
});

const result = await identity.getCddClaims();
expect(result).toEqual(issuedClaims);
});
});

describe('method: getMasterKey', () => {
const did = 'someDid';
const accountKey = 'someMasterKey';
Expand Down
29 changes: 29 additions & 0 deletions src/api/entities/Identity/index.ts
Expand Up @@ -6,10 +6,13 @@ import { TickerReservation } from '~/api/entities/TickerReservation';
import { Entity, PolymeshError } from '~/base';
import { Context } from '~/context';
import {
ClaimData,
ClaimType,
ErrorCode,
isCddProviderRole,
isTickerOwnerRole,
isTokenOwnerRole,
ResultSet,
Role,
SubCallback,
UnsubCallback,
Expand Down Expand Up @@ -247,6 +250,32 @@ export class Identity extends Entity<UniqueIdentifiers> {
return assembleResult(didRecords);
}

/**
* Retrieve the list of cdd claims for the current identity
*
* @param opts.size - page size
* @param opts.start - page offset
*/
public async getCddClaims(
opts: {
size?: number;
start?: number;
} = {}
): Promise<ResultSet<ClaimData>> {
const { context, did } = this;

const { size, start } = opts;

const result = await context.issuedClaims({
targets: [did],
claimTypes: [ClaimType.CustomerDueDiligence],
size,
start,
});

return result;
}

/**
* Check whether this Identity possesses all specified roles
*/
Expand Down

0 comments on commit 75cbc54

Please sign in to comment.