Skip to content

Commit

Permalink
remove unbuilding c code, and ...
Browse files Browse the repository at this point in the history
* Remove c code that didn't compile on my machine
* Replace with sjcl code
* Minor modifications to mnemonic interface more bitcoreish
  • Loading branch information
Ryan X. Charles committed Jun 21, 2014
1 parent b523eee commit 54c8e04
Show file tree
Hide file tree
Showing 9 changed files with 147 additions and 427 deletions.
6 changes: 0 additions & 6 deletions binding.gyp
Expand Up @@ -3,12 +3,6 @@
'node_shared_openssl%': 'true'
},
'targets': [
{
'target_name': 'cryptox',
'sources': [
'src/pbkdf2_sha512.cc'
],
},
{
'target_name': 'KeyModule',
'sources': [
Expand Down
1 change: 0 additions & 1 deletion browser/build.js
Expand Up @@ -29,7 +29,6 @@ var modules = [
'lib/HierarchicalKey',
'lib/BIP39',
'lib/BIP39WordlistEn',
'lib/cryptox',
'lib/Block',
'lib/Bloom',
'lib/Connection',
Expand Down
218 changes: 113 additions & 105 deletions browser/bundle.js

Large diffs are not rendered by default.

46 changes: 32 additions & 14 deletions lib/BIP39.js
@@ -1,32 +1,46 @@
var imports = require('soop').imports();
var coinUtil = imports.coinUtil || require('../util');
var cryptox = imports.cryptox || require('./cryptox');
var crypto = require('crypto');
var BIP39 = {};
var sjcl = imports.sjcl || require('./sjcl');
var SecureRandom = require('./SecureRandom');

var hmacSHA512 = function (key) {
var hasher = new sjcl.misc.hmac(key, sjcl.hash.sha512);
this.encrypt = function () {
return hasher.encrypt.apply(hasher, arguments);
};
};

var pbkdf2Sync_sha512 = function(password, salt, iterations, keylen) {
var derivedKey = sjcl.misc.pbkdf2(password, salt, iterations, 512, hmacSHA512);
return sjcl.codec.hex.fromBits(derivedKey)
};

var BIP39 = function() {
};

BIP39.mnemonic = function(wordlist, bits) {
if (!bits)
bits = 128;
if (bits % 32 != 0)
throw new Error("bits must be multiple of 32");
var bytes = crypto.randomBytes(bits / 8);
return BIP39.to_mnemonic(wordlist, bytes);
var buf = SecureRandom.getRandomBuffer(bits / 8);
return BIP39.entropy2mnemonic(wordlist, buf);
}

BIP39.to_mnemonic = function(wordlist, bytes) {
var hash = coinUtil.sha256(new Buffer(bytes));
BIP39.entropy2mnemonic = function(wordlist, buf) {
var hash = coinUtil.sha256(buf);
var bin = "";
var bits = bytes.length * 8;
for (var i = 0 ; i < bytes.length ; i++) {
bin = bin + ("00000000" + bytes[i].toString(2)).slice(-8);
var bits = buf.length * 8;
for (var i = 0 ; i < buf.length ; i++) {
bin = bin + ("00000000" + buf[i].toString(2)).slice(-8);
}
var hashbits = hash[0].toString(2);
hashbits = ("00000000" + hashbits).slice(-8).slice(0, bits/32);
bin = bin + hashbits;
if (bin.length % 11 != 0)
throw new Error("interal error - entropy not an even multiple of 11 bits - " + bin.length);
throw new Error("internal error - entropy not an even multiple of 11 bits - " + bin.length);
var mnemonic = "";
for (var i = 0 ; i < bin.length / 11 ; i++) {
for (var i = 0; i < bin.length / 11; i++) {
if (mnemonic != "")
mnemonic = mnemonic + " ";
var wi = parseInt(bin.slice(i*11, (i+1)*11), 2);
Expand All @@ -35,8 +49,12 @@ BIP39.to_mnemonic = function(wordlist, bytes) {
return mnemonic;
}

BIP39.mnemonic_to_seed = function(mnemonic, passphrase) {
return cryptox.pbkdf2Sync_sha512(mnemonic, "mnemonic" + passphrase, 2048, 64);
BIP39.mnemonic2seed = function(mnemonic, passphrase) {
if (!passphrase)
passphrase = "";
var hex = pbkdf2Sync_sha512(mnemonic, "mnemonic" + passphrase, 2048, 64);
var buf = new Buffer(hex, 'hex');
return buf;
}

module.exports = require('soop')(BIP39);
17 changes: 0 additions & 17 deletions lib/browser/cryptox.js

This file was deleted.

5 changes: 0 additions & 5 deletions lib/cryptox.js

This file was deleted.

49 changes: 0 additions & 49 deletions lib/node/cryptox.js

This file was deleted.

228 changes: 0 additions & 228 deletions src/pbkdf2_sha512.cc

This file was deleted.

0 comments on commit 54c8e04

Please sign in to comment.