<p>Elaborating on Andrew's answer:</p>

<p>You can calculate it from <code>size</code> and <code>vsize</code>.</p>

<h2>Transaction size</h2>

<p><a href="https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#transaction-size-calculations" rel="nofollow noreferrer">See BIP141</a></p>

<blockquote>
  <p>Transaction weight is defined as Base transaction size * 3 + Total
  transaction size (ie. the same method as calculating Block weight from
  Base size and Total size).</p>
  
  <p>Virtual transaction size is defined as Transaction weight / 4 (rounded
  up to the next integer).</p>
  
  <p>Base transaction size is the size of the transaction serialised with
  the witness data stripped.</p>
  
  <p>Total transaction size is the transaction size in bytes serialized as
  described in BIP144, including base data and witness data.</p>
</blockquote>

<h2>Witness size</h2>

<p>So, for example if you have a transaction that has a total size of 1200 bytes (including witness data), and a base size of 1000 bytes, the weight and virtual size are:</p>

<p><code>weight = 1000 bytes * 3 + 1200 bytes = 4200</code><br>
<code>vsize = 4200 / 4 = 1050 bytes</code></p>

<p>So, given <code>size</code> and <code>vsize</code>, you can calculate the size of witness data:</p>

<p><code>witness size = (4/3) * (total - vsize)</code><br>
<code>witness size = 200</code></p>

CC BY-SA 4.0
2018-10-22T19:26:37.477Z
2018-10-22T19:26:37.477Z
JBaczuk
<blockquote>
  <p>Will a loop through the "txinwitness" array of hex and do a byte length calculation on every one be enough?</p>
</blockquote>

<p>That is not enough as there are extra data in witnesses that is not included in the <code>txinwitness</code>.</p>

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

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

<pre><code>4x + 1y = weight
x + y = size
</code></pre>

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

CC BY-SA 4.0
2018-10-22T17:49:33.560Z
So x = (weight - size)/3, y = (4*size - weight)/3
Pieter Wuille
CC BY-SA 4.0
2018-10-22T17:43:29.007Z
2018-10-22T17:43:29.007Z
Ava Chow
<p>If you use <code>bitcoinjs-lib</code> like I do, after signing inputs for tx you extractTransaction and get the object:</p>
<pre><code>import { Psbt } from 'bitcoinjs-lib';

const psbt = new Psbt(...)

const allInputs = psbt.finalizeAllInputs();
const extractedTx = allInputs.extractTransaction(increaseFeeRate, customFee);
</code></pre>
<p>extractedTx is of following format:</p>
<pre><code>interface SignedTransaction {
  ins: SignedTxInput[],
  locktime: number,
  outs: SignedTxOutput[],
  version: number
}
</code></pre>
<p><code>ins</code> here is of following format:</p>
<pre><code>interface SignedTxInput {
  hash: Buffer,
  index: number,
  script: Buffer,
  sequence: number,
  witness: Buffer[],
}
</code></pre>
<p>Sum witness buffers for each input and you will have your witness weight.</p>

CC BY-SA 4.0
2022-03-14T13:38:06.883Z
2022-03-14T13:38:06.883Z
azrahel
<p>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.</p>

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

<pre><code>Buffer.byteLength('03fbcc1c24903bc2fb1d73czef518b859232341c39e4515367653d80536d587b62d6', 'hex');
</code></pre>

<segregated-witness><witness-data>
Calculating the witness size of a transaction
Jonas Svensson
# Calculating the witness size of a transaction

(Asked by: username_0 on 2018-10-22)

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');

```

**Tags:** segregated-witness, witness-data


---

## 3 answers

### Answer by username_1 on 2018-10-22. Score: 2 | Accepted answer

Elaborating on Andrew's answer:

You can calculate it from `size` and `vsize`.

## Transaction size

[See BIP141](https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki#transaction-size-calculations)

> Transaction weight is defined as Base transaction size \* 3 + Total
> transaction size (ie. the same method as calculating Block weight from
> Base size and Total size).
>
> Virtual transaction size is defined as Transaction weight / 4 (rounded
> up to the next integer).
>
> Base transaction size is the size of the transaction serialised with
> the witness data stripped.
>
> Total transaction size is the transaction size in bytes serialized as
> described in BIP144, including base data and witness data.

## Witness size

So, for example if you have a transaction that has a total size of 1200 bytes (including witness data), and a base size of 1000 bytes, the weight and virtual size are:

`weight = 1000 bytes * 3 + 1200 bytes = 4200`  
`vsize = 4200 / 4 = 1050 bytes`

So, given `size` and `vsize`, you can calculate the size of witness data:

`witness size = (4/3) * (total - vsize)`  
`witness size = 200`


---

### Answer by username_2 on 2018-10-22. Score: 3

> 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.

> **Comments:**
> **username_3**: So x = (weight - size)/3, y = (4*size - weight)/3  

---

### Answer by username_4 on 2022-03-14. Score: 0

If you use `bitcoinjs-lib` like I do, after signing inputs for tx you extractTransaction and get the object:

```
import { Psbt } from 'bitcoinjs-lib';

const psbt = new Psbt(...)

const allInputs = psbt.finalizeAllInputs();
const extractedTx = allInputs.extractTransaction(increaseFeeRate, customFee);

```

extractedTx is of following format:

```
interface SignedTransaction {
  ins: SignedTxInput[],
  locktime: number,
  outs: SignedTxOutput[],
  version: number
}

```

`ins` here is of following format:

```
interface SignedTxInput {
  hash: Buffer,
  index: number,
  script: Buffer,
  sequence: number,
  witness: Buffer[],
}

```

Sum witness buffers for each input and you will have your witness weight.


---

