bip39/index.js

106 lines
2.8 KiB
JavaScript
Raw Normal View History

2014-05-28 15:10:54 +00:00
var CryptoJS = require('crypto-js')
var crypto = require('crypto')
2014-03-10 02:49:53 +00:00
2014-06-23 03:40:38 +00:00
var includeFolder = require('include-folder')
var path = require('path')
var wordlists = includeFolder(path.join(__dirname, 'wordlists'))
2014-06-23 03:35:21 +00:00
function BIP39(language) {
2014-03-11 04:38:09 +00:00
language = language || 'en'
2014-06-23 03:40:38 +00:00
this.wordlist = JSON.parse(wordlists[language])
2014-03-10 02:49:53 +00:00
}
2014-06-23 03:35:21 +00:00
BIP39.prototype.mnemonicToSeed = function(mnemonic, password) {
2014-05-28 15:10:54 +00:00
var options = {iterations: 2048, hasher: CryptoJS.algo.SHA512, keySize: 512/32}
return CryptoJS.PBKDF2(mnemonic, salt(password), options).toString(CryptoJS.enc.Hex)
2014-03-10 02:49:53 +00:00
}
2014-06-23 03:35:21 +00:00
BIP39.prototype.entropyToMnemonic = function(entropy) {
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
2014-06-23 05:49:54 +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-03-11 08:01:14 +00:00
return this.wordlist[index]
2014-06-23 05:49:54 +00:00
}, this)
return words.join(' ')
2014-03-11 08:01:14 +00:00
}
2014-06-23 03:35:21 +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-06-23 03:35:21 +00:00
BIP39.prototype.validate = function(mnemonic) {
var words = mnemonic.split(' ')
2014-04-05 04:44:16 +00:00
if (words.length % 3 !== 0) return false
2014-04-05 04:44:16 +00:00
var wordlist = this.wordlist
var belongToList = words.every(function(word) {
2014-06-23 03:36:49 +00:00
return wordlist.indexOf(word) > -1
})
2014-04-05 04:44:16 +00:00
2014-06-23 03:35:21 +00:00
if (!belongToList) return false
2014-04-05 04:44:16 +00:00
2014-06-23 05:49:54 +00:00
// convert word indices to 11 bit binary strings
var bits = words.map(function(word) {
2014-06-23 05:49:54 +00:00
var index = wordlist.indexOf(word)
return lpad(index.toString(2), '0', 11)
2014-04-05 04:44:16 +00:00
}).join('')
2014-06-23 05:49:54 +00:00
// split the binary string into ENT/CS
var dividerIndex = Math.floor(bits.length / 33) * 32
2014-06-23 06:19:32 +00:00
var entropy = bits.slice(0, dividerIndex)
var checksum = bits.slice(dividerIndex)
2014-04-05 04:44:16 +00:00
2014-06-23 05:49:54 +00:00
// calculate the checksum and compare
var entropyBytes = entropy.match(/(.{1,8})/g).map(function(bin) {
2014-04-05 04:44:16 +00:00
return parseInt(bin, 2)
})
2014-06-23 05:49:54 +00:00
var entropyBuffer = new Buffer(entropyBytes)
var newChecksum = checksumBits(entropyBuffer)
return newChecksum === checksum
}
function checksumBits(entropyBuffer) {
var hash = crypto.createHash('sha256').update(entropyBuffer).digest()
// 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
}
2014-03-10 02:49:53 +00:00
function salt(password) {
return encode_utf8('mnemonic' + (password || ''))
}
2014-06-23 03:35:21 +00:00
function encode_utf8(s) {
2014-03-10 02:49:53 +00:00
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 lpad(str, padString, length) {
while (str.length < length) str = padString + str;
return str;
}
2014-06-23 03:35:53 +00:00
module.exports = BIP39