From 28d3a40704e942c7c880748a69319f2b13bc7541 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Thu, 28 Aug 2014 16:18:36 -0700 Subject: [PATCH] Key.prototype.set --- lib/expmt/stealth.js | 6 +++--- lib/kdf.js | 2 +- lib/key.js | 14 ++++++++++---- test/test.key.js | 10 +++++++++- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/lib/expmt/stealth.js b/lib/expmt/stealth.js index 20fe407..c5fdaea 100644 --- a/lib/expmt/stealth.js +++ b/lib/expmt/stealth.js @@ -22,9 +22,9 @@ Stealth.prototype.fromAddressBuffer = function(buf) { var sPubBuf = buf.slice(33, 66); var payloadPubkey = Pubkey().fromDER(pPubBuf); - this.payloadKey = Key(undefined, payloadPubkey); + this.payloadKey = Key({pubkey: payloadPubkey}); var scanPubkey = Pubkey().fromDER(sPubBuf); - this.scanKey = Key(undefined, scanPubkey); + this.scanKey = Key({pubkey: scanPubkey}); return this; }; @@ -78,7 +78,7 @@ Stealth.prototype.getReceivePubkeyAsSender = function(senderKey) { Stealth.prototype.getReceiveKey = function(senderPubkey) { var sharedKey = this.getSharedKeyAsReceiver(senderPubkey); var privkey = Privkey(this.payloadKey.privkey.bn.add(sharedKey.privkey.bn).mod(Point.getN())); - var key = Key(privkey); + var key = Key({privkey: privkey}); key.privkey2pubkey(); return key; diff --git a/lib/kdf.js b/lib/kdf.js index 91d0752..4cba312 100644 --- a/lib/kdf.js +++ b/lib/kdf.js @@ -14,7 +14,7 @@ KDF.buf2key = function(buf) { KDF.sha256hmac2key = function(buf) { var privkey = KDF.sha256hmac2privkey(buf); - var key = new Key(privkey); + var key = new Key({privkey: privkey}); key.privkey2pubkey(); return key; }; diff --git a/lib/key.js b/lib/key.js index a7a3aa5..0515072 100644 --- a/lib/key.js +++ b/lib/key.js @@ -5,11 +5,17 @@ var Random = require('./random'); var Bn = require('./bn'); var point = require('./point'); -var Key = function Key(privkey, pubkey) { +var Key = function Key(obj) { if (!(this instanceof Key)) - return new Key(privkey, pubkey); - this.privkey = privkey; - this.pubkey = pubkey; + return new Key(obj); + if (obj) + this.set(obj); +}; + +Key.prototype.set = function(obj) { + this.privkey = obj.privkey || this.privkey || undefined; + this.pubkey = obj.pubkey || this.pubkey || undefined; + return this; }; Key.prototype.fromRandom = function() { diff --git a/test/test.key.js b/test/test.key.js index b7e76c3..05ae214 100644 --- a/test/test.key.js +++ b/test/test.key.js @@ -16,12 +16,20 @@ describe('Key', function() { it('should make a key with a priv and pub', function() { var priv = new Privkey(); var pub = new Pubkey(); - var key = new Key(priv, pub); + var key = new Key({privkey: priv, pubkey: pub}); should.exist(key); should.exist(key.privkey); should.exist(key.pubkey); }); + describe("#set", function() { + + it('should make a new priv and pub', function() { + should.exist(Key().set({privkey: Privkey()}).privkey); + }); + + }); + describe("#fromRandom", function() { it('should make a new priv and pub', function() {