Merge pull request #769 from maraoz/add/tx_valid/tests
Add tx_valid.json tests
This commit is contained in:
commit
a1158dd46c
@ -883,7 +883,6 @@ ScriptInterpreter.prototype.step = function() {
|
|||||||
subscript.findAndDelete(tmpScript);
|
subscript.findAndDelete(tmpScript);
|
||||||
|
|
||||||
if (!this.checkSignatureEncoding(bufSig) || !this.checkPubkeyEncoding(bufPubkey)) {
|
if (!this.checkSignatureEncoding(bufSig) || !this.checkPubkeyEncoding(bufPubkey)) {
|
||||||
// serror is set
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -973,7 +972,6 @@ ScriptInterpreter.prototype.step = function() {
|
|||||||
var bufPubkey = this.stack[this.stack.length - ikey];
|
var bufPubkey = this.stack[this.stack.length - ikey];
|
||||||
|
|
||||||
if (!this.checkSignatureEncoding(bufSig) || !this.checkPubkeyEncoding(bufPubkey)) {
|
if (!this.checkSignatureEncoding(bufSig) || !this.checkPubkeyEncoding(bufPubkey)) {
|
||||||
// serror is set
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1045,6 +1043,16 @@ ScriptInterpreter.prototype.step = function() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Verifies a Script by executing it and returns true if it is valid.
|
||||||
|
* This function needs to be provided with the scriptSig and the scriptPubkey
|
||||||
|
* separately.
|
||||||
|
* @param {Script} scriptSig - the script's first part (corresponding to the tx input)
|
||||||
|
* @param {Script} scriptPubkey - the script's last part (corresponding to the tx output)
|
||||||
|
* @param {Transaction} [tx] - the Transaction containing the scriptSig in one input (used
|
||||||
|
* to check signature validity for some opcodes like OP_CHECKSIG)
|
||||||
|
* @param {number} nin - index of the transaction input containing the scriptSig verified.
|
||||||
|
* @param {number} flags - evaluation flags. See ScriptInterpreter.SCRIPT_* constants
|
||||||
|
*
|
||||||
* Translated from bitcoind's VerifyScript
|
* Translated from bitcoind's VerifyScript
|
||||||
*/
|
*/
|
||||||
ScriptInterpreter.prototype.verify = function(scriptSig, scriptPubkey, tx, nin, flags) {
|
ScriptInterpreter.prototype.verify = function(scriptSig, scriptPubkey, tx, nin, flags) {
|
||||||
@ -1054,6 +1062,9 @@ ScriptInterpreter.prototype.verify = function(scriptSig, scriptPubkey, tx, nin,
|
|||||||
if (_.isUndefined(nin)) {
|
if (_.isUndefined(nin)) {
|
||||||
nin = 0;
|
nin = 0;
|
||||||
}
|
}
|
||||||
|
if (_.isUndefined(flags)) {
|
||||||
|
flags = 0;
|
||||||
|
}
|
||||||
this.set({
|
this.set({
|
||||||
script: scriptSig,
|
script: scriptSig,
|
||||||
tx: tx,
|
tx: tx,
|
||||||
@ -1128,7 +1139,6 @@ ScriptInterpreter.prototype.verify = function(scriptSig, scriptPubkey, tx, nin,
|
|||||||
|
|
||||||
// evaluate redeemScript
|
// evaluate redeemScript
|
||||||
if (!this.evaluate())
|
if (!this.evaluate())
|
||||||
// serror is set
|
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (stackCopy.length === 0) {
|
if (stackCopy.length === 0) {
|
||||||
|
|||||||
@ -11,6 +11,7 @@ var BN = require('../crypto/bn');
|
|||||||
var Hash = require('../crypto/hash');
|
var Hash = require('../crypto/hash');
|
||||||
var ECDSA = require('../crypto/ecdsa');
|
var ECDSA = require('../crypto/ecdsa');
|
||||||
var $ = require('../util/preconditions');
|
var $ = require('../util/preconditions');
|
||||||
|
var _ = require('lodash');
|
||||||
|
|
||||||
var SIGHASH_SINGLE_BUG = '0000000000000000000000000000000000000000000000000000000000000001';
|
var SIGHASH_SINGLE_BUG = '0000000000000000000000000000000000000000000000000000000000000001';
|
||||||
var BITS_64_ON = 'ffffffffffffffff';
|
var BITS_64_ON = 'ffffffffffffffff';
|
||||||
@ -122,8 +123,8 @@ function sign(transaction, privateKey, sighashType, inputIndex, subscript) {
|
|||||||
* @return {boolean}
|
* @return {boolean}
|
||||||
*/
|
*/
|
||||||
function verify(transaction, signature, publicKey, inputIndex, subscript) {
|
function verify(transaction, signature, publicKey, inputIndex, subscript) {
|
||||||
$.checkArgument(transaction);
|
$.checkArgument(!_.isUndefined(transaction));
|
||||||
$.checkArgument(signature && signature.nhashtype);
|
$.checkArgument(!_.isUndefined(signature) && !_.isUndefined(signature.nhashtype));
|
||||||
var hashbuf = sighash(transaction, signature.nhashtype, inputIndex, subscript);
|
var hashbuf = sighash(transaction, signature.nhashtype, inputIndex, subscript);
|
||||||
return ECDSA.verify(hashbuf, signature, publicKey, 'little');
|
return ECDSA.verify(hashbuf, signature, publicKey, 'little');
|
||||||
}
|
}
|
||||||
|
|||||||
@ -202,7 +202,7 @@ describe('ScriptInterpreter', function() {
|
|||||||
var verified = interp.verify(scriptSig, scriptPubkey, spendtx, 0, flags);
|
var verified = interp.verify(scriptSig, scriptPubkey, spendtx, 0, flags);
|
||||||
verified.should.equal(expected);
|
verified.should.equal(expected);
|
||||||
};
|
};
|
||||||
describe('bitcoind fixtures', function() {
|
describe('bitcoind script evaluation fixtures', function() {
|
||||||
var testAllFixtures = function(set, expected) {
|
var testAllFixtures = function(set, expected) {
|
||||||
var c = 0;
|
var c = 0;
|
||||||
set.forEach(function(vector) {
|
set.forEach(function(vector) {
|
||||||
@ -221,13 +221,15 @@ describe('ScriptInterpreter', function() {
|
|||||||
testAllFixtures(script_valid, true);
|
testAllFixtures(script_valid, true);
|
||||||
testAllFixtures(script_invalid, false);
|
testAllFixtures(script_invalid, false);
|
||||||
|
|
||||||
|
});
|
||||||
|
describe('bitcoind transaction evaluation fixtures', function() {
|
||||||
var c = 0;
|
var c = 0;
|
||||||
tx_valid.forEach(function(vector) {
|
tx_valid.forEach(function(vector) {
|
||||||
if (vector.length === 1) {
|
if (vector.length === 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
c++;
|
c++;
|
||||||
it.skip('should pass tx_valid vector ' + c, function() {
|
it('should pass tx_valid vector ' + c, function() {
|
||||||
var inputs = vector[0];
|
var inputs = vector[0];
|
||||||
var txhex = vector[1];
|
var txhex = vector[1];
|
||||||
var flags = getFlags(vector[2]);
|
var flags = getFlags(vector[2]);
|
||||||
@ -240,8 +242,7 @@ describe('ScriptInterpreter', function() {
|
|||||||
if (txoutnum === -1) {
|
if (txoutnum === -1) {
|
||||||
txoutnum = 0xffffffff; //bitcoind casts -1 to an unsigned int
|
txoutnum = 0xffffffff; //bitcoind casts -1 to an unsigned int
|
||||||
}
|
}
|
||||||
var txkey = txid + ':' + txoutnum;
|
map[txid + ':' + txoutnum] = Script.fromBitcoindString(scriptPubKeyStr);
|
||||||
map[txkey] = Script.fromBitcoindString(scriptPubKeyStr);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
var tx = Transaction(txhex);
|
var tx = Transaction(txhex);
|
||||||
@ -249,9 +250,9 @@ describe('ScriptInterpreter', function() {
|
|||||||
var scriptSig = txin.script;
|
var scriptSig = txin.script;
|
||||||
var txidhex = txin.prevTxId.toString('hex');
|
var txidhex = txin.prevTxId.toString('hex');
|
||||||
var txoutnum = txin.outputIndex;
|
var txoutnum = txin.outputIndex;
|
||||||
var txkey = txidhex + ':' + txoutnum;
|
var scriptPubkey = map[txidhex + ':' + txoutnum];
|
||||||
var scriptPubkey = map[txkey];
|
|
||||||
should.exist(scriptPubkey);
|
should.exist(scriptPubkey);
|
||||||
|
should.exist(scriptSig);
|
||||||
var interp = ScriptInterpreter();
|
var interp = ScriptInterpreter();
|
||||||
var verified = interp.verify(scriptSig, scriptPubkey, tx, j, flags);
|
var verified = interp.verify(scriptSig, scriptPubkey, tx, j, flags);
|
||||||
verified.should.equal(true);
|
verified.should.equal(true);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user