mirror of https://github.com/status-im/bip39.git
consistent checksumBits naming
This commit is contained in:
parent
bb6bd4c492
commit
e908612959
69
index.js
69
index.js
|
@ -5,11 +5,6 @@ var randomBytes = require('randombytes')
|
||||||
// use unorm until String.prototype.normalize gets better browser support
|
// use unorm until String.prototype.normalize gets better browser support
|
||||||
var unorm = require('unorm')
|
var unorm = require('unorm')
|
||||||
|
|
||||||
function lpad (str, padString, length) {
|
|
||||||
while (str.length < length) str = padString + str
|
|
||||||
return str
|
|
||||||
}
|
|
||||||
|
|
||||||
var ENGLISH_WORDLIST = require('./wordlists/english.json')
|
var ENGLISH_WORDLIST = require('./wordlists/english.json')
|
||||||
var FRENCH_WORDLIST = require('./wordlists/french.json')
|
var FRENCH_WORDLIST = require('./wordlists/french.json')
|
||||||
var ITALIAN_WORDLIST = require('./wordlists/italian.json')
|
var ITALIAN_WORDLIST = require('./wordlists/italian.json')
|
||||||
|
@ -19,6 +14,30 @@ var DEFAULT_WORDLIST = ENGLISH_WORDLIST
|
||||||
|
|
||||||
var INVALID_MNEMONIC = 'Invalid mnemonic'
|
var INVALID_MNEMONIC = 'Invalid mnemonic'
|
||||||
var INVALID_ENTROPY = 'Invalid entropy'
|
var INVALID_ENTROPY = 'Invalid entropy'
|
||||||
|
var INVALID_CHECKSUM = 'Invalid mnemonic checksum'
|
||||||
|
|
||||||
|
function lpad (str, padString, length) {
|
||||||
|
while (str.length < length) str = padString + str
|
||||||
|
return str
|
||||||
|
}
|
||||||
|
|
||||||
|
function binaryToByte (bin) {
|
||||||
|
return parseInt(bin, 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
function bytesToBinary (bytes) {
|
||||||
|
return bytes.map(function (x) {
|
||||||
|
return lpad(x.toString(2), '0', 8)
|
||||||
|
}).join('')
|
||||||
|
}
|
||||||
|
|
||||||
|
function deriveChecksumBits (entropyBuffer) {
|
||||||
|
var ENT = entropyBuffer.length * 8
|
||||||
|
var CS = ENT / 32
|
||||||
|
var hash = createHash('sha256').update(entropyBuffer).digest()
|
||||||
|
|
||||||
|
return bytesToBinary([].slice.call(hash)).slice(0, CS)
|
||||||
|
}
|
||||||
|
|
||||||
function salt (password) {
|
function salt (password) {
|
||||||
return 'mnemonic' + (password || '')
|
return 'mnemonic' + (password || '')
|
||||||
|
@ -54,18 +73,18 @@ function mnemonicToEntropy (mnemonic, wordlist) {
|
||||||
|
|
||||||
// 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 entropyBits = bits.slice(0, dividerIndex)
|
||||||
var checksum = bits.slice(dividerIndex)
|
var checksumBits = bits.slice(dividerIndex)
|
||||||
|
|
||||||
// calculate the checksum and compare
|
// calculate the checksum and compare
|
||||||
var entropyBytes = entropy.match(/(.{1,8})/g).map(binaryToByte)
|
var entropyBytes = entropyBits.match(/(.{1,8})/g).map(binaryToByte)
|
||||||
if (entropy.length % 4 !== 0) throw new Error(INVALID_ENTROPY)
|
if (entropyBytes.length % 4 !== 0) throw new Error(INVALID_ENTROPY)
|
||||||
var entropyBuffer = new Buffer(entropyBytes)
|
|
||||||
|
|
||||||
var newChecksum = checksumBits(entropyBuffer)
|
var entropy = new Buffer(entropyBytes)
|
||||||
if (newChecksum !== checksum) throw new Error('Invalid mnemonic checksum')
|
var newChecksum = deriveChecksumBits(entropy)
|
||||||
|
if (newChecksum !== checksumBits) throw new Error(INVALID_CHECKSUM)
|
||||||
|
|
||||||
return entropyBuffer.toString('hex')
|
return entropy.toString('hex')
|
||||||
}
|
}
|
||||||
|
|
||||||
function entropyToMnemonic (entropyHex, wordlist) {
|
function entropyToMnemonic (entropyHex, wordlist) {
|
||||||
|
@ -80,9 +99,9 @@ function entropyToMnemonic (entropyHex, wordlist) {
|
||||||
|
|
||||||
var entropy = new Buffer(entropyHex, 'hex')
|
var entropy = new Buffer(entropyHex, 'hex')
|
||||||
var entropyBits = bytesToBinary([].slice.call(entropy))
|
var entropyBits = bytesToBinary([].slice.call(entropy))
|
||||||
var checksum = checksumBits(entropy)
|
var checksumBits = deriveChecksumBits(entropy)
|
||||||
|
|
||||||
var bits = entropyBits + checksum
|
var bits = entropyBits + checksumBits
|
||||||
var chunks = bits.match(/(.{1,11})/g)
|
var chunks = bits.match(/(.{1,11})/g)
|
||||||
var words = chunks.map(function (binary) {
|
var words = chunks.map(function (binary) {
|
||||||
var index = binaryToByte(binary)
|
var index = binaryToByte(binary)
|
||||||
|
@ -111,26 +130,6 @@ function validateMnemonic (mnemonic, wordlist) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
function binaryToByte (bin) {
|
|
||||||
return parseInt(bin, 2)
|
|
||||||
}
|
|
||||||
|
|
||||||
function bytesToBinary (bytes) {
|
|
||||||
return bytes.map(function (x) {
|
|
||||||
return lpad(x.toString(2), '0', 8)
|
|
||||||
}).join('')
|
|
||||||
}
|
|
||||||
|
|
||||||
function checksumBits (entropyBuffer) {
|
|
||||||
var hash = createHash('sha256').update(entropyBuffer).digest()
|
|
||||||
|
|
||||||
// Calculated constants from BIP39
|
|
||||||
var ENT = entropyBuffer.length * 8
|
|
||||||
var CS = ENT / 32
|
|
||||||
|
|
||||||
return bytesToBinary([].slice.call(hash)).slice(0, CS)
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
mnemonicToSeed: mnemonicToSeed,
|
mnemonicToSeed: mnemonicToSeed,
|
||||||
mnemonicToSeedHex: mnemonicToSeedHex,
|
mnemonicToSeedHex: mnemonicToSeedHex,
|
||||||
|
|
Loading…
Reference in New Issue