diff --git a/node_modules/slip39/src/slip39.js b/node_modules/slip39/src/slip39.js index aff1c16..62fe490 100644 --- a/node_modules/slip39/src/slip39.js +++ b/node_modules/slip39/src/slip39.js @@ -180,6 +180,12 @@ class Slip39 { return slipHelper.validateMnemonic(mnemonic); } + static decodeMnemonics = slipHelper.decodeMnemonics; + + static decodeMnemonic = slipHelper.decodeMnemonic; + + static combineMnemonics = slipHelper.combineMnemonics; + fromPath(path) { this.validatePath(path); diff --git a/node_modules/slip39/src/slip39_helper.js b/node_modules/slip39/src/slip39_helper.js index 8bcb956..4b77568 100644 --- a/node_modules/slip39/src/slip39_helper.js +++ b/node_modules/slip39/src/slip39_helper.js @@ -75,44 +75,21 @@ const SECRET_INDEX = 255; // // Helper functions for SLIP39 implementation. // -String.prototype.slip39EncodeHex = function () { +const slip39EncodeHex = function (str) { let bytes = []; - for (let i = 0; i < this.length; ++i) { - bytes.push(this.charCodeAt(i)); + for (let i = 0; i < str.length; ++i) { + bytes.push(str.charCodeAt(i)); } return bytes; }; -Array.prototype.slip39DecodeHex = function () { - let str = []; - const hex = this.toString().split(","); - for (let i = 0; i < hex.length; i++) { - str.push(String.fromCharCode(hex[i])); - } - return str.toString().replace(/,/g, ""); -}; - -Array.prototype.slip39Generate = function (m, v = (_) => _) { - let n = m || this.length; +const slip39Generate = function (m, v = (_) => _) { + let n = m; + const arr = [] for (let i = 0; i < n; i++) { - this[i] = v(i); - } - return this; -}; - -Array.prototype.toHexString = function () { - return Array.prototype.map - .call(this, function (byte) { - return ("0" + (byte & 0xff).toString(16)).slice(-2); - }) - .join(""); -}; - -Array.prototype.toByteArray = function (hexString) { - for (let i = 0; i < hexString.length; i = i + 2) { - this.push(parseInt(hexString.substr(i, 2), 16)); + arr[i] = v(i); } - return this; + return arr; }; const BIGINT_WORD_BITS = BigInt(8); @@ -208,11 +185,11 @@ function crypt( let IL = masterSecret.slice().slice(0, masterSecret.length / 2); let IR = masterSecret.slice().slice(masterSecret.length / 2); - const pwd = passphrase.slip39EncodeHex(); + const pwd = slip39EncodeHex(passphrase); const salt = getSalt(identifier, extendableBackupFlag); - let range = Array().slip39Generate(ROUND_COUNT); + let range = slip39Generate(ROUND_COUNT); range = encrypt ? range : range.reverse(); range.forEach((round) => { @@ -254,7 +231,7 @@ function splitSecret(threshold, shareCount, sharedSecret) { } // If the threshold is 1, then the digest of the shared secret is not used. if (threshold === 1) { - return Array().slip39Generate(shareCount, () => sharedSecret); + return slip39Generate(shareCount, () => sharedSecret); } const randomShareCount = threshold - 2; @@ -265,7 +242,7 @@ function splitSecret(threshold, shareCount, sharedSecret) { let baseShares = new Map(); let shares = []; if (randomShareCount) { - shares = Array().slip39Generate(randomShareCount, () => + shares = slip39Generate(randomShareCount, () => randomBytes(sharedSecret.length), ); shares.forEach((item, idx) => { @@ -302,14 +279,14 @@ function xor(a, b) { `Invalid padding in mnemonic or insufficient length of mnemonics (${a.length} or ${b.length})`, ); } - return Array().slip39Generate(a.length, (i) => a[i] ^ b[i]); + return slip39Generate(a.length, (i) => a[i] ^ b[i]); } function getSalt(identifier, extendableBackupFlag) { if (extendableBackupFlag) { return []; } else { - const salt = CUSTOMIZATION_STRING_NON_EXTENDABLE.slip39EncodeHex(); + const salt = slip39EncodeHex(CUSTOMIZATION_STRING_NON_EXTENDABLE); return salt.concat(identifier); } } @@ -340,7 +317,7 @@ function interpolate(shares, x) { logProd = logProd + LOG_TABLE[k ^ x]; }); - let results = Array().slip39Generate( + let results = slip39Generate( sharesValueLengths.values().next().value, () => 0, ); @@ -400,13 +377,11 @@ function get_customization_string(extendableBackupFlag) { } function rs1024CreateChecksum(data, extendableBackupFlag) { - const values = get_customization_string(extendableBackupFlag) - .slip39EncodeHex() + const values = slip39EncodeHex(get_customization_string(extendableBackupFlag)) .concat(data) - .concat(Array().slip39Generate(CHECKSUM_WORDS_LENGTH, () => 0)); + .concat(slip39Generate(CHECKSUM_WORDS_LENGTH, () => 0)); const polymod = rs1024Polymod(values) ^ 1; - const result = Array() - .slip39Generate(CHECKSUM_WORDS_LENGTH, (i) => (polymod >> (10 * i)) & 1023) + const result = slip39Generate(CHECKSUM_WORDS_LENGTH, (i) => (polymod >> (10 * i)) & 1023) .reverse(); return result; @@ -414,9 +389,7 @@ function rs1024CreateChecksum(data, extendableBackupFlag) { function rs1024VerifyChecksum(data, extendableBackupFlag) { return ( - rs1024Polymod( - get_customization_string(extendableBackupFlag) - .slip39EncodeHex() + rs1024Polymod(slip39EncodeHex(get_customization_string(extendableBackupFlag)) .concat(data), ) === 1 ); @@ -440,7 +413,7 @@ function intFromIndices(indices) { // function intToIndices(value, length, bits) { const mask = BigInt((1 << bits) - 1); - const result = Array().slip39Generate(length, (i) => + const result = slip39Generate(length, (i) => parseInt((value >> (BigInt(i) * BigInt(bits))) & mask, 10), ); return result.reverse(); @@ -1886,4 +1859,6 @@ exports = module.exports = { crypt, bitsToBytes, WORD_LIST, + decodeMnemonics, + decodeMnemonic, };