Your assumption in the following equation
transaction = transaction input(UTXO+sig) + transaction output
is wrong.
Correct would be
transaction = UTXO + sig[tx data + hash(UTXO)] + output
UTXO + sig[tx data + hash(UTXO)
is added for each input
output
is added for each output
This is JSON format of transaction that I have recently created and not yet broadcasted
. You may notice that it has 1 input and 1 output.
{
"hash": "7decf07997e58980b666e3cda17d4a930a21c8e99cad84c587a0e59d06b2dc86",
"ver": 1,
"vin_sz": 1,
"vout_sz": 1,
"lock_time": 0,
"size": 191,
"in": [
{
"prev_out": {
"hash": "5cbbbb09f4323badb86d32ef4b8929ce757c078bc399c2ec499daaf8f51c455e",
"n": 1
},
"scriptSig": "304402203a0a2a31e906997d8beed8ca5a81bd303f9fff2475cb0400983eef0fb718a573022069103db03857fb8c7b204e3ed3706ea6fa729708bc85492a7783ca11760a672101 02856209e1b0dfd542898c8a8c58da4a91f9f6a82cddb4157117fe18157fc28c94",
"sequence": 4294967295
}
],
"out": [
{
"value": "0.00010000",
"scriptPubKey": "OP_DUP OP_HASH160 c74eb0fc16d79cbf4399c80e7ec07162a41f30c6 OP_EQUALVERIFY OP_CHECKSIG"
}
]
}
As you may know, to prove that you are allowed to spend input of tx you need to provide Digital signature
- that is the scriptSig
element inside in
array.
scriptSig
are digitally signed following data:
- ID (hash) of particular input (prev_out)
- double SHA of new transaction data
Signing transaction data prevents anyone from executing so called Man-in-the-middle attack
and intercept and direct out
to their wallet, since they cannot provide correct digital signature for the malformed transaction.
Why not try yourself? Here is the hex of above tx:
01000000015e451cf5f8aa9d49ecc299c38b077c75ce29894bef326db8ad3b32f409bbbb5c010000006a47304402203a0a2a31e906997d8beed8ca5a81bd303f9fff2475cb0400983eef0fb718a573022069103db03857fb8c7b204e3ed3706ea6fa729708bc85492a7783ca11760a6721012102856209e1b0dfd542898c8a8c58da4a91f9f6a82cddb4157117fe18157fc28c94ffffffff0110270000000000001976a914c74eb0fc16d79cbf4399c80e7ec07162a41f30c688ac00000000
You can replace output address c74eb0fc16d79cbf4399c80e7ec07162a41f30c6
with double hash (RIPEDM160(SHA256(K)) of your public key.
Then try to broadcast the malformed tx with http://btc.blockr.io/tx/push to see it is impossible. Or if you are skilled you may use sendrawtransaction <tx hex here>
command in bitcoind
.