diff --git a/lib/crypto/signature.js b/lib/crypto/signature.js index e31e700a4..48cb923cc 100644 --- a/lib/crypto/signature.js +++ b/lib/crypto/signature.js @@ -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) diff --git a/lib/publickey.js b/lib/publickey.js index b44c94e55..9f87266f9 100644 --- a/lib/publickey.js +++ b/lib/publickey.js @@ -250,15 +250,15 @@ PublicKey.fromPrivateKey = function(privkey) { /** * Instantiate a PublicKey from a Buffer - * - * @param {Buffer} buf - A DER buffer + * @param {Buffer} buf - A DER hex buffer + * @param {bool} [strict] - if set to false, will loosen some conditions * @returns {PublicKey} A new valid instance of PublicKey */ -PublicKey.fromDER = PublicKey.fromBuffer = function(buf) { +PublicKey.fromBuffer = function(buf, strict) { if (!PublicKey._isBuffer(buf)) { throw new TypeError('Must be a hex buffer of DER encoded public key'); } - var info = PublicKey._transformDER(buf); + var info = PublicKey._transformDER(buf, strict); return new PublicKey(info.point, { compressed: info.compressed }); diff --git a/lib/script_interpreter.js b/lib/script_interpreter.js index e8f3f690a..2ff59616c 100644 --- a/lib/script_interpreter.js +++ b/lib/script_interpreter.js @@ -888,7 +888,8 @@ ScriptInterpreter.prototype.step = function() { } var fSuccess; - var sig = Signature.fromTxFormat(bufSig); + var sig = Signature.fromTxFormat(bufSig); + var pubkey = PublicKey.fromBuffer(bufPubkey, false); try { var sig = Signature.fromTxFormat(bufSig); var pubkey = PublicKey.fromBuffer(bufPubkey, false); diff --git a/lib/transaction/sighash.js b/lib/transaction/sighash.js index 6bd2ac40d..979bdf51a 100644 --- a/lib/transaction/sighash.js +++ b/lib/transaction/sighash.js @@ -85,10 +85,6 @@ function sighash(transaction, sighashType, inputNumber, subscript) { .write(txcopy.toBuffer()) .writeInt32LE(sighashType) .toBuffer(); - //console.log('actual:'); - //console.log(buf.toString('hex')); - //console.log('expected:'); - //console.log('01000000019ce5586f04dd407719ab7e2ed3583583b9022f29652702cfac5ed082013461fe0000000043410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8acffffffff010000000000000000000000000001000000'); return BufferReader(Hash.sha256sha256(buf)).readReverse(); } @@ -103,10 +99,6 @@ function verify(transaction, sig, pubkey, nin, subscript) { $.checkArgument(transaction); $.checkArgument(sig && sig.nhashtype); var hashbuf = sighash(transaction, sig.nhashtype, nin, subscript); - console.log('actual:'); - console.log(hashbuf.toString('hex')); - console.log('expected:'); - console.log('f4a222b692e7f86c299f878c4b981242238f49b467b8d990219fbf5cfc0838cd'); return ECDSA.verify(hashbuf, sig, pubkey, 'little'); }