|
@ -38,6 +38,11 @@ function mnemonicToEntropy (mnemonic, wordlist) { |
|
|
return lpad(index.toString(2), '0', 11) |
|
|
return lpad(index.toString(2), '0', 11) |
|
|
}).join('') |
|
|
}).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
|
|
|
// split the binary string into ENT/CS
|
|
|
var dividerIndex = Math.floor(bits.length / 33) * 32 |
|
|
var dividerIndex = Math.floor(bits.length / 33) * 32 |
|
|
var entropy = bits.slice(0, dividerIndex) |
|
|
var entropy = bits.slice(0, dividerIndex) |
|
@ -50,7 +55,7 @@ function mnemonicToEntropy (mnemonic, wordlist) { |
|
|
var entropyBuffer = new Buffer(entropyBytes) |
|
|
var entropyBuffer = new Buffer(entropyBytes) |
|
|
var newChecksum = checksumBits(entropyBuffer) |
|
|
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') |
|
|
return entropyBuffer.toString('hex') |
|
|
} |
|
|
} |
|
@ -59,6 +64,11 @@ function entropyToMnemonic (entropy, wordlist) { |
|
|
wordlist = wordlist || DEFAULT_WORDLIST |
|
|
wordlist = wordlist || DEFAULT_WORDLIST |
|
|
|
|
|
|
|
|
var entropyBuffer = new Buffer(entropy, 'hex') |
|
|
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 entropyBits = bytesToBinary([].slice.call(entropyBuffer)) |
|
|
var checksum = checksumBits(entropyBuffer) |
|
|
var checksum = checksumBits(entropyBuffer) |
|
|
|
|
|
|
|
|