Wallet: store txHash, vout separately instead of "from: txid:vout"

This commit is contained in:
Daniel Cousens 2014-08-17 16:42:00 +10:00
parent 735feab7ba
commit 33955a7fb5
2 changed files with 20 additions and 11 deletions

View file

@ -64,8 +64,7 @@ Wallet.prototype.createTx = function(to, value, fixedFee, changeAddress) {
var utxo = utxos[i] var utxo = utxos[i]
addresses.push(utxo.address) addresses.push(utxo.address)
var outpoint = utxo.from.split(':') txb.addInput(utxo.hash, utxo.index)
txb.addInput(outpoint[0], parseInt(outpoint[1]))
var fee = fixedFee === undefined ? estimatePaddedFee(txb.buildIncomplete(), this.network) : fixedFee 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) { Wallet.prototype.__processTx = function(tx, isPending) {
var txId = tx.getId() var txId = tx.getId()
var txHash = tx.getHash()
tx.outs.forEach(function(txOut, i) { tx.outs.forEach(function(txOut, i) {
var address var address
@ -112,7 +112,8 @@ Wallet.prototype.__processTx = function(tx, isPending) {
var output = txId + ':' + i var output = txId + ':' + i
this.outputs[output] = { this.outputs[output] = {
from: output, hash: txHash,
index: i,
value: txOut.value, value: txOut.value,
address: address, address: address,
pending: isPending pending: isPending
@ -233,12 +234,15 @@ Wallet.prototype.signWith = function(txb, addresses) {
return txb return txb
} }
function outputToUnspentOutput(output){ function outputToUnspentOutput(output) {
var hashAndIndex = output.from.split(":") var txid = new Buffer(output.hash)
// hash is little-endian, we want big-endian
Array.prototype.reverse.call(txid)
return { return {
hash: hashAndIndex[0], hash: txid.toString('hex'),
index: parseInt(hashAndIndex[1]), index: output.index,
address: output.address, address: output.address,
value: output.value, value: output.value,
pending: output.pending pending: output.pending
@ -271,11 +275,15 @@ function processUnspentOutputs(utxos) {
var key = utxo.hash + ':' + utxo.index var key = utxo.hash + ':' + utxo.index
// little-endian hash is what we use internally
Array.prototype.reverse(hash)
outputs[key] = { outputs[key] = {
from: key,
address: address, address: address,
value: value, hash: hash,
pending: utxo.pending index: utxo.index,
pending: utxo.pending,
value: value
} }
}) })

View file

@ -422,9 +422,10 @@ describe('Wallet', function() {
function verifyOutputAdded(index, pending) { function verifyOutputAdded(index, pending) {
var txOut = tx.outs[index] var txOut = tx.outs[index]
var key = tx.getId() + ":" + index var key = tx.getId() + ":" + index
var output = wallet.outputs[key] 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.value, txOut.value)
assert.equal(output.pending, pending) assert.equal(output.pending, pending)