Skip to content

Commit

Permalink
fix: feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Apr 6, 2020
1 parent cf1483f commit 326e090
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 18 deletions.
2 changes: 0 additions & 2 deletions src/api/entities/TickerReservation/__tests__/index.ts
@@ -1,6 +1,4 @@
import { Callback, Codec } from '@polkadot/types/types';
import BigNumber from 'bignumber.js';
import { Ticker } from 'polymesh-types/types';
import sinon from 'sinon';

import { SecurityToken } from '~/api/entities/SecurityToken';
Expand Down
21 changes: 15 additions & 6 deletions src/context/__tests__/index.ts
Expand Up @@ -4,7 +4,7 @@ import sinon from 'sinon';
import { Identity } from '~/api/entities';
import { Context } from '~/context';
import { polkadotMockUtils } from '~/testUtils/mocks';
import { balanceToBigNumber } from '~/utils';
import * as utilsModule from '~/utils';

jest.mock(
'@polkadot/api',
Expand Down Expand Up @@ -270,15 +270,18 @@ describe('Context class', () => {
);
});

test('should set currentPair to the new value', async () => {
test('should set new values for currentPair and getCurrentIdentity', async () => {
const publicKey = 'publicKey';
const newPublicKey = 'newPublicKey';
const newAddress = 'newAddress';
const newIdentityId = 'newIdentityId';
const accountKey = polkadotMockUtils.createMockAccountKey(newAddress);
const newCurrentPair = {
address: newAddress,
meta: {},
publicKey: newPublicKey,
};

polkadotMockUtils.configureMocks({
keyringOptions: {
addFromSeed: {
Expand All @@ -303,11 +306,11 @@ describe('Context class', () => {

polkadotMockUtils
.createQueryStub('identity', 'keyToIdentityIds')
.withArgs(newPublicKey)
.withArgs(accountKey)
.returns(
polkadotMockUtils.createMockOption(
polkadotMockUtils.createMockLinkedKeyInfo({
Unique: polkadotMockUtils.createMockIdentityId('newIdentityId'),
Unique: polkadotMockUtils.createMockIdentityId(newIdentityId),
})
)
);
Expand All @@ -317,9 +320,15 @@ describe('Context class', () => {
seed: 'Alice'.padEnd(32, ' '),
});

sinon
.stub(utilsModule, 'stringToAccountKey')
.withArgs(newAddress, context)
.returns(accountKey);

await context.setPair('5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY');

expect(context.currentPair).toEqual(newCurrentPair);
expect(context.getCurrentIdentity().did).toEqual(newIdentityId);
});
});

Expand Down Expand Up @@ -350,7 +359,7 @@ describe('Context class', () => {
});

const result = await context.accountBalance();
expect(result).toEqual(balanceToBigNumber(returnValue));
expect(result).toEqual(utilsModule.balanceToBigNumber(returnValue));
});

test('should return the account POLYX balance if accountId is set', async () => {
Expand All @@ -369,7 +378,7 @@ describe('Context class', () => {
});

const result = await context.accountBalance('accountId');
expect(result).toEqual(balanceToBigNumber(returnValue));
expect(result).toEqual(utilsModule.balanceToBigNumber(returnValue));
});
});

Expand Down
9 changes: 6 additions & 3 deletions src/context/index.ts
Expand Up @@ -7,7 +7,7 @@ import { IdentityId } from 'polymesh-types/types';
import { Identity } from '~/api/entities';
import { PolymeshError } from '~/base';
import { ErrorCode } from '~/types';
import { balanceToBigNumber } from '~/utils';
import { balanceToBigNumber, identityIdToString, stringToAccountKey } from '~/utils';

interface SignerData {
currentPair: IKeyringPair;
Expand Down Expand Up @@ -159,7 +159,10 @@ export class Context {
}

try {
identityIds = await identity.keyToIdentityIds(newCurrentPair.publicKey);
identityIds = await identity.keyToIdentityIds(
stringToAccountKey(newCurrentPair.address, this)
);

did = identityIds.unwrap().asUnique;
} catch (e) {
throw new PolymeshError({
Expand All @@ -169,7 +172,7 @@ export class Context {
}

this.currentPair = newCurrentPair;
this.currentIdentity = new Identity({ did: did.toString() }, this);
this.currentIdentity = new Identity({ did: identityIdToString(did) }, this);
}

/**
Expand Down
22 changes: 19 additions & 3 deletions src/utils/__tests__/index.ts
Expand Up @@ -2,6 +2,8 @@ import { bool, Bytes, u64 } from '@polkadot/types';
import * as createTypeModule from '@polkadot/types/create/createType';
import { Balance, Moment } from '@polkadot/types/interfaces';
import { ISubmittableResult } from '@polkadot/types/types';
import { u8aToString } from '@polkadot/util';
import * as utilsCrypto from '@polkadot/util-crypto';
import BigNumber from 'bignumber.js';
import {
AccountKey,
Expand Down Expand Up @@ -216,20 +218,34 @@ describe('stringToAccountKey and accountKeyToString', () => {
});

test('stringToAccountKey should convert a string to a polkadot AccountKey object', () => {
const value = 'someAccountKey';
const value = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';
const fakeResult = ('convertedAccountKey' as unknown) as AccountKey;
const context = polkadotMockUtils.getContextInstance();
const decodeAddressResult = utilsCrypto.decodeAddress(value);

mockCreateType.withArgs(context.polymeshApi.registry, 'AccountKey', value).returns(fakeResult);
sinon
.stub(utilsCrypto, 'decodeAddress')
.withArgs(value)
.returns(decodeAddressResult);

mockCreateType
.withArgs(context.polymeshApi.registry, 'AccountKey', decodeAddressResult)
.returns(fakeResult);

const result = stringToAccountKey(value, context);

expect(result).toBe(fakeResult);
});

test('accountKeyToString should convert a polkadot AccountKey object to a string', () => {
const fakeResult = 'someAccountKey';
const fakeResult = '5FHneW46xGXgs5mUiveU4sbTyGBzmstUspZC92UhjJM694ty';
const accountKey = polkadotMockUtils.createMockAccountKey(fakeResult);
const encodeAddressResult = utilsCrypto.encodeAddress(u8aToString(accountKey));

sinon
.stub(utilsCrypto, 'encodeAddress')
.withArgs(fakeResult)
.returns(encodeAddressResult);

const result = accountKeyToString(accountKey);
expect(result).toEqual(fakeResult);
Expand Down
12 changes: 8 additions & 4 deletions src/utils/index.ts
Expand Up @@ -3,7 +3,7 @@ import { createType } from '@polkadot/types/create/createType';
import { Balance, EventRecord, Moment } from '@polkadot/types/interfaces';
import { ISubmittableResult } from '@polkadot/types/types';
import { stringToU8a, u8aConcat, u8aFixLength, u8aToString } from '@polkadot/util';
import { blake2AsHex } from '@polkadot/util-crypto';
import * as utilsCrypto from '@polkadot/util-crypto';
import BigNumber from 'bignumber.js';
import stringify from 'json-stable-stringify';
import {
Expand Down Expand Up @@ -96,7 +96,7 @@ export function unserialize<UniqueIdentifiers extends object>(id: string): Uniqu
* Generate a Security Token's DID from a ticker
*/
export function tickerToDid(ticker: string): string {
return blake2AsHex(
return utilsCrypto.blake2AsHex(
u8aConcat(stringToU8a('SECURITY_TOKEN:'), u8aFixLength(stringToU8a(ticker), 96, true))
);
}
Expand Down Expand Up @@ -190,14 +190,18 @@ export function identityIdToString(identityId: IdentityId): string {
* @hidden
*/
export function stringToAccountKey(accountKey: string, context: Context): AccountKey {
return createType<'AccountKey'>(context.polymeshApi.registry, 'AccountKey', accountKey);
return createType<'AccountKey'>(
context.polymeshApi.registry,
'AccountKey',
utilsCrypto.decodeAddress(accountKey)
);
}

/**
* @hidden
*/
export function accountKeyToString(accountKey: AccountKey): string {
return u8aToString(accountKey);
return utilsCrypto.encodeAddress(u8aToString(accountKey));
}

/**
Expand Down

0 comments on commit 326e090

Please sign in to comment.