Skip to content

Commit

Permalink
Merge branch 'alpha' of github.com:PolymathNetwork/polymesh-sdk into …
Browse files Browse the repository at this point in the history
…feat/MSDK-378-account-permissions
  • Loading branch information
monitz87 committed Dec 13, 2020
2 parents eefb544 + 9551c44 commit 47f3829
Show file tree
Hide file tree
Showing 31 changed files with 991 additions and 208 deletions.
4 changes: 3 additions & 1 deletion scripts/transactions.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@
"enact_referendum": "enact_referendum",
"reject_referendum": "reject_referendum",
"override_referendum_enactment_period": "override_referendum_enactment_period",
"set_pending_pip_expiry": "set_pending_pip_expiry"
"set_pending_pip_expiry": "set_pending_pip_expiry",
"execute_scheduled_pip": "execute_scheduled_pip",
"expire_scheduled_pip": "expire_scheduled_pip"
},
"TechnicalCommittee": {
"set_vote_threshold": "set_vote_threshold",
Expand Down
33 changes: 31 additions & 2 deletions src/api/entities/CurrentIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import {
Instruction,
inviteAccount,
InviteAccountParams,
modifySignerPermissions,
ModifySignerPermissionsParams,
removeSecondaryKeys,
RemoveSecondaryKeysParams,
TransactionQueue,
Venue,
} from '~/internal';
Expand Down Expand Up @@ -51,12 +54,38 @@ export class CurrentIdentity extends Identity {
/**
* Remove a list of secondary keys associated with the Identity
*/
public removeSecondaryKeys(args: { signers: Signer[] }): Promise<TransactionQueue<void>> {
public removeSecondaryKeys(args: RemoveSecondaryKeysParams): Promise<TransactionQueue<void>> {
return removeSecondaryKeys.prepare(args, this.context);
}

/**
* Send an invitation to an Account to join to your Identity
* Revoke all permissions of a list of secondary keys associated with the Identity
*/
public revokePermissions(args: { secondaryKeys: Signer[] }): Promise<TransactionQueue<void>> {
const { secondaryKeys } = args;
const signers = secondaryKeys.map(signer => {
return {
signer,
permissions: { tokens: [], transactions: [], portfolios: [] },
};
});
return modifySignerPermissions.prepare({ secondaryKeys: signers }, this.context);
}

/**
* Modify all permissions of a list of secondary keys associated with the Identity
*
* @param args.secondaryKeys.permissions - list of permissions
* @param args.secondaryKeys.permissions.tokens - array of Security Tokens on which to grant permissions. A null value represents full permissions
* @param args.secondaryKeys.permissions.transactions - array of transaction tags that the Secondary Key has permission to execute. A null value represents full permissions
* @param args.secondaryKeys.permissions.portfolios - array of Portfolios for which to grant permissions. A null value represents full permissions
*/
public modifyPermissions(args: ModifySignerPermissionsParams): Promise<TransactionQueue<void>> {
return modifySignerPermissions.prepare(args, this.context);
}

/**
* Send an invitation to an Account to join this Identity
*
* @note this may create AuthorizationRequest which have to be accepted by
* the corresponding Account. An Account or Identity can
Expand Down
24 changes: 11 additions & 13 deletions src/api/entities/DefaultTrustedClaimIssuer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import BigNumber from 'bignumber.js';

import { Context, Entity, Identity } from '~/internal';
import { eventByIndexedArgs } from '~/middleware/queries';
import { EventIdEnum, ModuleIdEnum, Query } from '~/middleware/types';
import { eventByAddedTrustedClaimIssuer } from '~/middleware/queries';
import { Query } from '~/middleware/types';
import { ClaimType, Ensured, EventIdentifier } from '~/types';
import { MAX_TICKER_LENGTH } from '~/utils/constants';
import { padString } from '~/utils/internal';
Expand Down Expand Up @@ -61,30 +61,28 @@ export class DefaultTrustedClaimIssuer extends Entity<UniqueIdentifiers> {
}

/**
* Retrieve the identifier data (block number, date and event index) of the event that was emitted when the token was created
* Retrieve the identifier data (block number, date and event index) of the event that was emitted when the trusted claim issuer was added
*
* @note uses the middleware
* @note there is a possibility that the data is not ready by the time it is requested. In that case, `null` is returned
*/
public async addedAt(): Promise<EventIdentifier | null> {
const { ticker, identity, context } = this;

const result = await context.queryMiddleware<Ensured<Query, 'eventByIndexedArgs'>>(
eventByIndexedArgs({
moduleId: ModuleIdEnum.Compliancemanager,
eventId: EventIdEnum.TrustedDefaultClaimIssuerAdded,
eventArg1: padString(ticker, MAX_TICKER_LENGTH),
eventArg2: identity.did,
const result = await context.queryMiddleware<Ensured<Query, 'eventByAddedTrustedClaimIssuer'>>(
eventByAddedTrustedClaimIssuer({
ticker: padString(ticker, MAX_TICKER_LENGTH),
identityId: identity.did,
})
);

if (result.data.eventByIndexedArgs) {
if (result.data.eventByAddedTrustedClaimIssuer) {
// TODO remove null check once types fixed
/* eslint-disable @typescript-eslint/no-non-null-assertion */
return {
blockNumber: new BigNumber(result.data.eventByIndexedArgs.block_id),
blockDate: result.data.eventByIndexedArgs.block!.datetime,
eventIndex: result.data.eventByIndexedArgs.event_idx,
blockNumber: new BigNumber(result.data.eventByAddedTrustedClaimIssuer.block_id),
blockDate: result.data.eventByAddedTrustedClaimIssuer.block!.datetime,
eventIndex: result.data.eventByAddedTrustedClaimIssuer.event_idx,
};
/* eslint-enabled @typescript-eslint/no-non-null-assertion */
}
Expand Down
49 changes: 49 additions & 0 deletions src/api/entities/__tests__/CurrentIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
CurrentIdentity,
Identity,
inviteAccount,
modifySignerPermissions,
removeSecondaryKeys,
TransactionQueue,
Venue,
Expand All @@ -18,10 +19,13 @@ import * as utilsConversionModule from '~/utils/conversion';

describe('CurrentIdentity class', () => {
let context: Context;
let modifySignerPermissionsStub: sinon.SinonStub;

beforeAll(() => {
entityMockUtils.initMocks();
dsMockUtils.initMocks();

modifySignerPermissionsStub = sinon.stub(modifySignerPermissions, 'prepare');
});

beforeEach(() => {
Expand Down Expand Up @@ -103,6 +107,51 @@ describe('CurrentIdentity class', () => {
});
});

describe('method: revokePermissions', () => {
test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', async () => {
const did = 'someDid';
const identity = new CurrentIdentity({ did }, context);

const signers = [entityMockUtils.getAccountInstance({ address: 'someAccount' })];
const secondaryKeys = [
{
signer: signers[0],
permissions: { tokens: [], transactions: [], portfolios: [] },
},
];

const expectedQueue = ('someQueue' as unknown) as TransactionQueue<void>;

modifySignerPermissionsStub.withArgs({ secondaryKeys }, context).resolves(expectedQueue);

const queue = await identity.revokePermissions({ secondaryKeys: signers });

expect(queue).toBe(expectedQueue);
});
});

describe('method: modifyPermissions', () => {
test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', async () => {
const did = 'someDid';
const identity = new CurrentIdentity({ did }, context);

const secondaryKeys = [
{
signer: entityMockUtils.getAccountInstance({ address: 'someAccount' }),
permissions: { tokens: [], transactions: [], portfolios: [] },
},
];

const expectedQueue = ('someQueue' as unknown) as TransactionQueue<void>;

modifySignerPermissionsStub.withArgs({ secondaryKeys }, context).resolves(expectedQueue);

const queue = await identity.modifyPermissions({ secondaryKeys });

expect(queue).toBe(expectedQueue);
});
});

describe('method: inviteAccount', () => {
test('should prepare the procedure with the correct arguments and context, and return the resulting transaction queue', async () => {
const did = 'someDid';
Expand Down
15 changes: 6 additions & 9 deletions src/api/entities/__tests__/DefaultTrustedClaimIssuer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import BigNumber from 'bignumber.js';

import { Context, DefaultTrustedClaimIssuer, Entity, Identity } from '~/internal';
import { eventByIndexedArgs } from '~/middleware/queries';
import { EventIdEnum, ModuleIdEnum } from '~/middleware/types';
import { eventByAddedTrustedClaimIssuer } from '~/middleware/queries';
import { dsMockUtils } from '~/testUtils/mocks';
import { MAX_TICKER_LENGTH } from '~/utils/constants';
import * as utilsInternalModule from '~/utils/internal';
Expand Down Expand Up @@ -57,10 +56,8 @@ describe('DefaultTrustedClaimIssuer class', () => {
const did = 'someDid';
const ticker = 'SOMETICKER';
const variables = {
moduleId: ModuleIdEnum.Compliancemanager,
eventId: EventIdEnum.TrustedDefaultClaimIssuerAdded,
eventArg1: utilsInternalModule.padString(ticker, MAX_TICKER_LENGTH),
eventArg2: did,
ticker: utilsInternalModule.padString(ticker, MAX_TICKER_LENGTH),
identityId: did,
};

test('should return the event identifier object of the trusted claim issuer creation', async () => {
Expand All @@ -70,9 +67,9 @@ describe('DefaultTrustedClaimIssuer class', () => {
const fakeResult = { blockNumber, blockDate, eventIndex: eventIdx };
const trustedClaimIssuer = new DefaultTrustedClaimIssuer({ did, ticker }, context);

dsMockUtils.createApolloQueryStub(eventByIndexedArgs(variables), {
dsMockUtils.createApolloQueryStub(eventByAddedTrustedClaimIssuer(variables), {
/* eslint-disable @typescript-eslint/camelcase */
eventByIndexedArgs: {
eventByAddedTrustedClaimIssuer: {
block_id: blockNumber.toNumber(),
block: { datetime: blockDate },
event_idx: eventIdx,
Expand All @@ -88,7 +85,7 @@ describe('DefaultTrustedClaimIssuer class', () => {
test('should return null if the query result is empty', async () => {
const trustedClaimIssuer = new DefaultTrustedClaimIssuer({ did, ticker }, context);

dsMockUtils.createApolloQueryStub(eventByIndexedArgs(variables), {});
dsMockUtils.createApolloQueryStub(eventByAddedTrustedClaimIssuer(variables), {});
const result = await trustedClaimIssuer.addedAt();
expect(result).toBeNull();
});
Expand Down
57 changes: 52 additions & 5 deletions src/api/procedures/__tests__/consumeJoinIdentityAuthorization.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { u64 } from '@polkadot/types';
import { bool, u64 } from '@polkadot/types';
import BigNumber from 'bignumber.js';
import sinon from 'sinon';

Expand All @@ -10,22 +10,34 @@ import {
import { Account, AuthorizationRequest, Context, Identity } from '~/internal';
import { dsMockUtils, entityMockUtils, procedureMockUtils } from '~/testUtils/mocks';
import { Mocked } from '~/testUtils/types';
import { Authorization, AuthorizationType, TxTags } from '~/types';
import { Authorization, AuthorizationType, Signer, TxTags } from '~/types';
import * as utilsConversionModule from '~/utils/conversion';

describe('consumeJoinIdentityAuthorization procedure', () => {
let mockContext: Mocked<Context>;
let targetAddress: string;
let numberToU64Stub: sinon.SinonStub<[number | BigNumber, Context], u64>;
let booleanToBoolStub: sinon.SinonStub<[boolean, Context], bool>;
let rawTrue: bool;
let rawFalse: bool;
let authId: BigNumber;
let rawAuthId: u64;

beforeAll(() => {
dsMockUtils.initMocks();
targetAddress = 'someAddress';
dsMockUtils.initMocks({
contextOptions: {
currentPairAddress: targetAddress,
},
});
procedureMockUtils.initMocks();
entityMockUtils.initMocks();
numberToU64Stub = sinon.stub(utilsConversionModule, 'numberToU64');
booleanToBoolStub = sinon.stub(utilsConversionModule, 'booleanToBool');
authId = new BigNumber(1);
rawAuthId = dsMockUtils.createMockU64(authId.toNumber());
rawTrue = dsMockUtils.createMockBool(true);
rawFalse = dsMockUtils.createMockBool(false);

sinon.stub(utilsConversionModule, 'addressToKey');
});
Expand All @@ -36,6 +48,8 @@ describe('consumeJoinIdentityAuthorization procedure', () => {
addTransactionStub = procedureMockUtils.getAddTransactionStub();
mockContext = dsMockUtils.getContextInstance();
numberToU64Stub.withArgs(authId, mockContext).returns(rawAuthId);
booleanToBoolStub.withArgs(true, mockContext).returns(rawTrue);
booleanToBoolStub.withArgs(false, mockContext).returns(rawFalse);
});

afterEach(() => {
Expand Down Expand Up @@ -123,7 +137,7 @@ describe('consumeJoinIdentityAuthorization procedure', () => {

const transaction = dsMockUtils.createTxStub('identity', 'removeAuthorization');

const target = new Identity({ did: 'someOtherDid' }, mockContext);
let target: Signer = new Identity({ did: 'someOtherDid' }, mockContext);

const rawSignatory = dsMockUtils.createMockSignatory({
Identity: dsMockUtils.createMockIdentityId(target.did),
Expand Down Expand Up @@ -152,12 +166,45 @@ describe('consumeJoinIdentityAuthorization procedure', () => {
accept: false,
});

sinon.assert.calledWith(
addTransactionStub,
transaction,
{ paidByThirdParty: false },
rawSignatory,
rawAuthId,
rawFalse
);

target = new Account({ address: targetAddress }, mockContext);

await prepareConsumeJoinIdentityAuthorization.call(proc, {
authRequest: new AuthorizationRequest(
{
target,
issuer: entityMockUtils.getIdentityInstance(),
authId,
expiry: null,
data: {
type: AuthorizationType.JoinIdentity,
value: {
tokens: null,
transactions: null,
portfolios: null,
},
},
},
mockContext
),
accept: false,
});

sinon.assert.calledWith(
addTransactionStub,
transaction,
{ paidByThirdParty: true },
rawSignatory,
rawAuthId
rawAuthId,
rawTrue
);
});

Expand Down

0 comments on commit 47f3829

Please sign in to comment.