0
Hi I am learning bitcoin and learning how scripting works and how to use P2PKH. I was wondering how does OP_CHECKSIG work. I want to understand what is the data that the private key signs to create the digital signature itself?
Is there a simple explanation which illustrates how the digital signature is constructed for verification in OP_CHECKSIG? I understand that the Opcode uses the ECDSA algorithm for verifying the signature but I want to understand how the signature is generated for verification?
Pardon me if this is a basic question. Thanks
Thanks that was great! @JBaczuk Also is the way the signature is created differs for different sighash type? Is there a documentation where I can find where and how these transactions are signed? Or if you know could you explain say something like a SIGHASH_ALL|SIGHASH_ANYONECANPAY. How the Unlocking Script signs the inputs? – shubham saxena – 2019-08-30T06:50:09.323
Yes, see https://bitcoin.org/en/transactions-guide#signature-hash-types
– JBaczuk – 2019-08-30T20:25:26.770So from what I have read. The input contains the previous transaction hash, the index and the ScriptPubkey of this input that we are consuming? But is this ScriptPubkey a temporary value which is replaced by some other script? @JBaczuk – shubham saxena – 2019-09-09T10:25:38.080
No, the input does not contain the scriptpubkey, the scriptsig does while you are signing it, then it is replaced with the actual scriptsig which is a script that satisfies the scriptpubkey. The scriptsig will contain the transaction signature, this is why it is temporarily filled with the scriptpubkey – JBaczuk – 2019-09-09T12:10:57.853
So that means, I have to replace the scriptPubKey of the input with the actual signature appended with the hash code type and then consequently remove the last 4 bytes? This will be the input that is hashed twice and signed over by the private key? @JBaczuk – shubham saxena – 2019-09-09T12:19:09.137
Sorry I might have been confusing, I'd recommend following the link in my answer for how to sign a tx – JBaczuk – 2019-09-09T12:38:53.950
So I did go through the link. My question is more towards verifying a transaction signature rather than how to sign a tx. @JBaczuk – shubham saxena – 2019-09-09T12:40:17.560
Gotcha, see https://bitcoin.stackexchange.com/a/32308/60443
– JBaczuk – 2019-09-09T12:44:55.290Okay So i found the bug, It was related to storing the data in little Endian format! – shubham saxena – 2019-09-10T10:14:33.320
That will bite you while you're learning for sure – JBaczuk – 2019-09-10T11:57:07.147