From ae394fad114901f05b54c6f80e851c6b9fadb192 Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Sat, 5 Dec 2015 20:53:06 -0800 Subject: [PATCH] improve tx.maxSize for p2sh and multisig. --- lib/bcoin/tx.js | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/lib/bcoin/tx.js b/lib/bcoin/tx.js index c49cf046..881eac3b 100644 --- a/lib/bcoin/tx.js +++ b/lib/bcoin/tx.js @@ -355,7 +355,10 @@ TX.prototype.isCoinbase = function isCoinbase() { return this.inputs.length === 1 && +this.inputs[0].out.hash === 0; }; -TX.prototype.maxSize = function maxSize() { +TX.prototype.maxSize = function maxSize(m, n) { + m = m || 1; + n = n || 1; + // Create copy with 0-script inputs var copy = this.clone(); copy.inputs.forEach(function(input) { @@ -367,7 +370,8 @@ TX.prototype.maxSize = function maxSize() { // 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)) { + + if (bcoin.script.isPubkeyhash(s) || bcoin.script.isSimplePubkeyhash(s)) { // Signature + len size += 74; // Pub key + len @@ -375,11 +379,39 @@ TX.prototype.maxSize = function maxSize() { return; } - // Multisig - // Empty byte - size += 1; - // Signature + len - size += 74; + if (bcoin.script.isMultisig(s)) { + // Multisig + // Empty byte + size += 1; + // Signature + len + size += 74 * m; + return; + } + + // 1 empty byte + // 1 mcode byte + // loop: + // 1 byte key length + // 65? byte key + // 1 ncode byte + // 1 checkmultisig byte + if (bcoin.script.isScripthash(s)) { + // Multisig + // Empty byte + size += 1; + // Signature + len + size += 74 * m; + // Redeem script + // m byte + size += 1; + // 1 byte length + 65 byte pubkey + size += 66 * n; + // n byte + size += 1; + // checkmultisig byte + size += 1; + return; + } }); return size;