From f3c232a84b4c7cdd1d78fe3ae0656ebb8ef875ac Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Tue, 2 May 2023 11:20:08 +0200 Subject: [PATCH 1/2] feat: change importAccountWithStrategy return obj --- .../src/KeyringController.test.ts | 24 +++++++++++-------- .../src/KeyringController.ts | 10 ++++++-- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/packages/keyring-controller/src/KeyringController.test.ts b/packages/keyring-controller/src/KeyringController.test.ts index 3eb38287a16..1b2327113d7 100644 --- a/packages/keyring-controller/src/KeyringController.test.ts +++ b/packages/keyring-controller/src/KeyringController.test.ts @@ -254,15 +254,17 @@ describe('KeyringController', () => { const address = '0x51253087e6f8358b5f10c0a94315d69db3357859'; const newKeyring = { accounts: [address], type: 'Simple Key Pair' }; - const obj = await keyringController.importAccountWithStrategy( - AccountImportStrategy.privateKey, - [privateKey], - ); + const { keyringState, importedAccountAddress } = + await keyringController.importAccountWithStrategy( + AccountImportStrategy.privateKey, + [privateKey], + ); const modifiedState = { ...initialState, keyrings: [initialState.keyrings[0], newKeyring], }; - expect(obj).toStrictEqual(modifiedState); + expect(keyringState).toStrictEqual(modifiedState); + expect(importedAccountAddress).toBe(address); }); it('should not import account with strategy privateKey if wrong data is provided', async () => { @@ -295,17 +297,19 @@ describe('KeyringController', () => { const address = '0xb97c80fab7a3793bbe746864db80d236f1345ea7'; - const obj = await keyringController.importAccountWithStrategy( - AccountImportStrategy.json, - [input, somePassword], - ); + const { keyringState, importedAccountAddress } = + await keyringController.importAccountWithStrategy( + AccountImportStrategy.json, + [input, somePassword], + ); const newKeyring = { accounts: [address], type: 'Simple Key Pair' }; const modifiedState = { ...initialState, keyrings: [initialState.keyrings[0], newKeyring], }; - expect(obj).toStrictEqual(modifiedState); + expect(keyringState).toStrictEqual(modifiedState); + expect(importedAccountAddress).toBe(address); }); it('should throw when passed an unrecognized strategy', async () => { diff --git a/packages/keyring-controller/src/KeyringController.ts b/packages/keyring-controller/src/KeyringController.ts index a58bf0d328f..fe3e5c59b57 100644 --- a/packages/keyring-controller/src/KeyringController.ts +++ b/packages/keyring-controller/src/KeyringController.ts @@ -356,7 +356,10 @@ export class KeyringController extends BaseController< async importAccountWithStrategy( strategy: AccountImportStrategy, args: any[], - ): Promise { + ): Promise<{ + keyringState: KeyringMemState; + importedAccountAddress: string; + }> { let privateKey; switch (strategy) { case 'privateKey': @@ -400,7 +403,10 @@ export class KeyringController extends BaseController< const allAccounts = await this.#keyring.getAccounts(); this.updateIdentities(allAccounts); this.setSelectedAddress(accounts[0]); - return this.fullUpdate(); + return { + keyringState: await this.fullUpdate(), + importedAccountAddress: accounts[0], + }; } /** From 1276e3f42118d3c96eb9d9c4df550271a92cc1cf Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Tue, 2 May 2023 12:17:13 +0200 Subject: [PATCH 2/2] docs: update jsdoc return statement --- packages/keyring-controller/src/KeyringController.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/keyring-controller/src/KeyringController.ts b/packages/keyring-controller/src/KeyringController.ts index fe3e5c59b57..188c70d070e 100644 --- a/packages/keyring-controller/src/KeyringController.ts +++ b/packages/keyring-controller/src/KeyringController.ts @@ -351,7 +351,8 @@ export class KeyringController extends BaseController< * @param strategy - Import strategy name. * @param args - Array of arguments to pass to the underlying stategy. * @throws Will throw when passed an unrecognized strategy. - * @returns Promise resolving to current state when the import is complete. + * @returns Promise resolving to keyring current state and imported account + * address. */ async importAccountWithStrategy( strategy: AccountImportStrategy,