Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: evanlinjin on August 30, 2020, 08:01:01 AM



Title: Help with understanding Bitcoin script.
Post by: evanlinjin on August 30, 2020, 08:01:01 AM
I'm going through the book Mastering Bitcoin, and I have a query regarding the section on "Using Flow Control in Scripts".

So, there is an example of a redeem script as follows:

Code:
IF
  <Alice's PK> CHECKSIG
ELSE
  <Bob's PK> CHECKSIG
ENDIF

I'm wondering why it is structured in this manner, instead of the following:

Code:
IF
  <Alice's PK>
ELSE
  <Bob's PK>
ENDIF
CHECKSIG

Would the latter work? And if so, would it be the better version as it saves space by one opcode?

Thank you all in advance.


Title: Re: Help with understanding Bitcoin script.
Post by: pooya87 on August 30, 2020, 09:06:37 AM
yes the second one works and essentially does the same thing as the first one but in a more efficient way. this example is probably like this to show how OP_IFs work otherwise there is a better way of defining this particular script which is using multi-sig.
Code:
1 <Alice's PK> <Bob's PK> 2 CHECMULTIKSIG

usually the conditional scripts like this are more complex and each expression could contain multiple pubkeys with a multi-sig OP or sometimes locktimes or other complications which has to be defined like the first script.
examples:
Code:
IF
  <Alice's PK> <Joe's PK> CHECMULTIKSIG
ELSE
  <Bob's PK> CHECKSIG
ENDIF
Code:
IF
  <somedatetimevalue> CheckLocktimeVerify DROP <Alice's PK> CHECKSIG
ELSE
  2 <Alice's PK> <Bob's PK> 2 CHECKMULTISIG
ENDIF


Title: Re: Help with understanding Bitcoin script.
Post by: evanlinjin on August 30, 2020, 10:19:08 AM
Awesome! Thank you for your help with clarifying :)