Pubkey.prototype.set
This commit is contained in:
parent
356ddcfa4e
commit
e2824035bb
@ -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();
|
||||
|
||||
|
||||
@ -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;
|
||||
};
|
||||
|
||||
@ -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() {
|
||||
|
||||
@ -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) {
|
||||
|
||||
@ -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() {
|
||||
|
||||
|
||||
@ -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() {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user