From 6755b84fbf585ef50561ce13a1504add1772141d Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Thu, 27 Feb 2014 18:09:02 -0300 Subject: [PATCH] Transaction new tests not working --- Transaction.js | 14 ++++++++++- test/test.Transaction.js | 50 ++++++++++++++++++++++++++++++++++++---- test/testdata.js | 4 ++++ 3 files changed, 62 insertions(+), 6 deletions(-) diff --git a/Transaction.js b/Transaction.js index d3620392f..16564e962 100644 --- a/Transaction.js +++ b/Transaction.js @@ -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; @@ -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() { @@ -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()); }); @@ -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); } diff --git a/test/test.Transaction.js b/test/test.Transaction.js index 98889dee9..570a0b03e 100644 --- a/test/test.Transaction.js +++ b/test/test.Transaction.js @@ -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() { @@ -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)); + + + + }); + } + }); }); - - - - - diff --git a/test/testdata.js b/test/testdata.js index 7eaaa0b8a..3d587fc2f 100644 --- a/test/testdata.js +++ b/test/testdata.js @@ -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;