generalize script tests
This commit is contained in:
parent
ddd11e908c
commit
4b68c3725d
|
@ -896,7 +896,6 @@ ScriptInterpreter.prototype.step = function() {
|
||||||
fSuccess = this.tx.verify(sig, pubkey, this.nin, subscript);
|
fSuccess = this.tx.verify(sig, pubkey, this.nin, subscript);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
//invalid sig or pubkey
|
//invalid sig or pubkey
|
||||||
console.log('FALSEEEEEEEEEEEEEEEEee ' + e);
|
|
||||||
fSuccess = false;
|
fSuccess = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -57,30 +57,6 @@ Script.fromBitcoindString = function(str) {
|
||||||
var buf = bw.concat();
|
var buf = bw.concat();
|
||||||
return this.fromBuffer(buf);
|
return this.fromBuffer(buf);
|
||||||
};
|
};
|
||||||
|
|
||||||
//the script string format used in bitcoind data tests
|
|
||||||
Script.toBitcoindString = function() {
|
|
||||||
var str = '';
|
|
||||||
for (var i = 0; i < this.chunks.length; i++) {
|
|
||||||
var chunk = this.chunks[i];
|
|
||||||
if (chunk.buf) {
|
|
||||||
var buf = Script({
|
|
||||||
chunks: [chunk]
|
|
||||||
}).toBuffer();
|
|
||||||
var hex = buf.toString('hex');
|
|
||||||
str = str + ' ' + '0x' + hex;
|
|
||||||
} else if (typeof Opcode.str[chunk.opcodenum] !== 'undefined') {
|
|
||||||
var ostr = Opcode(chunk.opcodenum).toString();
|
|
||||||
str = str + ' ' + ostr.slice(3); //remove OP_
|
|
||||||
} else {
|
|
||||||
str = str + ' ' + '0x' + chunk.opcodenum.toString(16);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return str.substr(1);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
describe('ScriptInterpreter', function() {
|
describe('ScriptInterpreter', function() {
|
||||||
|
|
||||||
it('should make a new interp', function() {
|
it('should make a new interp', function() {
|
||||||
|
@ -225,50 +201,43 @@ describe('ScriptInterpreter', function() {
|
||||||
return flags;
|
return flags;
|
||||||
};
|
};
|
||||||
|
|
||||||
var c = 0;
|
|
||||||
describe.only('bitcoind fixtures', function() {
|
var testToFromString = function(script) {
|
||||||
script_valid.forEach(function(vector) {
|
var s = script.toString();
|
||||||
if (vector.length === 1) {
|
Script.fromString(s).toString().should.equal(s);
|
||||||
return;
|
};
|
||||||
}
|
|
||||||
c++;
|
var testFixture = function(vector, expected) {
|
||||||
var descstr = vector[3];
|
|
||||||
var fullScriptString = vector[0] + ' ' + vector[1];
|
|
||||||
var comment = descstr ? (' (' + descstr + ')') : '';
|
|
||||||
it('should pass script_valid vector #' + c + ': ' + fullScriptString + comment, function() {
|
|
||||||
var scriptSig = Script.fromBitcoindString(vector[0]);
|
var scriptSig = Script.fromBitcoindString(vector[0]);
|
||||||
var scriptPubkey = Script.fromBitcoindString(vector[1]);
|
var scriptPubkey = Script.fromBitcoindString(vector[1]);
|
||||||
var flags = getFlags(vector[2]);
|
var flags = getFlags(vector[2]);
|
||||||
|
|
||||||
|
|
||||||
|
//testToFromString(scriptSig);
|
||||||
|
//testToFromString(scriptPubkey);
|
||||||
|
|
||||||
var hashbuf = new Buffer(32);
|
var hashbuf = new Buffer(32);
|
||||||
hashbuf.fill(0);
|
hashbuf.fill(0);
|
||||||
var credtx = Transaction();
|
var credtx = Transaction();
|
||||||
//credtx.addTxin(hashbuf, 0xffffffff, Script('OP_0 OP_0'), 0xffffffff);
|
|
||||||
credtx.inputs.push(new Transaction.Input({
|
credtx.inputs.push(new Transaction.Input({
|
||||||
prevTxId: '0000000000000000000000000000000000000000000000000000000000000000',
|
prevTxId: '0000000000000000000000000000000000000000000000000000000000000000',
|
||||||
outputIndex: 0xffffffff,
|
outputIndex: 0xffffffff,
|
||||||
sequenceNumber: 0xffffffff,
|
sequenceNumber: 0xffffffff,
|
||||||
script: Script('OP_0 OP_0')
|
script: Script('OP_0 OP_0')
|
||||||
}));
|
}));
|
||||||
//credtx.addTxout(BN(0), scriptPubkey);
|
|
||||||
credtx._addOutput(new Transaction.Output({
|
credtx._addOutput(new Transaction.Output({
|
||||||
script: scriptPubkey,
|
script: scriptPubkey,
|
||||||
satoshis: 0
|
satoshis: 0
|
||||||
}));
|
}));
|
||||||
var idbuf = credtx.id;
|
var idbuf = credtx.id;
|
||||||
//console.log('idbuf: '+idbuf);
|
|
||||||
//console.log('expef: 9ce5586f04dd407719ab7e2ed3583583b9022f29652702cfac5ed082013461fe');
|
|
||||||
|
|
||||||
|
|
||||||
var spendtx = Transaction();
|
var spendtx = Transaction();
|
||||||
//spendtx.addTxin(idbuf, 0, scriptSig, 0xffffffff);
|
|
||||||
spendtx.inputs.push(new Transaction.Input({
|
spendtx.inputs.push(new Transaction.Input({
|
||||||
prevTxId: idbuf.toString('hex'),
|
prevTxId: idbuf.toString('hex'),
|
||||||
outputIndex: 0,
|
outputIndex: 0,
|
||||||
sequenceNumber: 0xffffffff,
|
sequenceNumber: 0xffffffff,
|
||||||
script: scriptSig
|
script: scriptSig
|
||||||
}));
|
}));
|
||||||
//spendtx.addTxout(BN(0), Script());
|
|
||||||
spendtx._addOutput(new Transaction.Output({
|
spendtx._addOutput(new Transaction.Output({
|
||||||
script: Script(),
|
script: Script(),
|
||||||
satoshis: 0
|
satoshis: 0
|
||||||
|
@ -278,40 +247,28 @@ describe('ScriptInterpreter', function() {
|
||||||
console.log(scriptSig.toString() + ' ' + scriptPubkey.toString());
|
console.log(scriptSig.toString() + ' ' + scriptPubkey.toString());
|
||||||
var verified = interp.verify(scriptSig, scriptPubkey, spendtx, 0, flags);
|
var verified = interp.verify(scriptSig, scriptPubkey, spendtx, 0, flags);
|
||||||
console.log(interp.errstr);
|
console.log(interp.errstr);
|
||||||
verified.should.equal(true);
|
verified.should.equal(expected);
|
||||||
});
|
};
|
||||||
});
|
describe.only('bitcoind fixtures', function() {
|
||||||
|
var testAllFixtures = function(set, expected) {
|
||||||
c = 0;
|
var c = 0;
|
||||||
script_invalid.forEach(function(vector) {
|
script_valid.forEach(function(vector) {
|
||||||
if (vector.length === 1) {
|
if (vector.length === 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
c++;
|
c++;
|
||||||
var descstr = vector[3];
|
var descstr = vector[3];
|
||||||
it('should pass script_invalid vector ' + c + '(' + descstr + ')', function() {
|
var fullScriptString = vector[0] + ' ' + vector[1];
|
||||||
var scriptSig = Script.fromBitcoindString(vector[0]);
|
var comment = descstr ? (' (' + descstr + ')') : '';
|
||||||
var scriptPubkey = Script.fromBitcoindString(vector[1]);
|
it('should pass script_valid vector #' + c + ': ' + fullScriptString + comment, function() {
|
||||||
var flags = getFlags(vector[2]);
|
testFixture(vector, expected);
|
||||||
|
|
||||||
var hashbuf = new Buffer(32);
|
|
||||||
hashbuf.fill(0);
|
|
||||||
var credtx = Transaction();
|
|
||||||
credtx.addTxin(hashbuf, 0xffffffff, Script('OP_0 OP_0'), 0xffffffff);
|
|
||||||
credtx.addTxout(BN(0), scriptPubkey);
|
|
||||||
|
|
||||||
var idbuf = credtx.hash();
|
|
||||||
var spendtx = Transaction();
|
|
||||||
spendtx.addTxin(idbuf, 0, scriptSig, 0xffffffff);
|
|
||||||
spendtx.addTxout(BN(0), Script());
|
|
||||||
|
|
||||||
var interp = ScriptInterpreter();
|
|
||||||
var verified = interp.verify(scriptSig, scriptPubkey, spendtx, 0, flags);
|
|
||||||
verified.should.equal(false);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
};
|
||||||
|
testAllFixtures(script_valid, true);
|
||||||
|
testAllFixtures(script_invalid, false);
|
||||||
|
|
||||||
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;
|
||||||
|
|
Loading…
Reference in New Issue