include i in sig obj + cosmetic improvements

This commit is contained in:
Ryan X. Charles 2014-08-19 16:27:28 -07:00
parent 39236fab14
commit 65c3545cb6
1 changed files with 16 additions and 10 deletions

View File

@ -1,26 +1,31 @@
var bn = require('./bn');
var BN = require('./bn');
var Point = require('./point');
var Pubkey = require('./pubkey');
var Signature = function Signature(r, s) {
var Signature = function Signature(r, s, i) {
if (!(this instanceof Signature))
return new Signature(r, s);
return new Signature(r, s, i);
this.r = r;
this.s = s;
this.i = i; //public key recovery parameter in range [0, 3]
};
Signature.prototype.fromCompressed = function(buf) {
var b1 = buf.slice(0, 1)[0];
var i = buf.slice(0, 1)[0];
var b2 = buf.slice(1, 33);
var b3 = buf.slice(33, 65);
if (!(b1 === 0 || b1 === 1 || b1 === 2 || b1 === 3))
if (!(i === 0 || i === 1 || i === 2 || i === 3))
throw new Error('signature: i must be 0, 1, 2, or 3');
if (b2.length !== 32)
throw new Error('signature: r must be 32 bytes');
if (b3.length !== 32)
throw new Error('signature: s must be 32 bytes');
this.r = bn.fromBuffer(b2);
this.s = bn.fromBuffer(b3);
this.i = i;
this.r = BN().fromBuffer(b2);
this.s = BN().fromBuffer(b3);
};
Signature.prototype.fromDER = function(buf) {
@ -53,7 +58,7 @@ Signature.parseDER = function(buf) {
var rlength = buf[2 + 1];
var rbuf = buf.slice(2 + 2, 2 + 2 + rlength);
var r = bn.fromBuffer(rbuf);
var r = BN().fromBuffer(rbuf);
var rneg = buf[2 + 1 + 1] === 0x00 ? true : false;
if (rlength !== rbuf.length)
throw new Error('signature: Length of r incorrect');
@ -64,7 +69,7 @@ Signature.parseDER = function(buf) {
var slength = buf[2 + 2 + rlength + 1];
var sbuf = buf.slice(2 + 2 + rlength + 2, 2 + 2 + rlength + 2 + slength);
var s = bn.fromBuffer(sbuf);
var s = BN().fromBuffer(sbuf);
var sneg = buf[2 + 2 + rlength + 2 + 2] === 0x00 ? true : false;
if (slength !== sbuf.length)
throw new Error('signature: Length of s incorrect');
@ -92,7 +97,8 @@ Signature.parseDER = function(buf) {
};
Signature.prototype.toCompressed = function(i) {
if (!(i === 0 || i === 1 || i ===2 || i ===3))
i = typeof i === 'number' ? i : this.i;
if (!(i === 0 || i === 1 || i === 2 || i === 3))
throw new Error('signature: i must be equal to 0, 1, 2, or 3');
var b1 = new Buffer([i]);