From baf6a24d23f7f4378d0f679d9e0f42210e4ae24e Mon Sep 17 00:00:00 2001 From: Esteban Ordano Date: Tue, 9 Dec 2014 11:04:31 -0300 Subject: [PATCH] Add ".publicKey" property to PrivateKey --- lib/privatekey.js | 10 ++++++++++ test/privatekey.js | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/lib/privatekey.js b/lib/privatekey.js index d5333d7..337216c 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -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; }; diff --git a/test/privatekey.js b/test/privatekey.js index f523345..c1d878a 100644 --- a/test/privatekey.js +++ b/test/privatekey.js @@ -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);