P2MS - Pay To Multisig

There are two ways of creating a multisig transaction: Pay-to-multisig, or a Pay-to-script-hash. currently we will focus on the P2MS or what we refet to as Bare mutisig or Raw multisig

P2MS enables multiple parties to agree to a transaction before it can be spent.

This is a script where M out of N possible signers must provide a valid signature for the funds to be spent.

For example, in a 2-of-3 multisig, any two of the three signers can authorize a transaction.

The visualization below allows you to see how this works:

Multisig Setup:Of
Alice

Alice

Bob

Bob

2 of 2 Multisig Wallet

Why is this limited to 3 participants?

As you can see from the visualization above, you have a threshold of 3, which is because standard P2MS is limited to a maximum of 3 public keys.

Standardness VS Validity

1. Locking (ScriptPubKey)

To create a raw P2MS script, include the following in the ScriptPubKey:

text
1 
2M
3<PubKey1>
4<PubKey2>
5<PubKeyN>
6N
7  
  • M: Number of required signatures
  • Public keys of all participants
  • N: Total number of public keys
  • OP_CHECKMULTISIG opcode

Example: Click on Output 1 of the transaction below to view the scriptPubKey for a 2-of-3 multisig transaction.

Example 2-of-3 Multisig Transaction
Inputs ()
Outputs ()

2. Unlocking (ScriptSig)

To spend bitcoin from a P2MS ScriptPubKey, the spenders must provide the required number of valid signatures.

For a 2-of-3 multisig, the unlocking script look like this

text
1 
2OP_0
3<Signature1>
4<Signature2>
5  

Click on Input 1 of the transaction to view the ScriptSig created to unlock the multisig output we gave before

Unlocking a 2-of-3 Multisig Output
Inputs ()
Outputs ()

Why is OP_0 Used in ScriptSig ?

OP_0 is used as a dummy value in scriptSig to address the "off-by-one" bug in the OP_CHECKMULTISIG opcode, which mistakenly removes an additional item from the stack. By inserting OP_0 at the start as a placeholder we prevent this bug from affecting signature verification.

Order of Signatures in P2MS

Providing signatures in the correct order is highly recommended for efficiency. If signatures are out of order, the script will still validate, but it may require more computational steps, which will slow down the verification process.

P2MS Signature Order Efficiency