From ff844e9935d36e6f9491ad164549ed5a52677732 Mon Sep 17 00:00:00 2001 From: Yemel Jardi Date: Fri, 2 Jan 2015 18:15:17 -0300 Subject: [PATCH 1/3] Change PrivateKey.toString from WIF to hex format --- lib/privatekey.js | 11 ++++++++++- lib/publickey.js | 3 +-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/privatekey.js b/lib/privatekey.js index 794860c..15e0eaa 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -283,12 +283,21 @@ PrivateKey.isValid = function(data, network){ return !PrivateKey.getValidationError(data, network); }; +/** + * Will output the PublicKey encoded as hex string + * + * @returns {String} + */ +PrivateKey.prototype.toString = function() { + return this.toBuffer().toString('hex'); +} + /** * Will output the PrivateKey to a WIF string * * @returns {String} A WIP representation of the private key */ -PrivateKey.prototype.toString = PrivateKey.prototype.toWIF = function() { +PrivateKey.prototype.toWIF = function() { var network = this.network; var compressed = this.compressed; diff --git a/lib/publickey.js b/lib/publickey.js index 182b7aa..3ce6f8e 100644 --- a/lib/publickey.js +++ b/lib/publickey.js @@ -409,8 +409,7 @@ PublicKey.prototype.toAddress = function(network) { * @returns {String} A DER hex encoded string */ PublicKey.prototype.toString = function() { - var compressed = _.isUndefined(this.compressed) || this.compressed; - return this.toDER(compressed).toString('hex'); + return this.toDER().toString('hex'); }; /** From 9f8e2c721fbf176313c2596d4af31f3c2a64012a Mon Sep 17 00:00:00 2001 From: Yemel Jardi Date: Fri, 2 Jan 2015 18:25:08 -0300 Subject: [PATCH 2/3] fix typo --- lib/privatekey.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/privatekey.js b/lib/privatekey.js index 15e0eaa..ac49996 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -284,7 +284,7 @@ PrivateKey.isValid = function(data, network){ }; /** - * Will output the PublicKey encoded as hex string + * Will output the PrivateKey encoded as hex string * * @returns {String} */ From 5e292490e8a9d4782664ca430c3a7d9e579acfdf Mon Sep 17 00:00:00 2001 From: Yemel Jardi Date: Fri, 2 Jan 2015 18:25:23 -0300 Subject: [PATCH 3/3] update tests --- test/privatekey.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/test/privatekey.js b/test/privatekey.js index 7cae44b..cdb26d4 100644 --- a/test/privatekey.js +++ b/test/privatekey.js @@ -135,7 +135,7 @@ describe('PrivateKey', function() { it('should create a livenet private key', function() { var privkey = new PrivateKey(BN.fromBuffer(buf), 'livenet'); - privkey.toString().should.equal(wifLivenet); + privkey.toWIF().should.equal(wifLivenet); }); it('should create a default network private key', function() { @@ -197,7 +197,7 @@ describe('PrivateKey', function() { it('should output this address correctly', function() { var privkey = PrivateKey.fromWIF(wifLivenetUncompressed); - privkey.toString().should.equal(wifLivenetUncompressed); + privkey.toWIF().should.equal(wifLivenetUncompressed); }); }); @@ -221,20 +221,20 @@ describe('PrivateKey', function() { it('should output known livenet address for console', function() { var privkey = PrivateKey.fromWIF('L3T1s1TYP9oyhHpXgkyLoJFGniEgkv2Jhi138d7R2yJ9F4QdDU2m'); privkey.inspect().should.equal( - '' + '' ); }); it('should output known testnet address for console', function() { var privkey = PrivateKey.fromWIF('cR4qogdN9UxLZJXCNFNwDRRZNeLRWuds9TTSuLNweFVjiaE4gPaq'); privkey.inspect().should.equal( - '' + '' ); }); it('outputs "uncompressed" for uncompressed imported WIFs', function() { var privkey = PrivateKey.fromWIF(wifLivenetUncompressed); - privkey.inspect().should.equal(''); + privkey.inspect().should.equal(''); }); }); @@ -261,7 +261,7 @@ describe('PrivateKey', function() { describe('#toBuffer', function() { it('should output known buffer', function() { var privkey = new PrivateKey(BN.fromBuffer(buf), 'livenet'); - privkey.toBuffer().toString('hex').should.equal(buf.toString('hex')); + privkey.toString().should.equal(buf.toString('hex')); }); }); @@ -316,7 +316,7 @@ describe('PrivateKey', function() { it('should parse this uncompressed livenet address correctly', function() { var privkey = PrivateKey.fromString(wifLivenetUncompressed); - privkey.toString().should.equal(wifLivenetUncompressed); + privkey.toString().should.equal("96c132224121b509b7d0a16245e957d9192609c5637c6228311287b1be21627a"); }); });