I was doing some research about the ways to cut out transaction fees when I came across SegWit. I discovered that SegWit was introduced to fix transaction malleability and improve block space efficiency, but I’m still a bit unclear and confused on how it works under the hood.
Could someone help and explain what exactly changes in a SegWit transaction compared to a legacy one? How does this result in lower transaction fees? Why does it fix the transaction malleability issue?
And also, some examples comparing a legacy vs. SegWit transaction format would be really helpful.
Yu kw, I did some digging into this sometime ago when I was trying to figure out why SegWit even matters, and TBH it’s a bit confusing at first, but it makes sense once you break it down.
First off, SegWit is not the first time Bitcoin did a kind of upgrade like this. There was something earlier called P2SH (Pay to Script Hash), where a very simple script like OP_HASH160 <somehash> OP_EQUAL would just make older nodes check if a hash matched. But then newer nodes knew to go one step further and actually run a more complex script hidden behind that hash. That was possible thanks to a soft fork, which let new rules exist without breaking the old ones.
SegWit used that same idea. The script for a SegWit output is super basic, like OP_0 <some hash> nothing fancy. But newer nodes see that and go, “okay, time to check the witness.” That is a new part of the transaction that holds the signature and pubkey.
Now here is where it gets kinda cooler.
In the old style legacy, the signature is part of what gets hashed to make the txid. So if anyone tweaks the signature, like if the same owner re signs it slightly differently, the txid also changes. That is what they mean by transaction malleability. It made some stuff like Lightning harder to build because you could not trust the txid would stay the same.
SegWit changes that. It removes the signature from the txid calculation. So even if someone tweaks the signature, the txid would not change anymore. That is how it fixes malleability.
And then about the fees, this is where I misunderstood it at first. SegWit txs are not always smaller. In fact, they can be bigger in raw bytes. But Bitcoin changed how fees are calculated, not just based on size, but weight… Witness data (the stuff in that new field) gets discounted, each byte of it only counts as 0.25 bytes when calculating how big the transaction is vbytes, basically… So yeah, SegWit transactions might take more bytes, but they’re lighter in weight, so you end up paying less.
Also, this gave Bitcoin a way to fit more into a block without bumping the block size limit directly. That’s why some people say SegWit increased the block size, it kinda did, in a roundabout way.
It took me a while to fully get it too, especially with all the jargon thrown around.