Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 9 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -107,40 +105,23 @@ class KeyringController extends EventEmitter {
*
* @fires KeyringController#unlock
* @param {string} password - The password to encrypt the vault with.
* @param {string|Array<number>} 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<object>} 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();
Copy link
Member

@Gudahtt Gudahtt Dec 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, another async call without an await. In this case the function wasn't doing anything async, so this should result in no functional change.

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.');
}
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 9 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
24 changes: 2 additions & 22 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down