When sending bitcoins to someone, you actually lock funds using that other person’s key. But this is not the only way of locking funds on a block chain. Funds can be locked according to the rules of any Smart Contract. A very basic form of such a contract is a Script Puzzle. This article explains how such a puzzle works and how you can create one yourself.

Prior to bitcoin 0.6 it was very hard to send funds to a script instead of an address. With the introduction of P2SH it got way easier. Now you can create your puzzle, generate an address of it and let other people send money to your puzzle address from within their favourite wallet. Such a puzzle address starts with a 3 instead of a 1 on the bitcoin blockchain, but can be used just like any other address.

Great, let’s make our hands dirty and generate such a puzzle address! To avoid going through the entire process of building the transaction from scratch, we’ll use a tool that let’s us specify the puzzle, test it and generate the address to pay to. PeerScript labs does exactly what we need!

PeerScript Labs

Important note: The scripts described in this article do not require signing of transaction outputs, making it possible for an attacker to redirect the outputs to his own address. This article assumes that the reader is aware of the risks of locking funds using simple scripts. For the sake of simplicity, transaction malleability is not covered in this article.

Building the Script Puzzle

PeerScript labs has one solved and many yet to solve puzzles specified. Let’s take the solved one for the sake of simplicity: x+5=6. Paying to the address generated from this puzzle would mean that anyone knowing that 1+5=6 is able to claim the funds sent to that address. To generate the address we first need to translate the puzzle into bitcoin’s Script language. When opening PeerScript labs’ puzzle definition file you’ll find the translation:

Lock script: app/puzzles/all.js line 14

A bitcoin script is executed from left to right. The script’s solution (“unlock” script) is always evaluated before the script it solves (“lock” script). So in this case the solved script would result in the execution of:

OP_1 OP_5 OP_ADD OP_6 OP_EQUAL

Let’s execute this in our head, it goes as follows: memorize number 1 (OP_1), memorize number 5 (OP_5), add the last two values we memorized and memorize the result being 6 (OP_ADD), now memorize another number 6 (OP_6) and check if last two memorized values are equal (OP_EQUAL) which is the case, the puzzle is solved!

Generating the puzzle address

Great, we solved the puzzle so we are sure we can claim the funds being locked by it. Let’s now generate the address that we can send funds to be locked by this puzzle.

Open the debug console in you browser (CTRL+SHIFT+I for firefox, chrome and opera). This console shows the P2SH address that you can pay to on Peercoin’s testnet with a standard testnet client.