|
1 | 1 | const { EventEmitter } = require('events'); |
2 | | -const { Buffer } = require('buffer'); |
3 | | -const bip39 = require('@metamask/bip39'); |
4 | 2 | const ObservableStore = require('obs-store'); |
5 | 3 | const encryptor = require('@metamask/browser-passworder'); |
6 | 4 | const { normalize: normalizeAddress } = require('@metamask/eth-sig-util'); |
@@ -107,40 +105,23 @@ class KeyringController extends EventEmitter { |
107 | 105 | * |
108 | 106 | * @fires KeyringController#unlock |
109 | 107 | * @param {string} password - The password to encrypt the vault with. |
110 | | - * @param {string|Array<number>} seedPhrase - The BIP39-compliant seed phrase, |
111 | | - * either as a string or an array of UTF-8 bytes that represent the string. |
| 108 | + * @param {Uint8Array | string} seedPhrase - The BIP39-compliant seed phrase, |
| 109 | + * either as a string or Uint8Array. |
112 | 110 | * @returns {Promise<object>} A Promise that resolves to the state. |
113 | 111 | */ |
114 | 112 | async createNewVaultAndRestore(password, seedPhrase) { |
115 | | - const seedPhraseAsBuffer = |
116 | | - typeof seedPhrase === 'string' |
117 | | - ? Buffer.from(seedPhrase, 'utf8') |
118 | | - : Buffer.from(seedPhrase); |
119 | | - |
120 | 113 | if (typeof password !== 'string') { |
121 | 114 | throw new Error('Password must be text.'); |
122 | 115 | } |
123 | | - |
124 | | - const wordlists = Object.values(bip39.wordlists); |
125 | | - if ( |
126 | | - wordlists.every( |
127 | | - (wordlist) => !bip39.validateMnemonic(seedPhraseAsBuffer, wordlist), |
128 | | - ) |
129 | | - ) { |
130 | | - throw new Error('Seed phrase is invalid.'); |
131 | | - } |
132 | | - |
133 | 116 | this.password = password; |
134 | 117 |
|
135 | | - this.clearKeyrings(); |
136 | | - const firstKeyring = await this.addNewKeyring( |
137 | | - KEYRINGS_TYPE_MAP.HD_KEYRING, |
138 | | - { |
139 | | - mnemonic: seedPhraseAsBuffer, |
140 | | - numberOfAccounts: 1, |
141 | | - }, |
142 | | - ); |
143 | | - const [firstAccount] = await firstKeyring.getAccounts(); |
| 118 | + await this.clearKeyrings(); |
| 119 | + const keyring = await this.addNewKeyring(KEYRINGS_TYPE_MAP.HD_KEYRING, { |
| 120 | + mnemonic: seedPhrase, |
| 121 | + numberOfAccounts: 1, |
| 122 | + }); |
| 123 | + const [firstAccount] = await keyring.getAccounts(); |
| 124 | + |
144 | 125 | if (!firstAccount) { |
145 | 126 | throw new Error('KeyringController - First Account not found.'); |
146 | 127 | } |
|
0 commit comments