From c0e5393595849d096136bd3f4774a34e2ce74e3a Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 16 Jun 2014 14:08:43 +1000 Subject: [PATCH] Transaction: remove .outpoint object --- src/transaction.js | 21 ++++++++------------- src/wallet.js | 10 ++++------ test/bitcoin.core.js | 4 ++-- test/transaction.js | 8 ++++---- test/wallet.js | 20 +++++++++++++------- 5 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/transaction.js b/src/transaction.js index 7c85424..51cfd00 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -1,5 +1,3 @@ -// FIXME: To all ye that enter here, be weary of Buffers, Arrays and Hex interchanging between the outpoints - var assert = require('assert') var bufferutils = require('./bufferutils') var crypto = require('./crypto') @@ -53,10 +51,8 @@ Transaction.prototype.addInput = function(tx, index) { assert.equal(typeof index, 'number', 'Expected number index, got ' + index) return (this.ins.push({ - outpoint: { - hash: hash, - index: index - }, + hash: hash, + index: index, script: Script.EMPTY, sequence: DEFAULT_SEQUENCE }) - 1) @@ -129,8 +125,8 @@ Transaction.prototype.toBuffer = function () { writeVarInt(this.ins.length) this.ins.forEach(function(txin) { - writeSlice(txin.outpoint.hash) - writeUInt32(txin.outpoint.index) + writeSlice(txin.hash) + writeUInt32(txin.index) writeVarInt(txin.script.buffer.length) writeSlice(txin.script.buffer) writeUInt32(txin.sequence) @@ -211,7 +207,8 @@ Transaction.prototype.clone = function () { newTx.ins = this.ins.map(function(txin) { return { - outpoint: txin.outpoint, + hash: txin.hash, + index: txin.index, script: txin.script, sequence: txin.sequence } @@ -261,10 +258,8 @@ Transaction.fromBuffer = function(buffer) { var sequence = readUInt32() tx.ins.push({ - outpoint: { - hash: hash, - index: vout - }, + hash: hash, + index: vout, script: Script.fromBuffer(script), sequence: sequence }) diff --git a/src/wallet.js b/src/wallet.js index 77756f3..1501456 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -170,14 +170,12 @@ function Wallet(seed, network) { }) tx.ins.forEach(function(txIn) { - var op = txIn.outpoint - // copy and convert to big-endian hex - var txinHash = new Buffer(op.hash) - Array.prototype.reverse.call(txinHash) - txinHash = txinHash.toString('hex') + var txinId = new Buffer(txIn.hash) + Array.prototype.reverse.call(txinId) + txinId = txinId.toString('hex') - var output = txinHash + ':' + op.index + var output = txinId + ':' + txIn.index if(me.outputs[output]) delete me.outputs[output] }) diff --git a/test/bitcoin.core.js b/test/bitcoin.core.js index 48f594c..af52885 100644 --- a/test/bitcoin.core.js +++ b/test/bitcoin.core.js @@ -147,7 +147,7 @@ describe('Bitcoin-core', function() { var prevOutIndex = input[1] // var prevOutScriptPubKey = input[2] // TODO: we don't have a ASM parser - var actualHash = txin.outpoint.hash + var actualHash = txin.hash // Test data is big-endian Array.prototype.reverse.call(actualHash) @@ -155,7 +155,7 @@ describe('Bitcoin-core', function() { assert.equal(actualHash.toString('hex'), prevOutHash) // we read UInt32, not Int32 - assert.equal(txin.outpoint.index & 0xffffffff, prevOutIndex) + assert.equal(txin.index & 0xffffffff, prevOutIndex) }) }) }) diff --git a/test/transaction.js b/test/transaction.js index 0233713..38f031f 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -51,8 +51,8 @@ describe('Transaction', function() { var input = tx.ins[0] assert.equal(input.sequence, 4294967295) - assert.equal(input.outpoint.index, 0) - assert.equal(input.outpoint.hash.toString('hex'), "344630cbff61fbc362f7e1ff2f11a344c29326e4ee96e787dc0d4e5cc02fd069") + assert.equal(input.index, 0) + assert.equal(input.hash.toString('hex'), "344630cbff61fbc362f7e1ff2f11a344c29326e4ee96e787dc0d4e5cc02fd069") assert.equal(input.script.toHex(), "493046022100ef89701f460e8660c80808a162bbf2d676f40a331a243592c36d6bd1f81d6bdf022100d29c072f1b18e59caba6e1f0b8cadeb373fd33a25feded746832ec179880c23901") }) @@ -118,8 +118,8 @@ describe('Transaction', function() { var input = tx.ins[0] assert.equal(input.sequence, 4294967295) - assert.equal(input.outpoint.index, 0) - assert.equal(input.outpoint.hash.toString('hex'), "576bc3c3285dbdccd8c3cbd8c03e10d7f77a5c839c744f34c3eb00511059b80c") + assert.equal(input.index, 0) + assert.equal(input.hash.toString('hex'), "576bc3c3285dbdccd8c3cbd8c03e10d7f77a5c839c744f34c3eb00511059b80c") assert.equal(input.script, Script.EMPTY) } diff --git a/test/wallet.js b/test/wallet.js index 7d9aa8c..ad94d03 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -328,11 +328,11 @@ describe('Wallet', function() { it("deletes corresponding 'output'", function(){ var txIn = spendTx.ins[0] - var txInId = new Buffer(txIn.outpoint.hash) + var txInId = new Buffer(txIn.hash) Array.prototype.reverse.call(txInId) txInId = txInId.toString('hex') - var expected = txInId + ':' + txIn.outpoint.index + var expected = txInId + ':' + txIn.index assert(expected in wallet.outputs) wallet.processConfirmedTx(spendTx) @@ -400,7 +400,8 @@ describe('Wallet', function() { var tx = wallet.createTx(to, value) assert.equal(tx.ins.length, 1) - assert.deepEqual(tx.ins[0].outpoint, { hash: fakeTxHash(3), index: 0 }) + assert.deepEqual(tx.ins[0].hash, fakeTxHash(3)) + assert.equal(tx.ins[0].index, 0) }) it('allows fee to be specified', function(){ @@ -408,8 +409,11 @@ describe('Wallet', function() { var tx = wallet.createTx(to, value, fee) assert.equal(tx.ins.length, 2) - assert.deepEqual(tx.ins[0].outpoint, { hash: fakeTxHash(3), index: 0 }) - assert.deepEqual(tx.ins[1].outpoint, { hash: fakeTxHash(2), index: 1 }) + + assert.deepEqual(tx.ins[0].hash, fakeTxHash(3)) + assert.equal(tx.ins[0].index, 0) + assert.deepEqual(tx.ins[1].hash, fakeTxHash(2)) + assert.equal(tx.ins[1].index, 1) }) it('allows fee to be set to zero', function(){ @@ -418,7 +422,8 @@ describe('Wallet', function() { var tx = wallet.createTx(to, value, fee) assert.equal(tx.ins.length, 1) - assert.deepEqual(tx.ins[0].outpoint, { hash: fakeTxHash(3), index: 0 }) + assert.deepEqual(tx.ins[0].hash, fakeTxHash(3)) + assert.equal(tx.ins[0].index, 0) }) it('ignores pending outputs', function(){ @@ -436,7 +441,8 @@ describe('Wallet', function() { var tx = wallet.createTx(to, value) assert.equal(tx.ins.length, 1) - assert.deepEqual(tx.ins[0].outpoint, { hash: fakeTxHash(3), index: 0 }) + assert.deepEqual(tx.ins[0].hash, fakeTxHash(3)) + assert.equal(tx.ins[0].index, 0) }) })