Merge pull request #924 from braydonf/add/publickey-getid
Added getID to PublicKey
This commit is contained in:
commit
69b38acfab
|
@ -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",
|
||||
|
|
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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() {
|
||||
|
|
Loading…
Reference in New Issue