@@ -8,7 +8,10 @@ const { normalize: normalizeAddress } = require('eth-sig-util');
88const SimpleKeyring = require ( 'eth-simple-keyring' ) ;
99const HdKeyring = require ( '@metamask/eth-hd-keyring' ) ;
1010
11- const keyringTypes = [ SimpleKeyring , HdKeyring ] ;
11+ const keyringTypes = [
12+ keyringBuilderFactory ( SimpleKeyring ) ,
13+ keyringBuilderFactory ( HdKeyring ) ,
14+ ] ;
1215
1316const 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+
894909module . exports = KeyringController ;
910+ module . exports . keyringBuilderFactory = keyringBuilderFactory ;
0 commit comments