From a6b9dd9473ca04c11f15804fd1eefa39f36c0566 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 19 May 2014 09:31:16 +1000 Subject: [PATCH] Transaction: remove hash:index notation --- src/transaction.js | 31 +++++++------------------------ src/wallet.js | 5 +++-- test/transaction.js | 18 ++++-------------- 3 files changed, 14 insertions(+), 40 deletions(-) diff --git a/src/transaction.js b/src/transaction.js index a4d18a8..c75b69b 100644 --- a/src/transaction.js +++ b/src/transaction.js @@ -29,15 +29,15 @@ function Transaction(doc) { if (doc.version) this.version = doc.version; if (doc.locktime) this.locktime = doc.locktime; if (doc.ins && doc.ins.length) { - doc.ins.forEach(function(input) { - this.addInput(new TransactionIn(input)) - }, this) + this.ins = doc.ins.map(function(input) { + return new TransactionIn(input) + }) } if (doc.outs && doc.outs.length) { - doc.outs.forEach(function(output) { - this.addOutput(new TransactionOut(output)) - }, this) + this.outs = doc.outs.map(function(output) { + return new TransactionOut(output) + }) } this.hash = this.hash || this.getHash() @@ -52,7 +52,6 @@ function Transaction(doc) { * - An existing TransactionIn object * - A transaction and an index * - A transaction hash and an index - * - A single string argument of the form txhash:index * * Note that this method does not sign the created input. */ @@ -62,15 +61,7 @@ Transaction.prototype.addInput = function (tx, outIndex) { return } - var hash - if (arguments[0].length > 65) { - var args = arguments[0].split(':') - hash = args[0] - outIndex = parseInt(args[1]) - - } else { - hash = typeof tx === "string" ? tx : tx.hash - } + var hash = typeof tx === "string" ? tx : tx.hash this.ins.push(new TransactionIn({ outpoint: { @@ -88,9 +79,7 @@ Transaction.prototype.addInput = function (tx, outIndex) { * * i) An existing TransactionOut object * ii) An address object or a string address, and a value - * iii) An address:value string * - * FIXME: This is a bit convoluted */ Transaction.prototype.addOutput = function (address, value) { if (arguments[0] instanceof TransactionOut) { @@ -99,12 +88,6 @@ Transaction.prototype.addOutput = function (address, value) { } if (typeof address === 'string') { - if (arguments[0].indexOf(':') >= 0) { - var args = arguments[0].split(':') - address = args[0] - value = parseInt(args[1]) - } - address = Address.fromBase58Check(address) } diff --git a/src/wallet.js b/src/wallet.js index b4acd3f..657f722 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -190,11 +190,12 @@ function Wallet(seed, network) { for (var i = 0; i < utxos.length; ++i) { var utxo = utxos[i] - tx.addInput(utxo.receive) - accum += utxo.value + var outpoint = utxo.receive.split(':') + tx.addInput(outpoint[0], parseInt(outpoint[1])) var fee = fixedFee == undefined ? estimateFeePadChangeOutput(tx) : fixedFee + accum += utxo.value subTotal = value + fee if (accum >= subTotal) { var change = accum - subTotal diff --git a/test/transaction.js b/test/transaction.js index 8341fec..e76d39c 100644 --- a/test/transaction.js +++ b/test/transaction.js @@ -127,11 +127,6 @@ describe('Transaction', function() { verifyTransactionIn() }) - it('allows a string in the form of txhash:index to be passed in', function() { - tx.addInput("0cb859105100ebc3344f749c835c7af7d7103ec0d8cbc3d8ccbd5d28c3c36b57:0") - verifyTransactionIn() - }) - function verifyTransactionIn() { assert.equal(tx.ins.length, 1) @@ -156,14 +151,9 @@ describe('Transaction', function() { verifyTransactionOut() }) - it('allows a string in the form of address:index to be passed in', function() { - tx.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3:40000") - verifyTransactionOut() - }) - it('allows a TransactionOut object to be passed in', function() { var txCopy = tx.clone() - txCopy.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3:40000") + txCopy.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3", 40000) var transactionOut = txCopy.outs[0] tx.addOutput(transactionOut) @@ -190,9 +180,9 @@ describe('Transaction', function() { describe('sign', function() { it('works', function() { - tx.addInput("0cb859105100ebc3344f749c835c7af7d7103ec0d8cbc3d8ccbd5d28c3c36b57:0") - tx.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3:40000") - tx.addOutput("1Bu3bhwRmevHLAy1JrRB6AfcxfgDG2vXRd:50000") + tx.addInput("0cb859105100ebc3344f749c835c7af7d7103ec0d8cbc3d8ccbd5d28c3c36b57", 0) + tx.addOutput("15mMHKL96tWAUtqF3tbVf99Z8arcmnJrr3", 40000) + tx.addOutput("1Bu3bhwRmevHLAy1JrRB6AfcxfgDG2vXRd", 50000) var key = ECKey.fromWIF('L44f7zxJ5Zw4EK9HZtyAnzCYz2vcZ5wiJf9AuwhJakiV4xVkxBeb') tx.sign(0, key)