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

Patch KeyringController to accept seedphrases passed as buffers/arrays of numbers #138

Merged
merged 2 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 14 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { EventEmitter } = require('events');
const bip39 = require('bip39');
const { Buffer } = require('buffer');
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Honestly a bit confused why this is necessary... but it seems to be for lint? It's just a node method? cc @Gudahtt

Copy link
Member

Choose a reason for hiding this comment

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

What did the lint error say?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

103:11  error  'Buffer' is not defined  no-undef
104:11  error  'Buffer' is not defined  no-undef

Screen Shot 2022-05-10 at 1 17 31 PM

Copy link
Contributor

Choose a reason for hiding this comment

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

This makes sense to me as buffer is a package in the Node standard library.

Copy link
Member

Choose a reason for hiding this comment

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

In a Node.js context, Buffer is a global. ESLint is complaining here because we've declared the environment as being commonjs in the ESLint config, so it is not aware that the Node.js globals exist.

The globals are set correctly in our Node.js ESLint configuration, but that isn't being used by this library for some reason. So that would be another way to solve this. Not a blocker though, this seems OK for now.

const bip39 = require('@metamask/bip39');
const ObservableStore = require('obs-store');
const encryptor = require('browser-passworder');
const { normalize: normalizeAddress } = require('eth-sig-util');
Expand Down Expand Up @@ -92,17 +93,25 @@ class KeyringController extends EventEmitter {
*
* @emits KeyringController#unlock
* @param {string} password - The password to encrypt the vault with
* @param {string} seed - The BIP44-compliant seed phrase.
* @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.
* @returns {Promise<Object>} A Promise that resolves to the state.
*/
createNewVaultAndRestore(password, seed) {
createNewVaultAndRestore(password, seedPhrase) {
const seedPhraseAsBuffer =
typeof seedPhrase === 'string'
? Buffer.from(seedPhrase, 'utf8')
: Buffer.from(seedPhrase);

if (typeof password !== 'string') {
return Promise.reject(new Error('Password must be text.'));
}

const wordlists = Object.values(bip39.wordlists);
if (
wordlists.every((wordlist) => !bip39.validateMnemonic(seed, wordlist))
wordlists.every(
(wordlist) => !bip39.validateMnemonic(seedPhraseAsBuffer, wordlist),
)
) {
return Promise.reject(new Error('Seed phrase is invalid.'));
}
Expand All @@ -112,7 +121,7 @@ class KeyringController extends EventEmitter {
return this.persistAllKeyrings(password)
.then(() => {
return this.addNewKeyring(KEYRINGS_TYPE_MAP.HD_KEYRING, {
mnemonic: seed,
mnemonic: seedPhraseAsBuffer,
numberOfAccounts: 1,
});
})
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
"lint:fix": "yarn lint:eslint --fix && yarn lint:misc --write"
},
"dependencies": {
"@metamask/bip39": "^4.0.0",
"@metamask/eth-hd-keyring": "^4.0.2",
"bip39": "^3.0.4",
"browser-passworder": "^2.0.3",
"eth-sig-util": "^3.0.1",
"eth-simple-keyring": "^4.2.0",
Expand Down
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1162,16 +1162,6 @@ bindings@^1.2.1, bindings@^1.5.0:
dependencies:
file-uri-to-path "1.0.0"

bip39@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/bip39/-/bip39-3.0.4.tgz#5b11fed966840b5e1b8539f0f54ab6392969b2a0"
integrity sha512-YZKQlb752TrUWqHWj7XAwCSjYEgGAk+/Aas3V7NyjQeZYsztO8JnQUaCWhcnL4T+jL8nvB8typ2jRPzTlgugNw==
dependencies:
"@types/node" "11.11.6"
create-hash "^1.1.0"
pbkdf2 "^3.0.9"
randombytes "^2.0.1"

bip66@^1.1.5:
version "1.1.5"
resolved "https://registry.yarnpkg.com/bip66/-/bip66-1.1.5.tgz#01fa8748785ca70955d5011217d1b3139969ca22"
Expand Down