Skip to content
Developer Handbook

Beyond P2PKH

On this page

The P2PKH case study locks each output to a public-key hash. BCH can also lock outputs with custom conditions. You can put a program directly in the output or commit to its hash and reveal the program at spend time.

Choose how to place the locking condition

Output typeIn the outputAt spend time
P2PKHA standard locking script containing a public-key hash.Provide the public key and signature.
Pay to Script (P2S)The locking program itself. Since May 2026, most bare locking scripts up to 201 bytes are eligible for standard relay.Provide the data the program expects. The program is visible as soon as the output is created.
Pay to Script Hash (P2SH)A standard locking program containing a 20-byte HASH160 or 32-byte HASH256 of the redeem script.Push the arguments and then the serialized redeem script. CashVM checks the hash and evaluates the redeem script with the arguments.

A P2SH CashAddr can represent the script hash. Paying directly to an arbitrary script requires the script bytecode. For constraints and exceptions, see Pay to Script and P2SH32.

Read a custom script

CashVM reads instructions and data pushes. In 02 aa bb 51, 02 tells CashVM to push the next two bytes as a single item, aabb. Then 51 pushes the number 1. Read the stack top first, as in the payment trace. It now holds [01, aabb]. Trace the P2PKH stack · Open the opcode table

Evaluate the unlocking and locking scripts together with the transaction they authorize.

Use introspection in a covenant

A covenant restricts how its UTXO can be spent. Introspection instructions let its locking script read fields of the spending transaction. For example, the funding UTXO could require that the next transaction's output 0 pay at least 50,000 sats and carry the intended recipient's locking bytecode. Anyone satisfying those conditions could spend it unless the script also requires authorization, such as a signature.

  1. Push 0 and use OP_OUTPUTVALUE to read the value of output 0. Compare it against 50,000 sats.
  2. Push 0 again and use OP_OUTPUTBYTECODE to read the output's locking bytecode. Compare those bytes against the expected recipient script.
  3. Consider every permitted output, change, fee, and use of other inputs. A rule on output 0 alone leaves the rest of the transaction free to change.

OP_INPUTINDEX gives the current input index. OP_UTXOVALUE and OP_UTXOBYTECODE use an input index to read the UTXO it spends. Token introspection can also check category, commitment, and amount. Introspection specification · Where CashScript fits

See VM limits for loops, functions, and input budgets.