Bitcoin

Constructing a hash-preimage for two p2pkh input transactions?

When confirming a transaction, Bitcoin nodes rebuild the hash pre-image used to generate each signature.

After serializing the hash pre-image for each input, the next step is to double-hash it and verify the signature for each input using its public key.

My question is: What is the exact serialized hash pre-image for each input? I haven’t found any clear guidance on how this should be structured.

For example, consider the following transaction: https://mempool.space/es/tx/592ea2010ec259252959b6047e227ceac6ffd0dbbe20bad71248960c18a5d890

This transaction uses the SIGHASH_ALL flag. This means that each signature signs all inputs and outputs.

Should I include the scriptPubKey of all inputs in the hash pre-image, or should I only include the scriptPubKey of the input being signed and leave the other inputs as empty scripts (0x00)?

Example of my attempt: Here is my attempt to construct a hash pre-image to sign both inputs:

01000000 - Version
02 - Number of inputs
19f4d40cb4204ad8429ec3d98d240661593edcfcfb8efcb3b684141ceb0df2e4 - First UTXO txid
00000000 - First UTXO output index
1976a914d52c9ce97c8dea0b71df687659a847a62c37732c88ac - scriptPubKey of first UTXO
ffffffff - Sequence for first input
22d07db06b68e4c15154db0ce0dd9c9dcd683a60a5117490451e0b52743492ca - Second UTXO txid
00000000 - Second UTXO output index
1976a91497ccdcca4f494b1bd652ad8f9d816b4bbb1108ac88ac - scriptPubKey of second UTXO
ffffffff - Sequence for second input
01 - Number of outputs
c047bfe54d000000 - Amount in satoshis
1976a91492512166101806dd6a695cbe56f883c3bea2a7e488ac - Output's scriptPubKey
00000000 - Locktime
01000000 - SIGHASH_ALL flag

If we try to construct a pre-image for one input (the second input’s scriptSig is set to 0x00):

01000000 - Version
02 - Number of inputs
19f4d40cb4204ad8429ec3d98d240661593edcfcfb8efcb3b684141ceb0df2e4 - First UTXO txid
00000000 - First UTXO output index
1976a914d52c9ce97c8dea0b71df687659a847a62c37732c88ac - scriptPubKey of first UTXO
ffffffff - Sequence for first input
00 - Empty scriptSig for second input (set to 0x00)
ffffffff - Sequence for second input
01 - Number of outputs
c047bfe54d000000 - Amount in satoshis
1976a91492512166101806dd6a695cbe56f883c3bea2a7e488ac - Output's scriptPubKey
00000000 - Locktime
01000000 - SIGHASH_ALL flag

Do I understand correctly how to set scriptPubKey for unsigned input in a hash pre-image? Should I clear the scriptSig of other inputs to 0x00?

Thanks for any guidance!

Related Articles

Back to top button