Trying to understand the Bitcoin script walk-through
There is a script walk-through at the bitcoin wiki. It is not all that easy to understand, since it could be formatted and presented better. I show here the current table as it looks on the Wiki, and then further down my hopefully improved version. Here is what the original looks like:
Checking process:
Stack | Script | Description |
---|---|---|
Empty. | <sig> <pubKey> OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG | scriptSig and scriptPubKey are combined. |
<sig> <pubKey> | OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG | Constants are added to the stack. |
<sig> <pubKey> <pubKey> | OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG | Top stack item is duplicated. |
<sig> <pubKey> <pubHashA> | <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG | Top stack item is hashed. |
<sig> <pubKey> <pubHashA> <pubKeyHash> | OP_EQUALVERIFY OP_CHECKSIG | Constant added. |
<sig> <pubKey> | OP_CHECKSIG | Equality is checked between the top two stack items. |
true | Empty. | Signature is checked for top two stack items. |
So, in order to understand this, there are a couple of things that are kind of obscure in that table, namely:
- In the column "Stack" the top of the stack is the last item, which means that since the text wraps in the field, if there are multiple lines, the bottom-most item is at the top of the stack
- In the column "Script", it is actually the same script in all rows, just less and less of it.
- The comments seem to be written as to what the state is before the script part executes
It might be cleaner to do like this instead:
- Let the topmost item in the "Stack" cell in a row, be at the top of the stack, and number them.
- Only write the part of the script that is executed in a row. List entire script before the table for clarity
- Description should descibe what happens when the code in the row, operates on the stack in the row
Then it becomes like this:
Checking process:
Entire script is:
<sig> <pubKey> OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
Stack | Script | Description |
---|---|---|
Empty. | <sig> <pubKey> | scriptSig and scriptPubKey are pushed onto the stack. |
|
OP_DUP | Top stack item is duplicated. |
|
OP_HASH160 | Top stack item is hashed. |
|
<pubKeyHash> | Constant pubKeyHash is pushed onto the top of the stack,. |
|
OP_EQUALVERIFY | Equality is checked between the top two stack items. If not equal, script returns false and transaction is invalidated |
|
OP_CHECKSIG | .Signature is checked for the two remaining stack items. |
true | Empty. |