<p>Depending on the type of transaction, 
inputs, outputs and other factors..add them all up here is a link to bitcoin docs giving byte size for everything  <a href="https://en.bitcoin.it/wiki/Protocol_documentation#tx" rel="nofollow noreferrer">https://en.bitcoin.it/wiki/Protocol_documentation#tx</a></p>

CC BY-SA 4.0
2020-01-06T04:53:32.563Z
2020-01-06T05:06:14.010Z
2020-01-06T05:06:14.010Z
Johnny Good
<p>Similarly to how a browser decodes a bunch of text, which is arranged in accordance with a certain protocol (html), and creates a nice interface you can view (provided it has all the right pieces, so starts with , has all matching &lt;> for tags, etc, otherwise your browser displays an error or a garbage page) ... </p>

<p>A bitcoin node will decode a bunch of text, arranged in accordance with a certain protocol (Bitcoin), and creates a transaction on the Bitcoin network (provided it has all the right pieces, so the first 8 characters are the version number, next 4 are the flag, next 2 are the tx count, etc, otherwise the network returns that its an invalid transaction).</p>

<p>So its literally just tied to the length of the text sent. To achieve certain things in bitcoin (multi-signature transactions, time lock transactions, etc), you need to include more text in the code that represents what you are trying to accomplish, making the transaction larger, just as a webpage would need to have <code>&lt;hr&gt;&lt;hr&gt;&lt;hr&gt;</code> if you wanted 3 horizontal lines, (adding 12 characters to the code of the webpage)</p>

CC BY-SA 4.0
2020-01-09T17:01:33.563Z
Why the downvote? Just trying to explain this in not-so-technical terms
hedgedandlevered
CC BY-SA 4.0
2020-01-06T17:51:08.010Z
2020-01-06T17:51:08.010Z
hedgedandlevered
<p>Since the activation of Segwit, transaction size is expressed in Weight Units (WU). Weight Units can be converted to a "virtual size" or virtual bytes (vB) by dividing by 4 and rounding up. The virtual size dictates how large your transaction is when paying fees.</p>

<h2>Non-Segwit Transactions</h2>

<p>For now, let's focus on Non-Segwit transactions. Each Non-Segwit byte of data is counted is 4 WU when determining size. Each transaction consists of the following fields:</p>

<ul>
<li>Version (4 bytes)</li>
<li># of Inputs (VarInt, 1 byte for numbers up to 252)</li>
<li>Inputs (varies)</li>
<li># of Outputs (VarInt, 1 byte for numbers up to 252)</li>
<li>Outputs (varies)</li>
<li>Locktime (4 bytes)</li>
</ul>

<p>Let's break each of these fields down by looking at a <a href="https://blockstream.info/tx/791bd9b3bda632ee1757de37db8939b0287ece4753f3077b9a5f5c7e28545967" rel="noreferrer">non-segwit raw transaction</a>:
<code>
0200000001d2eed35b0d55763981c635cab7788c28e3683af3d329947a17d7f6005390e6ef010000006a47304402200ffaac8f12e56f4af66109220812b76c7d6bb0e5906cf2de235b79496b3e080b0220037304894b648fa0b50e0d82ef9f58b537233ee282ab31fc04fb3d169d2c97cc0121030120a287eb98922752a89b39df64f7f3314036f6f096341c9189b0cb3c692aaeffffffff02404ff200000000001976a9140b818b11f9624e6a2d5b757a3e8fe45db3f6647788ace0b4b401000000001976a914a1e5e47fce1c5c0868107dba3851eb696c2ead5388ac00000000
</code></p>

<h3>Version</h3>

<p>The version of this transaction is <code>02000000</code> (2 in little-endian). The version is always 4 bytes (16 WU).</p>

<h3># Of Inputs</h3>

<p>The number of inputs is <code>01</code> (1 in VarInt notation). This indicates there is 1 input being spent by this transaction. This adds 1 byte (4 WU).</p>

<h3>Inputs</h3>

<p>All Inputs consist of the following fields:</p>

<ul>
<li>Referenced Transaction Hash (32 bytes)</li>
<li>Output Index (4 bytes)</li>
<li>Script Length (VarInt, 1 byte for numbers up to 252)</li>
<li>ScriptSig (varies)</li>
<li>Sequence (4 bytes)</li>
</ul>

<p>For our example transaction, the values are:</p>

<ul>
<li>RefTX: <code>d2eed35b0d55763981c635cab7788c28e3683af3d329947a17d7f6005390e6ef</code></li>
<li>Output Index: <code>01000000</code> (1 in little-endian)</li>
<li>Script Length: <code>6a</code> (106 in VarInt notation)</li>
<li>ScriptSig: <code>47304402200ffaac8f12e56f4af66109220812b76c7d6bb0e5906cf2de235b79496b3e080b0220037304894b648fa0b50e0d82ef9f58b537233ee282ab31fc04fb3d169d2c97cc0121030120a287eb98922752a89b39df64f7f3314036f6f096341c9189b0cb3c692aae</code></li>
<li>Sequence: <code>ffffffff</code></li>
</ul>

<p>In total, this input adds 147 bytes (588 WU).</p>

<h3># Of Outputs</h3>

<p>The number of outputs is <code>02</code> (2 in VarInt notation). This indicates there is 2 outputs being created in this transaction. This adds 1 byte (4 WU).</p>

<h3>Outputs</h3>

<p>All Outputs consist of the following fields:</p>

<ul>
<li>Value (8 bytes)</li>
<li>Script Length (VarInt, 1 byte for numbers up to 252)</li>
<li>ScriptPubKey (varies)</li>
</ul>

<p>For our example transaction, we have 2 outputs. The first output is:</p>

<ul>
<li>Value: <code>404ff20000000000</code> (15,880,000 satoshi, in little-endian)</li>
<li>Script Length: <code>19</code> (25 in VarInt notation)</li>
<li>ScriptPubKey: <code>76a9140b818b11f9624e6a2d5b757a3e8fe45db3f6647788ac</code></li>
</ul>

<p>In total, this output adds 34 bytes (136 WU).</p>

<p>The second output is:</p>

<ul>
<li>Value: <code>e0b4b40100000000</code> (28,620,000 satoshi, in little-endian)</li>
<li>Script Length: <code>19</code> (25 in VarInt notation)</li>
<li>ScriptPubKey: <code>76a914a1e5e47fce1c5c0868107dba3851eb696c2ead5388ac</code></li>
</ul>

<p>In total, this output adds 34 bytes (136 WU).</p>

<h3>Locktime</h3>

<p>For our example transaction, the locktime is <code>00000000</code> (0 in little-endian). Locktime is always 4 bytes (16 WU).</p>

<p>In total, this transaction is 900 WU. Converting to vB gives us 225 vB. With an <a href="https://blockstream.info/tx/efe6905300f6d7177a9429d3f33a68e3288c78b7ca35c6813976550d5bd3eed2?output:1" rel="noreferrer">Input Value</a> of 0.4491 BTC and a combined Output Value of 0.445 BTC, the total miner fee is 0.0041 or 410,000 satoshi. Converting this to sat/vB gives us a feerate of 1,822 sat/vB. (incredibly high!)</p>

<h2>Segwit Transactions</h2>

<p>A Segwit Transaction is any transaction which spends a Segwit input (aka an address beginning with "3" or "bc1"). Segwit Transactions include a few extra pieces of information like Witness data. Segwit Data is counted as 1 WU/byte and is therefore cheaper to include in transactions. This is why Segwit saves on fees. A Segwit Transaction consists of the following fields, with fields counting as Segwit Data being <strong>bolded</strong>:</p>

<ul>
<li>Version (4 bytes)</li>
<li><strong>Marker</strong> (1 byte)</li>
<li><strong>Flag</strong> (1 byte)</li>
<li># of Inputs (VarInt, 1 byte for numbers up to 252)</li>
<li>Inputs (varies)</li>
<li># of Outputs (VarInt, 1 byte for numbers up to 252)</li>
<li>Outputs (varies)</li>
<li><strong>Witness Data</strong> (varies)</li>
<li>Locktime (4 bytes)</li>
</ul>

<p>Again, let's break each section down in an example <a href="https://blockstream.info/tx/3d0acedc2f5787d2fbefa85e72ef6aac0465e78e4d3f05ae5f813488420bde38" rel="noreferrer">Segwit Transaction</a>:
<code>
02000000000101caba4ccb61cca412fe29ec553d286134a02335c04355a9cc1e056fe2403692cf1400000000fdffffff026406010000000000160014093c864a10516154d18d2accd61c6b2920a2040f60361e000000000017a9140195e8dd3d1527038a0a77e0b0e4515d6c8ab195870247304402200760efedbcee3bbd913661fb40c364caed622b331ee3b5bc70ac25df2d3763d5022020efdb41ef325089d146eb48948b6aaec85c086fa38a3df4d2a51ee729645ebc012102be338e0362fb01101a873a7502012cb82f7d9d303b4943fa0702829a763e49762b550900
</code></p>

<h3>Version</h3>

<p>The version is, like before, <code>02000000</code> (2 in little-endian) and is always 4 bytes (16 WU).</p>

<h3>Marker</h3>

<p>The marker is <code>00</code> (0 in VarInt notation). The Marker indicates that this Transaction is a Segwit Transaction. Nodes running Segwit will see this marker and know to parse the transaction appropriately, and nodes not running Segwit will treat the marker as the "# of Inputs" field, discarding it as invalid for having 0 inputs. This adds 1 byte (1 WU).</p>

<h3>Flag</h3>

<p>The flag is <code>01</code> (1 in VarInt notation). The Flag indicates that Witness Data will be present in the transaction, and adds 1 byte (1 WU).</p>

<h3># Of Inputs</h3>

<p>The number of inputs is <code>01</code> (1 in VarInt notation) indicating this transaction has 1 input. This adds 1 byte (4 WU).</p>

<h3>Inputs</h3>

<p>The first (and only) input is as follows:</p>

<ul>
<li>RefTX: <code>caba4ccb61cca412fe29ec553d286134a02335c04355a9cc1e056fe2403692cf</code></li>
<li>Output Index: <code>14000000</code> (14 in little-endian)</li>
<li>Script Length: <code>00</code> (0 in VarInt notation)</li>
<li>ScriptSig: [EMPTY]</li>
<li>Sequence: <code>fdffffff</code></li>
</ul>

<p>This input adds 41 bytes (164 WU).</p>

<h3># Of Outputs</h3>

<p>The number of Outputs is <code>02</code> (2 in VarInt notation) indicating there are 2 outputs created in this transaction. This adds 1 byte (4 WU).</p>

<h3>Outputs</h3>

<p>The first output is as follows:</p>

<ul>
<li>Value: <code>6406010000000000</code> (67,172 satoshi, in little-endian)</li>
<li>Script Length: <code>16</code> (22 in VarInt notation)</li>
<li>ScriptPubKey: <code>0014093c864a10516154d18d2accd61c6b2920a2040f</code></li>
</ul>

<p>This output adds 31 bytes (124 WU). The second output is as follows:</p>

<ul>
<li>Value: <code>60361e0000000000</code> (1,980,000 satoshi, in little-endian)</li>
<li>Script Length: <code>17</code> (23 in VarInt notation)</li>
<li>ScriptPubKey: <code>a9140195e8dd3d1527038a0a77e0b0e4515d6c8ab19587</code></li>
</ul>

<p>This output adds 32 bytes (128 WU).</p>

<h3>Witness Data</h3>

<p>The witness data consists of Data Pushes. I will not go into the details of Witness Programs in this answer. The witness program for this transaction is:
<code>
02 47 304402200760efedbcee3bbd913661fb40c364caed622b331ee3b5bc70ac25df2d3763d5022020efdb41ef325089d146eb48948b6aaec85c086fa38a3df4d2a51ee729645ebc01 21 02be338e0362fb01101a873a7502012cb82f7d9d303b4943fa0702829a763e4976
</code></p>

<p>The Witness Data adds 107 bytes (107 WU).</p>

<h3>Locktime</h3>

<p>The locktime for this transaction is <code>2b550900</code> (611,627 in little-endian) and is used as the Block height at which this transaction's outputs can be spent. This adds 4 bytes (16 WU).</p>

<p>In total, this transaction is 565 WU. Converting to vB, that's 142 vB. With an <a href="https://blockstream.info/tx/cf923640e26f051ecca95543c03523a03461283d55ec29fe12a4cc61cb4cbaca?output:20" rel="noreferrer">Input Value</a> of 0.02118172 BTC and a combined Output value of 0.02047172 BTC, the transaction fee is 0.00071 BTC (71,000 satoshi). The feerate of this transaction is 500 sat/vB. (also extremely high!)</p>

<h2>Estimating Fees</h2>

<p><strong>With this knowledge, how does a wallet know how much to use as a fee given a feerate?</strong>
It's quite simple. See <a href="https://bitcoin.stackexchange.com/a/92600/100415">this answer</a> for information on how a wallet handles this information given different address types. Your wallet estimates how much satoshi to set aside as fee based on the inputs, outputs, address types, and desired feerate before signing the transaction.</p>

<p>As for how wallets create transactions with the lowest fee possible, and how fee estimation algorithms that look at the current mempool work, they are wallet-dependent.</p>

CC BY-SA 4.0
2020-01-07T05:32:55.060Z
Just wowed by this answer, it is precisely what I was looking for!
Skelli
CC BY-SA 4.0
2020-01-07T22:11:23.347Z
Hey @Kelli, if you found this answer to your question helpful, please mark it as the accepted answer. Cheers :)
Septem151
CC BY-SA 4.0
2020-01-08T18:58:20.767Z
This is great! Three small nits: On the description of inputs `Script Length (1 byte typically)`, you could clarify by writing `Script Length (VarInt, 1 byte for numbers up to 252)`. Realizing that the VarInt limit is exceeded in many 2-of-3 p2sh inputs was a big lightbulb moment for me once. Secondly, I think it would be good to put a header when you switch to talk about segwit, it sneaks in a bit. Lastly, it's a bit weird that you link to blockcypher, because blockcypher actually doesn't correctly show the correct transaction size for segwit transactions, it shows the stripped size.
Murch
CC BY-SA 4.0
2020-01-08T21:03:53.817Z
@Murch I will edit the answer to include your changes, thank you for the suggestions! I use blockcypher as the links since they have an "API Call" button to show the raw transaction, but since the raw transactions are already present in the answer I'll switch them to blockstream.info references instead.
Septem151
CC BY-SA 4.0
2023-03-23T13:21:59.877Z
Why is the witness data 107? Blockstream shows withness to be ` 304402200760efedbcee3bbd913661fb40c364caed622b331ee3b5bc70ac25df2d3763d5022020efdb41ef325089d146eb48948b6aaec85c086fa38a3df4d2a51ee729645ebc01 02be338e0362fb01101a873a7502012cb82f7d9d303b4943fa0702829a763e4976 `

Where does the rest of the data come from?
pxr_64
CC BY-SA 4.0
2023-07-13T23:24:59.613Z
@pxr_64 Blockstream does not show the bytes representing the number of Data Pushes (02), nor the individual byte length VarInt bytes representing the length of each data push (47 and 21). They are only showing the bytes of the actual data value(s) being pushed.
Septem151
CC BY-SA 4.0
2020-01-07T00:28:33.680Z
2020-01-25T16:00:18.567Z
2020-01-25T16:00:18.567Z
Septem151
<p>From what I understand, the fees set on bitcoin transaction are based on a recommended fee (mostly, from a 3rd party service) per byte of the transaction size. What I am curious about is how do wallets arrive at the size of a transaction? </p>

<transaction-fees><transaction-weight>
How is the size of a bitcoin transaction calculated
Skelli
# How is the size of a bitcoin transaction calculated

(Asked by: username_0 on 2020-01-06)

From what I understand, the fees set on bitcoin transaction are based on a recommended fee (mostly, from a 3rd party service) per byte of the transaction size. What I am curious about is how do wallets arrive at the size of a transaction?

**Tags:** transaction-fees, transaction-weight


---

## 3 answers

### Answer by username_3 on 2020-01-07. Score: 18 | Accepted answer

Since the activation of Segwit, transaction size is expressed in Weight Units (WU). Weight Units can be converted to a "virtual size" or virtual bytes (vB) by dividing by 4 and rounding up. The virtual size dictates how large your transaction is when paying fees.

## Non-Segwit Transactions

For now, let's focus on Non-Segwit transactions. Each Non-Segwit byte of data is counted is 4 WU when determining size. Each transaction consists of the following fields:

* Version (4 bytes)
* # of Inputs (VarInt, 1 byte for numbers up to 252)
* Inputs (varies)
* # of Outputs (VarInt, 1 byte for numbers up to 252)
* Outputs (varies)
* Locktime (4 bytes)

Let's break each of these fields down by looking at a [non-segwit raw transaction](https://blockstream.info/tx/791bd9b3bda632ee1757de37db8939b0287ece4753f3077b9a5f5c7e28545967):
`0200000001d2eed35b0d55763981c635cab7788c28e3683af3d329947a17d7f6005390e6ef010000006a47304402200ffaac8f12e56f4af66109220812b76c7d6bb0e5906cf2de235b79496b3e080b0220037304894b648fa0b50e0d82ef9f58b537233ee282ab31fc04fb3d169d2c97cc0121030120a287eb98922752a89b39df64f7f3314036f6f096341c9189b0cb3c692aaeffffffff02404ff200000000001976a9140b818b11f9624e6a2d5b757a3e8fe45db3f6647788ace0b4b401000000001976a914a1e5e47fce1c5c0868107dba3851eb696c2ead5388ac00000000`

### Version

The version of this transaction is `02000000` (2 in little-endian). The version is always 4 bytes (16 WU).

### # Of Inputs

The number of inputs is `01` (1 in VarInt notation). This indicates there is 1 input being spent by this transaction. This adds 1 byte (4 WU).

### Inputs

All Inputs consist of the following fields:

* Referenced Transaction Hash (32 bytes)
* Output Index (4 bytes)
* Script Length (VarInt, 1 byte for numbers up to 252)
* ScriptSig (varies)
* Sequence (4 bytes)

For our example transaction, the values are:

* RefTX: `d2eed35b0d55763981c635cab7788c28e3683af3d329947a17d7f6005390e6ef`
* Output Index: `01000000` (1 in little-endian)
* Script Length: `6a` (106 in VarInt notation)
* ScriptSig: `47304402200ffaac8f12e56f4af66109220812b76c7d6bb0e5906cf2de235b79496b3e080b0220037304894b648fa0b50e0d82ef9f58b537233ee282ab31fc04fb3d169d2c97cc0121030120a287eb98922752a89b39df64f7f3314036f6f096341c9189b0cb3c692aae`
* Sequence: `ffffffff`

In total, this input adds 147 bytes (588 WU).

### # Of Outputs

The number of outputs is `02` (2 in VarInt notation). This indicates there is 2 outputs being created in this transaction. This adds 1 byte (4 WU).

### Outputs

All Outputs consist of the following fields:

* Value (8 bytes)
* Script Length (VarInt, 1 byte for numbers up to 252)
* ScriptPubKey (varies)

For our example transaction, we have 2 outputs. The first output is:

* Value: `404ff20000000000` (15,880,000 satoshi, in little-endian)
* Script Length: `19` (25 in VarInt notation)
* ScriptPubKey: `76a9140b818b11f9624e6a2d5b757a3e8fe45db3f6647788ac`

In total, this output adds 34 bytes (136 WU).

The second output is:

* Value: `e0b4b40100000000` (28,620,000 satoshi, in little-endian)
* Script Length: `19` (25 in VarInt notation)
* ScriptPubKey: `76a914a1e5e47fce1c5c0868107dba3851eb696c2ead5388ac`

In total, this output adds 34 bytes (136 WU).

### Locktime

For our example transaction, the locktime is `00000000` (0 in little-endian). Locktime is always 4 bytes (16 WU).

In total, this transaction is 900 WU. Converting to vB gives us 225 vB. With an [Input Value](https://blockstream.info/tx/efe6905300f6d7177a9429d3f33a68e3288c78b7ca35c6813976550d5bd3eed2?output:1) of 0.4491 BTC and a combined Output Value of 0.445 BTC, the total miner fee is 0.0041 or 410,000 satoshi. Converting this to sat/vB gives us a feerate of 1,822 sat/vB. (incredibly high!)

## Segwit Transactions

A Segwit Transaction is any transaction which spends a Segwit input (aka an address beginning with "3" or "bc1"). Segwit Transactions include a few extra pieces of information like Witness data. Segwit Data is counted as 1 WU/byte and is therefore cheaper to include in transactions. This is why Segwit saves on fees. A Segwit Transaction consists of the following fields, with fields counting as Segwit Data being **bolded**:

* Version (4 bytes)
* **Marker** (1 byte)
* **Flag** (1 byte)
* # of Inputs (VarInt, 1 byte for numbers up to 252)
* Inputs (varies)
* # of Outputs (VarInt, 1 byte for numbers up to 252)
* Outputs (varies)
* **Witness Data** (varies)
* Locktime (4 bytes)

Again, let's break each section down in an example [Segwit Transaction](https://blockstream.info/tx/3d0acedc2f5787d2fbefa85e72ef6aac0465e78e4d3f05ae5f813488420bde38):
`02000000000101caba4ccb61cca412fe29ec553d286134a02335c04355a9cc1e056fe2403692cf1400000000fdffffff026406010000000000160014093c864a10516154d18d2accd61c6b2920a2040f60361e000000000017a9140195e8dd3d1527038a0a77e0b0e4515d6c8ab195870247304402200760efedbcee3bbd913661fb40c364caed622b331ee3b5bc70ac25df2d3763d5022020efdb41ef325089d146eb48948b6aaec85c086fa38a3df4d2a51ee729645ebc012102be338e0362fb01101a873a7502012cb82f7d9d303b4943fa0702829a763e49762b550900`

### Version

The version is, like before, `02000000` (2 in little-endian) and is always 4 bytes (16 WU).

### Marker

The marker is `00` (0 in VarInt notation). The Marker indicates that this Transaction is a Segwit Transaction. Nodes running Segwit will see this marker and know to parse the transaction appropriately, and nodes not running Segwit will treat the marker as the "# of Inputs" field, discarding it as invalid for having 0 inputs. This adds 1 byte (1 WU).

### Flag

The flag is `01` (1 in VarInt notation). The Flag indicates that Witness Data will be present in the transaction, and adds 1 byte (1 WU).

### # Of Inputs

The number of inputs is `01` (1 in VarInt notation) indicating this transaction has 1 input. This adds 1 byte (4 WU).

### Inputs

The first (and only) input is as follows:

* RefTX: `caba4ccb61cca412fe29ec553d286134a02335c04355a9cc1e056fe2403692cf`
* Output Index: `14000000` (14 in little-endian)
* Script Length: `00` (0 in VarInt notation)
* ScriptSig: [EMPTY]
* Sequence: `fdffffff`

This input adds 41 bytes (164 WU).

### # Of Outputs

The number of Outputs is `02` (2 in VarInt notation) indicating there are 2 outputs created in this transaction. This adds 1 byte (4 WU).

### Outputs

The first output is as follows:

* Value: `6406010000000000` (67,172 satoshi, in little-endian)
* Script Length: `16` (22 in VarInt notation)
* ScriptPubKey: `0014093c864a10516154d18d2accd61c6b2920a2040f`

This output adds 31 bytes (124 WU). The second output is as follows:

* Value: `60361e0000000000` (1,980,000 satoshi, in little-endian)
* Script Length: `17` (23 in VarInt notation)
* ScriptPubKey: `a9140195e8dd3d1527038a0a77e0b0e4515d6c8ab19587`

This output adds 32 bytes (128 WU).

### Witness Data

The witness data consists of Data Pushes. I will not go into the details of Witness Programs in this answer. The witness program for this transaction is:
`02 47 304402200760efedbcee3bbd913661fb40c364caed622b331ee3b5bc70ac25df2d3763d5022020efdb41ef325089d146eb48948b6aaec85c086fa38a3df4d2a51ee729645ebc01 21 02be338e0362fb01101a873a7502012cb82f7d9d303b4943fa0702829a763e4976`

The Witness Data adds 107 bytes (107 WU).

### Locktime

The locktime for this transaction is `2b550900` (611,627 in little-endian) and is used as the Block height at which this transaction's outputs can be spent. This adds 4 bytes (16 WU).

In total, this transaction is 565 WU. Converting to vB, that's 142 vB. With an [Input Value](https://blockstream.info/tx/cf923640e26f051ecca95543c03523a03461283d55ec29fe12a4cc61cb4cbaca?output:20) of 0.02118172 BTC and a combined Output value of 0.02047172 BTC, the transaction fee is 0.00071 BTC (71,000 satoshi). The feerate of this transaction is 500 sat/vB. (also extremely high!)

## Estimating Fees

**With this knowledge, how does a wallet know how much to use as a fee given a feerate?**
It's quite simple. See [this answer](https://bitcoin.stackexchange.com/a/92600/100415) for information on how a wallet handles this information given different address types. Your wallet estimates how much satoshi to set aside as fee based on the inputs, outputs, address types, and desired feerate before signing the transaction.

As for how wallets create transactions with the lowest fee possible, and how fee estimation algorithms that look at the current mempool work, they are wallet-dependent.

> **Comments:**
> **username_0**: Just wowed by this answer, it is precisely what I was looking for!  
> **username_3**: Hey @Kelli, if you found this answer to your question helpful, please mark it as the accepted answer. Cheers :)  
> **username_4**: This is great! Three small nits: On the description of inputs `Script Length (1 byte typically)`, you could clarify by writing `Script Length (VarInt, 1 byte for numbers up to 252)`. Realizing that the VarInt limit is exceeded in many 2-of-3 p2sh inputs was a big lightbulb moment for me once. Secondly, I think it would be username_1 to put a header when you switch to talk about segwit, it sneaks in a bit. Lastly, it's a bit weird that you link to blockcypher, because blockcypher actually doesn't correctly show the correct transaction size for segwit transactions, it shows the stripped size.  
> **username_3**: @username_4 I will edit the answer to include your changes, thank you for the suggestions! I use blockcypher as the links since they have an "API Call" button to show the raw transaction, but since the raw transactions are already present in the answer I'll switch them to blockstream.info references instead.  
> **username_5**: Why is the witness data 107? Blockstream shows withness to be ` 304402200760efedbcee3bbd913661fb40c364caed622b331ee3b5bc70ac25df2d3763d5022020efdb41ef325089d146eb48948b6aaec85c086fa38a3df4d2a51ee729645ebc01 02be338e0362fb01101a873a7502012cb82f7d9d303b4943fa0702829a763e4976 `  Where does the rest of the data come from?  
> **username_3**: @username_5 Blockstream does not show the bytes representing the number of Data Pushes (02), nor the individual byte length VarInt bytes representing the length of each data push (47 and 21). They are only showing the bytes of the actual data value(s) being pushed.  

---

### Answer by username_1 on 2020-01-06. Score: 0

Depending on the type of transaction,
inputs, outputs and other factors..add them all up here is a link to bitcoin docs giving byte size for everything <https://en.bitcoin.it/wiki/Protocol_documentation#tx>


---

### Answer by username_2 on 2020-01-06. Score: -1

Similarly to how a browser decodes a bunch of text, which is arranged in accordance with a certain protocol (html), and creates a nice interface you can view (provided it has all the right pieces, so starts with , has all matching <> for tags, etc, otherwise your browser displays an error or a garbage page) ...

A bitcoin node will decode a bunch of text, arranged in accordance with a certain protocol (Bitcoin), and creates a transaction on the Bitcoin network (provided it has all the right pieces, so the first 8 characters are the version number, next 4 are the flag, next 2 are the tx count, etc, otherwise the network returns that its an invalid transaction).

So its literally just tied to the length of the text sent. To achieve certain things in bitcoin (multi-signature transactions, time lock transactions, etc), you need to include more text in the code that represents what you are trying to accomplish, making the transaction larger, just as a webpage would need to have `<hr><hr><hr>` if you wanted 3 horizontal lines, (adding 12 characters to the code of the webpage)

> **Comments:**
> **username_2**: Why the downvote? Just trying to explain this in not-so-technical terms  

---

