diff --git a/lib/ecdsa.js b/lib/ecdsa.js index 2fd8734..e2648aa 100644 --- a/lib/ecdsa.js +++ b/lib/ecdsa.js @@ -104,7 +104,7 @@ ECDSA.prototype.sig2pubkey = function() { //var Q = R.multiplyTwo(s, G, eNeg).mul(rInv); var Q = R.mul(s).add(G.mul(eNeg)).mul(rInv); - var pubkey = new Pubkey(Q); + var pubkey = new Pubkey({point: Q}); pubkey.compressed = this.sig.compressed; pubkey.validate(); diff --git a/lib/expmt/stealth.js b/lib/expmt/stealth.js index 66aebb6..877ff13 100644 --- a/lib/expmt/stealth.js +++ b/lib/expmt/stealth.js @@ -50,7 +50,7 @@ Stealth.prototype.fromRandom = function() { Stealth.prototype.getSharedKeyAsReceiver = function(senderPubkey) { var sharedSecretPoint = senderPubkey.point.mul(this.scanKey.privkey.bn); - var sharedSecretPubkey = Pubkey(sharedSecretPoint); + var sharedSecretPubkey = Pubkey({point: sharedSecretPoint}); var buf = sharedSecretPubkey.toDER(true); var sharedKey = KDF.sha256hmac2key(buf); @@ -59,7 +59,7 @@ Stealth.prototype.getSharedKeyAsReceiver = function(senderPubkey) { Stealth.prototype.getSharedKeyAsSender = function(senderKey) { var sharedSecretPoint = this.scanKey.pubkey.point.mul(senderKey.privkey.bn); - var sharedSecretPubkey = Pubkey(sharedSecretPoint); + var sharedSecretPubkey = Pubkey({point: sharedSecretPoint}); var buf = sharedSecretPubkey.toDER(true); var sharedKey = KDF.sha256hmac2key(buf); @@ -68,14 +68,14 @@ Stealth.prototype.getSharedKeyAsSender = function(senderKey) { Stealth.prototype.getReceivePubkeyAsReceiver = function(senderPubkey) { var sharedKey = this.getSharedKeyAsReceiver(senderPubkey); - var pubkey = Pubkey(this.payloadKey.pubkey.point.add(sharedKey.pubkey.point)); + var pubkey = Pubkey({point: this.payloadKey.pubkey.point.add(sharedKey.pubkey.point)}); return pubkey; }; Stealth.prototype.getReceivePubkeyAsSender = function(senderKey) { var sharedKey = this.getSharedKeyAsSender(senderKey); - var pubkey = Pubkey(this.payloadKey.pubkey.point.add(sharedKey.pubkey.point)); + var pubkey = Pubkey({point: this.payloadKey.pubkey.point.add(sharedKey.pubkey.point)}); return pubkey; }; diff --git a/lib/key.js b/lib/key.js index 3ad9bed..2616ba3 100644 --- a/lib/key.js +++ b/lib/key.js @@ -45,7 +45,7 @@ Key.prototype.getAddress = function(network) { }; Key.prototype.privkey2pubkey = function() { - this.pubkey = new Pubkey(point.getG().mul(this.privkey.bn), this.privkey.compressed); + this.pubkey = new Pubkey({point: point.getG().mul(this.privkey.bn), compressed: this.privkey.compressed}); }; Key.prototype.toString = function() { diff --git a/lib/pubkey.js b/lib/pubkey.js index 8a659a2..b4ffa5f 100644 --- a/lib/pubkey.js +++ b/lib/pubkey.js @@ -1,13 +1,19 @@ var Point = require('./point'); var bn = require('./bn'); -var Pubkey = function Pubkey(point, compressed) { +var Pubkey = function Pubkey(obj) { if (!(this instanceof Pubkey)) - return new Pubkey(point); - if (point && !point.getX() && !point.getY()) + return new Pubkey(obj); + if (obj) + this.set(obj); +}; + +Pubkey.prototype.set = function(obj) { + if (obj.point && !obj.point.getX() && !obj.point.getY()) throw new Error('Invalid point'); - this.point = point; - this.compressed = compressed; + this.point = obj.point || this.point; + this.compressed = typeof obj.compressed !== 'undefined' ? obj.compressed : this.compressed; + return this; }; Pubkey.prototype.fromDER = function(buf) { diff --git a/test/test.ecdsa.js b/test/test.ecdsa.js index 219001d..3a8238a 100644 --- a/test/test.ecdsa.js +++ b/test/test.ecdsa.js @@ -18,8 +18,10 @@ describe("ECDSA", function() { ecdsa.hashbuf = Hash.sha256(new Buffer('test data')); ecdsa.key = new Key(); ecdsa.key.privkey = new Privkey({bn: BN().fromBuffer(new Buffer('fee0a1f7afebf9d2a5a80c0c98a31c709681cce195cbcd06342b517970c0be1e', 'hex'))}); - ecdsa.key.pubkey = new Pubkey(point(BN().fromBuffer(new Buffer('ac242d242d23be966085a2b2b893d989f824e06c9ad0395a8a52f055ba39abb2', 'hex')), - BN().fromBuffer(new Buffer('4836ab292c105a711ed10fcfd30999c31ff7c02456147747e03e739ad527c380', 'hex')))); + ecdsa.key.pubkey = new Pubkey({ + point: point(BN().fromBuffer(new Buffer('ac242d242d23be966085a2b2b893d989f824e06c9ad0395a8a52f055ba39abb2', 'hex')), + BN().fromBuffer(new Buffer('4836ab292c105a711ed10fcfd30999c31ff7c02456147747e03e739ad527c380', 'hex'))) + }); describe('#set', function() { diff --git a/test/test.pubkey.js b/test/test.pubkey.js index 06e9b7a..3510b6d 100644 --- a/test/test.pubkey.js +++ b/test/test.pubkey.js @@ -12,10 +12,18 @@ describe('Pubkey', function() { it('should create a public key with a point', function() { var p = Point(); - var pk = new Pubkey(p); + var pk = new Pubkey({point: p}); should.exist(pk.point); }); + describe('#set', function() { + + it('should make a public key from a point', function() { + should.exist(Pubkey().set({point: Point.getG()}).point); + }); + + }); + describe('#fromDER', function() { it('should parse this uncompressed public key', function() {