Calculating the witness size of a transaction I want to calculate how many % of a block, and thus, transactions that are witness data. I need to calculate the size of the witness data in bytes in every transaction. And as I understand it, I cannot calculate this using existing size variables like vsize.

Will a loop through the "txinwitness" array of hex and do a byte length calculation on every one be enough? (Using NodeJS here)

Buffer.byteLength('03fbcc1c24903bc2fb1d73czef518b859232341c39e4515367653d80536d587b62d6', 'hex');
Will a loop through the "txinwitness" array of hex and do a byte length calculation on every one be enough?


That is not enough as there are extra data in witnesses that is not included in the txinwitness.

You can get the number of witness bytes and the number of non-witness bytes by solving a system of equations. The getrawtransaction RPC gives you two fields, size and weight. size is the total size in bytes as received over the wire. weight is the weight of the transaction as defined by BIP 141.

If you let x be the number of non-witness bytes, and y be the number of witness bytes, the definitions of size and weight gives you:

4x + 1y = weight
x + y = size


By plugging in the weight and size from the RPC, you can solve the system of equations for x and y to get the number of non-witness bytes and number of witness bytes respectively.