Trying to understand the Bitcoin script walk-through

published Dec 15, 2013 05:55   by admin ( last modified Dec 16, 2013 04:54 )

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.
  1. <pubKey>
  2. <sig>
OP_DUP Top stack item is duplicated.
  1. <pubKey>
  2. <pubKey>
  3. <sig>
OP_HASH160 Top stack item is hashed.
  1. <pubHashA>
  2. <pubKey>
  3. <sig>
<pubKeyHash> Constant pubKeyHash is pushed onto the top of the stack,.
  1. <pubKeyHash>
  2. <pubHashA>
  3. <pubKey>
  4. <sig>
OP_EQUALVERIFY Equality is checked between the top two stack items. If not equal, script returns false and transaction is invalidated
  1. <pubKey>
  2. <sig>
OP_CHECKSIG .Signature is checked for the two remaining stack items.
true Empty.