TeachMeBitcoin

P2MS ScriptPubKey Structure

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

P2MS ScriptPubKey Structure

The P2MS locking script is a "Template" script. It lists every authorized public key and the threshold required to unlock them. It is one of the longest script types in the Bitcoin protocol.

1. The Locking Script (ScriptPubKey)

A P2MS script follows this format: OP_M [PubKey1] [PubKey2] ... [PubKeyN] OP_N OP_CHECKMULTISIG

2. Threshold Values

The values M and N are represented by small integers (OP_1 through OP_16).

3. The Execution Logic

When the Bitcoin Virtual Machine hits OP_CHECKMULTISIG, it does the following:

  1. It pops the value N from the stack.

  2. It pops N public keys.

  3. It pops the value M.

  4. It pops M signatures.

  5. It iterates through the keys to see if the signatures are valid.

4. Raw Hex Example (2-of-2)

A 2-of-2 multisig script would look like this: 52 [33-byte Key1] [33-byte Key2] 52 ae

Component Hex Code Role
M (Threshold) 0x51 to 0x60 Required signatures
Public Keys Variable Authorized participants
N (Total Keys) 0x51 to 0x60 Total list size
Opcode 0xae The verification logic

In the next section, we will discuss the famous Off-by-One Bug in multisig.

☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!