diff --git a/lib/bcoin/http/rpc.js b/lib/bcoin/http/rpc.js index aa51dd85..7055074e 100644 --- a/lib/bcoin/http/rpc.js +++ b/lib/bcoin/http/rpc.js @@ -9,7 +9,6 @@ var bcoin = require('../env'); var utils = require('../utils'); var IP = require('../ip'); -var assert = utils.assert; var constants = bcoin.protocol.constants; function RPC(node) { @@ -2907,7 +2906,7 @@ RPC.prototype.getreceivedbyaddress = function getreceivedbyaddress(args, callbac RPC.prototype._toWalletTX = function _toWalletTX(tx, callback) { var self = this; - var receive, member, json; + var i, det, receive, member, sent, received, json; this.walletdb.tx.toDetails(this.wallet.id, tx, function(err, details) { if (err) @@ -2916,11 +2915,56 @@ RPC.prototype._toWalletTX = function _toWalletTX(tx, callback) { if (!details) return callback(new RPCError('TX not found.')); - receive = details.isReceive(); - member = details.getMember(); + det = []; + sent = 0; + received = 0; + receive = true; + + for (i = 0; i < details.inputs.length; i++) { + member = details.inputs[i]; + if (member.path) { + receive = false; + break; + } + } + + for (i = 0; i < details.outputs.length; i++) { + member = details.outputs[i]; + + if (member.path) { + if (member.path.change === 1) + continue; + + det.push({ + account: member.path.name, + address: member.address.toBase58(self.network), + category: 'receive', + amount: +utils.btc(member.value), + label: member.path.name, + vout: i + }); + + received += member.value; + + continue; + } + + det.push({ + account: '', + address: member.address + ? member.address.toBase58(self.network) + : null, + category: 'send', + amount: -(+utils.btc(member.value)), + fee: -(+utils.btc(details.fee)), + vout: i + }); + + sent += member.value; + } json = { - amount: +utils.btc(details.getValue()), + amount: +utils.btc(receive ? received : -sent), confirmations: details.confirmations, blockhash: details.block ? utils.revHex(details.block) : null, blockindex: details.index, @@ -2930,14 +2974,7 @@ RPC.prototype._toWalletTX = function _toWalletTX(tx, callback) { time: details.ps, timereceived: details.ps, 'bip125-replaceable': 'no', - details: [{ - account: member.path.name, - address: member.address.toBase58(self.network), - category: receive ? 'receive' : 'send', - amount: +utils.btc(member.value), - label: member.path.name, - vout: 0 - }], + details: det, hex: details.tx.toRaw().toString('hex') }; @@ -3196,7 +3233,8 @@ RPC.prototype.listsinceblock = function listsinceblock(args, callback) { RPC.prototype._toListTX = function _toListTX(tx, callback) { var self = this; - var receive, member, json; + var i, receive, member, det, sent, received; + var sendMember, recMember, json; this.walletdb.tx.toDetails(this.wallet.id, tx, function(err, details) { if (err) @@ -3205,14 +3243,41 @@ RPC.prototype._toListTX = function _toListTX(tx, callback) { if (!details) return callback(new RPCError('TX not found.')); - receive = details.isReceive(); - member = details.getMember(); + det = []; + sent = 0; + received = 0; + receive = true; + + for (i = 0; i < details.inputs.length; i++) { + member = details.inputs[i]; + if (member.path) { + receive = false; + break; + } + } + + for (i = 0; i < details.outputs.length; i++) { + member = details.outputs[i]; + + if (member.path) { + if (member.path.change === 1) + continue; + received += member.value; + recMember = member; + continue; + } + + sent += member.value; + sendMember = member; + } + + member = receive ? recMember : sendMember; json = { account: member.path.name, address: member.address.toBase58(self.network), category: receive ? 'receive' : 'send', - amount: +utils.btc(details.getValue()), + amount: +utils.btc(receive ? received : -sent), label: member.path.name, vout: 0, confirmations: details.confirmations, diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index 323f0f3d..b2fc2770 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -837,7 +837,7 @@ TXDB.prototype.isSpent = function isSpent(hash, index, callback) { TXDB.prototype._confirm = function _confirm(tx, info, callback, force) { var self = this; - var hash, batch, unlock, i, path, id; + var hash, batch, unlock, i, id; unlock = this._lock(_confirm, [tx, info, callback], force); @@ -887,7 +887,8 @@ TXDB.prototype._confirm = function _confirm(tx, info, callback, force) { var key = hash + '/' + i; // Only update coins if this output is ours. - paths = info.getPaths(address); + if (!info.hasPaths(address)) + return next(); self.getCoin(hash, i, function(err, coin) { if (err) @@ -1139,7 +1140,7 @@ TXDB.prototype.unconfirm = function unconfirm(hash, callback, force) { TXDB.prototype._unconfirm = function unconfirm(tx, info, callback, force) { var self = this; - var batch, unlock, hash, height, i, path, id; + var batch, unlock, hash, height, i, id; unlock = this._lock(unconfirm, [tx, info, callback], force);