Skip to content

Commit 4079eb4

Browse files
committed
instantiate keyrings with builder functions
1 parent 23c145c commit 4079eb4

2 files changed

Lines changed: 25 additions & 6 deletions

File tree

.eslintrc.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
module.exports = {
22
root: true,
3+
parserOptions: {
4+
ecmaVersion: 2018, // to support object rest spread, e.g. {...x, ...y}
5+
},
36
extends: ['@metamask/eslint-config'],
47
env: {
58
commonjs: true,

index.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ const { normalize: normalizeAddress } = require('eth-sig-util');
88
const SimpleKeyring = require('eth-simple-keyring');
99
const HdKeyring = require('@metamask/eth-hd-keyring');
1010

11-
const keyringTypes = [SimpleKeyring, HdKeyring];
11+
const keyringTypes = [
12+
keyringBuilderFactory(SimpleKeyring),
13+
keyringBuilderFactory(HdKeyring),
14+
];
1215

1316
const KEYRINGS_TYPE_MAP = {
1417
HD_KEYRING: 'HD Key Tree',
@@ -700,13 +703,12 @@ class KeyringController extends EventEmitter {
700703
async _restoreKeyring(serialized) {
701704
const { type, data } = serialized;
702705

703-
const keyring = await this._newKeyring(type);
706+
const keyring = await this._newKeyring(type, data);
704707
if (!keyring) {
705708
this._unsupportedKeyrings.push(serialized);
706709
return undefined;
707710
}
708711

709-
await keyring.deserialize(data);
710712
// getAccounts also validates the accounts for some keyrings
711713
await keyring.getAccounts();
712714
this.keyrings.push(keyring);
@@ -875,13 +877,15 @@ class KeyringController extends EventEmitter {
875877
}
876878

877879
async _newKeyring(type, data) {
878-
const Keyring = this.getKeyringClassForType(type);
880+
const keyringBuilder = this.getKeyringClassForType(type);
879881

880-
if (!Keyring) {
882+
if (!keyringBuilder) {
881883
return undefined;
882884
}
883885

884-
const keyring = new Keyring(data);
886+
const keyring = keyringBuilder();
887+
888+
await keyring.deserialize(data);
885889

886890
if (keyring.init) {
887891
await keyring.init();
@@ -891,4 +895,16 @@ class KeyringController extends EventEmitter {
891895
}
892896
}
893897

898+
function keyringBuilderFactory(KeyringClass, BridgeClass) {
899+
const builder = () => {
900+
const constructorDependencies = BridgeClass ? new BridgeClass() : undefined;
901+
return new KeyringClass(constructorDependencies);
902+
};
903+
904+
builder.type = KeyringClass.type;
905+
906+
return builder;
907+
}
908+
894909
module.exports = KeyringController;
910+
module.exports.keyringBuilderFactory = keyringBuilderFactory;

0 commit comments

Comments
 (0)