Browse Source

some invalid script tests working

patch-2
Manuel Araoz 11 years ago
parent
commit
03d200bad7
  1. 22
      Script.js
  2. 69
      test/test.ScriptInterpreter.js
  3. 4
      test/test.examples.js

22
Script.js

@ -33,8 +33,10 @@ function Script(buffer) {
} else { } else {
this.buffer = util.EMPTY_BUFFER; this.buffer = util.EMPTY_BUFFER;
} }
console.log(buffertools.toHex(this.buffer));
this.chunks = []; this.chunks = [];
this.parse(); this.parse();
console.log(this.chunks);
}; };
this.class = Script; this.class = Script;
@ -51,19 +53,25 @@ Script.prototype.parse = function() {
while (!parser.eof()) { while (!parser.eof()) {
var opcode = parser.word8(); var opcode = parser.word8();
var len; var len, chunk;
if (opcode > 0 && opcode < OP_PUSHDATA1) { if (opcode > 0 && opcode < OP_PUSHDATA1) {
// Read some bytes of data, opcode value is the length of data // Read some bytes of data, opcode value is the length of data
this.chunks.push(parser.buffer(opcode)); this.chunks.push(parser.buffer(opcode));
} else if (opcode == OP_PUSHDATA1) { } else if (opcode === OP_PUSHDATA1) {
len = parser.word8(); len = parser.word8();
this.chunks.push(parser.buffer(len)); chunk = parser.buffer(len);
} else if (opcode == OP_PUSHDATA2) { if (chunk.length < len) throw new Error('Invalid data size: not enough data');
this.chunks.push(chunk);
} else if (opcode === OP_PUSHDATA2) {
len = parser.word16le(); len = parser.word16le();
this.chunks.push(parser.buffer(len)); chunk = parser.buffer(len);
} else if (opcode == OP_PUSHDATA4) { if (chunk.length < len) throw new Error('Invalid data size: not enough data');
this.chunks.push(chunk);
} else if (opcode === OP_PUSHDATA4) {
len = parser.word32le(); len = parser.word32le();
this.chunks.push(parser.buffer(len)); chunk = parser.buffer(len);
if (chunk.length < len) throw new Error('Invalid data size: not enough data');
this.chunks.push(chunk);
} else { } else {
this.chunks.push(opcode); this.chunks.push(opcode);
} }

69
test/test.ScriptInterpreter.js

@ -23,40 +23,69 @@ describe('ScriptInterpreter', function() {
var si = new ScriptInterpreter(); var si = new ScriptInterpreter();
should.exist(si); should.exist(si);
}); });
testdata.dataScriptValid.forEach(function(datum) { var testScripts = function(data, valid) {
if (datum.length < 2) throw new Error('Invalid test data'); var i = 0;
var scriptSig = datum[0]; // script inputs data.forEach(function(datum) {
var scriptPubKey = datum[1]; // output script if (datum.length < 2) throw new Error('Invalid test data');
var human = scriptSig + ' ' + scriptPubKey; var scriptSig = datum[0]; // script inputs
it('should validate script ' + human, function(done) { var scriptPubKey = datum[1]; // output script
ScriptInterpreter.verify(Script.fromHumanReadable(scriptSig), var human = scriptSig + ' ' + scriptPubKey;
Script.fromHumanReadable(scriptPubKey), it('should ' + (!valid ? 'not ' : '') + 'validate script ' + human, function(done) {
null, 0, 0, // tx, output index, and hashtype console.log((!valid ? 'invalid ' : 'valid ') + human + ';' + (i++) + ' - ' + datum[2]);
function(err, result) { try {
should.not.exist(err); ScriptInterpreter.verify(
result.should.equal(true); Script.fromHumanReadable(scriptSig),
Script.fromHumanReadable(scriptPubKey),
null, 0, 0, // tx, output index, and hashtype
function(err, result) {
if (valid) {
should.not.exist(err);
} else {
var failed = (typeof err !== 'undefined') || (result === false);
console.log('err=' + err);
console.log('result=' + result);
failed.should.equal(true);
}
if (typeof result !== 'undefined') {
result.should.equal(valid);
}
done();
}
);
} catch (e) {
if (valid) {
console.log(e);
}
valid.should.equal(false);
done(); done();
} }
);
});
}); });
}); };
testScripts(testdata.dataScriptValid, true);
testScripts(testdata.dataScriptInvalid, false);
testdata.dataSigCanonical.forEach(function(datum) { testdata.dataSigCanonical.forEach(function(datum) {
it('should validate valid canonical signatures', function() { it('should validate valid canonical signatures', function() {
ScriptInterpreter.isCanonicalSignature(new Buffer(datum,'hex')).should.equal(true); ScriptInterpreter.isCanonicalSignature(new Buffer(datum, 'hex')).should.equal(true);
}); });
}); });
testdata.dataSigNonCanonical.forEach(function(datum) { testdata.dataSigNonCanonical.forEach(function(datum) {
it('should NOT validate invalid canonical signatures', function() { it('should NOT validate invalid canonical signatures', function() {
var sig; var sig;
var isHex; var isHex;
//is Hex? //is Hex?
try { try {
sig =new Buffer(datum,'hex'); sig = new Buffer(datum, 'hex');
isHex=1; isHex = 1;
} catch (e) { } } catch (e) {}
if (isHex) if (isHex)
ScriptInterpreter.isCanonicalSignature.bind(sig).should.throw(); ScriptInterpreter.isCanonicalSignature.bind(sig).should.
throw ();
}); });
}); });

4
test/test.examples.js

@ -15,8 +15,8 @@ var examples = [
]; ];
describe('Examples', function() { describe('Examples', function() {
before(mute); //before(mute);
after(unmute); //after(unmute);
examples.forEach(function(example) { examples.forEach(function(example) {
it('valid '+example, function() { it('valid '+example, function() {
var ex = require('../examples/'+example); var ex = require('../examples/'+example);

Loading…
Cancel
Save