Skip to content

Commit

Permalink
check entropyBytes for ENT constraints, for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed May 12, 2017
1 parent e908612 commit 110098d
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,15 @@ function mnemonicToEntropy (mnemonic, wordlist) {
return lpad(index.toString(2), '0', 11)
}).join('')

if (bits.length < 128) throw new Error(INVALID_MNEMONIC)
if (bits.length > 264) throw new Error(INVALID_MNEMONIC)

// split the binary string into ENT/CS
var dividerIndex = Math.floor(bits.length / 33) * 32
var entropyBits = bits.slice(0, dividerIndex)
var checksumBits = bits.slice(dividerIndex)

// calculate the checksum and compare
var entropyBytes = entropyBits.match(/(.{1,8})/g).map(binaryToByte)
if (entropyBytes.length < 16) throw new Error(INVALID_MNEMONIC)
if (entropyBytes.length > 32) throw new Error(INVALID_MNEMONIC)
if (entropyBytes.length % 4 !== 0) throw new Error(INVALID_ENTROPY)

var entropy = new Buffer(entropyBytes)
Expand Down

0 comments on commit 110098d

Please sign in to comment.