Skip to content

Basic Script Template Execution Example (Basic P2ST address)

Real transaction found at here

Context

In this example the nexa address: nexa:nqtsq5g5x624x7w9m6jmash47sjlvw3wu40v2dt433lx2ztr contains some Nexa which are being sent to two new addresses. To validate the spending of the nexa from a utxo in this address we need to deconstruct the Locking Script, deconstruct the Unlocking Script, and execute the Template Script as outlined in the Script Template Execution Model section above.

Execution

1. Deconstruct the Locking Script

First we need to deconstruct the Locking Script. The Locking Script for nexa:nqtsq5g5x624x7w9m6jmash47sjlvw3wu40v2dt433lx2ztr is 00511436955379c5dea5bec2f5f425f63a2ee55ec53575.

00 - The first part is the Group ID, in this Locking Script it is 00 indicating no group tokens.

51 - 0x51 is well-known template 1.

14 - size of the Hidden Args Hash 36955379c5dea5bec2f5f425f63a2ee55ec53575 - Hidden Args Hash, 20 bytes indicating it is a Hash160.

No visible args are included in this Locking Script.

2. Deconstruct the Unlocking Script

The Unlocking Script for spending Nexa from the address is 222103ce9399e0ed9a4fa054ed89c31a3c56aaa6c8a3b6b6512cd0cf96ecb644ecfcf7404ba1cddd36304aef46a9dde9f52df8eb36aa6eb74085073ccbde6d615e878fd577898152b6eb10ff5aed1ba673465ab3246ea5157b10a25c6fb85eac47d45d9a

No Template Script included. The Locking Script included a well-known template 1 byte not a Template Hash. The use of a well-known template in the Locking Script dictates that a Template Script is not included in the Unlocking Script.

22 - size of all Contraint Args. 0x22 = 34 in decimal. This tells us the next 34 bytes are all of the Hidden Args. First we need to verify that the hash of the Hidden Args matches the Hidden Args Hash in the Locking Script.

Hash160(2103ce9399e0ed9a4fa054ed89c31a3c56aaa6c8a3b6b6512cd0cf96ecb644ecfcf7) == 36955379c5dea5bec2f5f425f63a2ee55ec53575

The hash matches so we can continue.

21 - 1 byte, 0x21 = 33 in decimal, this is the size of the first Contraint Arg 03ce9399e0ed9a4fa054ed89c31a3c56aaa6c8a3b6b6512cd0cf96ecb644ecfcf7 - The first arg. Push this value on to the Alt Stack.

End of Constaint Args. Next is the Satisfier Args. Unlike the Hidden Args, each Satisfier Arg is pushed independently. Parse these.

40 - 1 byte, 0x40 = 64 in decimal, this is size of first Satisfier Arg. 4ba1cddd36304aef46a9dde9f52df8eb36aa6eb74085073ccbde6d615e878fd577898152b6eb10ff5aed1ba673465ab3246ea5157b10a25c6fb85eac47d45d9a - The first Satisfier Arg. Push this value on to the Main Stack.

End of Satisfier Args.

3. Push the Visible Args on to the Alt Stack

There were no Visible Args in the Locking Script so we can skip this step.

4. Execute the Template Script.

The stacks should now be ready for Template Script execution and look like the following

Examples

Alt Stack
03ce9399e0ed9a4fa054ed89c31a3c56aaa6c8a3b6b6512cd0cf96ecb644ecfcf7
Main Stack
4ba1cddd36304aef46a9dde9f52df8eb36aa6eb74085073ccbde6d615e878fd577898152b6eb10ff5aed1ba673465ab3246ea5157b10a25c6fb85eac47d45d9a

The Locking Script indicated that the Template Script to be used to evaluate outputs spent from this address is well-known template 1. Well-known template 1 is FROMALTSTACK CHECKSIGVERIFY

Execute the template:

Step 1. FROMALTSTACK
FROMALTSTACK pops the top item from the alt stack and puts it on the top of the main stack.

Alt Stack
Main Stack
03ce9399e0ed9a4fa054ed89c31a3c56aaa6c8a3b6b6512cd0cf96ecb644ecfcf7
4ba1cddd36304aef46a9dde9f52df8eb36aa6eb74085073ccbde6d615e878fd577898152b6eb10ff5aed1ba673465ab3246ea5157b10a25c6fb85eac47d45d9a

Step 2. CHECKSIGVERIFY
CHECKSIGVERIFY pops the top 2 items in the main stack. it expects the top to be a pubkey and the 2nd to be a signature. It then verifies the signature is valid for that pubkey. If it is an invalid signature script execution fails. otherwise the script continues.

Alt Stack
Main Stack

We are at the end of the script and the Main Stack is empty indicating a successful execution and a valid Unlocking Script.