Skip to content
This repository has been archived by the owner on Mar 7, 2023. It is now read-only.

Commit

Permalink
feat(Transaction): add function to estimate the size of a transaction…
Browse files Browse the repository at this point in the history
… given an amount
  • Loading branch information
jtormey committed Mar 4, 2016
1 parent 733bd2a commit a9e773c
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ var Transaction = function (unspentOutputs, toAddresses, amounts, fee, feePerKb,
this.addressesOfNeededPrivateKeys = [];
this.pathsOfNeededPrivateKeys = [];
this.fee = 0; // final used fee
this.unspentOutputs = unspentOutputs;
this.toAddresses = toAddresses;
var BITCOIN_DUST = 5460;
var forcedFee = Helpers.isNumber(fee) ? fee : null;
feePerKb = Helpers.isNumber(feePerKb) ? feePerKb : 10000;
this.feePerKb = Helpers.isNumber(feePerKb) ? feePerKb : 10000;

assert(toAddresses.length == amounts.length, 'The number of destiny addresses and destiny amounts should be the same.');
assert(this.amount > BITCOIN_DUST, this.amount + ' must be above dust threshold (' + BITCOIN_DUST + ' Satoshis)');
Expand All @@ -47,7 +49,7 @@ var Transaction = function (unspentOutputs, toAddresses, amounts, fee, feePerKb,
transaction.addInput(output.hash, output.index);
nIns += 1;
this.sizeEstimate = Helpers.guessSize(nIns, nOuts);
this.fee = Helpers.isPositiveNumber(forcedFee) ? forcedFee : Helpers.guessFee(nIns, nOuts, feePerKb);
this.fee = Helpers.isPositiveNumber(forcedFee) ? forcedFee : Helpers.guessFee(nIns, nOuts, this.feePerKb);

// Generate address from output script and add to private list so we can check if the private keys match the inputs later

Expand Down Expand Up @@ -175,6 +177,20 @@ Transaction.prototype.sign = function () {
return transaction;
};

Transaction.prototype.estimateSizeForAmount = function (amount) {
var sizeEstimate, accum = 0, nIns = 0;
var nOuts = this.toAddresses.length + 1;
var unspent = sortUnspentOutputs(this.unspentOutputs);

for (var i = 0; i < unspent.length; i++) {
nIns += 1;
sizeEstimate = Helpers.guessSize(nIns, nOuts);
accum += unspent[i].value;
if (accum >= amount) break;
}

return sizeEstimate;
};

function sortUnspentOutputs (unspentOutputs) {
var unspent = [];
Expand Down

0 comments on commit a9e773c

Please sign in to comment.