2014-02-18 08:17:23 -08:00
|
|
|
'use strict';
|
|
|
|
|
2014-03-08 13:42:08 -08:00
|
|
|
var chai = chai || require('chai');
|
|
|
|
var bitcore = bitcore || require('../bitcore');
|
2014-03-18 08:08:26 -07:00
|
|
|
var buffertools = require('buffertools');
|
2014-02-18 08:17:23 -08:00
|
|
|
|
|
|
|
var should = chai.should();
|
2014-03-10 09:44:22 -07:00
|
|
|
var testdata = testdata || require('./testdata');
|
2014-02-18 08:17:23 -08:00
|
|
|
|
|
|
|
var ScriptInterpreterModule = bitcore.ScriptInterpreter;
|
2014-03-09 09:07:11 -07:00
|
|
|
var Script = bitcore.Script;
|
2014-02-18 08:17:23 -08:00
|
|
|
var ScriptInterpreter;
|
|
|
|
|
|
|
|
describe('ScriptInterpreter', function() {
|
|
|
|
it('should initialze the main object', function() {
|
|
|
|
should.exist(ScriptInterpreterModule);
|
|
|
|
});
|
|
|
|
it('should be able to create class', function() {
|
2014-03-05 11:11:16 -08:00
|
|
|
ScriptInterpreter = ScriptInterpreterModule;
|
2014-02-18 08:17:23 -08:00
|
|
|
should.exist(ScriptInterpreter);
|
|
|
|
});
|
|
|
|
it('should be able to create instance', function() {
|
|
|
|
var si = new ScriptInterpreter();
|
|
|
|
should.exist(si);
|
|
|
|
});
|
2014-03-18 14:46:19 -07:00
|
|
|
var testScripts = function(data, valid) {
|
|
|
|
var i = 0;
|
|
|
|
data.forEach(function(datum) {
|
|
|
|
if (datum.length < 2) throw new Error('Invalid test data');
|
|
|
|
var scriptSig = datum[0]; // script inputs
|
|
|
|
var scriptPubKey = datum[1]; // output script
|
|
|
|
var human = scriptSig + ' ' + scriptPubKey;
|
|
|
|
it('should ' + (!valid ? 'not ' : '') + 'validate script ' + human, function(done) {
|
|
|
|
console.log((!valid ? 'invalid ' : 'valid ') + human + ';' + (i++) + ' - ' + datum[2]);
|
|
|
|
try {
|
|
|
|
ScriptInterpreter.verify(
|
|
|
|
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);
|
2014-03-07 09:43:29 -08:00
|
|
|
done();
|
|
|
|
}
|
2014-03-18 14:46:19 -07:00
|
|
|
|
|
|
|
});
|
2014-03-07 09:43:29 -08:00
|
|
|
});
|
2014-03-18 14:46:19 -07:00
|
|
|
};
|
|
|
|
testScripts(testdata.dataScriptValid, true);
|
|
|
|
testScripts(testdata.dataScriptInvalid, false);
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-03-17 12:57:23 -07:00
|
|
|
testdata.dataSigCanonical.forEach(function(datum) {
|
|
|
|
it('should validate valid canonical signatures', function() {
|
2014-03-18 14:46:19 -07:00
|
|
|
ScriptInterpreter.isCanonicalSignature(new Buffer(datum, 'hex')).should.equal(true);
|
2014-03-17 12:57:23 -07:00
|
|
|
});
|
|
|
|
});
|
2014-03-18 14:46:19 -07:00
|
|
|
testdata.dataSigNonCanonical.forEach(function(datum) {
|
2014-03-17 12:57:23 -07:00
|
|
|
it('should NOT validate invalid canonical signatures', function() {
|
|
|
|
var sig;
|
|
|
|
var isHex;
|
|
|
|
//is Hex?
|
|
|
|
try {
|
2014-03-18 14:46:19 -07:00
|
|
|
sig = new Buffer(datum, 'hex');
|
|
|
|
isHex = 1;
|
|
|
|
} catch (e) {}
|
2014-03-17 12:57:23 -07:00
|
|
|
|
|
|
|
if (isHex)
|
2014-03-18 14:46:19 -07:00
|
|
|
ScriptInterpreter.isCanonicalSignature.bind(sig).should.
|
|
|
|
throw ();
|
2014-03-17 12:57:23 -07:00
|
|
|
});
|
|
|
|
});
|
2014-03-18 14:46:19 -07:00
|
|
|
|
2014-03-17 12:57:23 -07:00
|
|
|
|
2014-02-18 08:17:23 -08:00
|
|
|
});
|