bip39/index.js

113 lines
3.1 KiB
JavaScript
Raw Normal View History

2014-03-11 04:38:09 +00:00
var Crypto = require('crypto-js')
2014-03-14 09:05:58 +00:00
var path = require('path')
var includeFolder = require('include-folder')
2014-03-15 02:10:39 +00:00
var Wordlists = includeFolder(path.join(__dirname, 'wordlists'))
var crypto = require('crypto')
2014-03-10 02:49:53 +00:00
2014-03-11 04:38:09 +00:00
module.exports = BIP39
function BIP39(language){
language = language || 'en'
this.wordlist = JSON.parse(Wordlists[language])
2014-03-10 02:49:53 +00:00
}
2014-03-11 04:38:09 +00:00
BIP39.prototype.mnemonicToSeed = function(mnemonic, password){
2014-03-10 02:49:53 +00:00
var options = {iterations: 2048, hasher: Crypto.algo.SHA512, keySize: 512/32}
return Crypto.PBKDF2(mnemonic, salt(password), options).toString(Crypto.enc.Hex)
}
2014-03-11 08:01:14 +00:00
BIP39.prototype.entropyToMnemonic = function(entropy){
var entropyBuffer = new Buffer(entropy, 'hex')
var hash = crypto.createHash('sha256').update(entropyBuffer).digest()
2014-03-11 08:01:14 +00:00
var combined = Buffer.concat([entropyBuffer, hash])
var bitLength = entropyBuffer.length * 8 + entropyBuffer.length / 4
var bits = bytesToBinary([].slice.call(combined)).substr(0, bitLength)
2014-03-11 08:01:14 +00:00
var chunks = (bits).match(/(.{1,11})/g)
2014-03-11 08:01:14 +00:00
return chunks.map(function(binary){
var index = parseInt(binary, 2)
return this.wordlist[index]
}, this).join(' ')
}
2014-03-31 06:07:34 +00:00
BIP39.prototype.generateMnemonic = function(strength){
2014-03-31 07:11:03 +00:00
strength = strength || 128
var entropy = crypto.randomBytes(strength/8).toString('hex')
2014-03-31 06:07:34 +00:00
return this.entropyToMnemonic(entropy)
}
2014-04-05 04:44:16 +00:00
BIP39.prototype.validate = function(mnemonic){
mnemonic = mnemonic.split(' ')
if(mnemonic.length % 3 !== 0) return false
var wordlist = this.wordlist
var belongToList = mnemonic.reduce(function(memo, m){
return memo && (wordlist.indexOf(m) > -1)
}, true)
if(!belongToList) return false
var bits = mnemonic.map(function(m){
var id = wordlist.indexOf(m)
return lpad(id.toString(2), '0', 11)
}).join('')
var length = bits.length
var dividerIndex = Math.floor(length / 33) * 32
var checksum = bits.substring(dividerIndex)
var data = bits.substring(0, dividerIndex)
var bytes = data.match(/(.{1,8})/g).map(function(bin){
return parseInt(bin, 2)
})
var hash = Crypto.SHA256(bytesToWordArray(bytes))
var checksumBits = bytesToBinary(wordsToBytes(hash.words))
var checksum2 = checksumBits.substr(0, length - dividerIndex)
return checksum === checksum2
}
2014-03-10 02:49:53 +00:00
function salt(password) {
return encode_utf8('mnemonic' + (password || ''))
}
function encode_utf8(s){
return unescape(encodeURIComponent(s))
}
2014-03-11 08:01:14 +00:00
//=========== helper methods from bitcoinjs-lib ========
function bytesToBinary(bytes) {
return bytes.map(function(x) {
return lpad(x.toString(2), '0', 8)
}).join('');
}
function bytesToWordArray(bytes) {
return new Crypto.lib.WordArray.init(bytesToWords(bytes), bytes.length)
}
function bytesToWords(bytes) {
var words = [];
for (var i = 0, b = 0; i < bytes.length; i++, b += 8) {
words[b >>> 5] |= bytes[i] << (24 - b % 32);
}
return words;
}
function wordsToBytes(words) {
var bytes = [];
for (var b = 0; b < words.length * 32; b += 8) {
bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF);
}
return bytes;
}
function lpad(str, padString, length) {
while (str.length < length) str = padString + str;
return str;
}