Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change importAccountWithStrategy return #1295

Merged
merged 4 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions packages/keyring-controller/src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,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 () => {
Expand Down Expand Up @@ -303,17 +305,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 () => {
Expand Down
13 changes: 10 additions & 3 deletions packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,16 @@ 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,
args: any[],
): Promise<KeyringMemState> {
): Promise<{
keyringState: KeyringMemState;
importedAccountAddress: string;
}> {
let privateKey;
switch (strategy) {
case 'privateKey':
Expand Down Expand Up @@ -405,7 +409,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],
};
}

/**
Expand Down