Add ".publicKey" property to PrivateKey

This commit is contained in:
Esteban Ordano 2014-12-09 11:04:31 -03:00
parent 5f4eb204b4
commit baf6a24d23
2 changed files with 17 additions and 0 deletions

View File

@ -82,6 +82,16 @@ var PrivateKey = function PrivateKey(data, network, compressed) {
value: info.network
});
Object.defineProperty(this, 'publicKey', {
configurable: false,
get: function() {
if (!info.publicKey) {
info.publicKey = this.toPublicKey();
}
return info.publicKey;
}
});
return this;
};

View File

@ -261,6 +261,13 @@ describe('PrivateKey', function() {
pubkey.toString().should.equal(pubhex);
});
it('should have a "publicKey" property', function() {
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc';
var privkey = new PrivateKey(BN(new Buffer(privhex, 'hex')));
privkey.publicKey.toString().should.equal(pubhex);
});
it('should convert this known PrivateKey to known PublicKey and preserve compressed=true', function() {
var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff';
var privkey = new PrivateKey(BN(new Buffer(privhex, 'hex')), 'livenet', true);