@@ -106,37 +106,24 @@ class KeyringController extends EventEmitter {
106106 * creates a new encrypted store with the given password,
107107 * creates a new HD wallet from the given seed with 1 account.
108108 *
109- * @emits KeyringController#unlock
109+ * @fires KeyringController#unlock
110110 * @param {string } password - The password to encrypt the vault with
111111 * @param {Uint8Array | string } seedPhrase - The BIP39-compliant seed phrase,
112112 * either as a string or Uint8Array.
113- * @returns {Promise<Object > } A Promise that resolves to the state.
113+ * @returns {Promise<object > } A Promise that resolves to the state.
114114 */
115115 createNewVaultAndRestore ( password , seedPhrase ) {
116- let encodedSeedPhrase = seedPhrase ;
117- if ( typeof encodedSeedPhrase === 'string' ) {
118- const indices = seedPhrase
119- . split ( ' ' )
120- . map ( ( word ) => wordlist . indexOf ( word ) ) ;
121- encodedSeedPhrase = new Uint8Array ( new Uint16Array ( indices ) . buffer ) ;
122- } else if (
123- seedPhrase instanceof Object &&
124- ! ( seedPhrase instanceof Uint8Array )
125- ) {
126- // when passed from the frontend to the background process a Uint8Array becomes a javascript object
127- encodedSeedPhrase = Uint8Array . from ( Object . values ( seedPhrase ) ) ;
128- }
129-
130- if ( typeof password !== 'string' ) {
131- throw new Error ( 'Password must be text.' ) ;
132- }
116+ const encodedSeedPhrase = this . _mnemonicToUint8Array ( seedPhrase ) ;
133117
134118 if ( ! bip39 . validateMnemonic ( encodedSeedPhrase , wordlist ) ) {
135- return Promise . reject ( new Error ( 'Seed phrase is invalid.' ) ) ;
119+ return Promise . reject (
120+ new Error ( 'KeyringController - Seed phrase is invalid.' ) ,
121+ ) ;
136122 }
137123
138124 this . password = password ;
139125
126+ this . clearKeyrings ( ) ;
140127 return this . persistAllKeyrings ( password )
141128 . then ( ( ) => {
142129 return this . addNewKeyring ( KEYRINGS_TYPE_MAP . HD_KEYRING , {
@@ -924,6 +911,52 @@ class KeyringController extends EventEmitter {
924911
925912 return keyring ;
926913 }
914+
915+ /*
916+ Utility Methods
917+ */
918+
919+ _uint8ArrayToString ( mnemonic ) {
920+ const recoveredIndices = Array . from (
921+ new Uint16Array ( new Uint8Array ( mnemonic ) . buffer ) ,
922+ ) ;
923+ return recoveredIndices . map ( ( i ) => wordlist [ i ] ) . join ( ' ' ) ;
924+ }
925+
926+ _stringToUint8Array ( mnemonic ) {
927+ const indices = mnemonic . split ( ' ' ) . map ( ( word ) => wordlist . indexOf ( word ) ) ;
928+ return new Uint8Array ( new Uint16Array ( indices ) . buffer ) ;
929+ }
930+
931+ _mnemonicToUint8Array ( mnemonic ) {
932+ let mnemonicData = mnemonic ;
933+ // when encrypted/decrypted, buffers get cast into js object with a property type set to buffer
934+ if ( mnemonic && mnemonic . type && mnemonic . type === 'Buffer' ) {
935+ mnemonicData = mnemonic . data ;
936+ }
937+
938+ if (
939+ // this block is for backwards compatibility with vaults that were previously stored as buffers, number arrays or plain text strings
940+ typeof mnemonicData === 'string' ||
941+ Buffer . isBuffer ( mnemonicData ) ||
942+ Array . isArray ( mnemonicData )
943+ ) {
944+ let mnemonicAsString = mnemonicData ;
945+ if ( Array . isArray ( mnemonicData ) ) {
946+ mnemonicAsString = Buffer . from ( mnemonicData ) . toString ( ) ;
947+ } else if ( Buffer . isBuffer ( mnemonicData ) ) {
948+ mnemonicAsString = mnemonicData . toString ( ) ;
949+ }
950+ return this . _stringToUint8Array ( mnemonicAsString ) ;
951+ } else if (
952+ mnemonicData instanceof Object &&
953+ ! ( mnemonicData instanceof Uint8Array )
954+ ) {
955+ // when encrypted/decrypted the Uint8Array becomes a js object we need to cast back to a Uint8Array
956+ return Uint8Array . from ( Object . values ( mnemonicData ) ) ;
957+ }
958+ return mnemonicData ;
959+ }
927960}
928961
929962/**
0 commit comments