Skip to content

Commit

Permalink
Add getKeyringsByType method (#1376)
Browse files Browse the repository at this point in the history
* feat: add getKeyringsByType method

* fix: use type unknown instead of Keyring

* chore: add deprecation tags
  • Loading branch information
mikesposito authored May 25, 2023
1 parent bc1d8ec commit 992e968
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/keyring-controller/src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,32 @@ describe('KeyringController', () => {
});
});

describe('getKeyringsByType', () => {
describe('when existing type is provided', () => {
it('should return keyrings of the right type', async () => {
await withController(async ({ controller }) => {
const keyrings = controller.getKeyringsByType(
KeyringTypes.hd,
) as KeyringObject[];
expect(keyrings).toHaveLength(1);
expect(keyrings[0].type).toBe(KeyringTypes.hd);
expect(keyrings[0].getAccounts()).toStrictEqual(
controller.state.keyrings[0].accounts.map(normalize),
);
});
});
});

describe('when non existing type is provided', () => {
it('should return an empty array', async () => {
await withController(async ({ controller }) => {
const keyrings = controller.getKeyringsByType('fake');
expect(keyrings).toHaveLength(0);
});
});
});
});

describe('importAccountWithStrategy', () => {
describe('using strategy privateKey', () => {
describe('when correct key is provided', () => {
Expand Down
16 changes: 16 additions & 0 deletions packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,29 @@ export class KeyringController extends BaseController<
* Returns the currently initialized keyring that manages
* the specified `address` if one exists.
*
* @deprecated Use of this method is discouraged as actions executed directly on
* keyrings are not being reflected in the KeyringController state and not
* persisted in the vault.
* @param account - An account address.
* @returns Promise resolving to keyring of the `account` if one exists.
*/
async getKeyringForAccount(account: string): Promise<unknown> {
return this.#keyring.getKeyringForAccount(account);
}

/**
* Returns all keyrings of the given type.
*
* @deprecated Use of this method is discouraged as actions executed directly on
* keyrings are not being reflected in the KeyringController state and not
* persisted in the vault.
* @param type - Keyring type name.
* @returns An array of keyrings of the given type.
*/
getKeyringsByType(type: KeyringTypes | string): unknown[] {
return this.#keyring.getKeyringsByType(type);
}

/**
* Imports an account with the specified import strategy.
*
Expand Down

0 comments on commit 992e968

Please sign in to comment.