From d1a570135d6b9ed75914c4e58963bcfd82a492c6 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Tue, 2 Sep 2014 15:25:16 -0700 Subject: [PATCH] getSharedKeypair & getReceivePubkey --- lib/expmt/stealthaddress.js | 21 +++++++++++++++++++-- test/stealthaddress.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/lib/expmt/stealthaddress.js b/lib/expmt/stealthaddress.js index 9f6e3f987..0d8212853 100644 --- a/lib/expmt/stealthaddress.js +++ b/lib/expmt/stealthaddress.js @@ -1,6 +1,7 @@ var Stealthkey = require('./stealthkey'); var Base58check = require('../base58check'); var Pubkey = require('../pubkey'); +var KDF = require('../kdf'); var StealthAddress = function StealthAddress(addrstr) { if (!(this instanceof StealthAddress)) @@ -27,8 +28,8 @@ StealthAddress.prototype.set = function(obj) { StealthAddress.prototype.fromStealthkey = function(stealthkey) { this.set({ - payloadPubkey: stealthkey.payloadPubkey, - scanPubkey: stealthkey.scanPubkey + payloadPubkey: stealthkey.payloadKeypair.pubkey, + scanPubkey: stealthkey.scanKeypair.pubkey }); return this; }; @@ -53,6 +54,22 @@ StealthAddress.prototype.fromString = function(str) { return this; }; +StealthAddress.prototype.getSharedKeypair = function(senderKeypair) { + var sharedSecretPoint = this.scanPubkey.point.mul(senderKeypair.privkey.bn); + var sharedSecretPubkey = Pubkey(sharedSecretPoint); + var buf = sharedSecretPubkey.toDER(true); + var sharedKeypair = KDF.sha256hmac2keypair(buf); + + return sharedKeypair; +}; + +StealthAddress.prototype.getReceivePubkey = function(senderKeypair) { + var sharedKeypair = this.getSharedKeypair(senderKeypair); + var pubkey = Pubkey(this.payloadPubkey.point.add(sharedKeypair.pubkey.point)); + + return pubkey; +}; + StealthAddress.prototype.toBuffer = function() { var pBuf = this.payloadPubkey.toDER(true); var sBuf = this.scanPubkey.toDER(true); diff --git a/test/stealthaddress.js b/test/stealthaddress.js index 872b1de0a..e4962be62 100644 --- a/test/stealthaddress.js +++ b/test/stealthaddress.js @@ -61,6 +61,43 @@ describe('StealthAddress', function() { }); + describe('#getSharedKeypair', function() { + + it('should return a key', function() { + var sa = new StealthAddress(); + sa.payloadPubkey = stealthkey.payloadKeypair.pubkey; + sa.scanPubkey = stealthkey.scanKeypair.pubkey; + var key = sa.getSharedKeypair(senderKeypair); + (key instanceof Keypair).should.equal(true); + }); + + it('should return the same key as Stealthkey.prototype.getSharedKeypairAsReceiver', function() { + var sa = new StealthAddress(); + sa.payloadPubkey = stealthkey.payloadKeypair.pubkey; + sa.scanPubkey = stealthkey.scanKeypair.pubkey; + var key = sa.getSharedKeypair(senderKeypair); + + var key2 = stealthkey.getSharedKeypairAsReceiver(senderKeypair.pubkey); + key.toString().should.equal(key2.toString()); + }); + + }); + + describe('#getReceivePubkey', function() { + + it('should return a pubkey', function() { + var pubkey = StealthAddress().fromStealthkey(stealthkey).getReceivePubkey(senderKeypair); + (pubkey instanceof Pubkey).should.equal(true); + }); + + it('should return the same pubkey as getReceivePubkeyAsReceiver', function() { + var pubkey = StealthAddress().fromStealthkey(stealthkey).getReceivePubkey(senderKeypair); + var pubkey2 = stealthkey.getReceivePubkeyAsReceiver(senderKeypair.pubkey); + pubkey2.toString().should.equal(pubkey.toString()); + }); + + }); + describe('#toBuffer', function() { it('should return this known address buffer', function() {