From bdc7131d0eb06b246e0df24acb7e9a64a0c88369 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Tue, 20 May 2014 14:07:22 +1000 Subject: [PATCH] Transaction: renames getHash to getId In turn also removes the inherent calculation of tx.hash after deserialization. --- src/transaction.js | 17 +++++++++++------ src/wallet.js | 6 +++--- test/transaction.js | 15 ++++++++++----- test/wallet.js | 3 +-- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/transaction.js b/src/transaction.js index a30308b..4479577 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -25,7 +25,6 @@ function Transaction(doc) { this.outs = [] if (doc) { - if (doc.hash) this.hash = doc.hash; if (doc.version) this.version = doc.version; if (doc.locktime) this.locktime = doc.locktime; if (doc.ins && doc.ins.length) { @@ -39,8 +38,6 @@ function Transaction(doc) { return new TransactionOut(output) }) } - - this.hash = this.hash || this.getHash() } } @@ -54,8 +51,16 @@ function Transaction(doc) { * * Note that this method does not sign the created input. */ -Transaction.prototype.addInput = function (tx, outIndex) { - var hash = typeof tx === "string" ? tx : tx.hash +Transaction.prototype.addInput = function(tx, outIndex) { + var hash + + if (typeof tx === 'string') { + hash = tx + + } else { + assert(tx instanceof Transaction, 'Unexpected input: ' + tx) + hash = tx.getId() + } this.ins.push(new TransactionIn({ outpoint: { @@ -209,7 +214,7 @@ Transaction.prototype.hashForSignature = function(prevOutScript, inIndex, hashTy return crypto.hash256(buffer) } -Transaction.prototype.getHash = function () { +Transaction.prototype.getId = function () { var buffer = crypto.hash256(this.toBuffer()) // Big-endian is used for TxHash diff --git a/src/wallet.js b/src/wallet.js index 657f722..87293bb 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -146,9 +146,9 @@ function Wallet(seed, network) { } function processTx(tx, isPending) { - var txhash = tx.getHash() + var txid = tx.getId() - tx.outs.forEach(function(txOut, i){ + tx.outs.forEach(function(txOut, i) { var address try { @@ -158,7 +158,7 @@ function Wallet(seed, network) { } if (isMyAddress(address)) { - var output = txhash + ':' + i + var output = txid + ':' + i me.outputs[output] = { receive: output, diff --git a/test/transaction.js b/test/transaction.js index 54d1e7c..78a5ca4 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -74,11 +74,6 @@ describe('Transaction', function() { assert.deepEqual(output.script, Address.fromBase58Check('n1gqLjZbRH1biT5o4qiVMiNig8wcCPQeB9').toOutputScript()) }) - it('assigns hash to deserialized object', function() { - var hashHex = "a9d4599e15b53f3eb531608ddb31f48c695c3d0b3538a6bda871e8b34f2f430c" - assert.equal(tx.hash, hashHex) - }) - it('decodes large inputs correctly', function() { // transaction has only 1 input var tx = new Transaction() @@ -251,5 +246,15 @@ describe('Transaction', function() { assert.equal(tx.toHex(), expected) }) }) + + describe('getId', function() { + it('returns the expected txid', function() { + var tx = new Transaction() + tx.addInput('d6f72aab8ff86ff6289842a0424319bf2ddba85dc7c52757912297f948286389', 0) + tx.addOutput('mrCDrCybB6J1vRfbwM5hemdJz73FwDBC8r', 1) + + assert.equal(tx.getId(), '7c3275f1212fd1a2add614f47a1f1f7b6d9570a97cb88e0e2664ab1752976e9f') + }) + }) }) diff --git a/test/wallet.js b/test/wallet.js index 88b7dcd..cc8b760 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -303,7 +303,6 @@ describe('Wallet', function() { function outputCount(){ return Object.keys(wallet.outputs).length } - }) describe("when tx ins outpoint contains a known txhash:i", function(){ @@ -340,7 +339,7 @@ describe('Wallet', function() { function verifyOutputAdded(index, pending) { var txOut = tx.outs[index] - var key = tx.getHash() + ":" + index + var key = tx.getId() + ":" + index var output = wallet.outputs[key] assert.equal(output.receive, key) assert.equal(output.value, txOut.value)