Browse Source

fix transaction serialization/deserialization

[closes #58]
hk-custom-address
Wei Lu 11 years ago
parent
commit
45e55a07b0
  1. 19
      src/transaction.js
  2. 26
      test/transaction.js

19
src/transaction.js

@ -16,7 +16,7 @@ var Transaction = function (doc) {
this.outs = []; this.outs = [];
this.timestamp = null; this.timestamp = null;
this.block = null; this.block = null;
if (doc) { if (doc) {
if (typeof doc == "string" || Array.isArray(doc)) { if (typeof doc == "string" || Array.isArray(doc)) {
doc = Transaction.deserialize(doc) doc = Transaction.deserialize(doc)
@ -68,11 +68,11 @@ Transaction.objectify = function (txs) {
Transaction.prototype.addInput = function (tx, outIndex) { Transaction.prototype.addInput = function (tx, outIndex) {
if (arguments[0] instanceof TransactionIn) { if (arguments[0] instanceof TransactionIn) {
this.ins.push(arguments[0]); this.ins.push(arguments[0]);
} }
else if (arguments[0].length > 65) { else if (arguments[0].length > 65) {
var args = arguments[0].split(':'); var args = arguments[0].split(':');
return this.addInput(args[0], args[1]); return this.addInput(args[0], args[1]);
} }
else { else {
this.ins.push(new TransactionIn({ this.ins.push(new TransactionIn({
outpoint: { outpoint: {
@ -104,7 +104,7 @@ Transaction.prototype.addOutput = function (address, value) {
var args = arguments[0].split(':'); var args = arguments[0].split(':');
address = args[0]; address = args[0];
value = parseInt(args[1]); value = parseInt(args[1]);
} }
this.outs.push(new TransactionOut({ this.outs.push(new TransactionOut({
value: value, value: value,
script: Script.createOutputScript(address) script: Script.createOutputScript(address)
@ -248,9 +248,9 @@ Transaction.prototype.clone = function ()
* Returns an object with properties 'impact', 'type' and 'addr'. * Returns an object with properties 'impact', 'type' and 'addr'.
* *
* 'impact' is an object, see Transaction#calcImpact. * 'impact' is an object, see Transaction#calcImpact.
* *
* 'type' can be one of the following: * 'type' can be one of the following:
* *
* recv: * recv:
* This is an incoming transaction, the wallet received money. * This is an incoming transaction, the wallet received money.
* 'addr' contains the first address in the wallet that receives money * 'addr' contains the first address in the wallet that receives money
@ -458,6 +458,7 @@ Transaction.deserialize = function(buffer) {
}); });
} }
obj.locktime = readAsInt(4); obj.locktime = readAsInt(4);
return new Transaction(obj); return new Transaction(obj);
} }
@ -535,10 +536,12 @@ var TransactionIn = function (data) {
else if (data.outpoint) else if (data.outpoint)
this.outpoint = data.outpoint this.outpoint = data.outpoint
else else
this.outpoint = { hash: data.hash, index: data.index } this.outpoint = { hash: data.hash, index: data.index }
if (data.scriptSig) if (data.scriptSig)
this.script = Script.fromScriptSig(data.scriptSig) this.script = Script.fromScriptSig(data.scriptSig)
else if (data.script)
this.script = data.script
else else
this.script = new Script(data.script) 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(); if (this.script.buffer.length > 0) this.address = this.script.toAddress();
this.value = this.value =
Array.isArray(data.value) ? util.bytesToNum(data.value) Array.isArray(data.value) ? util.bytesToNum(data.value)
: "string" == typeof data.value ? parseInt(data.value) : "string" == typeof data.value ? parseInt(data.value)
: data.value instanceof BigInteger ? parseInt(data.value.toString()) : data.value instanceof BigInteger ? parseInt(data.value.toString())

26
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)
})
})
})
Loading…
Cancel
Save