<p>Yes, you are correct. When serializing the transaction for signing, the input you are targeting is set to the ScriptPubKey of the UTXO it is trying to spend, while the other inputs are left empty, This means that for an N input transaction, you would need to create N different serializations.</p>

CC BY-SA 4.0
2024-10-17T13:00:14.157Z
2024-10-17T13:00:14.157Z
sr_gi
<p>Thanks sr_gi!</p>
<p>After testing, I have managed to get the answer: for a two P2PKH-input transaction, the Hash-preimage has the following structure (for the first input):</p>
<pre><code>01000000 - Version
02 - Number of inputs
c43959452301573767dc4c3430de6230a0c573414b204b74079d3a6a467e0f29 - First UTXO txid
00000000 - First UTXO index
1976a914df638f7f911f97a3dd764c1eaffebdc932ba6fe288ac - scriptPubKey of first UTXO
ffffffff - Sequence for first input
c43959452301573767dc4c3430de6230a0c573414b204b74079d3a6a467e0f29 - Second UTXO txid
01000000 - Second UTXO index
00 - Empty scriptSig for second input (set to 0x00)
ffffffff - Sequence for second input
01 - Number of outputs
1027000000000000 - Amount in satoshis
1976a9140983b6a0b7885957f8aa8ffb5de0d365c663b40088ac - Output's scriptPubKey
5fb93000 - Locktime
01000000 - SIGHASH_ALL flag
</code></pre>
<p>For the second input, the hash-preimage will set the first input scriptPubKey to 0x00 and the second input scriptPubKey to the corresponding scriptPubKey.</p>

CC BY-SA 4.0
2024-10-23T09:42:10.123Z
2024-10-23T11:19:51.010Z
2024-10-23T11:19:51.010Z
SalvaZ
<p>When verifying a transaction, a Bitcoin node rebuilds the hash-preimage used to create each signature.</p>
<p>After serializing the hash-preimage for each input, the next step is to double-hash it and verify the signature for each input using the corresponding public key.</p>
<p>My question is: what is the exact serialized hash-preimage for each input? I haven't found clear guidance on how this should be constructed.</p>
<p>For example, take this transaction:
<a href="https://mempool.space/es/tx/592ea2010ec259252959b6047e227ceac6ffd0dbbe20bad71248960c18a5d890" rel="nofollow noreferrer">https://mempool.space/es/tx/592ea2010ec259252959b6047e227ceac6ffd0dbbe20bad71248960c18a5d890</a></p>
<p>This transaction uses the SIGHASH_ALL flag, meaning that each signature signs all inputs and outputs.</p>
<p>Should the hash-preimage include the scriptPubKey of every input, or only the scriptPubKey of the input being signed, leaving the other inputs with an empty script (0x00)?</p>
<p>Example of My Attempt:
Here is my attempt to construct the hash-preimage for signing both inputs:</p>
<pre><code>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
</code></pre>
<p>When I try to construct the preimage for just one input (with the second input's scriptSig set to 0x00):</p>
<pre><code>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
</code></pre>
<p>Am I correctly understanding how to set the scriptPubKey for non-signed inputs in the hash-preimage? Should I be clearing the scriptSig of other inputs as 0x00?</p>
<p>Thanks for any guidance!</p>

CC BY-SA 4.0
2024-09-27T17:20:14.097Z
Maybe this could help:
https://github.com/bitcoinjs/bitcoinjs-lib/blob/34e1644b5fb60055793ec3078f2e4f48b2648ca6/ts_src/psbt.ts#L1593
bordalix
<transactions><transaction-verification><transaction-input><input-script><sighash>
Hash-Preimage Construction for a two p2pkh Input transaction?
SalvaZ
# Hash-Preimage Construction for a two p2pkh Input transaction?

(Asked by: username_0 on 2024-09-27)

When verifying a transaction, a Bitcoin node rebuilds the hash-preimage used to create each signature.
After serializing the hash-preimage for each input, the next step is to double-hash it and verify the signature for each input using the corresponding public key.
My question is: what is the exact serialized hash-preimage for each input? I haven't found clear guidance on how this should be constructed.
For example, take this transaction:
https://mempool.space/es/tx/592ea2010ec259252959b6047e227ceac6ffd0dbbe20bad71248960c18a5d890
This transaction uses the SIGHASH_ALL flag, meaning that each signature signs all inputs and outputs.
Should the hash-preimage include the scriptPubKey of every input, or only the scriptPubKey of the input being signed, leaving the other inputs with an empty script (0x00)?
Example of My Attempt:
Here is my attempt to construct the hash-preimage for signing 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

When I try to construct the preimage for just one input (with the second input's scriptSig 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

Am I correctly understanding how to set the scriptPubKey for non-signed inputs in the hash-preimage? Should I be clearing the scriptSig of other inputs as 0x00?
Thanks for any guidance!

**Tags:** transactions, transaction-verification, transaction-input, input-script, sighash

> **Comments:**
> **username_1**: Maybe this could help: https://github.com/bitcoinjs/bitcoinjs-lib/blob/34e1644b5fb60055793ec3078f2e4f48b2648ca6/ts_src/psbt.ts#L1593  

---

## 2 answers

### Answer by username_2 on 2024-10-17. Score: 2

Yes, you are correct. When serializing the transaction for signing, the input you are targeting is set to the ScriptPubKey of the UTXO it is trying to spend, while the other inputs are left empty, This means that for an N input transaction, you would need to create N different serializations.


---

### Answer by username_0 on 2024-10-23. Score: 1

Thanks username_2!
After testing, I have managed to get the answer: for a two P2PKH-input transaction, the Hash-preimage has the following structure (for the first input):
01000000 - Version
02 - Number of inputs
c43959452301573767dc4c3430de6230a0c573414b204b74079d3a6a467e0f29 - First UTXO txid
00000000 - First UTXO index
1976a914df638f7f911f97a3dd764c1eaffebdc932ba6fe288ac - scriptPubKey of first UTXO
ffffffff - Sequence for first input
c43959452301573767dc4c3430de6230a0c573414b204b74079d3a6a467e0f29 - Second UTXO txid
01000000 - Second UTXO index
00 - Empty scriptSig for second input (set to 0x00)
ffffffff - Sequence for second input
01 - Number of outputs
1027000000000000 - Amount in satoshis
1976a9140983b6a0b7885957f8aa8ffb5de0d365c663b40088ac - Output's scriptPubKey
5fb93000 - Locktime
01000000 - SIGHASH_ALL flag

For the second input, the hash-preimage will set the first input scriptPubKey to 0x00 and the second input scriptPubKey to the corresponding scriptPubKey.


---

