Browse Source

assert that length of entropy is a multitude of 4.

limit max entropy to 1024 bytes, at which point we've reached the max size of the checksum.
300
Ruben de Vries 8 years ago
parent
commit
2a5f482d7f
  1. 12
      index.js
  2. 20
      test/index.js

12
index.js

@ -38,6 +38,11 @@ function mnemonicToEntropy (mnemonic, wordlist) {
return lpad(index.toString(2), '0', 11)
}).join('')
// max entropy is 1024; (1024×8)+((1024×8)÷32) = 8448
if (bits.length > 8448) {
throw new Error('Invalid mnemonic')
}
// split the binary string into ENT/CS
var dividerIndex = Math.floor(bits.length / 33) * 32
var entropy = bits.slice(0, dividerIndex)
@ -50,7 +55,7 @@ function mnemonicToEntropy (mnemonic, wordlist) {
var entropyBuffer = new Buffer(entropyBytes)
var newChecksum = checksumBits(entropyBuffer)
if(newChecksum !== checksum) throw new Error('Invalid mnemonic checksum')
if (newChecksum !== checksum) throw new Error('Invalid mnemonic checksum')
return entropyBuffer.toString('hex')
}
@ -59,6 +64,11 @@ function entropyToMnemonic (entropy, wordlist) {
wordlist = wordlist || DEFAULT_WORDLIST
var entropyBuffer = new Buffer(entropy, 'hex')
if (entropyBuffer.length === 0 || entropyBuffer.length > 1024 || entropyBuffer.length % 4 !== 0) {
throw new Error('Invalid entropy')
}
var entropyBits = bytesToBinary([].slice.call(entropyBuffer))
var checksum = checksumBits(entropyBuffer)

20
test/index.js

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save