How are transaction fees claimed from a technical point of view? A typical transaction has one or more inputs and one or more outputs. Since there is no amount specified for inputs, the rule is that ALL of the inputs is always transferred. Any discrepancy between the value contained in the inputs and the total value of the outputs is lost forever.

When a transaction is first submitted there may be a discrepancy between the values of the inputs and outputs. The miner assembling the block may redirect that unaccounted for difference to themselves or any address they wish, called a "transaction fee", however my question is how do they do this?

It would seem the only way to do this would be to add new output to the transaction and new script to send the value to the output. So in other words the miner would change the transaction from the form submitted by the original poster, to a new form including the fee transmittal.

However, isn't the transaction hashed to prevent someone from altering it? If a miner can alter it, why not alter it to just direct all the input to themselves?
A typical transaction has one or more inputs and one or more outputs. Since there is no amount specified for inputs, the rule is that ALL of the inputs is always transferred. Any discrepancy between the value contained in the inputs and the total value of the outputs is lost forever.


This is not correct. The difference between the input amount and the output amount is exactly what we call fee. It is not lost.


  It would seem the only way to do this would be to add new output to the transaction and new script to send the value to the output. So in other words the miner would change the transaction from the form submitted by the original poster, to a new form including the fee transmittal.


That is not possible. Miners cannot modify transactions.

Instead, there is simply a rule that the coinbase transaction (the first transaction in each block, created by the miner) is allowed to have outputs whose summed amount is up to the subsidy value (currently 25 BTC, soon 12.5 BTC) plus the sum of all the fees of the other transactions in the block.