From a5f79c7651fcb7511c2034d51fecd45d0d6b52c6 Mon Sep 17 00:00:00 2001 From: "Ryan X. Charles" Date: Thu, 28 Aug 2014 16:38:21 -0700 Subject: [PATCH] Signature.prototype.set --- lib/ecdsa.js | 2 +- lib/signature.js | 17 +++++++++++------ test/test.ecdsa.js | 2 +- test/test.signature.js | 12 ++++++++++-- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/ecdsa.js b/lib/ecdsa.js index c5fe908..ec57b23 100644 --- a/lib/ecdsa.js +++ b/lib/ecdsa.js @@ -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; }; diff --git a/lib/signature.js b/lib/signature.js index 775d9b4..d6d3739 100644 --- a/lib/signature.js +++ b/lib/signature.js @@ -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) { diff --git a/test/test.ecdsa.js b/test/test.ecdsa.js index f17a8a4..b8ff3bc 100644 --- a/test/test.ecdsa.js +++ b/test/test.ecdsa.js @@ -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); diff --git a/test/test.signature.js b/test/test.signature.js index d52bcd5..1d6972b 100644 --- a/test/test.signature.js +++ b/test/test.signature.js @@ -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'); });