bitcore/test/test.Transaction.js

125 lines
3.5 KiB
JavaScript
Raw Normal View History

2014-02-06 07:57:47 -08:00
'use strict';
var chai = chai || require('chai');
chai.Assertion.includeStack = true;
var bitcore = bitcore || require('../bitcore');
2014-02-06 07:57:47 -08:00
var should = chai.should();
var Transaction = bitcore.Transaction;
2014-02-27 13:09:02 -08:00
var In;
var Out;
var Script = bitcore.Script;
2014-03-14 23:27:06 -07:00
var util = bitcore.util;
2014-02-27 13:09:02 -08:00
var buffertools = require('buffertools');
2014-03-10 09:44:22 -07:00
var testdata = testdata || require('./testdata');
2014-02-06 07:57:47 -08:00
// Read tests from test/data/tx_valid.json and tx_invalid.json
// Format is an array of arrays
// Inner arrays are either [ "comment" ]
// or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"], serializedTransaction, enforceP2SH
// ... where all scripts are stringified scripts.
// Returns an object with the Transaction object, and an array of input objects
function parse_test_transaction(entry) {
// Ignore comments
if (entry.length !== 3) return;
var inputs = {};
entry[0].forEach(function(vin) {
var hash = (vin[0]);
var index = vin[1];
var scriptPubKey = Script.fromHumanReadable(vin[2]);
var mapKey = [hash, index];
inputs[mapKey] = scriptPubKey;
2014-03-20 07:20:51 -07:00
});
var raw = new Buffer(entry[1], 'hex');
var tx = new Transaction();
tx.parse(raw);
// Sanity check transaction has been parsed correctly
buffertools.toHex(tx.serialize()).should.equal(buffertools.toHex(raw));
return {
'transaction': tx,
'inputs': inputs
};
}
2014-02-18 06:39:47 -08:00
describe('Transaction', function() {
2014-04-04 14:24:34 -07:00
it('should initialze the main object', function() {
2014-02-06 07:57:47 -08:00
should.exist(Transaction);
2014-02-27 13:09:02 -08:00
In = Transaction.In;
Out = Transaction.Out;
should.exist(In);
should.exist(Out);
2014-02-06 07:57:47 -08:00
});
2014-03-14 13:38:42 -07:00
2014-04-04 14:24:34 -07:00
it('should be able to create instance', function() {
2014-02-06 07:57:47 -08:00
var t = new Transaction();
should.exist(t);
});
/*
* Bitcoin core transaction tests
*/
// Verify that known valid transactions are intepretted correctly
var coreTest = function(data, valid) {
2014-04-04 13:33:36 -07:00
buffertools.extend();
data.forEach(function(datum) {
if (datum.length < 3) return;
var raw = datum[1];
var verifyP2SH = datum[2];
2014-04-04 13:33:36 -07:00
var testTx = parse_test_transaction(datum);
var tx = testTx.transaction;
2014-03-26 14:04:29 -07:00
describe((valid ? '' : 'in') + 'valid tx=' + raw, function() {
2014-04-04 14:24:34 -07:00
it('should parse correctly', function() {
buffertools.toHex(tx.serialize()).toLowerCase().should.equal(raw.toLowerCase());
2014-03-26 14:04:29 -07:00
});
2014-04-04 13:33:36 -07:00
var inputs = tx.inputs();
2014-03-26 14:04:29 -07:00
var j = 0;
inputs.forEach(function(input) {
var i = j;
j += 1;
it('should validate input #' + i, function(done) {
2014-04-04 13:33:36 -07:00
var outpointHash = new Buffer(input[0].length);
input[0].copy(outpointHash);
input[0] = buffertools.reverse(outpointHash);
2014-03-26 14:04:29 -07:00
input[0] = buffertools.toHex(input[0]);
var mapKey = [input];
var scriptPubKey = testTx.inputs[mapKey];
if (!scriptPubKey) throw new Error('Bad test: ' + datum);
2014-04-04 13:33:36 -07:00
tx.verifyInput(
2014-03-26 14:04:29 -07:00
i,
scriptPubKey, {
verifyP2SH: verifyP2SH,
dontVerifyStrictEnc: true
},
function(err, results) {
2014-04-04 14:24:34 -07:00
if (valid) {
should.not.exist(err);
should.exist(results);
results.should.equal(valid);
} else {
var invalid = (typeof err !== 'undefined') || results === false;
invalid.should.equal(true);
}
2014-03-26 14:04:29 -07:00
done();
}
);
});
});
});
});
};
coreTest(testdata.dataTxValid, true);
coreTest(testdata.dataTxInvalid, false);
2014-02-27 13:09:02 -08:00
});