Skip to content

Commit

Permalink
feat: rename master key and signing key. MSDK-294
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Sep 24, 2020
1 parent af5cad3 commit 1f7196c
Show file tree
Hide file tree
Showing 21 changed files with 232 additions and 238 deletions.
4 changes: 2 additions & 2 deletions src/api/entities/AuthorizationRequest.ts
Expand Up @@ -47,8 +47,8 @@ export class AuthorizationRequest extends Entity<UniqueIdentifiers> {
*
* | Type | Data |
* |----------------------------|--------|
* | Attest Master Key Rotation | DID |
* | Rotate Master Key | DID |
* | Attest Primary Key Rotation| DID |
* | Rotate Primary Key | DID |
* | Transfer Ticker | Ticker |
* | Add MultiSig Signer | N/A |
* | Transfer Token Ownership | Ticker |
Expand Down
26 changes: 13 additions & 13 deletions src/api/entities/CurrentIdentity.ts
@@ -1,38 +1,38 @@
import { Identity } from '~/api/entities';
import { inviteAccount, InviteAccountParams, removeSigningKeys } from '~/api/procedures';
import { inviteAccount, InviteAccountParams, removeSecondaryKeys } from '~/api/procedures';
import { TransactionQueue } from '~/base';
import { Signer, SigningKey, SubCallback, UnsubCallback } from '~/types';
import { SecondaryKey, Signer, SubCallback, UnsubCallback } from '~/types';

/**
* Represents the Identity associated to the current [[Account]]
*/
export class CurrentIdentity extends Identity {
/**
* Get the list of signing keys related to the Identity
* Get the list of secondary keys related to the Identity
*
* @note can be subscribed to
*/
public async getSigningKeys(): Promise<SigningKey[]>;
public async getSigningKeys(callback: SubCallback<SigningKey[]>): Promise<UnsubCallback>;
public async getSecondaryKeys(): Promise<SecondaryKey[]>;
public async getSecondaryKeys(callback: SubCallback<SecondaryKey[]>): Promise<UnsubCallback>;

// eslint-disable-next-line require-jsdoc
public async getSigningKeys(
callback?: SubCallback<SigningKey[]>
): Promise<SigningKey[] | UnsubCallback> {
public async getSecondaryKeys(
callback?: SubCallback<SecondaryKey[]>
): Promise<SecondaryKey[] | UnsubCallback> {
const { context } = this;

if (callback) {
return context.getSigningKeys(callback);
return context.getSecondaryKeys(callback);
}

return context.getSigningKeys();
return context.getSecondaryKeys();
}

/**
* Remove a list of signing keys associated with the Identity
* Remove a list of secondary keys associated with the Identity
*/
public removeSigningKeys(args: { signers: Signer[] }): Promise<TransactionQueue<void>> {
return removeSigningKeys.prepare(args, this.context);
public removeSecondaryKeys(args: { signers: Signer[] }): Promise<TransactionQueue<void>> {
return removeSecondaryKeys.prepare(args, this.context);
}

/**
Expand Down
14 changes: 7 additions & 7 deletions src/api/entities/Identity/__tests__/index.ts
Expand Up @@ -329,9 +329,9 @@ describe('Identity class', () => {
});
});

describe('method: getMasterKey', () => {
describe('method: getPrimaryKey', () => {
const did = 'someDid';
const accountId = 'someMasterKey';
const accountId = 'somePrimaryKey';

let accountIdToStringStub: sinon.SinonStub<[AccountId], string>;
let didRecordsStub: sinon.SinonStub;
Expand All @@ -347,19 +347,19 @@ describe('Identity class', () => {
/* eslint-disable @typescript-eslint/camelcase */
rawDidRecord = dsMockUtils.createMockDidRecord({
roles: [],
master_key: dsMockUtils.createMockAccountId(accountId),
signing_keys: [],
primary_key: dsMockUtils.createMockAccountId(accountId),
secondary_keys: [],
});
/* eslint-enabled @typescript-eslint/camelcase */
});

test('should return a MasterKey', async () => {
test('should return a PrimaryKey', async () => {
const mockContext = dsMockUtils.getContextInstance();
const identity = new Identity({ did }, mockContext);

didRecordsStub.returns(rawDidRecord);

const result = await identity.getMasterKey();
const result = await identity.getPrimaryKey();
expect(result).toEqual(accountId);
});

Expand All @@ -375,7 +375,7 @@ describe('Identity class', () => {
});

const callback = sinon.stub();
const result = await identity.getMasterKey(callback);
const result = await identity.getPrimaryKey(callback);

expect(result).toBe(unsubCallback);
sinon.assert.calledWithExactly(callback, accountId);
Expand Down
12 changes: 6 additions & 6 deletions src/api/entities/Identity/index.ts
Expand Up @@ -212,15 +212,15 @@ export class Identity extends Entity<UniqueIdentifiers> {
}

/**
* Retrieve the master key associated with the Identity
* Retrieve the primary key associated with the Identity
*
* @note can be subscribed to
*/
public async getMasterKey(): Promise<string>;
public async getMasterKey(callback: SubCallback<string>): Promise<UnsubCallback>;
public async getPrimaryKey(): Promise<string>;
public async getPrimaryKey(callback: SubCallback<string>): Promise<UnsubCallback>;

// eslint-disable-next-line require-jsdoc
public async getMasterKey(callback?: SubCallback<string>): Promise<string | UnsubCallback> {
public async getPrimaryKey(callback?: SubCallback<string>): Promise<string | UnsubCallback> {
const {
context: {
polymeshApi: {
Expand All @@ -231,8 +231,8 @@ export class Identity extends Entity<UniqueIdentifiers> {
context,
} = this;

const assembleResult = ({ master_key: masterKey }: DidRecord): string => {
return accountIdToString(masterKey);
const assembleResult = ({ primary_key: primaryKey }: DidRecord): string => {
return accountIdToString(primaryKey);
};

const rawDid = stringToIdentityId(did, context);
Expand Down
28 changes: 14 additions & 14 deletions src/api/entities/__tests__/CurrentIdentity.ts
@@ -1,10 +1,10 @@
import sinon from 'sinon';

import { CurrentIdentity, Identity } from '~/api/entities';
import { inviteAccount, removeSigningKeys } from '~/api/procedures';
import { inviteAccount, removeSecondaryKeys } from '~/api/procedures';
import { Context, TransactionQueue } from '~/base';
import { dsMockUtils, entityMockUtils } from '~/testUtils/mocks';
import { SigningKey, SubCallback } from '~/types';
import { SecondaryKey, SubCallback } from '~/types';

describe('CurrentIdentity class', () => {
let context: Context;
Expand Down Expand Up @@ -32,44 +32,44 @@ describe('CurrentIdentity class', () => {
expect(CurrentIdentity.prototype instanceof Identity).toBe(true);
});

describe('method: getSigningKeys', () => {
test('should return a list of Signers', async () => {
describe('method: getSecondaryKeys', () => {
test('should return a list of Secondaries', async () => {
const fakeResult = [
{
signer: entityMockUtils.getAccountInstance({ address: 'someAddress' }),
permissions: [],
},
];

dsMockUtils.configureMocks({ contextOptions: { signingKeys: fakeResult } });
dsMockUtils.configureMocks({ contextOptions: { secondaryKeys: fakeResult } });

const did = 'someDid';

const identity = new CurrentIdentity({ did }, context);

const result = await identity.getSigningKeys();
const result = await identity.getSecondaryKeys();
expect(result).toEqual(fakeResult);
});

test('should allow subscription', async () => {
const unsubCallback = 'unsubCallBack';

const getSigningKeysStub = dsMockUtils
const getSecondaryKeysStub = dsMockUtils
.getContextInstance()
.getSigningKeys.resolves(unsubCallback);
.getSecondaryKeys.resolves(unsubCallback);

const did = 'someDid';

const identity = new CurrentIdentity({ did }, context);

const callback = (() => [] as unknown) as SubCallback<SigningKey[]>;
const result = await identity.getSigningKeys(callback);
const callback = (() => [] as unknown) as SubCallback<SecondaryKey[]>;
const result = await identity.getSecondaryKeys(callback);
expect(result).toEqual(unsubCallback);
sinon.assert.calledWithExactly(getSigningKeysStub, callback);
sinon.assert.calledWithExactly(getSecondaryKeysStub, callback);
});
});

describe('method: removeSigningKeys', () => {
describe('method: removeSecondaryKeys', () => {
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);
Expand All @@ -79,11 +79,11 @@ describe('CurrentIdentity class', () => {
const expectedQueue = ('someQueue' as unknown) as TransactionQueue<void>;

sinon
.stub(removeSigningKeys, 'prepare')
.stub(removeSecondaryKeys, 'prepare')
.withArgs({ signers }, context)
.resolves(expectedQueue);

const queue = await identity.removeSigningKeys({ signers });
const queue = await identity.removeSecondaryKeys({ signers });

expect(queue).toBe(expectedQueue);
});
Expand Down
10 changes: 5 additions & 5 deletions src/api/procedures/__tests__/inviteAccount.ts
Expand Up @@ -95,7 +95,7 @@ describe('inviteAccount procedure', () => {
},
});

mockContext.getSigningKeys.resolves([
mockContext.getSecondaryKeys.resolves([
{
signer,
permissions: [],
Expand Down Expand Up @@ -140,11 +140,11 @@ describe('inviteAccount procedure', () => {
);
});

test('should throw an error if the passed account is already present in the signing keys list', async () => {
test('should throw an error if the passed account is already present in the secondary keys list', async () => {
const signer = entityMockUtils.getAccountInstance({ address: 'someFakeAccount' });
const signerValue = { type: SignerType.Account, value: args.targetAccount };

mockContext.getSigningKeys.resolves([
mockContext.getSecondaryKeys.resolves([
{
signer,
permissions: [],
Expand All @@ -157,7 +157,7 @@ describe('inviteAccount procedure', () => {
const proc = procedureMockUtils.getInstance<InviteAccountParams, void>(mockContext);

await expect(prepareInviteAccount.call(proc, args)).rejects.toThrow(
'You cannot add an account that is already present in your signing keys list'
'You cannot add an account that is already present in your secondary keys list'
);
});

Expand Down Expand Up @@ -188,7 +188,7 @@ describe('inviteAccount procedure', () => {
},
});

mockContext.getSigningKeys.resolves([
mockContext.getSecondaryKeys.resolves([
{
signer,
permissions: [],
Expand Down
37 changes: 17 additions & 20 deletions src/api/procedures/__tests__/registerIdentity.ts
@@ -1,6 +1,6 @@
import { AccountId, Moment } from '@polkadot/types/interfaces';
import { AccountId } from '@polkadot/types/interfaces';
import { ISubmittableResult } from '@polkadot/types/types';
import { SigningKey as MeshSigningKey } from 'polymesh-types/types';
import { SecondaryKey as MeshSecondaryKey } from 'polymesh-types/types';
import sinon from 'sinon';

import { Identity } from '~/api/entities';
Expand All @@ -13,15 +13,17 @@ import {
import { Context, PostTransactionValue } from '~/base';
import { dsMockUtils, entityMockUtils, procedureMockUtils } from '~/testUtils/mocks';
import { Mocked } from '~/testUtils/types';
import { Permission, RoleType, SigningKey } from '~/types';
import { Permission, RoleType, SecondaryKey } from '~/types';
import { PolymeshTx } from '~/types/internal';
import * as utilsModule from '~/utils';

describe('registerIdentity procedure', () => {
let mockContext: Mocked<Context>;
let stringToAccountIdStub: sinon.SinonStub<[string, Context], AccountId>;
let dateToMomentStub: sinon.SinonStub<[Date, Context], Moment>;
let signingKeyToMeshSigningKeyStub: sinon.SinonStub<[SigningKey, Context], MeshSigningKey>;
let secondaryKeyToMeshSecondaryKeyStub: sinon.SinonStub<
[SecondaryKey, Context],
MeshSecondaryKey
>;
let addTransactionStub: sinon.SinonStub;
let registerIdentityTransaction: PolymeshTx<unknown[]>;
let identity: PostTransactionValue<Identity>;
Expand All @@ -31,8 +33,7 @@ describe('registerIdentity procedure', () => {
procedureMockUtils.initMocks();
dsMockUtils.initMocks();
stringToAccountIdStub = sinon.stub(utilsModule, 'stringToAccountId');
dateToMomentStub = sinon.stub(utilsModule, 'dateToMoment');
signingKeyToMeshSigningKeyStub = sinon.stub(utilsModule, 'signingKeyToMeshSigningKey');
secondaryKeyToMeshSecondaryKeyStub = sinon.stub(utilsModule, 'secondaryKeyToMeshSecondaryKey');
identity = ('identity' as unknown) as PostTransactionValue<Identity>;
});

Expand All @@ -56,32 +57,30 @@ describe('registerIdentity procedure', () => {

test('should add a cddRegisterIdentity transaction to the queue', async () => {
const targetAccount = 'someAccount';
const expiry = new Date('10/10/2050');
const signingKeys = [
const secondaryKeys = [
{
signer: new Identity({ did: 'someValue' }, mockContext),
permissions: [Permission.Full],
},
];
const args = {
targetAccount,
expiry,
signingKeys,
secondaryKeys,
};
const rawAccountId = dsMockUtils.createMockAccountId(targetAccount);
const rawExpiry = dsMockUtils.createMockMoment(expiry.getTime());
const rawSigningKey = dsMockUtils.createMockSigningKey({
const rawSecondaryKey = dsMockUtils.createMockSecondaryKey({
signer: dsMockUtils.createMockSignatory({
Identity: dsMockUtils.createMockIdentityId(signingKeys[0].signer.did),
Identity: dsMockUtils.createMockIdentityId(secondaryKeys[0].signer.did),
}),
permissions: [dsMockUtils.createMockPermission(signingKeys[0].permissions[0])],
permissions: [dsMockUtils.createMockPermission(secondaryKeys[0].permissions[0])],
});

const proc = procedureMockUtils.getInstance<RegisterIdentityParams, Identity>(mockContext);

stringToAccountIdStub.withArgs(targetAccount, mockContext).returns(rawAccountId);
dateToMomentStub.withArgs(expiry, mockContext).returns(rawExpiry);
signingKeyToMeshSigningKeyStub.withArgs(signingKeys[0], mockContext).returns(rawSigningKey);
secondaryKeyToMeshSecondaryKeyStub
.withArgs(secondaryKeys[0], mockContext)
.returns(rawSecondaryKey);

let result = await prepareRegisterIdentity.call(proc, args);

Expand All @@ -92,8 +91,7 @@ describe('registerIdentity procedure', () => {
resolvers: sinon.match.array,
}),
rawAccountId,
rawExpiry,
[rawSigningKey]
[rawSecondaryKey]
);
expect(result).toBe(identity);

Expand All @@ -106,7 +104,6 @@ describe('registerIdentity procedure', () => {
resolvers: sinon.match.array,
}),
rawAccountId,
null,
[]
);
expect(result).toBe(identity);
Expand Down

0 comments on commit 1f7196c

Please sign in to comment.