When you sign a transaction, you do not sign the raw bytes. You sign a hash of selected parts, called the signature hash. A one-byte sighash flag at the end of the signature says which parts.
The flags
- SIGHASH_ALL (default): commits to all inputs and outputs.
- SIGHASH_NONE: commits to inputs only; anyone can change the outputs.
- SIGHASH_SINGLE: commits to the output with the same index as this input.
- ANYONECANPAY can be combined with each, so the signature commits only to its own input.
Upgrades
BIP143 (SegWit v0) made signers commit to the amount being spent, which fixed quadratic hashing and helps hardware wallets. BIP341 (Taproot) went further and commits to all spent amounts and scripts, and added SIGHASH_DEFAULT, which behaves like ALL but saves a byte.