From b38f7fa319eb1da7f8fe18c6c1076fc1e18f4150 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Thu, 8 Jan 2015 17:43:59 -0500 Subject: [PATCH 1/3] Added getID to PublicKey - Tests in BloomFilter in [bitcoin core](https://github.com/bitcoin/bitcoin/blob/master/src/test/bloom_tests.cpp#L85) use the CPubKey.GetID as input into the BloomFilter - The hash is calculated when generating an Address however the hash itself is not currently available --- lib/publickey.js | 10 ++++++++++ test/publickey.js | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/publickey.js b/lib/publickey.js index a5d5e23..0bba32a 100644 --- a/lib/publickey.js +++ b/lib/publickey.js @@ -3,6 +3,7 @@ var Address = require('./address'); var BN = require('./crypto/bn'); var Point = require('./crypto/point'); +var Hash = require('./crypto/hash'); var JSUtil = require('./util/js'); var Network = require('./networks'); var _ = require('lodash'); @@ -384,6 +385,15 @@ PublicKey.prototype.toBuffer = PublicKey.prototype.toDER = function() { } }; +/* + * Will return a sha256 + ripemd160 hash of the serialized public key + * @see https://github.com/bitcoin/bitcoin/blob/master/src/pubkey.h#L141 + * @returns {Buffer} + */ +PublicKey.prototype.getID = function getID() { + return Hash.sha256ripemd160(this.toBuffer()) +} + /** * Will return an address for the public key * diff --git a/test/publickey.js b/test/publickey.js index 22f4ae4..3843f5d 100644 --- a/test/publickey.js +++ b/test/publickey.js @@ -8,6 +8,7 @@ var Point = bitcore.crypto.Point; var BN = bitcore.crypto.BN; var PublicKey = bitcore.PublicKey; var PrivateKey = bitcore.PrivateKey; +var Address = bitcore.Address; var Networks = bitcore.Networks; /* jshint maxlen: 200 */ @@ -340,6 +341,25 @@ describe('PublicKey', function() { }); + describe('hashes', function() { + + // wif private key, address + // see: https://github.com/bitcoin/bitcoin/blob/master/src/test/key_tests.cpp#L20 + var data = [ + ['5HxWvvfubhXpYYpS3tJkw6fq9jE9j18THftkZjHHfmFiWtmAbrj', '1QFqqMUD55ZV3PJEJZtaKCsQmjLT6JkjvJ'], + ['5KC4ejrDjv152FGwP386VD1i2NYc5KkfSMyv1nGy1VGDxGHqVY3', '1F5y5E5FMc5YzdJtB9hLaUe43GDxEKXENJ'], + ['Kwr371tjA9u2rFSMZjTNun2PXXP3WPZu2afRHTcta6KxEUdm1vEw', '1NoJrossxPBKfCHuJXT4HadJrXRE9Fxiqs'], + ['L3Hq7a8FEQwJkW1M2GNKDW28546Vp5miewcCzSqUD9kCAXrJdS3g', '1CRj2HyM1CXWzHAXLQtiGLyggNT9WQqsDs'] + ]; + + data.forEach(function(d){ + var publicKey = PrivateKey.fromWIF(d[0]).toPublicKey(); + var address = Address.fromString(d[1]); + address.hashBuffer.should.deep.equal(publicKey.getID()); + }); + + }); + describe('#toString', function() { it('should print this known public key', function() { From 81fa162a7fa40b97316fc4a08ff3ded9c0c467f8 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 9 Jan 2015 10:08:01 -0500 Subject: [PATCH 2/3] Fix .jshintrc and add missing semis --- .jshintrc | 2 +- lib/publickey.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.jshintrc b/.jshintrc index 1d70b69..40fd8c6 100644 --- a/.jshintrc +++ b/.jshintrc @@ -28,7 +28,7 @@ "maxcomplexity": 6, // Cyclomatic complexity (http://en.wikipedia.org/wiki/Cyclomatic_complexity) "maxdepth": 4, // Maximum depth of nested control structures "maxlen": 120, // Maximum number of cols in a line - "multistr": true // Allow use of multiline EOL escaping + "multistr": true, // Allow use of multiline EOL escaping "predef": [ // Extra globals. "after", diff --git a/lib/publickey.js b/lib/publickey.js index 0bba32a..0ce572c 100644 --- a/lib/publickey.js +++ b/lib/publickey.js @@ -391,8 +391,8 @@ PublicKey.prototype.toBuffer = PublicKey.prototype.toDER = function() { * @returns {Buffer} */ PublicKey.prototype.getID = function getID() { - return Hash.sha256ripemd160(this.toBuffer()) -} + return Hash.sha256ripemd160(this.toBuffer()); +}; /** * Will return an address for the public key From 84cc1a004207c07f33ae0c71b5670383778b1f86 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Fri, 9 Jan 2015 10:15:48 -0500 Subject: [PATCH 3/3] Indicate internal use of getID with an underscore. --- lib/publickey.js | 4 ++-- test/publickey.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/publickey.js b/lib/publickey.js index 0ce572c..6977e1c 100644 --- a/lib/publickey.js +++ b/lib/publickey.js @@ -385,12 +385,12 @@ PublicKey.prototype.toBuffer = PublicKey.prototype.toDER = function() { } }; -/* +/** * Will return a sha256 + ripemd160 hash of the serialized public key * @see https://github.com/bitcoin/bitcoin/blob/master/src/pubkey.h#L141 * @returns {Buffer} */ -PublicKey.prototype.getID = function getID() { +PublicKey.prototype._getID = function _getID() { return Hash.sha256ripemd160(this.toBuffer()); }; diff --git a/test/publickey.js b/test/publickey.js index 3843f5d..f778b5c 100644 --- a/test/publickey.js +++ b/test/publickey.js @@ -355,7 +355,7 @@ describe('PublicKey', function() { data.forEach(function(d){ var publicKey = PrivateKey.fromWIF(d[0]).toPublicKey(); var address = Address.fromString(d[1]); - address.hashBuffer.should.deep.equal(publicKey.getID()); + address.hashBuffer.should.deep.equal(publicKey._getID()); }); });