diff --git a/index.js b/index.js index 0e4d88ea..83c57686 100644 --- a/index.js +++ b/index.js @@ -247,7 +247,7 @@ class KeyringController extends EventEmitter { if ((!opts || !opts.mnemonic) && type === KEYRINGS_TYPE_MAP.HD_KEYRING) { keyring.generateRandomMnemonic(); - keyring.addAccounts(); + await keyring.addAccounts(); } const accounts = await keyring.getAccounts(); diff --git a/test/index.js b/test/index.js index 4d961496..02713e3e 100644 --- a/test/index.js +++ b/test/index.js @@ -280,6 +280,22 @@ describe('KeyringController', function () { sinon.assert.calledOnce(initSpy); }); + + it('should add HD Key Tree when addAccounts is asynchronous', async function () { + const originalAccAccounts = HdKeyring.prototype.addAccounts; + sinon.stub(HdKeyring.prototype, 'addAccounts').callsFake(function () { + return new Promise((resolve) => { + setImmediate(() => { + resolve(originalAccAccounts.bind(this)()); + }); + }); + }); + + const keyring = await keyringController.addNewKeyring('HD Key Tree'); + + const keyringAccounts = await keyring.getAccounts(); + expect(keyringAccounts).toHaveLength(1); + }); }); describe('restoreKeyring', function () {