Signature.prototype.set

This commit is contained in:
Ryan X. Charles 2014-08-28 16:38:21 -07:00
parent 28d3a40704
commit a5f79c7651
4 changed files with 23 additions and 10 deletions

View File

@ -166,7 +166,7 @@ ECDSA.prototype.sign = function() {
var s = k.invm(N).mul(e.add(d.mul(r))).mod(N);
} while (r.cmp(0) <= 0 || s.cmp(0) <= 0);
this.sig = new Signature(r, s, undefined, this.key.pubkey.compressed);
this.sig = new Signature({r: r, s: s, compressed: this.key.pubkey.compressed});
return this.sig;
};

View File

@ -2,14 +2,19 @@ var BN = require('./bn');
var Point = require('./point');
var Pubkey = require('./pubkey');
var Signature = function Signature(r, s, i, compressed) {
var Signature = function Signature(obj) {
if (!(this instanceof Signature))
return new Signature(r, s, i, compressed);
return new Signature(obj);
if (obj)
this.set(obj);
};
this.r = r;
this.s = s;
this.i = i; //public key recovery parameter in range [0, 3]
this.compressed = compressed;
Signature.prototype.set = function(obj) {
this.r = obj.r || this.r || undefined;
this.s = obj.s || this.s || undefined;
this.i = typeof obj.i !== 'undefined' ? obj.i : this.i; //public key recovery parameter in range [0, 3]
this.compressed = typeof obj.compressed !== 'undefined' ? obj.compressed : this.compressed; //whether the recovered pubkey is compressed
return this;
};
Signature.prototype.fromCompact = function(buf) {

View File

@ -48,7 +48,7 @@ describe("ECDSA", function() {
ecdsa.key.privkey.bn = BN().fromBuffer(Hash.sha256(new Buffer('test')));
ecdsa.key.privkey2pubkey();
ecdsa.hashbuf = hashbuf;
ecdsa.sig = new Signature(r, s);
ecdsa.sig = new Signature({r: r, s: s});
ecdsa.calci();
ecdsa.sig.i.should.equal(1);

View File

@ -9,6 +9,14 @@ describe('Signature', function() {
should.exist(sig);
});
describe('#set', function() {
it('should set compressed', function() {
should.exist(Signature().set({compressed: true}));
});
});
describe('#fromCompact', function() {
it('should create a signature from a compressed signature', function() {
@ -110,7 +118,7 @@ describe('Signature', function() {
it('should convert these known r and s values into a known signature', function() {
var r = bn('63173831029936981022572627018246571655303050627048489594159321588908385378810');
var s = bn('4331694221846364448463828256391194279133231453999942381442030409253074198130');
var sig = new Signature(r, s);
var sig = new Signature({r: r, s: s});
var der = sig.toDER(r, s);
der.toString('hex').should.equal('30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72');
});
@ -122,7 +130,7 @@ describe('Signature', function() {
it('should convert this signature in to hex DER', function() {
var r = bn('63173831029936981022572627018246571655303050627048489594159321588908385378810');
var s = bn('4331694221846364448463828256391194279133231453999942381442030409253074198130');
var sig = new Signature(r, s);
var sig = new Signature({r: r, s: s});
var hex = sig.toString();
hex.should.equal('30450221008bab1f0a2ff2f9cb8992173d8ad73c229d31ea8e10b0f4d4ae1a0d8ed76021fa02200993a6ec81755b9111762fc2cf8e3ede73047515622792110867d12654275e72');
});