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

Commit

Permalink
patch serialize and _initFromMnemonic to account for instances where …
Browse files Browse the repository at this point in the history
…mnemonic could be buffer array or string
  • Loading branch information
adonesky1 committed Apr 4, 2022
1 parent c2faaac commit af97b41
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@ class HdKeyring extends SimpleKeyring {
}

serialize() {
const mnemonicAsBuffer =
typeof this.mnemonic === 'string'
? Buffer.from(this.mnemonic, 'utf8')
: this.mnemonic;

return Promise.resolve({
mnemonic: this.mnemonic,
mnemonic: Array.from(mnemonicAsBuffer.values()),
numberOfAccounts: this.wallets.length,
hdPath: this.hdPath,
});
Expand Down Expand Up @@ -80,6 +85,13 @@ class HdKeyring extends SimpleKeyring {

/* PRIVATE METHODS */

/**
* Sets appropriate properties for the keyring based on the given
* BIP39-compliant mnemonic.
*
* @param {string|Array<number>|Buffer} mnemonic - A seed phrase represented
* as a string, an array of UTF-8 bytes, or a Buffer.
*/
_initFromMnemonic(mnemonic) {
if (this.root) {
throw new Error(
Expand All @@ -93,7 +105,15 @@ class HdKeyring extends SimpleKeyring {
'Eth-Hd-Keyring: Invalid secret recovery phrase provided',
);
}
this.mnemonic = mnemonic;

if (typeof mnemonic === 'string') {
this.mnemonic = Buffer.from(mnemonic, 'utf8');
} else if (Array.isArray(mnemonic)) {
this.mnemonic = Buffer.from(mnemonic);
} else {
this.mnemonic = mnemonic;
}

// eslint-disable-next-line node/no-sync
const seed = bip39.mnemonicToSeedSync(mnemonic);
this.hdWallet = hdkey.fromMasterSeed(seed);
Expand Down

0 comments on commit af97b41

Please sign in to comment.