> For the complete documentation index, see [llms.txt](https://hub.bsvblockchain.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hub.bsvblockchain.org/wiki/bitcoin-script-and-opcodes/script/complex-script-examples.md).

# Complex Script Examples

The following are examples of complex scripts that use Bitcoin's scripting language to create puzzles requiring specific conditions to be met before the locked funds can be spent.

1. Example 1: 2 of 3 Multisignature using PKH in outputs (P2MSH) This example uses the same OP\_CHECKMULTISIG opcode as a simple multisignature transaction however the output is constructed such that it contains only public key hashes rather than public keys. scriptPubKey: TOALTSTACK TOALTSTACK 2 SWAP DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG 5 CHECKMULTISIG scriptSig: 1 This script offers users locking keys in a multisignature script a means to hide the public keys being used from the network until such time as the coin is being spent. Assuming that all parties are using best practice to prevent Address reuse this script protects the keyholders in a case of ECDSA being broken. **Note:** The current version of the scripting engine also includes a bug that requires an additional value to be placed on the stack before the signatures. In this example, OP\_1 has been used to push a 1 to the top of the stack, but in theory any piece of data can be used. This value is consumed by the checking process but is not evaluated. Checking process:

| Stack         | Script                                                                                                                                           | Description                                                          |
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ | -------------------------------------------------------------------- |
| Empty.        | 1 TOALTSTACK TOALTSTACK 2 SWAP DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG | scriptSig and scriptPubKey are combined.                             |
| 1             | TOALTSTACK TOALTSTACK 2 SWAP DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG   | Constants from scriptSig are added to the stack.                     |
| 1 ALTSTACK:   | TOALTSTACK 2 SWAP DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG              | PK3 is pushed to altstack                                            |
| 1 ALTSTACK:   | 2 SWAP DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG                         | PK2 is pushed to altstack                                            |
| 1 2 ALTSTACK: | SWAP DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG                           | 2 is pushed to stack (number of sigs required)                       |
| 1 2 ALTSTACK: | DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG                                | PK1 is brought to front of stack                                     |
| 1 2 ALTSTACK: | HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG                                    | PK1 is duplicated                                                    |
| 1 2 ALTSTACK: | EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG                                            | duplicate PK1 is used to create PKH1                                 |
| 1 2 ALTSTACK: | EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG                                            | PKH1 from ScriptPubKey is pushed to stack                            |
| 1 2 ALTSTACK: | FROMALTSTACK DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG                                                        | EQUALVERIFY test                                                     |
| 1 2 ALTSTACK: | DUP HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG                                                                     | PK2 is moved from ALTSTACK to MAINSTACK                              |
| 1 2 ALTSTACK: | HASH160 EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG                                                                         | PK2 is duplicated                                                    |
| 1 2 ALTSTACK: | EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG                                                                                 | duplicate PK2 is used to create PKH2                                 |
| 1 2 ALTSTACK: | EQUALVERIFY FROMALTSTACK DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG                                                                                 | PKH2 from ScriptPubKey is pushed to stack                            |
| 1 2 ALTSTACK: | FROMALTSTACK DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG                                                                                             | EQUALVERIFY test                                                     |
| 1 2           | DUP HASH160 EQUALVERIFY 3 CHECKMULTISIG                                                                                                          | PK3 is moved from ALTSTACK to MAINSTACK                              |
| 1 2           | HASH160 EQUALVERIFY 3 CHECKMULTISIG                                                                                                              | PK3 is duplicated                                                    |
| 1 2           | EQUALVERIFY 3 CHECKMULTISIG                                                                                                                      | duplicate PK3 is used to create PKH3                                 |
| 1 2           | EQUALVERIFY 3 CHECKMULTISIG                                                                                                                      | PKH3 from ScriptPubKey is pushed to stack                            |
| 1 2           | 3 CHECKMULTISIG                                                                                                                                  | EQUALVERIFY test                                                     |
| 1 2 3         | CHECKMULTISIG                                                                                                                                    | 3 is pushed to stack (number of Pubkeys to check signatures against) |
| true          | Empty.                                                                                                                                           | Multisignature script evaluation is performed                        |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://hub.bsvblockchain.org/wiki/bitcoin-script-and-opcodes/script/complex-script-examples.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
