Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
instantiate keyrings with builder functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bergarces committed Dec 2, 2022
1 parent 23c145c commit 1dfa4de
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
30 changes: 24 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;

0 comments on commit 1dfa4de

Please sign in to comment.