Skip to content

Commit

Permalink
Transaction new tests not working
Browse files Browse the repository at this point in the history
  • Loading branch information
maraoz committed Mar 5, 2014
1 parent d77cc28 commit 6755b84
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
14 changes: 13 additions & 1 deletion Transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ function spec(b) {
}
if (data.o) {
this.o = data.o;
} else {
if (data.oTxHash && typeof data.oIndex !== 'undefined') {
var hash = new Buffer(data.oTxHash, 'hex');
hash = buffertools.reverse(hash);
var voutBuf = new Buffer(4);
voutBuf.writeUInt32LE(data.oIndex, 0);
this.o = Buffer.concat([hash, voutBuf]);
}
}
this.s = Buffer.isBuffer(data.s) ? data.s :
Buffer.isBuffer(data.script) ? data.script : util.EMPTY_BUFFER;
Expand All @@ -44,7 +52,8 @@ function spec(b) {
var qbuf = new Buffer(4);
qbuf.writeUInt32LE(this.q, 0);

return Buffer.concat([this.o, slen, this.s, qbuf]);
var ret = Buffer.concat([this.o, slen, this.s, qbuf]);
return ret;
};

TransactionIn.prototype.getOutpointHash = function getOutpointHash() {
Expand Down Expand Up @@ -144,6 +153,7 @@ function spec(b) {
bufs.push(buf);

bufs.push(util.varIntBuf(this.ins.length));
console.log(this.ins.length);
this.ins.forEach(function (txin) {
bufs.push(txin.serialize());
});
Expand Down Expand Up @@ -640,6 +650,8 @@ function spec(b) {

Transaction.prototype.parse = function (parser) {
if (Buffer.isBuffer(parser)) {
console.dir(parser);
this._buffer = parser;
parser = new Parser(parser);
}

Expand Down
42 changes: 41 additions & 1 deletion test/test.Transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ var should = chai.should();

var TransactionModule = bitcore.Transaction;
var Transaction;
var In;
var Out;
var Script = bitcore.Script.class();
var buffertools = require('buffertools');
var test_data = require('./testdata');

describe('Transaction', function() {
it('should initialze the main object', function() {
Expand All @@ -15,14 +20,49 @@ describe('Transaction', function() {
it('should be able to create class', function() {
Transaction = TransactionModule.class();
should.exist(Transaction);
In = Transaction.In;
Out = Transaction.Out;
should.exist(In);
should.exist(Out);
});
it('should be able to create instance', function() {
var t = new Transaction();
should.exist(t);
});
});

// Read tests from test/data/tx_valid.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.
test_data.dataTxValid.forEach(function(datum) {
if (datum.length === 3) {
it('valid tx=' + datum[1], function() {
var inputs = datum[0];
var mapprevOutScriptPubKeys = {};
var ins = [];
inputs.forEach(function(vin) {
var hash = vin[0];
var index = vin[1];
var scriptPubKey = vin[2];
var input = new In({
s: scriptPubKey,
q: 0xffffffff,
oTxHash: hash,
oIndex: index
});
//mapprevOutScriptPubKeys[input] = new Script(scriptPubKey);
ins.push(input);

});
var raw = new Buffer(datum[1]);
var tx = new Transaction();
tx.parse(raw);
buffertools.toHex(tx.serialize()).should.equal(buffertools.toHex(raw));



});
}
});
});
4 changes: 4 additions & 0 deletions test/testdata.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ var fs = require('fs');
var dataValid = JSON.parse(fs.readFileSync('test/data/base58_keys_valid.json'));
var dataInvalid = JSON.parse(fs.readFileSync('test/data/base58_keys_invalid.json'));
var dataEncodeDecode = JSON.parse(fs.readFileSync('test/data/base58_encode_decode.json'));
var dataTxValid = JSON.parse(fs.readFileSync('test/data/tx_valid.json'));
var dataTxInvalid = JSON.parse(fs.readFileSync('test/data/tx_invalid.json'));

module.exports.dataValid = dataValid;
module.exports.dataInvalid = dataInvalid;
module.exports.dataEncodeDecode = dataEncodeDecode;
module.exports.dataTxValid = dataTxValid;
module.exports.dataTxInvalid = dataTxInvalid;

0 comments on commit 6755b84

Please sign in to comment.