Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Commit e2ebefb

Browse files
authored
Remove bip39 dependency (#179)
* remove bip39 dependence altogether
1 parent 965d72d commit e2ebefb

File tree

4 files changed

+20
-52
lines changed

4 files changed

+20
-52
lines changed

index.js

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
const { EventEmitter } = require('events');
2-
const { Buffer } = require('buffer');
3-
const bip39 = require('@metamask/bip39');
42
const ObservableStore = require('obs-store');
53
const encryptor = require('@metamask/browser-passworder');
64
const { normalize: normalizeAddress } = require('@metamask/eth-sig-util');
@@ -107,40 +105,23 @@ class KeyringController extends EventEmitter {
107105
*
108106
* @fires KeyringController#unlock
109107
* @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.
112110
* @returns {Promise<object>} A Promise that resolves to the state.
113111
*/
114112
async createNewVaultAndRestore(password, seedPhrase) {
115-
const seedPhraseAsBuffer =
116-
typeof seedPhrase === 'string'
117-
? Buffer.from(seedPhrase, 'utf8')
118-
: Buffer.from(seedPhrase);
119-
120113
if (typeof password !== 'string') {
121114
throw new Error('Password must be text.');
122115
}
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-
133116
this.password = password;
134117

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+
144125
if (!firstAccount) {
145126
throw new Error('KeyringController - First Account not found.');
146127
}

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write"
3131
},
3232
"dependencies": {
33-
"@metamask/bip39": "^4.0.0",
3433
"@metamask/browser-passworder": "^4.0.1",
3534
"@metamask/eth-hd-keyring": "^5.0.1",
3635
"@metamask/eth-sig-util": "5.0.2",

test/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,15 @@ describe('KeyringController', function () {
189189
password,
190190
'test test test palace city barely security section midnight wealth south deer',
191191
),
192-
).rejects.toThrow('Seed phrase is invalid.');
192+
).rejects.toThrow(
193+
'Eth-Hd-Keyring: Invalid secret recovery phrase provided',
194+
);
195+
196+
await expect(() =>
197+
keyringController.createNewVaultAndRestore(password, 1234),
198+
).rejects.toThrow(
199+
'Eth-Hd-Keyring: Invalid secret recovery phrase provided',
200+
);
193201
});
194202

195203
it('accepts mnemonic passed as type array of numbers', async function () {

yarn.lock

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -758,18 +758,6 @@ __metadata:
758758
languageName: node
759759
linkType: hard
760760

761-
"@metamask/bip39@npm:^4.0.0":
762-
version: 4.0.0
763-
resolution: "@metamask/bip39@npm:4.0.0"
764-
dependencies:
765-
"@types/node": 11.11.6
766-
create-hash: ^1.1.0
767-
pbkdf2: ^3.0.9
768-
randombytes: ^2.0.1
769-
checksum: 0d629de806dd0a6c6ea2ff4ab4752931b15eda5abcf2975f3beed8b4161bcc4765ec97bd8ba0146059b10178f6cf1753f74223655908bc585a76b565f26d4f16
770-
languageName: node
771-
linkType: hard
772-
773761
"@metamask/browser-passworder@npm:^4.0.1":
774762
version: 4.0.2
775763
resolution: "@metamask/browser-passworder@npm:4.0.2"
@@ -1158,13 +1146,6 @@ __metadata:
11581146
languageName: node
11591147
linkType: hard
11601148

1161-
"@types/node@npm:11.11.6":
1162-
version: 11.11.6
1163-
resolution: "@types/node@npm:11.11.6"
1164-
checksum: 075f1c011cf568e49701419acbcb55c24906b3bb5a34d9412a3b88f228a7a78401a5ad4d3e1cd6855c99aaea5ef96e37fc86ca097e50f06da92cf822befc1fff
1165-
languageName: node
1166-
linkType: hard
1167-
11681149
"@types/pbkdf2@npm:^3.0.0":
11691150
version: 3.1.0
11701151
resolution: "@types/pbkdf2@npm:3.1.0"
@@ -2786,7 +2767,6 @@ __metadata:
27862767
dependencies:
27872768
"@lavamoat/allow-scripts": ^2.1.0
27882769
"@metamask/auto-changelog": ^3.0.0
2789-
"@metamask/bip39": ^4.0.0
27902770
"@metamask/browser-passworder": ^4.0.1
27912771
"@metamask/eslint-config": ^10.0.0
27922772
"@metamask/eslint-config-jest": ^10.0.0
@@ -5383,7 +5363,7 @@ __metadata:
53835363
languageName: node
53845364
linkType: hard
53855365

5386-
"pbkdf2@npm:^3.0.17, pbkdf2@npm:^3.0.9":
5366+
"pbkdf2@npm:^3.0.17":
53875367
version: 3.1.1
53885368
resolution: "pbkdf2@npm:3.1.1"
53895369
dependencies:
@@ -5545,7 +5525,7 @@ __metadata:
55455525
languageName: node
55465526
linkType: hard
55475527

5548-
"randombytes@npm:^2.0.1, randombytes@npm:^2.0.6, randombytes@npm:^2.1.0":
5528+
"randombytes@npm:^2.0.6, randombytes@npm:^2.1.0":
55495529
version: 2.1.0
55505530
resolution: "randombytes@npm:2.1.0"
55515531
dependencies:

0 commit comments

Comments
 (0)