3

I'm quoting this example of BIP65

BIP 65 has this bitcoin script code

 IF
    <now + 3 months> CHECKLOCKTIMEVERIFY DROP
    <Lenny's pubkey> CHECKSIGVERIFY
    1
 ELSE
    2
 ENDIF
 <Alice's pubkey> <Bob's pubkey> 2 CHECKMULTISIG

And with this bitcoin scriptSig is possible unlock the transaction

  • Now with this ScriptSig 0 <Alice's signature> <Bob's signature> 0
  • after 3 months with this ScirpSig 0 <Alice/Bob's signature> <Lenny's signature> 1

But I don't understand this part of If-then-else

ELSE
    2

When is it executed and what I don't understand inside the if-then-else condition?

  • 1
    Your ELSE loop was indented inside the IF condition, which is not the case in BIP 65. I have edited it to reflect the correct script since you have quoted that example. – Ugam Kamat Oct 3 at 11:17
4

Bitcoin uses a stack based language due to which the logical condition that will execute the IF or ELSE statements comes prior to the operator IF or ELSE. Let's take both your cases one by one and see how it gets executed on the stack.

Case 1: ScriptSig: 0 <Alice's signature> <Bob's signature> 0

  • The stack starts at 0 <Alice's signature> <Bob's signature> 0
  • Since 0 is on top of the stack, the ELSE condition will execute and drop the 0
  • number 2 is pushed on the stack
  • Stack is now 0 <Alice's signature> <Bob's signature> 2
  • script outside the IF/ELSE statement is pushed to the stack
  • Stack is now 0 <Alice's signature> <Bob's signature> 2 <Alice's pubkey> <Bob's pubkey> 2 CHECKMULTISIG
  • This is a normal 2-of-2 Multi-sig operation that will execute by verifying both Alice's and Bob's signature to the public keys

Case 2: ScriptSig: 0 <Alice/Bob's signature> <Lenny's signature> 1

  • The stack starts at 0 <Alice/Bob's signature> <Lenny's signature> 1

  • Since 1 is on top of the stack, the IF statement will execute and drop the 1

  • <now + 3 months> CHECKLOCKTIMEVERIFY is pushed to the stack and verified whether 3 months have passed. If yes, the execution will continue.

  • DROP operator drops the TRUE on top of the stack if verification is successful

  • <Lenny's pubkey> CHECKSIGVERIFY is pushed to the stack

  • Stack is now 0 <Alice/Bob's signature> <Lenny's signature> <Lenny's pubkey> CHECKSIGVERIFY

  • CHECKSIGVERIFY will consume the top 2 items of the stack. It will check whether Lenny's signature matches Lenny's pubkey. If yes, the execution will continue.

  • Stack is now 0 <Alice/Bob's signature>
  • 1 is pushed to the stack
  • Stack is now 0 <Alice/Bob's signature> 1
  • Script outside the IF/ELSE statement is pushed to the stack
  • Stack is now 0 <Alice/Bob's signature> 1 <Alice's pubkey> <Bob's pubkey> 2 CHECKMULTISIG
  • This is now a normal 1-of-2 multisig operation and will execute if Alice or Bob provides a valid signatures to their pub key.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.