diff --git a/browser/bitcoinjs-lib.js b/browser/bitcoinjs-lib.js index 2da8ab0..4b98cfd 100644 --- a/browser/bitcoinjs-lib.js +++ b/browser/bitcoinjs-lib.js @@ -188,7 +188,20 @@ f+(((m|~n)^p)+c[2]):64>b?f+((m&p|n&~p)+c[3]):f+((m^(n|~p))+c[4]),f|=0,f=f<>>5]|=128<<24-a%32;e[(a+64>>>9<<4)+14]=(b<<8|b>>>24)&16711935|(b<<24|b>>>8)&4278255360;g.sigBytes=4*(e.length+1);this._process();g=this._hash;e=g.words;for(b=0;5>b;b++)a=e[b],e[b]=(a<<8|a>>>24)&16711935|(a<<24|a>>>8)&4278255360;return g},clone:function(){var e=l.clone.call(this);e._hash=this._hash.clone();return e}});j.RIPEMD160=l._createHelper(k);j.HmacRIPEMD160=l._createHmacHelper(k)})(Math); -module.exports.RIPEMD160 = CryptoJS.RIPEMD160; +module.exports.ripemd160 = function(bytes) { + if (!Buffer.isBuffer(bytes)) { + throw new Error('arg should be a buffer'); + } + var w = new CryptoJS.lib.WordArray.init(Crypto.util.bytesToWords(bytes), bytes.length); + var wordArray = CryptoJS.RIPEMD160(w); + var words = wordArray.words; + var answer = []; + for (var b = 0; b < words.length * 32; b += 8) { + answer.push((words[b >>> 5] >>> (24 - b % 32)) & 0xFF); + } + return answer; +}; + Bitcoin = {}; diff --git a/examples/example.html b/examples/example.html index 01f3174..e2b1320 100644 --- a/examples/example.html +++ b/examples/example.html @@ -19,6 +19,7 @@ div.innerHTML += s + '
'; }; + print('

Address

' ); var addrStrings = [ "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa", "1A1zP1eP5QGefi2DMPTfTL5SLmv7Dixxxx", @@ -38,8 +39,7 @@ } }); - print('
'); - + print('

KeyModule

' ); /* Using bitcore root module */ @@ -50,11 +50,11 @@ print ('Private:' + bitcore.buffertools.toHex(k.private)); print ('Public:' + bitcore.buffertools.toHex(k.public)); - print('
'); + print('

PeerManager

' ); var p = new bitcore.PeerManager(); - print('
'); + print('

Util

' ); var coinUtil = bitcore.util; var pk = '03d95e184cce34c3cfa58e9a277a09a7c5ed1b2a8134ea1e52887bc66fa3f47071' @@ -64,10 +64,20 @@ pubKeyHash = coinUtil.sha256ripe160(pk); print(bitcore.buffertools.toHex(pubKeyHash)); + + var Buffer = bitcore.Buffer; + pubKeyHash = coinUtil.ripe160(new bitcore.Buffer('hola')); print(bitcore.buffertools.toHex(pubKeyHash)); - print('
'); + var bu = new Buffer('a5c756101065ac5b8f689139e6d856fa99e54b5000b6428b43729d334cc9277d', 'hex'); + print(bitcore.buffertools.toHex(bu)); + + var pubKeyHash2 = coinUtil.ripe160(bu); + print(bitcore.buffertools.toHex(pubKeyHash2)); + + + print('

WalletKey

'); var WalletKey = bitcore.WalletKey; var networks = bitcore.networks; diff --git a/test/test.util.js b/test/test.util.js index e224550..99ff949 100644 --- a/test/test.util.js +++ b/test/test.util.js @@ -36,6 +36,16 @@ describe('util', function() { }); }); }); + describe('#ripe160', function() { + var pk = 'a5c756101065ac5b8f689139e6d856fa99e54b5000b6428b43729d334cc9277d'; + it('should work for ' + pk, function() { + var pubKeyHash = coinUtil.ripe160(new Buffer(pk,'hex')); + var pkh = buffertools.toHex(pubKeyHash); + pkh.should.equal('d166a41f27fd4b158f70314e5eee8998bf3d97d5'); + }); + }); + + describe('#sha256', function() { var pk = '03d95e184cce34c3cfa58e9a277a09a7c5ed1b2a8134ea1e52887bc66fa3f47071' it('should work for ' + pk, function() { @@ -61,7 +71,7 @@ describe('util', function() { ]; ripemdData.forEach(function(datum) { it('should work for ' + datum[0], function() { - var r = coinUtil.ripe160(datum[0]); + var r = coinUtil.ripe160( new bitcore.Buffer(datum[0])); buffertools.toHex(r).should.equal(datum[1]); }); it('should work for Buffer ' + datum[0], function() { diff --git a/util/util.js b/util/util.js index 8a01292..7f3dab4 100644 --- a/util/util.js +++ b/util/util.js @@ -16,11 +16,12 @@ var sha256 = exports.sha256 = function (data) { }; var ripe160 = exports.ripe160 = function (data) { + if (!Buffer.isBuffer(data)) { + throw new Error('arg should be a buffer'); + } + if (!process.versions) { - var RIPEMD160 = bjs.RIPEMD160; - var WordArray = bjs.WordArray; - data = data.toString(); - var result = RIPEMD160(data) + ''; + var result = bjs.ripemd160(data); return new Buffer(result, 'hex'); } return new Buffer(crypto.createHash('rmd160').update(data).digest('binary'), 'binary');