diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index 32df2726..e616219d 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -785,21 +785,21 @@ TX.prototype.maxSize = function maxSize() { return total; }; -TX.prototype.getUnspent = function getUnspent(unspent, hardFee) { +TX.prototype.getUnspent = function getUnspent(unspent, fee) { var tx = this.clone(); var cost = tx.funds('out'); - var fee = 1; + var totalkb = 1; var total = cost.addn(constants.tx.fee); var inputs = []; var lastAdded = 0; - var size, addFee, change; + var size, newkb, change; - if (!hardFee) - hardFee = this._fee; + if (!fee) + fee = this._fee; - if (hardFee) { - total = cost.add(hardFee); - this._fee = hardFee; + if (fee) { + total = cost.add(fee); + this._fee = fee; } function addInput(unspent) { @@ -815,7 +815,7 @@ TX.prototype.getUnspent = function getUnspent(unspent, hardFee) { // Transfer `total` funds maximum. unspent.every(addInput); - if (!hardFee) { + if (!fee) { // Add dummy output (for `change`) to // calculate maximum TX size. tx.output({ @@ -829,9 +829,9 @@ TX.prototype.getUnspent = function getUnspent(unspent, hardFee) { // Calculate max possible size after signing. size = tx.maxSize(); - addFee = Math.ceil(size / 1024) - fee; - total.iaddn(addFee * constants.tx.fee); - fee += addFee; + newkb = Math.ceil(size / 1024) - totalkb; + total.iaddn(newkb * constants.tx.fee); + totalkb += newkb; // Failed to get enough funds, add more inputs. if (tx.funds('in').cmp(total) < 0) @@ -844,7 +844,7 @@ TX.prototype.getUnspent = function getUnspent(unspent, hardFee) { // Still failing to get enough funds. if (tx.funds('in').cmp(total) < 0) - return null; + return; // How much money is left after filling outputs. change = tx.funds('in').sub(total); @@ -862,8 +862,11 @@ TX.prototype.getUnspent = function getUnspent(unspent, hardFee) { TX.prototype.fillUnspent = function fillUnspent(unspent, changeAddress, fee) { var result = this.getUnspent(unspent, fee); + if (fee) + this._fee = fee; + if (!result) - return result; + return; this.changeAddress = changeAddress || this.changeAddress