bip39/index.js

141 lines
3.8 KiB
JavaScript
Raw Normal View History

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