From af97b4180053c31182c9056c7dbe968d7630cd82 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 4 Apr 2022 13:45:30 -0500 Subject: [PATCH] patch serialize and _initFromMnemonic to account for instances where mnemonic could be buffer array or string --- index.js | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 0414370..ddeead4 100644 --- a/index.js +++ b/index.js @@ -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, }); @@ -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|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( @@ -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);