Skip to content

Commit 23c145c

Browse files
committed
Adds async init method to keyrings
1 parent 74c7e21 commit 23c145c

3 files changed

Lines changed: 60 additions & 6 deletions

File tree

index.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,11 @@ class KeyringController extends EventEmitter {
237237
* @returns {Promise<Keyring>} The new keyring.
238238
*/
239239
async addNewKeyring(type, opts) {
240-
const Keyring = this.getKeyringClassForType(type);
241-
const keyring = new Keyring(opts);
240+
const keyring = await this._newKeyring(type, opts);
241+
242242
if ((!opts || !opts.mnemonic) && type === KEYRINGS_TYPE_MAP.HD_KEYRING) {
243243
keyring.generateRandomMnemonic();
244-
keyring.addAccounts();
244+
await keyring.addAccounts();
245245
}
246246

247247
const accounts = await keyring.getAccounts();
@@ -700,12 +700,12 @@ class KeyringController extends EventEmitter {
700700
async _restoreKeyring(serialized) {
701701
const { type, data } = serialized;
702702

703-
const Keyring = this.getKeyringClassForType(type);
704-
if (!Keyring) {
703+
const keyring = await this._newKeyring(type);
704+
if (!keyring) {
705705
this._unsupportedKeyrings.push(serialized);
706706
return undefined;
707707
}
708-
const keyring = new Keyring();
708+
709709
await keyring.deserialize(data);
710710
// getAccounts also validates the accounts for some keyrings
711711
await keyring.getAccounts();
@@ -873,6 +873,22 @@ class KeyringController extends EventEmitter {
873873
);
874874
}
875875
}
876+
877+
async _newKeyring(type, data) {
878+
const Keyring = this.getKeyringClassForType(type);
879+
880+
if (!Keyring) {
881+
return undefined;
882+
}
883+
884+
const keyring = new Keyring(data);
885+
886+
if (keyring.init) {
887+
await keyring.init();
888+
}
889+
890+
return keyring;
891+
}
876892
}
877893

878894
module.exports = KeyringController;

test/index.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const sinon = require('sinon');
66
const Wallet = require('ethereumjs-wallet').default;
77

88
const KeyringController = require('..');
9+
const { KeyringMockWithInit } = require('./lib/mock-keyring');
910
const mockEncryptor = require('./lib/mock-encryptor');
1011

1112
const password = 'password123';
@@ -33,6 +34,7 @@ describe('KeyringController', function () {
3334
beforeEach(async function () {
3435
keyringController = new KeyringController({
3536
encryptor: mockEncryptor,
37+
keyringTypes: [KeyringMockWithInit],
3638
});
3739

3840
await keyringController.createNewVaultAndKeychain(password);
@@ -240,6 +242,23 @@ describe('KeyringController', function () {
240242
const allAccounts = await keyringController.getAccounts();
241243
expect(allAccounts).toHaveLength(3);
242244
});
245+
246+
it('should call init method if available', async function () {
247+
const Keyring = keyringController.getKeyringClassForType(
248+
'Keyring Mock With Init',
249+
);
250+
251+
const initSpy = sinon.spy(Keyring.prototype, 'init');
252+
253+
const keyring = await keyringController.addNewKeyring(
254+
'Keyring Mock With Init',
255+
);
256+
257+
const keyringAccounts = await keyring.getAccounts();
258+
expect(keyringAccounts).toHaveLength(0);
259+
260+
expect(initSpy.calledOnce).toBe(true);
261+
});
243262
});
244263

245264
describe('restoreKeyring', function () {

test/lib/mock-keyring.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class KeyringMockWithInit {
2+
init() {
3+
return Promise.resolve();
4+
}
5+
6+
getAccounts() {
7+
return [];
8+
}
9+
10+
serialize() {
11+
return Promise.resolve({});
12+
}
13+
}
14+
15+
KeyringMockWithInit.type = 'Keyring Mock With Init';
16+
17+
module.exports = {
18+
KeyringMockWithInit,
19+
};

0 commit comments

Comments
 (0)