Skip to content

Commit

Permalink
feat: use getIdentityClaimsFromChain
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Jan 5, 2021
1 parent 9b5aa18 commit c18e10d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
51 changes: 32 additions & 19 deletions src/Claims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import {
modifyClaims,
ModifyClaimsParams,
} from '~/internal';
import {
didsWithClaims,
issuerDidsWithClaimsByTarget,
scopesByIdentity,
} from '~/middleware/queries';
import { didsWithClaims, issuerDidsWithClaimsByTarget } from '~/middleware/queries';
import { ClaimTypeEnum, Query } from '~/middleware/types';
import {
ClaimData,
Expand All @@ -20,10 +16,11 @@ import {
IdentityWithClaims,
ResultSet,
Scope,
ScopedClaim,
ScopeType,
} from '~/types';
import { ClaimOperation, ProcedureMethod } from '~/types/internal';
import {
middlewareScopeToScope,
scopeToMiddlewareScope,
signerToString,
toIdentityWithClaimsArray,
Expand Down Expand Up @@ -227,32 +224,48 @@ export class Claims {
* 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 })
);
const { data: issuedClaimsData } = await context.issuedClaims({
targets: [did],
claimTypes: [
ClaimType.Accredited,
ClaimType.Affiliate,
ClaimType.Blocked,
ClaimType.BuyLockup,
ClaimType.Exempted,
ClaimType.InvestorUniqueness,
ClaimType.Jurisdiction,
ClaimType.KnowYourCustomer,
ClaimType.SellLockup,
],
});

// const identityClaimsFromChain = await context.getIdentityClaimsFromChain({
// targets: [did],
// claimTypes: [ClaimType.Accredited, ClaimType.Affiliate, ClaimType.Blocked, ClaimType.BuyLockup, ClaimType.Exempted, ClaimType.InvestorUniqueness, ClaimType.Jurisdiction, ClaimType.KnowYourCustomer, ClaimType.SellLockup],
// trustedClaimIssuers: undefined,
// includeExpired: true,
// });

return issuedClaimsData.map(({ claim }) => {
const {
scope: { type, value },
} = claim as ScopedClaim;

// 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);
if (type === ScopeType.Ticker) {
ticker = removePadding(value);
}

return {
scope: scope ? middlewareScopeToScope(scope) : null,
scope: { type, value: ticker ?? value },
ticker,
};
});
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/Claims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ describe('Claims Class', () => {
});
});

describe('method: getClaimScopes', () => {
describe.only('method: getClaimScopes', () => {
test('should return a list of scopes and tickers', async () => {
const target = 'someTarget';
const scopes = [
Expand Down
2 changes: 1 addition & 1 deletion src/base/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ export class Context {
{ claim_issuer: claimissuer, issuance_date: issuanceDate, expiry: rawExpiry, claim },
]) => {
const { target } = key.args[0] as Claim1stKey;
const expiry = rawExpiry ? momentToDate(rawExpiry.unwrap()) : null;
const expiry = !rawExpiry.isEmpty ? momentToDate(rawExpiry.unwrap()) : null;
if ((!includeExpired && (expiry === null || expiry > new Date())) || includeExpired) {
data.push({
target: new Identity({ did: identityIdToString(target) }, this),
Expand Down
8 changes: 7 additions & 1 deletion src/base/__tests__/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,13 @@ describe('Context class', () => {
expiry: dsMockUtils.createMockOption(dsMockUtils.createMockMoment(expiryOne.getTime())),
}
),
tuple({ args: [claim1stKey] }, identityClaim),
tuple(
{ args: [claim1stKey] },
{
...identityClaim,
expiry: dsMockUtils.createMockOption(),
}
),
tuple(
{ args: [claim1stKey] },
{
Expand Down

0 comments on commit c18e10d

Please sign in to comment.