Skip to content

Commit 26ba2c2

Browse files
committed
limit max entropy to 1024 bytes, at which point we've reached the max size of the checksum
1 parent aff8984 commit 26ba2c2

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ function mnemonicToEntropy (mnemonic, wordlist) {
4242
return lpad(index.toString(2), '0', 11)
4343
}).join('')
4444

45+
// max entropy is 1024; (1024×8)+((1024×8)÷32) = 8448
46+
assert(bits.length <= 8448, 'Invalid mnemonic')
47+
4548
// split the binary string into ENT/CS
4649
var dividerIndex = Math.floor(bits.length / 33) * 32
4750
var entropy = bits.slice(0, dividerIndex)
@@ -64,7 +67,7 @@ function entropyToMnemonic (entropy, wordlist) {
6467

6568
var entropyBuffer = new Buffer(entropy, 'hex')
6669

67-
assert(entropyBuffer.length && entropyBuffer.length % 4 === 0, 'Invalid entropy')
70+
assert(entropyBuffer.length > 0 && entropyBuffer.length <= 1024 && entropyBuffer.length % 4 === 0, 'Invalid entropy')
6871

6972
var entropyBits = bytesToBinary([].slice.call(entropyBuffer))
7073
var checksum = checksumBits(entropyBuffer)

test/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ describe('BIP39', function () {
8585
e = _e;
8686
}
8787
assert(e && e.message === 'Invalid entropy');
88+
89+
var e;
90+
try {
91+
BIP39.entropyToMnemonic(new Buffer(new Array(1028 + 1).join('00'), 'hex'));
92+
e = null;
93+
} catch (_e) {
94+
e = _e;
95+
}
96+
assert(e && e.message === 'Invalid entropy');
8897
})
8998
})
9099

0 commit comments

Comments
 (0)