From 7c945cdc01ae02ca93daca75aadebe46db4254cd Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Fri, 22 Aug 2014 17:43:22 -0700 Subject: [PATCH] add "compressed" feature to pubkeys ...not just privkeys. since, of course, they can be compressed or uncompressed. --- lib/address.js | 6 ++---- lib/key.js | 6 +++--- lib/pubkey.js | 13 ++++++++++--- test/test.address.js | 3 ++- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lib/address.js b/lib/address.js index 43be364..608d0b8 100644 --- a/lib/address.js +++ b/lib/address.js @@ -11,10 +11,8 @@ function Address(hashbuf, network, type) { this.type = type; }; -Address.prototype.fromPubkey = function(pubkey, network, compressed) { - if (typeof compressed === 'undefined') - compressed = true; - this.hashbuf = Hash.sha256ripemd160(pubkey.toDER(compressed)); +Address.prototype.fromPubkey = function(pubkey, network) { + this.hashbuf = Hash.sha256ripemd160(pubkey.toBuffer()); this.network = network || 'mainnet'; this.type = 'pubkeyhash'; return this; diff --git a/lib/key.js b/lib/key.js index c48d3f6..a7a3aa5 100644 --- a/lib/key.js +++ b/lib/key.js @@ -34,12 +34,12 @@ Key.prototype.fromString = function(str) { } }; -Key.prototype.getAddress = function(network, compressed) { - return (new Address()).fromPubkey(this.pubkey, network, compressed); +Key.prototype.getAddress = function(network) { + return Address().fromPubkey(this.pubkey, network); }; Key.prototype.privkey2pubkey = function() { - this.pubkey = new Pubkey(point.getG().mul(this.privkey.bn)); + this.pubkey = new Pubkey(point.getG().mul(this.privkey.bn), this.privkey.compressed); }; Key.prototype.toString = function() { diff --git a/lib/pubkey.js b/lib/pubkey.js index 0c93574..8a659a2 100644 --- a/lib/pubkey.js +++ b/lib/pubkey.js @@ -1,12 +1,13 @@ var Point = require('./point'); var bn = require('./bn'); -var Pubkey = function Pubkey(point) { +var Pubkey = function Pubkey(point, compressed) { if (!(this instanceof Pubkey)) return new Pubkey(point); if (point && !point.getX() && !point.getY()) throw new Error('Invalid point'); this.point = point; + this.compressed = compressed; }; Pubkey.prototype.fromDER = function(buf) { @@ -18,14 +19,17 @@ Pubkey.prototype.fromDER = function(buf) { var x = bn(xbuf); var y = bn(ybuf); this.point = Point(x, y); + this.compressed = false; } else if (buf[0] == 0x03) { var xbuf = buf.slice(1); var x = bn(xbuf); this.fromX(true, x); + this.compressed = true; } else if (buf[0] == 0x02) { var xbuf = buf.slice(1); var x = bn(xbuf); this.fromX(false, x); + this.compressed = true; } else { throw new Error('Invalid DER format pubkey'); } @@ -43,10 +47,12 @@ Pubkey.prototype.fromX = function(odd, x) { }; Pubkey.prototype.toBuffer = function() { - return this.toDER(true); + var compressed = typeof this.compressed === 'undefined' ? true : this.compressed; + return this.toDER(compressed); }; Pubkey.prototype.toDER = function(compressed) { + compressed = typeof this.compressed === 'undefined' ? compressed : this.compressed; if (typeof compressed !== 'boolean') throw new Error('Must specify whether the public key is compressed or not (true or false)'); @@ -70,7 +76,8 @@ Pubkey.prototype.toDER = function(compressed) { }; Pubkey.prototype.toString = function() { - return this.toDER(true).toString('hex'); + var compressed = typeof this.compressed === 'undefined' ? true : this.compressed; + return this.toDER(compressed).toString('hex'); }; //https://www.iacr.org/archive/pkc2003/25670211/25670211.pdf diff --git a/test/test.address.js b/test/test.address.js index d773a13..bdd8c93 100644 --- a/test/test.address.js +++ b/test/test.address.js @@ -26,7 +26,8 @@ describe('Address', function() { var pubkey = new Pubkey(); pubkey.fromDER(new Buffer('0285e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004', 'hex')); var address = new Address(); - address.fromPubkey(pubkey, 'mainnet', false); + pubkey.compressed = false; + address.fromPubkey(pubkey, 'mainnet'); address.toString().should.equal('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX'); });