Skip to content

Commit

Permalink
fix(root): allow instancing the SDK without an account
Browse files Browse the repository at this point in the history
  • Loading branch information
monitz87 committed Jun 25, 2021
1 parent 888a4f9 commit 2d83614
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 15 deletions.
33 changes: 18 additions & 15 deletions src/base/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@ interface ConstructorParams {
ss58Format?: number;
}

interface AddPairBaseParams {
keyring: CommonKeyring;
}

type AddPairParams = {
accountSeed?: string;
accountUri?: string;
accountMnemonic?: string;
pair?: KeyringPair;
};

/**
* Context in which the SDK is being used
*
Expand Down Expand Up @@ -264,25 +275,15 @@ export class Context {
/**
* Add a signing pair to the Keyring
*/
public addPair(params: {
accountSeed?: string;
accountUri?: string;
accountMnemonic?: string;
pair?: KeyringPair;
}): KeyringPair {
return Context._addPair({ ...params, keyring: this.keyring });
public addPair(params: AddPairParams): KeyringPair {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return Context._addPair({ ...params, keyring: this.keyring })!;
}

/**
* @hidden
*/
private static _addPair(params: {
accountSeed?: string;
accountUri?: string;
accountMnemonic?: string;
pair?: KeyringPair;
keyring: CommonKeyring;
}): KeyringPair {
private static _addPair(params: AddPairBaseParams & AddPairParams): KeyringPair | undefined {
const { accountSeed, accountUri, accountMnemonic, keyring, pair } = params;

let newPair: KeyringPair;
Expand All @@ -299,13 +300,15 @@ export class Context {
newPair = keyring.addFromUri(accountUri);
} else if (accountMnemonic) {
newPair = keyring.addFromMnemonic(accountMnemonic);
} else {
} else if (pair) {
/*
* NOTE @monitz87: the only way to avoid this assertion is to import the Keyring package
* which doesn't make sense just for this
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
newPair = keyring.addPair(pair as any);
} else {
return;
}

return newPair;
Expand Down
27 changes: 27 additions & 0 deletions src/base/__tests__/Context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { createMockAccountId } from '~/testUtils/mocks/dataSources';
import {
ClaimType,
CorporateActionKind,
KeyringPair,
SecondaryKey,
Signer,
TargetTreatment,
Expand Down Expand Up @@ -1619,6 +1620,32 @@ describe('Context class', () => {

sinon.assert.calledWith(dsMockUtils.getKeyringInstance().addFromUri, accountUri);
});

test('should add a new pair to the keyring via pair', async () => {
const newPair = {
address: '5GrwvaEF5zXb26Fz9rcQpDWS57CtERHpNehXCPcNoHGKutQY',
meta: {},
publicKey: 'publicKey',
};
dsMockUtils.configureMocks({
keyringOptions: {
getPairs: [newPair],
},
});

const context = await Context.create({
polymeshApi: dsMockUtils.getApiInstance(),
middlewareApi: dsMockUtils.getMiddlewareApi(),
accountSeed: '0x6'.padEnd(66, '0'),
});

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const pair = ('something' as unknown) as any;

context.addPair({ pair });

sinon.assert.calledWith(dsMockUtils.getKeyringInstance().addPair, pair);
});
});

describe('method: getDividendDistributionsForTokens', () => {
Expand Down

0 comments on commit 2d83614

Please sign in to comment.