diff --git a/lib/script_interpreter.js b/lib/script_interpreter.js index d683947..9a135aa 100644 --- a/lib/script_interpreter.js +++ b/lib/script_interpreter.js @@ -879,9 +879,8 @@ ScriptInterpreter.prototype.step = function() { }); // Drop the signature, since there's no way for a signature to sign itself - console.log(subscript.toString()); - subscript.findAndDelete(Script().add(bufSig)); - console.log(subscript.toString()); + var tmpScript = Script().add(bufSig); + subscript.findAndDelete(tmpScript); if (!this.checkSignatureEncoding(bufSig) || !this.checkPubkeyEncoding(bufPubkey)) { // serror is set @@ -890,11 +889,12 @@ ScriptInterpreter.prototype.step = function() { var fSuccess; try { - var sig = Signature().fromTxFormat(bufSig); - var pubkey = PublicKey().fromBuffer(bufPubkey, false); + var sig = Signature.fromTxFormat(bufSig); + var pubkey = PublicKey.fromBuffer(bufPubkey, false); fSuccess = this.tx.verify(sig, pubkey, this.nin, subscript); } catch (e) { //invalid sig or pubkey + console.log('FALSEEEEEEEEEEEEEEEEee ' + e); fSuccess = false; } @@ -980,8 +980,8 @@ ScriptInterpreter.prototype.step = function() { var fOk; try { - var sig = Signature().fromTxFormat(bufSig); - var pubkey = PublicKey().fromBuffer(bufPubkey, false); + var sig = Signature.fromTxFormat(bufSig); + var pubkey = PublicKey.fromBuffer(bufPubkey, false); fOk = this.tx.verify(sig, pubkey, this.nin, subscript); } catch (e) { //invalid sig or pubkey @@ -1088,13 +1088,13 @@ ScriptInterpreter.prototype.verify = function(scriptSig, scriptPubkey, tx, nin, return false; if (this.stack.length === 0) { - this.errstr = 'SCRIPT_ERR_EVAL_FALSE'; + this.errstr = 'SCRIPT_ERR_EVAL_FALSE_NO_RESULT'; return false; } var buf = this.stack[this.stack.length - 1]; if (!ScriptInterpreter.castToBool(buf)) { - this.errstr = 'SCRIPT_ERR_EVAL_FALSE'; + this.errstr = 'SCRIPT_ERR_EVAL_FALSE_IN_STACK'; return false; } @@ -1131,12 +1131,12 @@ ScriptInterpreter.prototype.verify = function(scriptSig, scriptPubkey, tx, nin, return false; if (stackCopy.length === 0) { - this.errstr = 'SCRIPT_ERR_EVAL_FALSE'; + this.errstr = 'SCRIPT_ERR_EVAL_FALSE_NO_P2SH_STACK'; return false; } if (!ScriptInterpreter.castToBool(stackCopy[stackCopy.length - 1])) { - this.errstr = 'SCRIPT_ERR_EVAL_FALSE'; + this.errstr = 'SCRIPT_ERR_EVAL_FALSE_IN_P2SH_STACK'; return false; } else { return true; diff --git a/lib/transaction/sighash.js b/lib/transaction/sighash.js index 3d8eca6..d2bec4a 100644 --- a/lib/transaction/sighash.js +++ b/lib/transaction/sighash.js @@ -84,6 +84,10 @@ 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(); } @@ -97,6 +101,10 @@ function sign(transaction, keypair, nhashtype, nin, subscript) { function verify(transaction, sig, pubkey, nin, subscript) { var hashbuf = sighash(transaction, sig.nhashtype, nin, subscript); hashbuf = new BufferReader(hashbuf).readReverse(); + console.log('actual:'); + console.log(hashbuf.toString('hex')); + console.log('expected:'); + console.log('f4a222b692e7f86c299f878c4b981242238f49b467b8d990219fbf5cfc0838cd'); return ECDSA.verify(hashbuf, sig, pubkey, 'little'); } diff --git a/lib/transaction/transaction.js b/lib/transaction/transaction.js index 8839ab7..5dcbba0 100644 --- a/lib/transaction/transaction.js +++ b/lib/transaction/transaction.js @@ -383,4 +383,11 @@ Transaction.prototype.isValidSignature = function(signature) { return this.inputs[signature.inputIndex].isValidSignature(self, signature); }; +/** + * @returns {bool} whether the signature is valid for this transaction input + */ +Transaction.prototype.verify = function(sig, pubkey, nin, subscript) { + return Sighash.verify(this, sig, pubkey, nin, subscript); +}; + module.exports = Transaction; diff --git a/test/script_interpreter.js b/test/script_interpreter.js index e3a72bd..3b43a2d 100644 --- a/test/script_interpreter.js +++ b/test/script_interpreter.js @@ -240,8 +240,39 @@ describe('ScriptInterpreter', function() { var scriptPubkey = Script.fromBitcoindString(vector[1]); var flags = getFlags(vector[2]); - var spendtx = Transaction(); + var hashbuf = new Buffer(32); + hashbuf.fill(0); + var credtx = Transaction(); + //credtx.addTxin(hashbuf, 0xffffffff, Script('OP_0 OP_0'), 0xffffffff); + credtx.inputs.push(new Transaction.Input({ + prevTxId: '0000000000000000000000000000000000000000000000000000000000000000', + outputIndex: 0xffffffff, + sequenceNumber: 0xffffffff, + script: Script('OP_0 OP_0') + })); + //credtx.addTxout(BN(0), scriptPubkey); + credtx._addOutput(new Transaction.Output({ + script: scriptPubkey, + satoshis: 0 + })); + var idbuf = credtx.id; + //console.log('idbuf: '+idbuf); + //console.log('expef: 9ce5586f04dd407719ab7e2ed3583583b9022f29652702cfac5ed082013461fe'); + + var spendtx = Transaction(); + //spendtx.addTxin(idbuf, 0, scriptSig, 0xffffffff); + spendtx.inputs.push(new Transaction.Input({ + prevTxId: idbuf.toString('hex'), + outputIndex: 0, + sequenceNumber: 0xffffffff, + script: scriptSig + })); + //spendtx.addTxout(BN(0), Script()); + credtx._addOutput(new Transaction.Output({ + script: Script(), + satoshis: 0 + })); var interp = ScriptInterpreter(); console.log(scriptSig.toString() + ' ' + scriptPubkey.toString()); var verified = interp.verify(scriptSig, scriptPubkey, spendtx, 0, flags);