I'm doing my homework by reading the C++ code of the 0.1.0 release of Bitcoin available here. I'm interested in better understanding how to validate transaction 1 of block 170. This transaction has one input, that points to output 0 of transaction 0 of block 9.
script.cpp
contains a function EvalScript()
. This function is called to execute the following script:
---- Bloc 170 / Transaction 1 / Input 0 / ScriptSig -----
PUSH 304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901
---- Separator -----
OP_CODESEPARATOR
---- Bloc 9 / Transaction 0 / Output 0 / ScriptPubKey -----
PUSH 0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3
OP_CHECKSIG
I had a look at the code for OP_CHECKSIG
. This code creates a script that is going to replace the one that lies in ScriptSign of input 0 of a copy of transaction 1 of block 170 later in another function SignatureHash()
. The first steps are pretty straightforward:
valtype& vchSig = stacktop(-2);
valtype& vchPubKey = stacktop(-1);
This puts the signature (DER format) followed by the hashtype (0x01, so SIGHASH_ALL) in vchSig
, and the public key prefixed with compression flag (0x04, so no compression) in vchPubKey
.
CScript scriptCode(pbegincodehash, pend);
This creates a script that is nothing more than ScriptPubKey of Block 9 / Transaction 0 / Output 0.
And then...:
// Drop the signature, since there's no way for a signature to sign itself
scriptCode.FindAndDelete(CScript(vchSig));
I don't understand this. How could the signature that lies in ScriptSig of input 0 of transaction 1 of block 170 be found in ScriptPubKey of output 0 of transaction 0 of block 9, a previous block?
Of course, I know that this very transaction is a very basic one considering how complex a Bitcoin transaction can be, as explained by Satoshi himself here. But what would be a transaction where this code would be useful?