Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use crypto-js as a module from npm #57

Merged
merged 3 commits into from
Mar 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@
"scripts": {
"test": "istanbul test ./node_modules/.bin/_mocha -- --reporter list test/*.js",
"compile": "browserify src/index.js -s Bitcoin | uglifyjs > bitcoinjs-min.js"
},
"dependencies": {
"crypto-js": "~3.1.2-2"
}
}
1 change: 0 additions & 1 deletion src/address.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
var base58 = require('./base58');
var Crypto = require('./crypto-js/crypto');
var conv = require('./convert');
var util = require('./util');
var mainnet = require('./network').mainnet.addressVersion;
Expand Down
22 changes: 15 additions & 7 deletions src/base58.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// https://en.bitcoin.it/wiki/Base58Check_encoding

var BigInteger = require('./jsbn/jsbn');
var Crypto = require('./crypto-js/crypto');
var Crypto = require('crypto-js');
var SHA256 = Crypto.SHA256;
var WordArray = Crypto.lib.WordArray;
var conv = require('./convert');
var util = require('./util');

var alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
var base = BigInteger.valueOf(58);
Expand Down Expand Up @@ -80,22 +83,27 @@ module.exports.decode = function (input) {

module.exports.checkEncode = function(input, vbyte) {
vbyte = vbyte || 0;
var front = [vbyte].concat(input);
var checksum = Crypto.SHA256(Crypto.SHA256(front, {asBytes: true}), {asBytes: true})
.slice(0,4);
return module.exports.encode(front.concat(checksum));
var front = [vbyte].concat(input)
return module.exports.encode(front.concat(getChecksum(front)));
}

module.exports.checkDecode = function(input) {
var bytes = module.exports.decode(input),
front = bytes.slice(0,bytes.length-4),
back = bytes.slice(bytes.length-4);
var checksum = Crypto.SHA256(Crypto.SHA256(front,{asBytes: true}), {asBytes: true})
.slice(0,4);
var checksum = getChecksum(front)
if (""+checksum != ""+back) {
throw new Error("Checksum failed");
}
var o = front.slice(1);
o.version = front[0];
return o;
}

function getChecksum(bytes) {
var wordArray = util.bytesToWordArray(bytes)
return conv.hexToBytes(SHA256(SHA256(wordArray)).toString()).slice(0,4);
}

module.exports.getChecksum = getChecksum

27 changes: 16 additions & 11 deletions src/ecdsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var util = require('./util');
var SecureRandom = require('./jsbn/rng');
var BigInteger = require('./jsbn/jsbn');
var conv = require('./convert')
var Crypto = require('./crypto-js/crypto.js')
var HmacSHA256 = require('crypto-js/hmac-sha256');

var ECPointFp = require('./jsbn/ec').ECPointFp;

Expand Down Expand Up @@ -39,16 +39,21 @@ function implShamirsTrick(P, k, Q, l)
};

function deterministicGenerateK(hash,key) {
var v = [];
var k = [];
for (var i = 0;i < 32;i++) v.push(1);
for (var i = 0;i < 32;i++) k.push(0);
k = Crypto.HMAC(Crypto.SHA256,v.concat([0]).concat(key).concat(hash),k,{ asBytes: true })
v = Crypto.HMAC(Crypto.SHA256,v,k,{ asBytes: true })
k = Crypto.HMAC(Crypto.SHA256,v.concat([1]).concat(key).concat(hash),k,{ asBytes: true })
v = Crypto.HMAC(Crypto.SHA256,v,k,{ asBytes: true })
v = Crypto.HMAC(Crypto.SHA256,v,k,{ asBytes: true })
return BigInteger.fromByteArrayUnsigned(v);
var vArr = [];
var kArr = [];
for (var i = 0;i < 32;i++) vArr.push(1);
for (var i = 0;i < 32;i++) kArr.push(0);
var v = util.bytesToWordArray(vArr)
var k = util.bytesToWordArray(kArr)

k = HmacSHA256(util.bytesToWordArray(vArr.concat([0]).concat(key).concat(hash)), k)
v = HmacSHA256(v, k)
vArr = util.wordArrayToBytes(v)
k = HmacSHA256(util.bytesToWordArray(vArr.concat([1]).concat(key).concat(hash)), k)
v = HmacSHA256(v,k)
v = HmacSHA256(v,k)
vArr = util.wordArrayToBytes(v)
return BigInteger.fromByteArrayUnsigned(vArr);
}

var ECDSA = {
Expand Down
1 change: 0 additions & 1 deletion src/eckey.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
var BigInteger = require('./jsbn/jsbn');
var sec = require('./jsbn/sec');
var base58 = require('./base58');
var Crypto = require('./crypto-js/crypto');
var util = require('./util');
var conv = require('./convert');
var Address = require('./address');
Expand Down
17 changes: 9 additions & 8 deletions src/hdwallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ var base58 = require('./base58.js')
var assert = require('assert')
var format = require('util').format
var util = require('./util.js')
var Crypto = require('./crypto-js/crypto.js')
var Crypto = require('crypto-js');
var HmacSHA512 = Crypto.HmacSHA512
var ECKey = require('./eckey.js').ECKey
var ECPubKey = require('./eckey.js').ECPubKey
var Address = require('./address.js')
Expand All @@ -12,7 +13,8 @@ var Network = require('./network')
var HDWallet = module.exports = function(seed, network) {
if (seed === undefined) return

var I = Crypto.HMAC(Crypto.SHA512, seed, 'Bitcoin seed', { asBytes: true })
var seedWords = util.bytesToWordArray(convert.stringToBytes(seed))
var I = util.wordArrayToBytes(HmacSHA512(seedWords, 'Bitcoin seed'))
this.chaincode = I.slice(32)
this.network = network || 'mainnet'
if(!Network.hasOwnProperty(this.network)) {
Expand All @@ -32,10 +34,7 @@ function arrayEqual(a, b) {
return !(a < b || a > b)
}

HDWallet.getChecksum = function(buffer) {
assert.equal(buffer.length, HDWallet.LENGTH)
return Crypto.SHA256(Crypto.SHA256(buffer, { asBytes: true }), { asBytes: true }).slice(0, 4)
}
HDWallet.getChecksum = base58.getChecksum;

HDWallet.fromMasterHex = function(hex) {
var bytes = convert.hexToBytes(hex)
Expand Down Expand Up @@ -188,19 +187,20 @@ HDWallet.prototype.derive = function(i) {
, iBytes = util.numToBytes(i, 4).reverse()
, cPar = this.chaincode
, usePriv = i >= HDWallet.HIGHEST_BIT
, SHA512 = Crypto.algo.SHA512

if (usePriv) {
assert(this.priv, 'Private derive on public key')

// If 1, private derivation is used:
// let I = HMAC-SHA512(Key = cpar, Data = 0x00 || kpar || i) [Note:]
var kPar = this.priv.toBytes().slice(0, 32)
I = Crypto.HMAC(Crypto.SHA512, [0].concat(kPar, iBytes), cPar, { asBytes: true })
I = util.HmacFromBytesToBytes(SHA512, [0].concat(kPar, iBytes), cPar)
} else {
// If 0, public derivation is used:
// let I = HMAC-SHA512(Key = cpar, Data = χ(kpar*G) || i)
var KPar = this.pub.toBytes(true)
I = Crypto.HMAC(Crypto.SHA512, KPar.concat(iBytes), cPar, { asBytes: true })
I = util.HmacFromBytesToBytes(SHA512, KPar.concat(iBytes), cPar)
}

// Split I = IL || IR into two 32-byte sequences, IL and IR.
Expand Down Expand Up @@ -239,3 +239,4 @@ HDWallet.prototype.getKeyVersion = function() {
}

HDWallet.prototype.toString = HDWallet.prototype.toBase58

2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ module.exports = {
ECPubKey: Key.ECPubKey,
Message: require('./message'),
BigInteger: require('./jsbn/jsbn'),
Crypto: require('./crypto-js/crypto'),
Crypto: require('crypto-js'), //should we expose this at all?
Script: require('./script'),
Opcode: require('./opcode'),
Transaction: require('./transaction').Transaction,
Expand Down
4 changes: 2 additions & 2 deletions src/message.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// Implements Bitcoin's feature for signing arbitrary messages.

var Crypto = require('./crypto-js/crypto');
var SHA256 = require('crypto-js/sha256');
var ecdsa = require('./ecdsa');
var conv = require('./convert');
var util = require('./util');
Expand All @@ -24,7 +24,7 @@ Message.makeMagicMessage = function (message) {

Message.getHash = function (message) {
var buffer = Message.makeMagicMessage(message);
return Crypto.SHA256(Crypto.SHA256(buffer, {asBytes: true}), {asBytes: true});
return util.wordArrayToBytes(SHA256(SHA256(util.bytesToWordArray(buffer))));
};

Message.signMessage = function (key, message, compressed) {
Expand Down
8 changes: 3 additions & 5 deletions src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ var BigInteger = require('./jsbn/jsbn');
var Script = require('./script');
var util = require('./util');
var conv = require('./convert');
var Crypto = require('./crypto-js/crypto');
var Wallet = require('./wallet');
var ECKey = require('./eckey').ECKey;
var ECDSA = require('./ecdsa');
var Address = require('./address');
var Message = require('./message');

var Transaction = function (doc) {
if (!(this instanceof Transaction)) { return new Transaction(doc); }
Expand Down Expand Up @@ -207,9 +207,7 @@ function (connectedScript, inIndex, hashType)

buffer = buffer.concat(util.numToBytes(parseInt(hashType),4));

var hash1 = Crypto.SHA256(buffer, {asBytes: true});

return Crypto.SHA256(hash1, {asBytes: true});
return Message.getHash(buffer)
};

/**
Expand All @@ -220,7 +218,7 @@ function (connectedScript, inIndex, hashType)
Transaction.prototype.getHash = function ()
{
var buffer = this.serialize();
return Crypto.SHA256(Crypto.SHA256(buffer, {asBytes: true}), {asBytes: true}).reverse();
return Message.getHash(buffer).reverse()
};

/**
Expand Down
26 changes: 23 additions & 3 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
var Crypto = require('./crypto-js/crypto');
var Crypto = require('crypto-js');
var RIPEMD160 = Crypto.RIPEMD160;
var SHA256 = Crypto.SHA256;
var HMAC= Crypto.algo.HMAC;
var WordArray = Crypto.lib.WordArray;

/**
* Create a byte array representing a number with the given length
Expand Down Expand Up @@ -45,7 +49,15 @@ exports.wordsToBytes = function (words) {
bytes.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF);
}
return bytes;
},
}

exports.bytesToWordArray = function (bytes) {
return new WordArray.init(exports.bytesToWords(bytes), bytes.length)
}

exports.wordArrayToBytes = function (wordArray) {
return exports.wordsToBytes(wordArray.words)
}

/**
* Calculate RIPEMD160(SHA256(data)).
Expand All @@ -54,7 +66,15 @@ exports.wordsToBytes = function (words) {
* array.
*/
exports.sha256ripe160 = function (data) {
return Crypto.RIPEMD160(Crypto.SHA256(data, {asBytes: true}), {asBytes: true});
var wordArray = RIPEMD160(SHA256(exports.bytesToWordArray(data)))
return exports.wordArrayToBytes(wordArray)
}


exports.HmacFromBytesToBytes = function (hasher, message, key) {
var hmac = HMAC.create(hasher, exports.bytesToWordArray(key))
hmac.update(exports.bytesToWordArray(message))
return exports.wordArrayToBytes(hmac.finalize())
}

exports.error = function(msg) {
Expand Down
27 changes: 27 additions & 0 deletions test/base58.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,31 @@ describe('base58', function() {
assert.equal(base58.encode(conv.hexToBytes(hex)), enc);
})
})

describe('checkEncode', function() {
it('handles known examples', function() {
var input = [
171, 210, 178, 125, 2, 16, 86, 184, 248, 88, 235,
163, 244, 160, 83, 156, 184, 186, 45, 167, 169, 164,
67, 125, 163, 89, 106, 243, 207, 193, 149, 206
]
var vbyte = 239

assert.equal(base58.checkEncode(input, vbyte),
'92tb9mjz6q9eKZjYvLsgk87kPrMoh7BGRumSzPeUGhmigtsfrbP');
})
})

describe('checkDecode', function() {
it('handles known examples', function() {
var input = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa'
var expected = [
98, 233, 7, 177, 92, 191, 39, 213, 66, 83,
153, 235, 246, 240, 251, 80, 235, 184, 143, 24
];
expected.version = 0

assert.deepEqual(base58.checkDecode(input), expected);
})
})
})
15 changes: 8 additions & 7 deletions test/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ var assert = require('assert');
var bitcoinjs = require('../');
var sec = require('../src/jsbn/sec');
var BigInteger = require('../src/jsbn/jsbn.js');
var Crypto = require('../src/crypto-js/crypto.js');
var SHA256 = require('crypto-js/sha256');

var SecureRandom = require('../src/jsbn/rng');
var rng = new SecureRandom();

var ecparams = sec('secp256k1');
var ECPointFp = bitcoinjs.ECPointFp;
var util = require('../src/util');

function sha256FromBytesToBytes(message){
return util.wordArrayToBytes(SHA256(util.bytesToWordArray(message)))
}

it('Keys & Key Management', function () {
var p1 = bitcoinjs.Key().getPub()['export']('bytes');
Expand All @@ -36,9 +41,7 @@ it('Signing and Verifying', function () {
assert.ok(s1.verify(BigInteger.ZERO, sig_a));

var message = new BigInteger(1024, rng).toByteArrayUnsigned();
var hash = Crypto.SHA256(message, {
asBytes: true
});
var hash = sha256FromBytesToBytes(message);
var sig_b = s1.sign(hash);
assert.ok(sig_b, 'Sign random string');
assert.ok(s1.verify(hash, sig_b));
Expand All @@ -50,9 +53,7 @@ it('Signing and Verifying', function () {
'8a33f50d7cefb96a5dab897b5efcb99cbafb0d777cb83fc9b2115b69c0fa' +
'3d82507b932b84e4');

var hash2 = Crypto.SHA256(message2, {
asBytes: true
});
var hash2 = sha256FromBytesToBytes(message2);

var sig_c = bitcoinjs.convert.hexToBytes(
'3044022038d9b8dd5c9fbf330565c1f51d72a59ba869aeb2c2001be959d3' +
Expand Down
33 changes: 33 additions & 0 deletions test/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var util = require('../src/util.js')
var assert = require('assert')

describe('util', function() {

describe('byte array and word array conversions', function(){
var bytes, wordArray;

beforeEach(function(){
bytes = [
98, 233, 7, 177, 92, 191, 39, 213, 66, 83,
153, 235, 246, 240, 251, 80, 235, 184, 143, 24
]
wordArray = {
words: [1659439025, 1556031445, 1112775147, -151979184, -340226280],
sigBytes: 20
}
})

describe('bytesToWords', function() {
it('works', function() {
assert.deepEqual(util.bytesToWordArray(bytes), wordArray)
})
})

describe('bytesToWords', function() {
it('works', function() {
assert.deepEqual(util.wordArrayToBytes(wordArray), bytes)
})
})
})

})