Comparing version 0.1.0 to 1.0.1
67
index.js
@@ -1,8 +0,12 @@ | ||
Crypto = require('crypto-js') | ||
var Crypto = require('crypto-js') | ||
var Wordlists = require('require-json-tree')('./wordlists') | ||
module.exports = { | ||
mnemonicToSeed: mnemonicToSeed | ||
module.exports = BIP39 | ||
function BIP39(language){ | ||
language = language || 'en' | ||
this.wordlist = Wordlists[language] | ||
} | ||
function mnemonicToSeed(mnemonic, password){ | ||
BIP39.prototype.mnemonicToSeed = function(mnemonic, password){ | ||
var options = {iterations: 2048, hasher: Crypto.algo.SHA512, keySize: 512/32} | ||
@@ -12,2 +16,17 @@ return Crypto.PBKDF2(mnemonic, salt(password), options).toString(Crypto.enc.Hex) | ||
BIP39.prototype.entropyToMnemonic = function(entropy){ | ||
var entropyBytes = hexToBytes(entropy) | ||
var bits = bytesToBinary(entropyBytes) | ||
var hash = Crypto.SHA256(bytesToWordArray(entropyBytes)) | ||
var checksumBits = bytesToBinary(wordsToBytes(hash.words)) | ||
var checksum = checksumBits.substr(0, bits.length / 32) | ||
var chunks = (bits + checksum).match(/(.{1,11})/g) | ||
return chunks.map(function(binary){ | ||
var index = parseInt(binary, 2) | ||
return this.wordlist[index] | ||
}, this).join(' ') | ||
} | ||
function salt(password) { | ||
@@ -20,1 +39,41 @@ return encode_utf8('mnemonic' + (password || '')) | ||
} | ||
//=========== helper methods from bitcoinjs-lib ======== | ||
function hexToBytes(hex) { | ||
return hex.match(/../g).map(function(x) { | ||
return parseInt(x,16) | ||
}); | ||
} | ||
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; | ||
} | ||
{ | ||
"name": "bip39", | ||
"version": "0.1.0", | ||
"version": "1.0.1", | ||
"description": "Bitcoin BIP39: Mnemonic code for generating deterministic keys", | ||
@@ -16,3 +16,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"crypto-js": "^3.1.2-2" | ||
"crypto-js": "^3.1.2-2", | ||
"require-json-tree": "~1.0.0" | ||
}, | ||
@@ -19,0 +20,0 @@ "devDependencies": { |
@@ -11,6 +11,11 @@ bip39 | ||
```javascript | ||
bip39 = require('bip39') | ||
bip39.mnemonicToSeed('crazy horse battery staple') | ||
// wait for it... | ||
// 'd6b0f05e5e039b4b4adb72e84f3d5df121cc2dba8b236a1b3a5a8d487b9641096717364fc96e24ddbc648e0e6badbff72332e7d44dc2a42796c2d7e58ee632de' | ||
var BIP39 = require('bip39') | ||
bip39 = new BIP39() // 'en' is the default language | ||
bip39.entropyToMnemonic('1337') // hex input | ||
// 'basket actual' | ||
bip39.mnemonicToSeed('basket actual') // wait for it... | ||
// '5cf2d4a8b0355e90295bdfc565a022a409af063d5365bb57bf74d9528f494bfa4400f53d8349b80fdae44082d7f9541e1dba2b003bcfec9d0d53781ca676651f' | ||
``` | ||
@@ -20,3 +25,2 @@ | ||
- seedToMnemonic | ||
- generateMnemonic |
var vectors = require('./vectors.json').english | ||
var bip39 = require('../index.js') | ||
var BIP39 = require('../index.js') | ||
var wordlist = require('../Wordlists/en.json') | ||
var assert = require('assert') | ||
var bip39 = new BIP39() | ||
describe('constructor', function(){ | ||
it('defaults language to english', function(){ | ||
assert.deepEqual(bip39.wordlist, wordlist) | ||
}) | ||
}) | ||
describe('mnemonicToSeed', function(){ | ||
@@ -12,1 +21,9 @@ vectors.forEach(function(v, i){ | ||
}) | ||
describe('entropyToMnemonic', function(){ | ||
vectors.forEach(function(v, i){ | ||
it('works for tests vector ' + i, function(){ | ||
assert.equal(bip39.entropyToMnemonic(v[0]), v[1]) | ||
}) | ||
}) | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
35116
7
2259
1
25
2
1
+ Addedrequire-json-tree@~1.0.0
+ Addedrequire-json-tree@1.0.0(transitive)