include i in sig obj + cosmetic improvements
This commit is contained in:
parent
39236fab14
commit
65c3545cb6
|
@ -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))
|
if (!(this instanceof Signature))
|
||||||
return new Signature(r, s);
|
return new Signature(r, s, i);
|
||||||
|
|
||||||
this.r = r;
|
this.r = r;
|
||||||
this.s = s;
|
this.s = s;
|
||||||
|
this.i = i; //public key recovery parameter in range [0, 3]
|
||||||
};
|
};
|
||||||
|
|
||||||
Signature.prototype.fromCompressed = function(buf) {
|
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 b2 = buf.slice(1, 33);
|
||||||
var b3 = buf.slice(33, 65);
|
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');
|
throw new Error('signature: i must be 0, 1, 2, or 3');
|
||||||
if (b2.length !== 32)
|
if (b2.length !== 32)
|
||||||
throw new Error('signature: r must be 32 bytes');
|
throw new Error('signature: r must be 32 bytes');
|
||||||
if (b3.length !== 32)
|
if (b3.length !== 32)
|
||||||
throw new Error('signature: s must be 32 bytes');
|
throw new Error('signature: s must be 32 bytes');
|
||||||
|
|
||||||
this.r = bn.fromBuffer(b2);
|
this.i = i;
|
||||||
this.s = bn.fromBuffer(b3);
|
this.r = BN().fromBuffer(b2);
|
||||||
|
this.s = BN().fromBuffer(b3);
|
||||||
};
|
};
|
||||||
|
|
||||||
Signature.prototype.fromDER = function(buf) {
|
Signature.prototype.fromDER = function(buf) {
|
||||||
|
@ -53,7 +58,7 @@ Signature.parseDER = function(buf) {
|
||||||
|
|
||||||
var rlength = buf[2 + 1];
|
var rlength = buf[2 + 1];
|
||||||
var rbuf = buf.slice(2 + 2, 2 + 2 + rlength);
|
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;
|
var rneg = buf[2 + 1 + 1] === 0x00 ? true : false;
|
||||||
if (rlength !== rbuf.length)
|
if (rlength !== rbuf.length)
|
||||||
throw new Error('signature: Length of r incorrect');
|
throw new Error('signature: Length of r incorrect');
|
||||||
|
@ -64,7 +69,7 @@ Signature.parseDER = function(buf) {
|
||||||
|
|
||||||
var slength = buf[2 + 2 + rlength + 1];
|
var slength = buf[2 + 2 + rlength + 1];
|
||||||
var sbuf = buf.slice(2 + 2 + rlength + 2, 2 + 2 + rlength + 2 + slength);
|
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;
|
var sneg = buf[2 + 2 + rlength + 2 + 2] === 0x00 ? true : false;
|
||||||
if (slength !== sbuf.length)
|
if (slength !== sbuf.length)
|
||||||
throw new Error('signature: Length of s incorrect');
|
throw new Error('signature: Length of s incorrect');
|
||||||
|
@ -92,7 +97,8 @@ Signature.parseDER = function(buf) {
|
||||||
};
|
};
|
||||||
|
|
||||||
Signature.prototype.toCompressed = function(i) {
|
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');
|
throw new Error('signature: i must be equal to 0, 1, 2, or 3');
|
||||||
|
|
||||||
var b1 = new Buffer([i]);
|
var b1 = new Buffer([i]);
|
||||||
|
|
Loading…
Reference in New Issue