diff --git a/index.js b/index.js index 12f29e39..35c87de4 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,4 @@ const { EventEmitter } = require('events'); -const { Buffer } = require('buffer'); -const bip39 = require('@metamask/bip39'); const ObservableStore = require('obs-store'); const encryptor = require('@metamask/browser-passworder'); const { normalize: normalizeAddress } = require('@metamask/eth-sig-util'); @@ -107,40 +105,23 @@ class KeyringController extends EventEmitter { * * @fires KeyringController#unlock * @param {string} password - The password to encrypt the vault with. - * @param {string|Array} seedPhrase - The BIP39-compliant seed phrase, - * either as a string or an array of UTF-8 bytes that represent the string. + * @param {Uint8Array | string} seedPhrase - The BIP39-compliant seed phrase, + * either as a string or Uint8Array. * @returns {Promise} A Promise that resolves to the state. */ async createNewVaultAndRestore(password, seedPhrase) { - const seedPhraseAsBuffer = - typeof seedPhrase === 'string' - ? Buffer.from(seedPhrase, 'utf8') - : Buffer.from(seedPhrase); - if (typeof password !== 'string') { throw new Error('Password must be text.'); } - - const wordlists = Object.values(bip39.wordlists); - if ( - wordlists.every( - (wordlist) => !bip39.validateMnemonic(seedPhraseAsBuffer, wordlist), - ) - ) { - throw new Error('Seed phrase is invalid.'); - } - this.password = password; - this.clearKeyrings(); - const firstKeyring = await this.addNewKeyring( - KEYRINGS_TYPE_MAP.HD_KEYRING, - { - mnemonic: seedPhraseAsBuffer, - numberOfAccounts: 1, - }, - ); - const [firstAccount] = await firstKeyring.getAccounts(); + await this.clearKeyrings(); + const keyring = await this.addNewKeyring(KEYRINGS_TYPE_MAP.HD_KEYRING, { + mnemonic: seedPhrase, + numberOfAccounts: 1, + }); + const [firstAccount] = await keyring.getAccounts(); + if (!firstAccount) { throw new Error('KeyringController - First Account not found.'); } diff --git a/package.json b/package.json index cafec1c5..b03ef9bd 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,6 @@ "lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write" }, "dependencies": { - "@metamask/bip39": "^4.0.0", "@metamask/browser-passworder": "^4.0.1", "@metamask/eth-hd-keyring": "^5.0.1", "@metamask/eth-sig-util": "5.0.2", diff --git a/test/index.js b/test/index.js index 4fdb7e37..30e8c3f4 100644 --- a/test/index.js +++ b/test/index.js @@ -189,7 +189,15 @@ describe('KeyringController', function () { password, 'test test test palace city barely security section midnight wealth south deer', ), - ).rejects.toThrow('Seed phrase is invalid.'); + ).rejects.toThrow( + 'Eth-Hd-Keyring: Invalid secret recovery phrase provided', + ); + + await expect(() => + keyringController.createNewVaultAndRestore(password, 1234), + ).rejects.toThrow( + 'Eth-Hd-Keyring: Invalid secret recovery phrase provided', + ); }); it('accepts mnemonic passed as type array of numbers', async function () { diff --git a/yarn.lock b/yarn.lock index a466c7fe..a99eb022 100644 --- a/yarn.lock +++ b/yarn.lock @@ -758,18 +758,6 @@ __metadata: languageName: node linkType: hard -"@metamask/bip39@npm:^4.0.0": - version: 4.0.0 - resolution: "@metamask/bip39@npm:4.0.0" - dependencies: - "@types/node": 11.11.6 - create-hash: ^1.1.0 - pbkdf2: ^3.0.9 - randombytes: ^2.0.1 - checksum: 0d629de806dd0a6c6ea2ff4ab4752931b15eda5abcf2975f3beed8b4161bcc4765ec97bd8ba0146059b10178f6cf1753f74223655908bc585a76b565f26d4f16 - languageName: node - linkType: hard - "@metamask/browser-passworder@npm:^4.0.1": version: 4.0.2 resolution: "@metamask/browser-passworder@npm:4.0.2" @@ -1158,13 +1146,6 @@ __metadata: languageName: node linkType: hard -"@types/node@npm:11.11.6": - version: 11.11.6 - resolution: "@types/node@npm:11.11.6" - checksum: 075f1c011cf568e49701419acbcb55c24906b3bb5a34d9412a3b88f228a7a78401a5ad4d3e1cd6855c99aaea5ef96e37fc86ca097e50f06da92cf822befc1fff - languageName: node - linkType: hard - "@types/pbkdf2@npm:^3.0.0": version: 3.1.0 resolution: "@types/pbkdf2@npm:3.1.0" @@ -2786,7 +2767,6 @@ __metadata: dependencies: "@lavamoat/allow-scripts": ^2.1.0 "@metamask/auto-changelog": ^3.0.0 - "@metamask/bip39": ^4.0.0 "@metamask/browser-passworder": ^4.0.1 "@metamask/eslint-config": ^10.0.0 "@metamask/eslint-config-jest": ^10.0.0 @@ -5383,7 +5363,7 @@ __metadata: languageName: node linkType: hard -"pbkdf2@npm:^3.0.17, pbkdf2@npm:^3.0.9": +"pbkdf2@npm:^3.0.17": version: 3.1.1 resolution: "pbkdf2@npm:3.1.1" dependencies: @@ -5545,7 +5525,7 @@ __metadata: languageName: node linkType: hard -"randombytes@npm:^2.0.1, randombytes@npm:^2.0.6, randombytes@npm:^2.1.0": +"randombytes@npm:^2.0.6, randombytes@npm:^2.1.0": version: 2.1.0 resolution: "randombytes@npm:2.1.0" dependencies: