Bitcoin Transaction Signing I wrote a couple of algorithms that perform the ECDSA and also the verification. So I used my algorithm and the RPC call for signing and I got a couple of questions.


Since we have the ephemeral private key which has to be random how can I sign two times the same transaction and get the same signature as Bitcoin RPC does.
What is the hash that should be signed, the whole transaction without the signature, do I have to exclude any fields?
Since we have the ephemeral private key which has to be random how can I sign two times the same transaction and get the same signature as Bitcoin RPC does.


This "ephemeral private key" is known as a nonce. Bitcoin Core, and most other wallet software, use a technique called Deterministic Nonces. Specifically, they use the algorithm defined in RFC 6979 to produce the nonce deterministically. It essentially hashes together the message and the private key being used and uses that hash as the nonce. If you sign the same message with the same private key multiple times, the same nonce will be used and thus you always get the same signature.


  What is the hash that should be signed, the whole transaction without the signature, do I have to exclude any fields?


For non-segwit inputs, the message that is signed is the entire transaction with all other input scripts empty. For the particular input being signed, the scriptPubKey or the redeemScript if there is one, is put in the input script. Then the sighash type is appended to the end. This is hashed twice and that hash is then signed.

For segwit, the message is a bit more complicated and described in BIP 143.