bip39/index.js

153 lines
4.4 KiB
JavaScript
Raw Normal View History

2015-03-18 19:23:26 +11:00
var createHash = require('create-hash')
var pbkdf2 = require('pbkdf2').pbkdf2Sync
2015-03-17 15:20:18 +11:00
var randomBytes = require('randombytes')
2015-10-02 00:28:57 +10:00
// use unorm until String.prototype.normalize gets better browser support
var unorm = require('unorm')
2014-03-10 10:49:53 +08:00
function lpad (str, padString, length) {
while (str.length < length) str = padString + str
return str
}
2017-03-14 16:10:53 +03:00
var ENGLISH_WORDLIST = require('./wordlists/english.json')
var FRENCH_WORDLIST = require('./wordlists/french.json')
var ITALIAN_WORDLIST = require('./wordlists/italian.json')
2017-03-14 16:10:53 +03:00
var JAPANESE_WORDLIST = require('./wordlists/japanese.json')
var SPANISH_WORDLIST = require('./wordlists/spanish.json')
2017-03-14 16:10:53 +03:00
var DEFAULT_WORDLIST = ENGLISH_WORDLIST
2014-06-23 13:40:38 +10:00
var INVALID_MNEMONIC = 'Invalid mnemonic'
var INVALID_ENTROPY = 'Invalid entropy'
2015-10-02 00:35:03 +10:00
function salt (password) {
return 'mnemonic' + (password || '')
}
2015-10-02 00:30:01 +10:00
function mnemonicToSeed (mnemonic, password) {
2015-09-28 23:00:57 +09:00
var mnemonicBuffer = new Buffer(unorm.nfkd(mnemonic), 'utf8')
2015-10-02 00:35:03 +10:00
var saltBuffer = new Buffer(salt(unorm.nfkd(password)), 'utf8')
2015-03-27 14:41:52 +11:00
return pbkdf2(mnemonicBuffer, saltBuffer, 2048, 64, 'sha512')
}
2015-10-02 00:30:01 +10:00
function mnemonicToSeedHex (mnemonic, password) {
return mnemonicToSeed(mnemonic, password).toString('hex')
2014-03-10 10:49:53 +08:00
}
2015-10-02 00:30:01 +10:00
function mnemonicToEntropy (mnemonic, wordlist) {
wordlist = wordlist || DEFAULT_WORDLIST
var words = unorm.nfkd(mnemonic).split(' ')
if (words.length % 3 !== 0) throw new Error(INVALID_MNEMONIC)
2016-10-06 00:08:49 +11:00
if (words.some(function (word) {
return wordlist.indexOf(word) === -1
})) throw new Error(INVALID_MNEMONIC)
// convert word indices to 11 bit binary strings
2015-10-02 00:30:01 +10:00
var bits = words.map(function (word) {
var index = wordlist.indexOf(word)
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 entropy = bits.slice(0, dividerIndex)
var checksum = bits.slice(dividerIndex)
// calculate the checksum and compare
2015-10-02 00:30:01 +10:00
var entropyBytes = entropy.match(/(.{1,8})/g).map(function (bin) {
return parseInt(bin, 2)
})
if (entropy.length % 4 !== 0) throw new TypeError(INVALID_ENTROPY)
var entropyBuffer = new Buffer(entropyBytes)
var newChecksum = checksumBits(entropyBuffer)
if (newChecksum !== checksum) throw new Error('Invalid mnemonic checksum')
return entropyBuffer.toString('hex')
}
function entropyToMnemonic (entropyHex, wordlist) {
2014-08-17 10:37:14 +10:00
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)
// multiple of 4
if (entropyHex.length % 8 !== 0) throw new Error(INVALID_ENTROPY)
var entropy = new Buffer(entropyHex, 'hex')
var entropyBits = bytesToBinary([].slice.call(entropy))
var checksum = checksumBits(entropy)
2014-03-11 16:01:14 +08:00
2014-06-23 15:49:54 +10:00
var bits = entropyBits + checksum
var chunks = bits.match(/(.{1,11})/g)
2014-03-11 16:01:14 +08:00
2015-10-02 00:30:01 +10:00
var words = chunks.map(function (binary) {
2014-03-11 16:01:14 +08:00
var index = parseInt(binary, 2)
2014-06-23 15:49:54 +10:00
2014-08-17 10:37:14 +10:00
return wordlist[index]
})
2014-06-23 15:49:54 +10:00
2016-10-06 00:08:39 +11:00
return wordlist === JAPANESE_WORDLIST ? words.join('\u3000') : words.join(' ')
2014-03-11 16:01:14 +08:00
}
2015-10-02 00:30:01 +10:00
function generateMnemonic (strength, rng, wordlist) {
2014-03-31 15:11:03 +08:00
strength = strength || 128
if (strength % 32 !== 0) throw new TypeError(INVALID_ENTROPY)
2015-03-17 15:20:18 +11:00
rng = rng || randomBytes
2014-06-25 23:47:30 +10:00
var hex = rng(strength / 8).toString('hex')
2014-08-17 10:37:14 +10:00
return entropyToMnemonic(hex, wordlist)
2014-03-31 14:07:34 +08:00
}
2015-10-02 00:30:01 +10:00
function validateMnemonic (mnemonic, wordlist) {
try {
mnemonicToEntropy(mnemonic, wordlist)
} catch (e) {
return false
}
2014-06-23 15:49:54 +10:00
return true
2014-06-23 15:49:54 +10:00
}
function bytesToBinary (bytes) {
return bytes.map(function (x) {
return lpad(x.toString(2), '0', 8)
}).join('')
}
2015-10-02 00:30:01 +10:00
function checksumBits (entropyBuffer) {
2015-03-18 19:23:26 +11:00
var hash = createHash('sha256').update(entropyBuffer).digest()
2014-06-23 15:49:54 +10:00
// Calculated constants from BIP39
var ENT = entropyBuffer.length * 8
var CS = ENT / 32
2014-04-05 12:44:16 +08:00
2014-06-23 16:19:32 +10:00
return bytesToBinary([].slice.call(hash)).slice(0, CS)
2014-04-05 12:44:16 +08:00
}
2014-08-17 10:37:14 +10:00
module.exports = {
mnemonicToSeed: mnemonicToSeed,
mnemonicToSeedHex: mnemonicToSeedHex,
mnemonicToEntropy: mnemonicToEntropy,
2014-08-17 10:37:14 +10:00
entropyToMnemonic: entropyToMnemonic,
generateMnemonic: generateMnemonic,
2015-08-13 17:01:50 +10:00
validateMnemonic: validateMnemonic,
wordlists: {
2017-03-14 16:10:53 +03:00
EN: ENGLISH_WORDLIST,
JA: JAPANESE_WORDLIST,
english: ENGLISH_WORDLIST,
french: FRENCH_WORDLIST,
italian: ITALIAN_WORDLIST,
japanese: JAPANESE_WORDLIST,
spanish: SPANISH_WORDLIST
2015-08-13 17:01:50 +10:00
}
2014-08-17 10:37:14 +10:00
}