diff --git a/index.js b/index.js index 12ff0e4..4b48559 100644 --- a/index.js +++ b/index.js @@ -6,17 +6,17 @@ var crypto = require('crypto') module.exports = BIP39 -function BIP39(language){ +function BIP39(language) { language = language || 'en' this.wordlist = JSON.parse(Wordlists[language]) } -BIP39.prototype.mnemonicToSeed = function(mnemonic, password){ +BIP39.prototype.mnemonicToSeed = function(mnemonic, password) { var options = {iterations: 2048, hasher: CryptoJS.algo.SHA512, keySize: 512/32} return CryptoJS.PBKDF2(mnemonic, salt(password), options).toString(CryptoJS.enc.Hex) } -BIP39.prototype.entropyToMnemonic = function(entropy){ +BIP39.prototype.entropyToMnemonic = function(entropy) { var entropyBuffer = new Buffer(entropy, 'hex') var hash = crypto.createHash('sha256').update(entropyBuffer).digest() @@ -25,31 +25,31 @@ BIP39.prototype.entropyToMnemonic = function(entropy){ var bits = bytesToBinary([].slice.call(combined)).substr(0, bitLength) var chunks = (bits).match(/(.{1,11})/g) - return chunks.map(function(binary){ + return chunks.map(function(binary) { var index = parseInt(binary, 2) return this.wordlist[index] }, this).join(' ') } -BIP39.prototype.generateMnemonic = function(strength){ +BIP39.prototype.generateMnemonic = function(strength) { strength = strength || 128 var entropy = crypto.randomBytes(strength/8).toString('hex') return this.entropyToMnemonic(entropy) } -BIP39.prototype.validate = function(mnemonic){ +BIP39.prototype.validate = function(mnemonic) { mnemonic = mnemonic.split(' ') - if(mnemonic.length % 3 !== 0) return false + if (mnemonic.length % 3 !== 0) return false var wordlist = this.wordlist - var belongToList = mnemonic.reduce(function(memo, m){ + var belongToList = mnemonic.reduce(function(memo, m) { return memo && (wordlist.indexOf(m) > -1) }, true) - if(!belongToList) return false + if (!belongToList) return false - var bits = mnemonic.map(function(m){ + var bits = mnemonic.map(function(m) { var id = wordlist.indexOf(m) return lpad(id.toString(2), '0', 11) }).join('') @@ -59,7 +59,7 @@ BIP39.prototype.validate = function(mnemonic){ var checksum = bits.substring(dividerIndex) var data = bits.substring(0, dividerIndex) - var bytes = data.match(/(.{1,8})/g).map(function(bin){ + var bytes = data.match(/(.{1,8})/g).map(function(bin) { return parseInt(bin, 2) }) var hash = crypto.createHash('sha256').update(new Buffer(bytes)).digest() @@ -73,7 +73,7 @@ function salt(password) { return encode_utf8('mnemonic' + (password || '')) } -function encode_utf8(s){ +function encode_utf8(s) { return unescape(encodeURIComponent(s)) }