which private key use to Sign this transaction?
When you have a transaction with multiple inputs, each input must be signed. The general format for a transaction is:
Transaction
{version}
{flag (optional)}
{input counter}
{inputs}
{output counter}
{outputs}
{witnesses (optional)}
{lock time}
version
(4 Bytes) - Transaction format version
flag
(2 Byte Array) - Optional flag, if present, must be 0001, which indicates there is witness data in this transaction
input counter
(Variable Length) - Number of inputs in the transaction represented by a Variable Length Integer.
inputs
(based on Input Counter) - List of all transaction inputs which will be spent and which reference unspent transaction outputs from previous transactions.
output counter
(Variable Length) - Number of outputs in the transaction represented by a Variable Length Integer.
outputs
(based on Output Counter) - List of all transaction outputs where the coins will be sent and which will become unspent transaction outputs to be spent in future transactions.
witnesses
(variable) - Optional list of all witnesses only if Flag is present, which are used in transaction validation according to specifications for Segregated Witness.
locktime
(4 Bytes) - If non-zero and sequence numbers are < ffffffff
: it represents either the block height or timestamp when transaction is final.
Inputs
And the general format for each input is:
{previous tx hash}
{previous utxo index}
{scriptSig length}
{scriptSig}
{sequence number}
previous tx hash
(32 Bytes) - Hash of the previous transaction. This is used to lookup the transaction in order to validate that this input can be spent.
previous utxo index
(4 Bytes) - Previous Unspent Transaction Output (UTXO) index. Since transactions can have multiple outputs, in order to specify which output, you can provide a zero-based index.
scriptSig length
(Variable Length) - Length in bytes of the scriptSig.
scriptSig
(based on scriptSig length
)- The first part of the script that is executed before the scriptPubKey from the UTXO. When they are both executed in order, the result must be true in order to be considered a valid spend.
sequence number
(4 Bytes) - Used as a relative lock time if transaction version is >= 2. See BIP68.
You can see that each input requires a scriptSig
, which is where the signature is (unless it is a segwit tx, then it is in the witnesses
field in the transaction above)