Key.prototype.set

This commit is contained in:
Ryan X. Charles 2014-08-28 16:18:36 -07:00
parent 75c1503a92
commit 28d3a40704
4 changed files with 23 additions and 9 deletions

View File

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

View File

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

View File

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

View File

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