Skip to content

Commit

Permalink
Add addNewAccountWithoutUpdate method (#288)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickycodes committed Oct 22, 2020
1 parent bafc675 commit b7e3316
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/keyring/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,22 @@ export class KeyringController extends BaseController<KeyringConfig, KeyringStat
return this.fullUpdate();
}

/**
* Adds a new account to the default (first) HD seed phrase keyring without updating identities in preferences
*
* @returns - Promise resolving to current state when the account is added
*/
async addNewAccountWithoutUpdate(): Promise<KeyringMemState> {
const primaryKeyring = privates.get(this).keyring.getKeyringsByType('HD Key Tree')[0];
/* istanbul ignore if */
if (!primaryKeyring) {
throw new Error('No HD keyring found');
}
await privates.get(this).keyring.addNewAccount(primaryKeyring);
await this.verifySeedPhrase();
return this.fullUpdate();
}

/**
* Effectively the same as creating a new keychain then populating it
* using the given seed phrase
Expand Down
13 changes: 13 additions & 0 deletions tests/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,23 @@ describe('KeyringController', () => {
});

it('should add new account', async () => {
const initialIdentitiesLength = Object.keys(preferences.state.identities).length;
const currentKeyringMemState = await keyringController.addNewAccount();
expect(initialState.keyrings).toHaveLength(1);
expect(initialState.keyrings[0].accounts).not.toBe(currentKeyringMemState.keyrings);
expect(currentKeyringMemState.keyrings[0].accounts).toHaveLength(2);
const identitiesLength = Object.keys(preferences.state.identities).length;
expect(identitiesLength).toBeGreaterThan(initialIdentitiesLength);
});

it('should add new account without updating', async () => {
const initialIdentitiesLength = Object.keys(preferences.state.identities).length;
const currentKeyringMemState = await keyringController.addNewAccountWithoutUpdate();
expect(initialState.keyrings).toHaveLength(1);
expect(initialState.keyrings[0].accounts).not.toBe(currentKeyringMemState.keyrings);
expect(currentKeyringMemState.keyrings[0].accounts).toHaveLength(2);
const identitiesLength = Object.keys(preferences.state.identities).length;
expect(identitiesLength).toEqual(initialIdentitiesLength);
});

it('should create new vault and keychain', async () => {
Expand Down

0 comments on commit b7e3316

Please sign in to comment.