diff --git a/.eslintrc.js b/.eslintrc.js index f371d0a7..534d285c 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,5 +1,8 @@ module.exports = { root: true, + parserOptions: { + ecmaVersion: 2018, // to support object rest spread, e.g. {...x, ...y} + }, extends: ['@metamask/eslint-config'], env: { commonjs: true, diff --git a/index.js b/index.js index 47e79384..572a8ab2 100644 --- a/index.js +++ b/index.js @@ -8,7 +8,10 @@ const { normalize: normalizeAddress } = require('eth-sig-util'); const SimpleKeyring = require('eth-simple-keyring'); const HdKeyring = require('@metamask/eth-hd-keyring'); -const keyringTypes = [SimpleKeyring, HdKeyring]; +const keyringTypes = [ + keyringBuilderFactory(SimpleKeyring), + keyringBuilderFactory(HdKeyring), +]; const KEYRINGS_TYPE_MAP = { HD_KEYRING: 'HD Key Tree', @@ -700,13 +703,12 @@ class KeyringController extends EventEmitter { async _restoreKeyring(serialized) { const { type, data } = serialized; - const keyring = await this._newKeyring(type); + const keyring = await this._newKeyring(type, data); if (!keyring) { this._unsupportedKeyrings.push(serialized); return undefined; } - await keyring.deserialize(data); // getAccounts also validates the accounts for some keyrings await keyring.getAccounts(); this.keyrings.push(keyring); @@ -875,20 +877,36 @@ class KeyringController extends EventEmitter { } async _newKeyring(type, data) { - const Keyring = this.getKeyringClassForType(type); + const keyringBuilder = this.getKeyringClassForType(type); - if (!Keyring) { + if (!keyringBuilder) { return undefined; } - const keyring = new Keyring(data); + console.log('INSTANTIATING AND INITIALISING KEYRING', type, data); + + const keyring = keyringBuilder(data); if (keyring.init) { await keyring.init(); } + await keyring.deserialize(data); + return keyring; } } +function keyringBuilderFactory(KeyringClass, BridgeClass) { + const builder = (opts) => + new KeyringClass({ + ...opts, + ...(BridgeClass && { bridge: new BridgeClass() }), + }); + builder.type = KeyringClass.type; + + return builder; +} + module.exports = KeyringController; +module.exports.keyringBuilderFactory = keyringBuilderFactory;