From 98362d5449082756cdf9b217228629db8321025f Mon Sep 17 00:00:00 2001 From: Guillem Date: Mon, 11 May 2026 09:37:37 +0200 Subject: [PATCH] fix embedded mnemonic share index bits --- src/functions/bip39_mnemonic.js | 12 ++++++++---- test/jsbtc.test.js | 4 ++++ 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/functions/bip39_mnemonic.js b/src/functions/bip39_mnemonic.js index 462579a..d6f9a41 100644 --- a/src/functions/bip39_mnemonic.js +++ b/src/functions/bip39_mnemonic.js @@ -297,12 +297,16 @@ module.exports = function (S) { sharesVerify: false, embeddedIndex: false, hex: true}); + let wordCount = m.trim().split(/\s+/).length; let e = S.mnemonicToEntropy(m, {wordList: A.wordList, checkSum: A.checkSumVerify, hex: false}); let bits; - if (A.embeddedIndex) - bits = Math.ceil(Math.log2(total))+1; - else + if (A.embeddedIndex) { + let availableBits = e.length * 8 / 32; + bits = Math.ceil(Math.log2(total + 1)); + if (bits > availableBits) + throw new Error(`Maximum ${2 ** availableBits - 1} shares allowed for ${wordCount} mnemonic words`); + } else bits = 8; let shares = S.__split_secret(threshold, total, e, bits); @@ -366,4 +370,4 @@ module.exports = function (S) { return S.entropyToMnemonic(S.__restore_secret(s), A); } -}; \ No newline at end of file +}; diff --git a/test/jsbtc.test.js b/test/jsbtc.test.js index 99a4ce8..4cc4096 100644 --- a/test/jsbtc.test.js +++ b/test/jsbtc.test.js @@ -273,6 +273,10 @@ describe(`${(browser) ? 'Browser' : 'Node'} test jsbtc library`, function () { s = splitMnemonic(3, 8, m, {embeddedIndex: true}); equal(m, combineMnemonic(s)); + s = splitMnemonic(3, 15, m, {embeddedIndex: true}); + equal(m, combineMnemonic(s.slice(0, 3))); + expect(() => splitMnemonic(3, 16, m, {embeddedIndex: true})) + .to.throw('Maximum 15 shares allowed for 12 mnemonic words'); for (let q = 0; q < 10; q++) { let t = Math.floor(Math.random() * (10 )) + 2;