From cdd98df3fda6a4b32af466ce88f23e11e4c3ce4f Mon Sep 17 00:00:00 2001 From: Shuffledex Date: Wed, 22 Apr 2020 16:39:10 -0300 Subject: [PATCH] feat: accept both types (identity or string) --- src/Polymesh.ts | 16 +++++++------- src/api/entities/AuthorizationRequest.ts | 13 ++++++------ .../__tests__/AuthorizationRequest.ts | 7 +++++-- .../__tests__/consumeAuthorizationRequests.ts | 12 ++++++++--- .../consumeAuthorizationRequests.ts | 10 ++++----- src/types/index.ts | 2 ++ src/utils/__tests__/index.ts | 21 +++++++++++++++++++ src/utils/index.ts | 11 ++++++++++ 8 files changed, 69 insertions(+), 23 deletions(-) diff --git a/src/Polymesh.ts b/src/Polymesh.ts index abe0f21adc..359b076a16 100644 --- a/src/Polymesh.ts +++ b/src/Polymesh.ts @@ -8,7 +8,7 @@ import { PolymeshError, TransactionQueue } from '~/base'; import { Context } from '~/context'; import { ErrorCode } from '~/types'; import { SignerType } from '~/types/internal'; -import { signerToSignatory, tickerToString } from '~/utils'; +import { signerToSignatory, tickerToString, valueToDid } from '~/utils'; /** * Main entry point of the Polymesh SDK @@ -120,9 +120,11 @@ export class Polymesh { * Retrieve all the ticker reservations currently owned by an identity. This includes * Security Tokens that have already been launched * - * @param args.did - identity ID as stored in the blockchain + * @param args.did - identity representation or identity ID as stored in the blockchain */ - public async getTickerReservations(args?: { did: string }): Promise { + public async getTickerReservations(args?: { + did: string | Identity; + }): Promise { const { context: { polymeshApi: { @@ -137,7 +139,7 @@ export class Polymesh { let identity: string; if (args) { - identity = args.did; + identity = valueToDid(args.did); } else { identity = context.getCurrentIdentity().did; } @@ -231,9 +233,9 @@ export class Polymesh { /** * Retrieve all the Security Tokens owned by an identity * - * @param args.did - identity ID as stored in the blockchain + * @param args.did - identity representation or identity ID as stored in the blockchain */ - public async getSecurityTokens(args?: { did: string }): Promise { + public async getSecurityTokens(args?: { did: string | Identity }): Promise { const { context: { polymeshApi: { @@ -248,7 +250,7 @@ export class Polymesh { let identity: string; if (args) { - identity = args.did; + identity = valueToDid(args.did); } else { identity = context.getCurrentIdentity().did; } diff --git a/src/api/entities/AuthorizationRequest.ts b/src/api/entities/AuthorizationRequest.ts index d9abbd4aa2..401a0aa5d0 100644 --- a/src/api/entities/AuthorizationRequest.ts +++ b/src/api/entities/AuthorizationRequest.ts @@ -1,5 +1,6 @@ import BigNumber from 'bignumber.js'; +import { Identity } from '~/api/entities/Identity'; import { consumeAuthorizationRequests } from '~/api/procedures'; import { Entity, TransactionQueue } from '~/base'; import { Context } from '~/context'; @@ -33,14 +34,14 @@ export class AuthorizationRequest extends Entity { } /** - * ID of the identity to which the request was emitted + * Identity to which the request was emitted */ - public targetDid: string; + public targetIdentity: Identity; /** - * ID of the identity that emitted the request + * Identity that emitted the request */ - public issuerDid: string; + public issuerIdentity: Identity; /** * authorization request data corresponding to type of authorization @@ -79,8 +80,8 @@ export class AuthorizationRequest extends Entity { const { authId } = identifiers; - this.targetDid = targetDid; - this.issuerDid = issuerDid; + this.targetIdentity = new Identity({ did: targetDid }, context); + this.issuerIdentity = new Identity({ did: issuerDid }, context); this.authId = authId; this.expiry = expiry; this.data = data; diff --git a/src/api/entities/__tests__/AuthorizationRequest.ts b/src/api/entities/__tests__/AuthorizationRequest.ts index cdfd44e771..98add2fbf5 100644 --- a/src/api/entities/__tests__/AuthorizationRequest.ts +++ b/src/api/entities/__tests__/AuthorizationRequest.ts @@ -1,6 +1,7 @@ import BigNumber from 'bignumber.js'; import sinon from 'sinon'; +import { Identity } from '~/api/entities/Identity'; import { consumeAuthorizationRequests } from '~/api/procedures'; import { Entity, TransactionQueue } from '~/base'; import { Context } from '~/context'; @@ -36,6 +37,8 @@ describe('AuthorizationRequest class', () => { test('should assign target did, issuer did, expiry and data to instance', () => { const targetDid = 'someDid'; const issuerDid = 'otherDid'; + const targetIdentity = new Identity({ did: targetDid }, context); + const issuerIdentity = new Identity({ did: issuerDid }, context); const expiry = new Date(); const data = ('something' as unknown) as Authorization; const authRequest = new AuthorizationRequest( @@ -43,8 +46,8 @@ describe('AuthorizationRequest class', () => { context ); - expect(authRequest.targetDid).toBe(targetDid); - expect(authRequest.issuerDid).toBe(issuerDid); + expect(authRequest.targetIdentity).toEqual(targetIdentity); + expect(authRequest.issuerIdentity).toEqual(issuerIdentity); expect(authRequest.expiry).toBe(expiry); expect(authRequest.data).toBe(data); }); diff --git a/src/api/procedures/__tests__/consumeAuthorizationRequests.ts b/src/api/procedures/__tests__/consumeAuthorizationRequests.ts index 587f3d8173..c644984383 100644 --- a/src/api/procedures/__tests__/consumeAuthorizationRequests.ts +++ b/src/api/procedures/__tests__/consumeAuthorizationRequests.ts @@ -3,7 +3,7 @@ import BigNumber from 'bignumber.js'; import { AuthIdentifier } from 'polymesh-types/types'; import sinon from 'sinon'; -import { AuthorizationRequest } from '~/api/entities'; +import { AuthorizationRequest, Identity } from '~/api/entities'; import { ConsumeAuthorizationRequestsParams, isAuthorized, @@ -177,7 +177,10 @@ describe('consumeAuthorizationRequests procedure', () => { const boundFunc = isAuthorized.bind(proc); expect(boundFunc(args)).toBe(true); - args.authRequests[0].targetDid = 'notTheCurrentIdentity'; + args.authRequests[0].targetIdentity = new Identity( + { did: 'notTheCurrentIdentity' }, + mockContext + ); expect(boundFunc(args)).toBe(false); }); @@ -225,7 +228,10 @@ describe('consumeAuthorizationRequests procedure', () => { const boundFunc = isAuthorized.bind(proc); expect(boundFunc(args)).toBe(true); - args.authRequests[0].targetDid = 'notTheCurrentIdentity'; + args.authRequests[0].targetIdentity = new Identity( + { did: 'notTheCurrentIdentity' }, + mockContext + ); expect(boundFunc(args)).toBe(false); }); diff --git a/src/api/procedures/consumeAuthorizationRequests.ts b/src/api/procedures/consumeAuthorizationRequests.ts index 9cefd5aee5..714b876478 100644 --- a/src/api/procedures/consumeAuthorizationRequests.ts +++ b/src/api/procedures/consumeAuthorizationRequests.ts @@ -34,8 +34,8 @@ export async function prepareConsumeAuthorizationRequests( const requestIds = liveRequests.map(({ authId }) => numberToU64(authId, context)); this.addTransaction(tx.identity.batchAcceptAuthorization, {}, requestIds); } else { - const authIdentifiers = liveRequests.map(({ authId, targetDid }) => - authTargetToAuthIdentifier({ authId, did: targetDid }, context) + const authIdentifiers = liveRequests.map(({ authId, targetIdentity }) => + authTargetToAuthIdentifier({ authId, did: targetIdentity.did }, context) ); this.addTransaction(tx.identity.batchRemoveAuthorization, {}, authIdentifiers); } @@ -50,10 +50,10 @@ export function isAuthorized( ): boolean { const { did } = this.context.getCurrentIdentity(); - return authRequests.filter(isLive).every(({ targetDid, issuerDid }) => { - let condition = did === targetDid; + return authRequests.filter(isLive).every(({ targetIdentity, issuerIdentity }) => { + let condition = did === targetIdentity.did; if (!accept) { - condition = condition || did === issuerDid; + condition = condition || did === issuerIdentity.did; } return condition; diff --git a/src/types/index.ts b/src/types/index.ts index 88023e2cb7..83b1339265 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -1,5 +1,7 @@ import BigNumber from 'bignumber.js'; +import { Identity } from '~/api/entities/Identity'; + export enum TransactionStatus { /** * the transaction is prepped to run diff --git a/src/utils/__tests__/index.ts b/src/utils/__tests__/index.ts index 41d727f0f3..48a8ec14bf 100644 --- a/src/utils/__tests__/index.ts +++ b/src/utils/__tests__/index.ts @@ -22,6 +22,7 @@ import { } from 'polymesh-types/types'; import sinon from 'sinon'; +import { Identity } from '~/api/entities'; import { PostTransactionValue } from '~/base'; import { polkadotMockUtils } from '~/testUtils/mocks'; import { Authorization, AuthorizationType, KnownTokenType, TokenIdentifierType } from '~/types'; @@ -74,6 +75,7 @@ import { u64ToBigNumber, unserialize, unwrapValues, + valueToDid, } from '../'; jest.mock( @@ -193,6 +195,25 @@ describe('stringToIdentityId and identityIdToString', () => { }); }); +describe('valueToDid', () => { + test('valueToDid should return the Indentity DID string', () => { + const did = 'someDid'; + const context = polkadotMockUtils.getContextInstance(); + const identity = new Identity({ did }, context); + + const result = valueToDid(identity); + + expect(result).toBe(did); + }); + + test('valueToDid should return the same DID string that it receives', () => { + const did = 'someDid'; + const result = valueToDid(did); + + expect(result).toBe(did); + }); +}); + describe('stringToAccountKey and accountKeyToString', () => { beforeAll(() => { polkadotMockUtils.initMocks(); diff --git a/src/utils/index.ts b/src/utils/index.ts index c5096fe49b..5db3aadd12 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -23,6 +23,7 @@ import { TokenName, } from 'polymesh-types/types'; +import { Identity } from '~/api/entities/Identity'; import { PolymeshError, PostTransactionValue } from '~/base'; import { Context } from '~/context'; import { @@ -187,6 +188,16 @@ export function identityIdToString(identityId: IdentityId): string { return identityId.toString(); } +/** + * @hidden + */ +export function valueToDid(value: string | Identity): string { + if (typeof value === 'string') { + return value; + } + return value.did; +} + /** * @hidden */