Skip to content

Commit

Permalink
feat: accept both types (identity or string)
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Apr 22, 2020
1 parent fd6ff08 commit cdd98df
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 23 deletions.
16 changes: 9 additions & 7 deletions src/Polymesh.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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<TickerReservation[]> {
public async getTickerReservations(args?: {
did: string | Identity;
}): Promise<TickerReservation[]> {
const {
context: {
polymeshApi: {
Expand All @@ -137,7 +139,7 @@ export class Polymesh {
let identity: string;

if (args) {
identity = args.did;
identity = valueToDid(args.did);
} else {
identity = context.getCurrentIdentity().did;
}
Expand Down Expand Up @@ -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<SecurityToken[]> {
public async getSecurityTokens(args?: { did: string | Identity }): Promise<SecurityToken[]> {
const {
context: {
polymeshApi: {
Expand All @@ -248,7 +250,7 @@ export class Polymesh {
let identity: string;

if (args) {
identity = args.did;
identity = valueToDid(args.did);
} else {
identity = context.getCurrentIdentity().did;
}
Expand Down
13 changes: 7 additions & 6 deletions 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';
Expand Down Expand Up @@ -33,14 +34,14 @@ export class AuthorizationRequest extends Entity<UniqueIdentifiers> {
}

/**
* 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
Expand Down Expand Up @@ -79,8 +80,8 @@ export class AuthorizationRequest extends Entity<UniqueIdentifiers> {

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;
Expand Down
7 changes: 5 additions & 2 deletions 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';
Expand Down Expand Up @@ -36,15 +37,17 @@ 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(
{ targetDid, issuerDid, expiry, data, authId: new BigNumber(1) },
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);
});
Expand Down
12 changes: 9 additions & 3 deletions src/api/procedures/__tests__/consumeAuthorizationRequests.ts
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
});
Expand Down Expand Up @@ -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);
});
Expand Down
10 changes: 5 additions & 5 deletions src/api/procedures/consumeAuthorizationRequests.ts
Expand Up @@ -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);
}
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions 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
Expand Down
21 changes: 21 additions & 0 deletions src/utils/__tests__/index.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -74,6 +75,7 @@ import {
u64ToBigNumber,
unserialize,
unwrapValues,
valueToDid,
} from '../';

jest.mock(
Expand Down Expand Up @@ -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();
Expand Down
11 changes: 11 additions & 0 deletions src/utils/index.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit cdd98df

Please sign in to comment.