Skip to content

Commit

Permalink
TypeErrors for entropy length
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed May 12, 2017
1 parent 3854893 commit bb6bd4c
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function mnemonicToEntropy (mnemonic, wordlist) {

// calculate the checksum and compare
var entropyBytes = entropy.match(/(.{1,8})/g).map(binaryToByte)
if (entropy.length % 4 !== 0) throw new TypeError(INVALID_ENTROPY)
if (entropy.length % 4 !== 0) throw new Error(INVALID_ENTROPY)
var entropyBuffer = new Buffer(entropyBytes)

var newChecksum = checksumBits(entropyBuffer)
Expand All @@ -72,11 +72,11 @@ function entropyToMnemonic (entropyHex, wordlist) {
wordlist = wordlist || DEFAULT_WORDLIST

// 128 <= ENT <= 256
if (entropyHex.length < 32) throw new Error(INVALID_ENTROPY)
if (entropyHex.length > 64) throw new Error(INVALID_ENTROPY)
if (entropyHex.length < 32) throw new TypeError(INVALID_ENTROPY)
if (entropyHex.length > 64) throw new TypeError(INVALID_ENTROPY)

// multiple of 4
if (entropyHex.length % 8 !== 0) throw new Error(INVALID_ENTROPY)
if (entropyHex.length % 8 !== 0) throw new TypeError(INVALID_ENTROPY)

var entropy = new Buffer(entropyHex, 'hex')
var entropyBits = bytesToBinary([].slice.call(entropy))
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ test('invalid entropy', function (t) {

t.throws(function () {
bip39.entropyToMnemonic(new Buffer('', 'hex'))
}, /^Error: Invalid entropy$/, 'throws for empty entropy')
}, /^TypeError: Invalid entropy$/, 'throws for empty entropy')

t.throws(function () {
bip39.entropyToMnemonic(new Buffer('000000', 'hex'))
}, /^Error: Invalid entropy$/, 'throws for entropy that\'s not a multitude of 4 bytes')
}, /^TypeError: Invalid entropy$/, 'throws for entropy that\'s not a multitude of 4 bytes')

t.throws(function () {
bip39.entropyToMnemonic(new Buffer(new Array(1028 + 1).join('00'), 'hex'))
}, /^Error: Invalid entropy$/, 'throws for entropy that is larger than 1024')
}, /^TypeError: Invalid entropy$/, 'throws for entropy that is larger than 1024')
})

test('UTF8 passwords', function (t) {
Expand Down

0 comments on commit bb6bd4c

Please sign in to comment.