Browse Source

all script_valid tests passing!

patch-2
Manuel Araoz 11 years ago
parent
commit
ddd11e908c
  1. 25
      lib/crypto/signature.js
  2. 8
      lib/publickey.js
  3. 1
      lib/script_interpreter.js
  4. 8
      lib/transaction/sighash.js

25
lib/crypto/signature.js

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

8
lib/publickey.js

@ -250,15 +250,15 @@ PublicKey.fromPrivateKey = function(privkey) {
/** /**
* Instantiate a PublicKey from a Buffer * Instantiate a PublicKey from a Buffer
* * @param {Buffer} buf - A DER hex buffer
* @param {Buffer} buf - A DER buffer * @param {bool} [strict] - if set to false, will loosen some conditions
* @returns {PublicKey} A new valid instance of PublicKey * @returns {PublicKey} A new valid instance of PublicKey
*/ */
PublicKey.fromDER = PublicKey.fromBuffer = function(buf) { PublicKey.fromBuffer = function(buf, strict) {
if (!PublicKey._isBuffer(buf)) { if (!PublicKey._isBuffer(buf)) {
throw new TypeError('Must be a hex buffer of DER encoded public key'); 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, { return new PublicKey(info.point, {
compressed: info.compressed compressed: info.compressed
}); });

1
lib/script_interpreter.js

@ -889,6 +889,7 @@ ScriptInterpreter.prototype.step = function() {
var fSuccess; var fSuccess;
var sig = Signature.fromTxFormat(bufSig); var sig = Signature.fromTxFormat(bufSig);
var pubkey = PublicKey.fromBuffer(bufPubkey, false);
try { try {
var sig = Signature.fromTxFormat(bufSig); var sig = Signature.fromTxFormat(bufSig);
var pubkey = PublicKey.fromBuffer(bufPubkey, false); var pubkey = PublicKey.fromBuffer(bufPubkey, false);

8
lib/transaction/sighash.js

@ -85,10 +85,6 @@ function sighash(transaction, sighashType, inputNumber, subscript) {
.write(txcopy.toBuffer()) .write(txcopy.toBuffer())
.writeInt32LE(sighashType) .writeInt32LE(sighashType)
.toBuffer(); .toBuffer();
//console.log('actual:');
//console.log(buf.toString('hex'));
//console.log('expected:');
//console.log('01000000019ce5586f04dd407719ab7e2ed3583583b9022f29652702cfac5ed082013461fe0000000043410479be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8acffffffff010000000000000000000000000001000000');
return BufferReader(Hash.sha256sha256(buf)).readReverse(); return BufferReader(Hash.sha256sha256(buf)).readReverse();
} }
@ -103,10 +99,6 @@ function verify(transaction, sig, pubkey, nin, subscript) {
$.checkArgument(transaction); $.checkArgument(transaction);
$.checkArgument(sig && sig.nhashtype); $.checkArgument(sig && sig.nhashtype);
var hashbuf = sighash(transaction, sig.nhashtype, nin, subscript); 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'); return ECDSA.verify(hashbuf, sig, pubkey, 'little');
} }

Loading…
Cancel
Save