From 45e55a07b006cc49a565a591d8cda9a7dd75065c Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Sun, 9 Mar 2014 13:46:20 +0800 Subject: [PATCH] fix transaction serialization/deserialization [closes #58] --- src/transaction.js | 19 +++++++++++-------- test/transaction.js | 26 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 test/transaction.js diff --git a/src/transaction.js b/src/transaction.js index 6f7f15b..12b14b2 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -16,7 +16,7 @@ var Transaction = function (doc) { this.outs = []; this.timestamp = null; this.block = null; - + if (doc) { if (typeof doc == "string" || Array.isArray(doc)) { doc = Transaction.deserialize(doc) @@ -68,11 +68,11 @@ Transaction.objectify = function (txs) { Transaction.prototype.addInput = function (tx, outIndex) { if (arguments[0] instanceof TransactionIn) { this.ins.push(arguments[0]); - } + } else if (arguments[0].length > 65) { var args = arguments[0].split(':'); return this.addInput(args[0], args[1]); - } + } else { this.ins.push(new TransactionIn({ outpoint: { @@ -104,7 +104,7 @@ Transaction.prototype.addOutput = function (address, value) { var args = arguments[0].split(':'); address = args[0]; value = parseInt(args[1]); - } + } this.outs.push(new TransactionOut({ value: value, script: Script.createOutputScript(address) @@ -248,9 +248,9 @@ Transaction.prototype.clone = function () * Returns an object with properties 'impact', 'type' and 'addr'. * * 'impact' is an object, see Transaction#calcImpact. - * + * * 'type' can be one of the following: - * + * * recv: * This is an incoming transaction, the wallet received money. * 'addr' contains the first address in the wallet that receives money @@ -458,6 +458,7 @@ Transaction.deserialize = function(buffer) { }); } obj.locktime = readAsInt(4); + return new Transaction(obj); } @@ -535,10 +536,12 @@ var TransactionIn = function (data) { else if (data.outpoint) this.outpoint = data.outpoint else - this.outpoint = { hash: data.hash, index: data.index } + this.outpoint = { hash: data.hash, index: data.index } if (data.scriptSig) this.script = Script.fromScriptSig(data.scriptSig) + else if (data.script) + this.script = data.script else this.script = new Script(data.script) @@ -567,7 +570,7 @@ var TransactionOut = function (data) { if (this.script.buffer.length > 0) this.address = this.script.toAddress(); - this.value = + this.value = Array.isArray(data.value) ? util.bytesToNum(data.value) : "string" == typeof data.value ? parseInt(data.value) : data.value instanceof BigInteger ? parseInt(data.value.toString()) diff --git a/test/transaction.js b/test/transaction.js new file mode 100644 index 0000000..53e9fd5 --- /dev/null +++ b/test/transaction.js @@ -0,0 +1,26 @@ +var Transaction = require('../src/transaction.js').Transaction +var convert = require('../src/convert.js') +var assert = require('assert') + +describe('Transaction', function() { + describe('deserialize', function() { + var tx, serializedTx + beforeEach(function() { + serializedTx = [ + '0100000001344630cbff61fbc362f7e1ff2f11a344c29326e4ee96e78', + '7dc0d4e5cc02fd069000000004a493046022100ef89701f460e8660c8', + '0808a162bbf2d676f40a331a243592c36d6bd1f81d6bdf022100d29c0', + '72f1b18e59caba6e1f0b8cadeb373fd33a25feded746832ec179880c2', + '3901ffffffff0100f2052a010000001976a914dd40dedd8f7e3746662', + '4c4dacc6362d8e7be23dd88ac00000000' + ].join('') + }) + + it('works', function() { + var actual = Transaction.deserialize(serializedTx).serialize() + var expected = convert.hexToBytes(serializedTx) + assert.deepEqual(actual, expected) + }) + }) + +})