I know that the bitcoin fee is calculated based on the number of bytes in a transaction (i.e. satoshi per byte). But what bytes are calculated? The bytes of the encoded/serialized transaction or of the raw transaction?

For example, I have the transaction below. The size of the raw bytes is 192. The encoded(hex) is 384. I used a fee of "1" satoshi to generate it so that I can get a sample of what the size will be like. Would be safe to multiple satoshi with the raw size to get the fee and then re-make the transaction? (e.g. 1000 satoshi * 192 = 192000 satoshi fee).


One hexadecimal digit represents 4 bits which is half of a byte (8 bits). Therefore, the raw byte length of a transaction is half of the length of its hexadecimal representation.

In your case, there are 384 hexadecimal digits, and the raw byte length of the transaction is 192 bytes. Depending on whether the fee rate is given in [satoshi/byte], [satoshi/kilobyte] or [BTC/kilobyte], you need to apply the corresponding unit conversions. Note that Bitcoin uses kilobyte here for 1,000 bytes.

There are two more pitfalls here:

  1. Since the activation of segwit, fees are paid per weight, not raw byte length. In practice this means that transactions with segwit inputs require less fees than what would correspond to their raw byte length. I've explored the topic further here: Medium: PSA: Wrong fee rates on block explorers

  2. Signatures in Bitcoin have a variable length. It is therefore common that your fee estimate is off by a byte or two, especially if your transaction has few inputs. You can get around that by either using a slightly conservative estimate (which is for example how this is addressed in Bitcoin Core), or by repeating the signature for the transaction until the size fits your estimate.

| improve this answer | |

generally yes. I got this here from the forum (lost the link):

fee = (n_inputs * 148 + n_outputs * 34 + 10) * price_per_byte

SegWit slightly changes this, where instead of paying per byte, you pay per unit of weight: 1 byte of non witness data = 4 weight, 1 byte of witness data = 1 weight.

| improve this answer | |
  • I understand that size (raw bytes) may be deterministic but I would rather prefer to calculate it using the len(raw bytes) * price_per byte than other complicated calc like you mentioned (n_inputs * 148 + n_outputs * 34 + 10) – Books Jan 17 '18 at 18:51
  • when assembling the raw tx, it is only afterwards the signatures come in. The signatures can be of different length (72, 73 or 74 bytes - I think Pieter last recently mentioned, only the last two appear in the wild), due to R- or S-Value corrections. So before sig, the formula would be a good approach for P2SH or P2PKH tx (SegWit have slight changes). To be absolutly sure, I think one would need to sign the tx, calculate the length, and then adopt the tx fees. But how would one present this to a user interface? And what if many inputs/outputs? At least the values are fixed length. – pebwindkraft Jan 17 '18 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.