From 33955a7fb5a2bacb2cfe2237a15bab3f8db6284f Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Sun, 17 Aug 2014 16:42:00 +1000 Subject: [PATCH] Wallet: store txHash, vout separately instead of "from: txid:vout" --- src/wallet.js | 28 ++++++++++++++++++---------- test/wallet.js | 3 ++- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/wallet.js b/src/wallet.js index 8177a35..26f8bfd 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -64,8 +64,7 @@ Wallet.prototype.createTx = function(to, value, fixedFee, changeAddress) { var utxo = utxos[i] addresses.push(utxo.address) - var outpoint = utxo.from.split(':') - txb.addInput(outpoint[0], parseInt(outpoint[1])) + txb.addInput(utxo.hash, utxo.index) var fee = fixedFee === undefined ? estimatePaddedFee(txb.buildIncomplete(), this.network) : fixedFee @@ -97,6 +96,7 @@ Wallet.prototype.processConfirmedTx = function(tx){ Wallet.prototype.__processTx = function(tx, isPending) { var txId = tx.getId() + var txHash = tx.getHash() tx.outs.forEach(function(txOut, i) { var address @@ -112,7 +112,8 @@ Wallet.prototype.__processTx = function(tx, isPending) { var output = txId + ':' + i this.outputs[output] = { - from: output, + hash: txHash, + index: i, value: txOut.value, address: address, pending: isPending @@ -233,12 +234,15 @@ Wallet.prototype.signWith = function(txb, addresses) { return txb } -function outputToUnspentOutput(output){ - var hashAndIndex = output.from.split(":") +function outputToUnspentOutput(output) { + var txid = new Buffer(output.hash) + + // hash is little-endian, we want big-endian + Array.prototype.reverse.call(txid) return { - hash: hashAndIndex[0], - index: parseInt(hashAndIndex[1]), + hash: txid.toString('hex'), + index: output.index, address: output.address, value: output.value, pending: output.pending @@ -271,11 +275,15 @@ function processUnspentOutputs(utxos) { var key = utxo.hash + ':' + utxo.index + // little-endian hash is what we use internally + Array.prototype.reverse(hash) + outputs[key] = { - from: key, address: address, - value: value, - pending: utxo.pending + hash: hash, + index: utxo.index, + pending: utxo.pending, + value: value } }) diff --git a/test/wallet.js b/test/wallet.js index 9917c83..1333926 100644 --- a/test/wallet.js +++ b/test/wallet.js @@ -422,9 +422,10 @@ describe('Wallet', function() { function verifyOutputAdded(index, pending) { var txOut = tx.outs[index] + var key = tx.getId() + ":" + index var output = wallet.outputs[key] - assert.equal(output.from, key) + assert.deepEqual(output.hash, tx.getHash()) assert.equal(output.value, txOut.value) assert.equal(output.pending, pending)