diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index a28faa68..586e1fdf 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -70,7 +70,7 @@ TX.prototype.input = function input(i, index) { var input = { out: { - tx: i.tx || null, + tx: (i.out ? i.out.tx : i.tx) || null, hash: hash, index: i.out ? i.out.index : i.index, }, @@ -180,6 +180,36 @@ TX.prototype.verify = function verify() { }, this); }; +TX.prototype.maxSize = function maxSize() { + // Create copy with 0-script inputs + var copy = this.clone(); + copy.inputs.forEach(function(input) { + input.script = []; + }); + + var size = copy.render().length; + + // Add size for signatures and public keys + copy.inputs.forEach(function(input) { + var s = input.out.tx.outputs[input.out.index].script; + if (bcoin.script.isPubkeyhash(s)) { + // Signature + len + size += 74; + // Pub key + len + size += 34; + return; + } + + // Multisig + // Empty byte + size += 1; + // Signature + len + size += 74; + }); + + return size; +}; + TX.prototype.toJSON = function toJSON() { // Compact representation var ts = new Array(4); diff --git a/test/wallet-test.js b/test/wallet-test.js index eafea0be..170bcf11 100644 --- a/test/wallet-test.js +++ b/test/wallet-test.js @@ -67,7 +67,9 @@ describe('Wallet', function() { .input(src, 0) .out(w.getAddress(), 5460); + var maxSize = tx.maxSize(); w.sign(tx); + assert(tx.render().length <= maxSize); assert(tx.verify()); });