<pre><code>OP_IF
  OP_3 
OP_ELSE
  OP_IF
    lock1 OP_CLTV OP_DROP OP_2
  OP_ELSE
    lock2 OP_CLTV OP_DROP OP_1
  OP_ENDIF
OP_ENDIF
pub1 pub2 pub3 OP_3 OP_CHECKMULTISIG
</code></pre>

<p>some pseudocode:</p>

<pre><code>var x = stack.pop ( );  // take the last item from stack and remove it
if ( x != 0 )
{
  stack.push ( 3 );  // push the number '3' to the stack
}
else
{
  var y = stack.pop ( ); // take next item
  if ( y != 0 )
  {
    stack.push ( locktime1 ); // push value
    stack.compare_top_value_with_timestamp_and_abort_if_not_valid ( );
    stack.pop ( ); // remove locktime1 if passed
    stack.push ( 2 );
  }
  else
  {
    stack.push ( locktime2 ); // push value
    stack.compare_top_value_with_timestamp_and_abort_if_not_valid ( );
    stack.pop ( ); // remove locktime2 if passed
    stack.push ( 1 );
  }
}
stack.push ( pub1 );
stack.push ( pub2 );
stack.push ( pub3 );
stack.push ( 3 );
stack.check_multi_sig ( );
</code></pre>

CC BY-SA 3.0
2017-07-22T22:18:48.410Z
Thank you very much may I ask why you start with OP_IF and not OP_3 and put the pub keys at the end pf the script? I just want to underastand not mechanically copy paste. Some // comments on steps would be useful.
skydanc3r
CC BY-SA 3.0
2017-07-22T22:52:04.437Z
Answer updated. I can not explain the things better. You have to understand how m-of-n multisig scripts work
amaclin
CC BY-SA 3.0
2017-07-23T15:50:17.320Z
If i would like to state exactly which two pubkeys cand spend can this be done? For example after lock1 expires pub1 and pub3 can spend and after lock2 just pub2?
skydanc3r
CC BY-SA 3.0
2017-07-23T15:55:34.073Z
@skydanc3r, my script works as described in starting topic. Any two after locktime1 or even one of them after locktime2. Of course, it is possible to imagine another logic and create another script
amaclin
CC BY-SA 3.0
2017-07-23T17:27:14.140Z
asked again in a different question with full description in topic. https://bitcoin.stackexchange.com/questions/57005/create-an-arbitrary-script-with-two-locktimes-and-p2sh
skydanc3r
CC BY-SA 3.0
2017-07-22T21:35:16.273Z
2017-07-22T22:49:32.220Z
2017-07-22T22:49:32.220Z
amaclin
<p>Besides the 520 bytes consensus rule limit, is there a limit on how many of each OP_IF, OP_ELSE and OP_ENDIF can be used in a single script?</p>

<p>For example, a company that has 3 people in its purchase department would like to make a fast purchase if all people in the department agree and slow it down in case not everybody agrees, but not make it impossible in the final:</p>

<ul>
<li><p>Alice, Bob and Carol store funds in a P2SH address. Initially, all 3/3 of signatures are required initially.</p></li>
<li><p>After some time, (OP_CHECKLOCKTIMEVERIFY) just two of them can spend (2/3 signatures required).</p></li>
<li><p>After more time, (first OP_CHECKLOCKTIMEVERIFY + more time), any one can spend (1/3 signatures required).</p></li>
</ul>

<p>How would a script like this look like?</p>

CC BY-SA 3.0
2017-07-22T22:03:10.243Z
I don't think there's any rule on how many you can have. They do need to be matched, though.
Nick ODell
CC BY-SA 3.0
2017-07-22T22:38:35.177Z
there is limit of 201 total operations in script as far as I remember.
amaclin
<script><p2sh>
How many IF/ELSE/ENDIF can be used in a script?
skydanc3r
# How many IF/ELSE/ENDIF can be used in a script?

(Asked by: username_0 on 2017-07-22)

Besides the 520 bytes consensus rule limit, is there a limit on how many of each OP\_IF, OP\_ELSE and OP\_ENDIF can be used in a single script?

For example, a company that has 3 people in its purchase department would like to make a fast purchase if all people in the department agree and slow it down in case not everybody agrees, but not make it impossible in the final:

* Alice, Bob and Carol store funds in a P2SH address. Initially, all 3/3 of signatures are required initially.
* After some time, (OP\_CHECKLOCKTIMEVERIFY) just two of them can spend (2/3 signatures required).
* After more time, (first OP\_CHECKLOCKTIMEVERIFY + more time), any one can spend (1/3 signatures required).

How would a script like this look like?

**Tags:** script, p2sh

> **Comments:**
> **username_1**: I don't think there's any rule on how many you can have. They do need to be matched, though.  
> **username_2**: there is limit of 201 total operations in script as far as I remember.  

---

## 1 answers

### Answer by username_2 on 2017-07-22. Score: 1 | Accepted answer

OP_IF
  OP_3 
OP_ELSE
  OP_IF
    lock1 OP_CLTV OP_DROP OP_2
  OP_ELSE
    lock2 OP_CLTV OP_DROP OP_1
  OP_ENDIF
OP_ENDIF
pub1 pub2 pub3 OP_3 OP_CHECKMULTISIG

some pseudocode:
var x = stack.pop ( );  // take the last item from stack and remove it
if ( x != 0 )
{
  stack.push ( 3 );  // push the number '3' to the stack
}
else
{
  var y = stack.pop ( ); // take next item
  if ( y != 0 )
  {
    stack.push ( locktime1 ); // push value
    stack.compare_top_value_with_timestamp_and_abort_if_not_valid ( );
    stack.pop ( ); // remove locktime1 if passed
    stack.push ( 2 );
  }
  else
  {
    stack.push ( locktime2 ); // push value
    stack.compare_top_value_with_timestamp_and_abort_if_not_valid ( );
    stack.pop ( ); // remove locktime2 if passed
    stack.push ( 1 );
  }
}
stack.push ( pub1 );
stack.push ( pub2 );
stack.push ( pub3 );
stack.push ( 3 );
stack.check_multi_sig ( );

> **Comments:**
> **username_0**: Thank you very much may I ask why you start with OP_IF and not OP_3 and put the pub keys at the end pf the script? I just want to underastand not mechanically copy paste. Some // comments on steps would be useful.  
> **username_2**: Answer updated. I can not explain the things better. You have to understand how m-of-n multisig scripts work  
> **username_0**: If i would like to state exactly which two pubkeys cand spend can this be done? For example after lock1 expires pub1 and pub3 can spend and after lock2 just pub2?  
> **username_2**: @username_0, my script works as described in starting topic. Any two after locktime1 or even one of them after locktime2. Of course, it is possible to imagine another logic and create another script  
> **username_0**: asked again in a different question with full description in topic. https://bitcoin.stackexchange.com/questions/57005/create-an-arbitrary-script-with-two-locktimes-and-p2sh  

---

