getSharedKeypair & getReceivePubkey

This commit is contained in:
Ryan X. Charles 2014-09-02 15:25:16 -07:00
parent 48ae69cab0
commit d1a570135d
2 changed files with 56 additions and 2 deletions

View File

@ -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);

View File

@ -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() {