Skip to content

Instantly share code, notes, and snippets.

@afk11
Last active April 30, 2017 11:45
Show Gist options
  • Save afk11/cb276f78b7407773e76e81f6d5b62d4f to your computer and use it in GitHub Desktop.
Save afk11/cb276f78b7407773e76e81f6d5b62d4f to your computer and use it in GitHub Desktop.

Signing scripts with logical operators

Full script

HASH160 DUP <R-HASH> EQUAL
IF
    "24h" CHECKSEQUENCEVERIFY
    2DROP
    <Alice's pubkey>
ELSE
    <Commit-Revocation-Hash> EQUAL
    NOTIF
        "Timestamp" CHECKLOCKTIMEVERIFY DROP
    ENDIF
    <Bob's pubkey>
ENDIF
CHECKSIG

Mutually exclusive paths

Exclusive pathway 1: IF:true

OP_HASH160 OP_DUP e86beced2d630b4a883f55f61ab3a6e7ad0af11b OP_EQUAL
OP_IF
7017 OP_CHECKSEQUENCEVERIFY OP_2DROP 03c8f1054d85d3fb4da80cb2269e2fbd89e653acf2a6941d5af81aa0f6ba3b741e
OP_ELSE
OP_NOTIF
OP_ENDIF
OP_ENDIF
OP_CHECKSIG

Exclusive pathway 2: IF:false NOTIF:false

OP_HASH160 OP_DUP e86beced2d630b4a883f55f61ab3a6e7ad0af11b OP_EQUAL
OP_IF
OP_ELSE
d46ebee54ff720a92aa97a28ecfd3f4706da0b59 OP_EQUAL
OP_NOTIF
7017 OP_CHECKLOCKTIMEVERIFY OP_DROP
OP_ENDIF
039797e1c03ecc637cda522eff95a0ce3a0c12b533305eabadd7b51af45e23e578
OP_ENDIF
OP_CHECKSIG

Exclusive pathway 3: IF: false NOTIF: true

OP_HASH160 OP_DUP e86beced2d630b4a883f55f61ab3a6e7ad0af11b OP_EQUAL
OP_IF
OP_ELSE
d46ebee54ff720a92aa97a28ecfd3f4706da0b59 OP_EQUAL
OP_NOTIF
OP_ENDIF
039797e1c03ecc637cda522eff95a0ce3a0c12b533305eabadd7b51af45e23e578
OP_ENDIF
OP_CHECKSIG

Sign Steps

For coins protected on this script using P2SH, we can try break down the parts required for signing:

Path 1: IF:true

OP_HASH160 OP_DUP e86beced2d630b4a883f55f61ab3a6e7ad0af11b OP_EQUAL
This EQUAL is true, so the correct preimage is required

7017 OP_CHECKSEQUENCEVERIFY 
We know the current time.

OP_2DROP 
Doesn't require any action

03c8f1054d85d3fb4da80cb2269e2fbd89e653acf2a6941d5af81aa0f6ba3b741e
OP_CHECKSIG
Sign with Alices key

ScriptSig

Preimage(e86beced2d630b4a883f55f61ab3a6e7ad0af11b) Signature(AliceKey)

Path 2: IF:false NOTIF:false

OP_HASH160 OP_DUP e86beced2d630b4a883f55f61ab3a6e7ad0af11b OP_EQUAL
This EQUAL is false, so any incorrect value can be provided

d46ebee54ff720a92aa97a28ecfd3f4706da0b59 OP_EQUAL
This EQUAL is false, so any incorrect value can be provided for NOTIF to enter:

7017 OP_CHECKLOCKTIMEVERIFY 
We know the current time. 

OP_DROP
Doesn't require any action

039797e1c03ecc637cda522eff95a0ce3a0c12b533305eabadd7b51af45e23e578
OP_CHECKSIG
Sign with Bobs key

ScriptSig

"" "" Signature(BobKey)

Path 3: IF: false NOTIF: true

OP_HASH160 OP_DUP e86beced2d630b4a883f55f61ab3a6e7ad0af11b OP_EQUAL
This EQUAL is false, so any incorrect value can be provided

d46ebee54ff720a92aa97a28ecfd3f4706da0b59 OP_EQUAL
This EQUAL is true, so the correct value must be provided:

039797e1c03ecc637cda522eff95a0ce3a0c12b533305eabadd7b51af45e23e578
OP_CHECKSIG
Sign with Bobs key

ScriptSig

"" Preimage(d46ebee54ff720a92aa97a28ecfd3f4706da0b59) Signature(BobKey)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment