|
|
@ -83,15 +83,16 @@ Signature.parseDER = function(buf, strict) { |
|
|
|
if (typeof strict === 'undefined') { |
|
|
|
strict = true; |
|
|
|
} |
|
|
|
console.log('strict: '+strict); |
|
|
|
|
|
|
|
if (!Buffer.isBuffer(buf)) |
|
|
|
if (!Buffer.isBuffer(buf)) { |
|
|
|
throw new Error('DER formatted signature should be a buffer'); |
|
|
|
} |
|
|
|
|
|
|
|
var header = buf[0]; |
|
|
|
|
|
|
|
if (header !== 0x30) |
|
|
|
if (header !== 0x30) { |
|
|
|
throw new Error('Header byte should be 0x30'); |
|
|
|
} |
|
|
|
|
|
|
|
var length = buf[1]; |
|
|
|
var buflength = buf.slice(2).length; |
|
|
@ -102,30 +103,35 @@ Signature.parseDER = function(buf, strict) { |
|
|
|
} |
|
|
|
|
|
|
|
var rheader = buf[2 + 0]; |
|
|
|
if (rheader !== 0x02) |
|
|
|
if (rheader !== 0x02) { |
|
|
|
throw new Error('Integer byte for r should be 0x02'); |
|
|
|
} |
|
|
|
|
|
|
|
var rlength = buf[2 + 1]; |
|
|
|
var rbuf = buf.slice(2 + 2, 2 + 2 + rlength); |
|
|
|
var r = BN().fromBuffer(rbuf); |
|
|
|
var rneg = buf[2 + 1 + 1] === 0x00 ? true : false; |
|
|
|
if (rlength !== rbuf.length) |
|
|
|
if (rlength !== rbuf.length) { |
|
|
|
throw new Error('Length of r incorrect'); |
|
|
|
} |
|
|
|
|
|
|
|
var sheader = buf[2 + 2 + rlength + 0]; |
|
|
|
if (sheader !== 0x02) |
|
|
|
if (sheader !== 0x02) { |
|
|
|
throw new Error('Integer byte for s should be 0x02'); |
|
|
|
} |
|
|
|
|
|
|
|
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 sneg = buf[2 + 2 + rlength + 2 + 2] === 0x00 ? true : false; |
|
|
|
if (slength !== sbuf.length) |
|
|
|
if (slength !== sbuf.length) { |
|
|
|
throw new Error('Length of s incorrect'); |
|
|
|
} |
|
|
|
|
|
|
|
var sumlength = 2 + 2 + rlength + 2 + slength; |
|
|
|
if (length !== sumlength - 2) |
|
|
|
if (length !== sumlength - 2) { |
|
|
|
throw new Error('Length of signature incorrect'); |
|
|
|
} |
|
|
|
|
|
|
|
var obj = { |
|
|
|
header: header, |
|
|
@ -150,8 +156,9 @@ Signature.prototype.toCompact = function(i, compressed) { |
|
|
|
i = typeof i === 'number' ? i : this.i; |
|
|
|
compressed = typeof compressed === 'boolean' ? compressed : this.compressed; |
|
|
|
|
|
|
|
if (!(i === 0 || i === 1 || i === 2 || i === 3)) |
|
|
|
if (!(i === 0 || i === 1 || i === 2 || i === 3)) { |
|
|
|
throw new Error('i must be equal to 0, 1, 2, or 3'); |
|
|
|
} |
|
|
|
|
|
|
|
var val = i + 27 + 4; |
|
|
|
if (compressed === false) |
|
|
|