Bitcoinjs-lib: How to calculate fees in segwit multi-sig

Created on 8 Jan 2018  路  16Comments  路  Source: bitcoinjs/bitcoinjs-lib

Hello

I'm doing a multi-sig transaction and i'm not sure how i should calculate the transaction fee.

I want 1 party to sign a tx and pass the transaction to another party to sign and then broadcast.

If i understand this correctly, i need to call getVirtualSize to calculate the TX Fee, However the witness field isn't completed yet. Because i'm waiting for the other party to sign the tx

how to / question / docs

Most helpful comment

https://gist.github.com/dabura667/1bb77d63d38bfd99a0ce453db74e0115~~

(Edit: Gist is gone, here's a copy https://gist.github.com/junderw/b43af3253ea5865ed52cb51c200ac19c)

// 2-of-3 multisig with 3 inputs sent to 2 P2PKH outputs.
getByteCount({'MULTISIG-P2SH-P2WSH:2-3': 3}, {'P2PKH': 2})
// 373 virtual Bytes (1/4 weight)

All 16 comments

https://gist.github.com/dabura667/1bb77d63d38bfd99a0ce453db74e0115~~

(Edit: Gist is gone, here's a copy https://gist.github.com/junderw/b43af3253ea5865ed52cb51c200ac19c)

// 2-of-3 multisig with 3 inputs sent to 2 P2PKH outputs.
getByteCount({'MULTISIG-P2SH-P2WSH:2-3': 3}, {'P2PKH': 2})
// 373 virtual Bytes (1/4 weight)

remove P2SH- if you want to measure for native segwit

This assumes compressed pubkeys in all cases.

It also assumes the S value is always 32 bytes length and the R value of each signature is always 33 bytes (with the 0x00 padding) so that we estimate the maximum possible size. This will usually create a 1 weight (4 weight for non-segwit) variance per signature contained.

Example:
https://www.smartbit.com.au/tx/c8e32240ed79baa07564b30216b9d148f41ef9141b96ff6ebb0e6dd08f49ac96

getByteCount({'P2PKH': 77}, {'P2PKH': 2})
// 11474

but if you look at the API response by clicking the </> button, it says 11433... if we search the page for "asm": "3045022100 (Which means the r value was 33 bytes for the input) we get 36 hits... 77 inputs - 36 == 41 inputs with a 32 byte r value...

11474 - 11433 == 41 exactly. So we overestimated by 41 bytes because there were 77 signatures and 53% of them were 32 byte r values. (s values are always 32 or less, because s values must be low-s due to recent standardness rules)

@dcousens I wonder if there's a way we can include this function as a prototype for Transaction or TransactionBuilder..... hmmm....

@dabura667 I've been working on a size estimation helper I was eventually going to push here https://github.com/blocktrail/blocktrail-sdk-nodejs/blob/master/lib/size_estimation.js It's a little heavier, but think it's pretty clear and avoids the magic numbers that many size estimators happen to have.

This assumes compressed pubkeys in all cases.

This is a curious limitation :P technically you need to know the private key and all other secret information to do size estimation. Hashlocks take whose size can be variable.. Interesting challenges in terms of exposing all these degrees of freedom in a nice way. (arguments? options? notice the compressed arg that gets passed from estimateUtxo to estimateP2PKHStackSize)

my colleague made this a while back https://github.com/you21979/node-bitcoin-util-fee/blob/master/index.js

Doesn't support segwit... but it's very... verbose.

... very verbose.

@afk11 does size_estimation.js allow estimation of P2SH-P2WSH ?

Yes it does.

@dabura667 you should incorporate https://gist.github.com/dabura667/1bb77d63d38bfd99a0ce453db74e0115~~ into https://github.com/bitcoinjs/coinselect (maybe? if relevant)

(Edit: new link https://gist.github.com/junderw/b43af3253ea5865ed52cb51c200ac19c)

Or at least, help us make https://github.com/bitcoinjs/coinselect better to avoid needing your gist :+1:

@junderw I don't believe it does,
In the estimateUtxo function, the array of allowed script contains only:

var allowedTypes = [types.MULTISIG, types.P2PKH, types.P2PK]

However correct me if i'm wrong :)

@dabura667 i'm going to use your gist, It's quiet handy and easy to understand

the gist is no longer available -- anyone know where one can access it or a similar resource ?

@rbndg it does support P2SH-P2WSH - the allowedTypes vars refer to the types of scripts which can be nested _inside_ a P2SH-P2WSH script

@tipu I have a copy on my gist: I'll replace the links.

https://gist.github.com/junderw/b43af3253ea5865ed52cb51c200ac19c

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dcousens picture dcousens  路  3Comments

LeonYanghaha picture LeonYanghaha  路  3Comments

Beardcoding picture Beardcoding  路  3Comments

ghost picture ghost  路  3Comments

tuyennvtb picture tuyennvtb  路  3Comments