Sapio Programming 101
Stateful Smart Contracts for Bitcoin with OP_CTV
Who what where why
Jeremy Rubin: bitcoin researcher & developer
Judica: a new org devoted to bitcoin r&d
Sapio: a new paradigm for bitcoin smart contracts
Where: VR
Why: so that bitcoin fixes this
Background
BIP-119 OP_CTV:
Simple Covenants for Bitcoin
BIP-119 CheckTemplateVerify Summary
This talk is only on Sapio, a CTV contracting language.
What if No CTV?
We have a CTV Emulation Oracle!
CTV is just this but no trusted servers.
This Talk is All About Concrete Examples
My Goals:
Get you excited to use Sapio.
Get feedback on how you’d use it.
Teach you the very basics.
Non Goals:
Cover everything exciting.
This is your 101 class.
Liquid: A Contract Parable
Federation Managed Funds Script
11 <watchman keys> 15 CHECKMULTISIG NOTIF 4032 CSV <backup keys> CHECKMULTISIG ENDIF
Federation Managed Funds MiniScript
11 <watchman keys> 15 CHECKMULTISIG NOTIF 4032 CSV <backup keys> CHECKMULTISIG ENDIF
or_c(multi(11, <15 watchman keys>), and_v(multi(2, <3 backup keys>), older(4032)))
Federation Managed Funds MiniScript Policy
11 <watchman keys> 15 CHECKMULTISIG NOTIF 4032 CSV <backup keys> CHECKMULTISIG ENDIF
or_c(multi(11, <15 watchman keys>), and_v(multi(2, <3 backup keys>), older(4032)))
or(thresh(11, <15 watchman keys>), and(thresh(2, <3 backup keys>), older(4032)))
Federation Managed Funds MiniScript Policy Sapio
Sapio → Policy → Miniscript → Script
Sapio:
Policy: or(thresh(3,pk(A),pk(B),pk(C),pk(D)),and(thresh(2,pk(E),pk(F),pk(G)),older(4199029)))
MiniScript: andor(multi(2,E,F,G),older(7672384),multi(3,A,B,C,D))
Script: 2 x('E') x('F') x('G') 3 OP_CHECKMULTISIG
OP_NOTIF
3 x('A') x('B') x('C') x('D') 4 OP_CHECKMULTISIG
OP_ELSE
x('751240') OP_CHECKSEQUENCEVERIFY
OP_ENDIF
Backup Recovery .
Federation Spend .
Federation Spend
Backup Recovery .
Backup Recovery ,
Federation Spend .
Backup Recovery .
Recap:
Recent Liquid Bug
I
A
M
A
B
L
O
C
K
C
H
A
I
N
S
H
O
R
T
CTV to the Rescue
G
same as before
Backup Operator (Looks Familiar)
Policy: or(thresh(3,pk(A),pk(B),pk(C),pk(D)),and(thresh(2,pk(E),pk(F),pk(G)),older(4199029)))
MiniScript: andor(thresh_m(2,E,F,G),older(7672384),thresh_m(3,A,B,C,D))
Script:
2 x('E') x('F') x('G') 3 OP_CHECKMULTISIG
OP_NOTIF 3 x('A') x('B') x('C') x('D') 4 OP_CHECKMULTISIG
OP_ELSE x('751240') OP_CHECKSEQUENCEVERIFY OP_ENDIF
FederatedPegIn
Policy:
or( and( thresh(2,pk(E),pk(F),pk(G)), txtmpl(H(BackupOperator))),
thresh(3,pk(A),pk(B),pk(C),pk(D)))
MiniScript:
or_i(and_v(txtmpl(H(BackupOperator)),thresh_m(2,E,F,G)),thresh_m(3,A,B,C,D))
Script:
OP_IF
x(H(BackupOperator)) OP_CHECKTEMPLATEVERIFY OP_DROP
2 x('E') x('F') x('G') 3 OP_CHECKMULTISIG
OP_ELSE
3 x('A') x('B') x('C') x('D') 4 OP_CHECKMULTISIG
OP_ENDIF
11/15
2/3
Peg In
UTXO
Peg Out
UTXO
Backup
2/3
& 4 weeks
11/15
Peg Out
UTXO
Backup
UTXO
11/15
2/3
Peg In
UTXO
Peg Out
UTXO
Backup
2/3
& 4 weeks
11/15
Peg Out
UTXO
Backup
UTXO
D.R.Y. a little harder
don’t repeat yourself,
don’t repeat yourself,
don’t repeat yourself,
don’t repeat yourself...
Why DRY?
Helper Function
Class
Factory
State
Machine
What else...
can we do?
TIC
TAC
TOE
What else...
can we do?
should
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
C
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
CANCELED
Options Contracts, CoinJoins,
Wallet Vaults, Fundraisers,
Lightning Network, Games,
Payment Pools, Wills,
Escrows, BMM Chains,
Congestion Control,
Subscriptions, and more….
So much to do…
so little time
Instead lets go deep!
Boring is the new Exciting:
Fees
Estimation
We can estimate how much fees are needed presently.
But fee rates may change by the time our contract goes out!
Simple Contract
ctv(h)
utxo
1 BTC
Tx h
Simple Contract: CPFP
ctv(h)
utxo
1 BTC
Tx h
Some TX
0.001 fee
0.999 BTC
Simple Contract: CPFP
ctv(h)
utxo
1 BTC
Tx h
Some TX
0.001 fee
2.001 BTC
3 BTC
Simple Contract: Gas/Anchor Output
ctv(h)
utxo
0 BTC
Tx h
1 BTC
Simple Contract: Gas/Anchor Output
ctv(h)
utxo
0
BTC
Tx h
1 BTC
Some TX
0.001 fee
2.001 BTC
2 BTC
Simple Contract: Conditionalizing
ctv(h)
utxo
0
BTC
Tx h
1 BTC
ctv(h)
utxo
1
BTC
Tx h
Simple Contract: Gas/Anchor Input
ctv(h)
utxo
1 BTC
Tx h
0.001 BTC
Simple Contract: Conditionalizing
ctv(h)
utxo
1 BTC
Tx h
0.001 BTC
ctv(h)
utxo
1 BTC
Tx h
How to Pick a Fee Method
Composability
Sapio is a lot like React
signal
Refundable
UTXO
1 day
refund
UTXO
receive
UTXO
0
0
11/15
2/3
Peg In
UTXO
Peg Out
UTXO
Backup
2/3
& 4 weeks
11/15
Peg Out
UTXO
Backup
UTXO
11/15
Refundable
UTXO
1 day
refund
UTXO
0
0
0
Fees (Again)
11/15
2/3
Peg In
UTXO
Peg Out
UTXO
Backup
2/3
& 4 weeks
11/15
Peg Out
UTXO
Backup
UTXO
11/15
Refundable
UTXO
1 day
refund
UTXO
0
0
11/15
2/3
Peg In
UTXO
Peg Out
UTXO
Backup
2/3
& 4 weeks
11/15
Peg Out
UTXO
Backup
UTXO
11/15
Refundable
UTXO
1 day
refund
UTXO
0
0
0
2/3
Peg In
UTXO
Backup
2/3
& 4 weeks
11/15
Peg Out
UTXO
Backup
UTXO
11/15
Refundable
UTXO
0
0
11/15
2/3
Peg In
UTXO
Peg Out
UTXO
Backup
2/3
& 4 weeks
11/15
Peg Out
UTXO
Backup
UTXO
11/15
Refundable
UTXO
1 day
refund
UTXO
0
0
0
Fee TX
fee
change
2/3
Peg In
UTXO
Backup
2/3
& 4 weeks
11/15
Peg Out
UTXO
Backup
UTXO
11/15
Refundable
UTXO
0
0
Fee TX
fee
change
⚠ ATOMIC ⚠
⚠ ATOMIC ⚠
2/3
Peg In
UTXO
Backup
2/3
& 4 weeks
11/15
Peg Out
UTXO
Backup
UTXO
11/15
Refundable
UTXO
0
0
Fee TX
fee
change
Fee TX
fee
change
⚠ ATOMIC ⚠
2/3
Peg In
UTXO
Backup
2/3
& 4 weeks
11/15
Peg Out
UTXO
Backup
UTXO
11/15
Refundable
UTXO
0
0
Fee TX
fee
change
Fee TX
fee
change
⚠ ATOMIC ⚠
2/3
Peg In
UTXO
Backup
2/3
& 4 weeks
11/15
Peg Out
UTXO
Backup
UTXO
11/15
Refundable
UTXO
0
0
Fee TX
fee
change
Fee TX
fee
change
Fee TX
fee
more
change
Traits & Composability
Sapio is a lot like React
1000+ Slides on Sapio Contract Design
Left Intentionally Missing
Sapio Programming 102 Syllabus
What’s Next for Sapio?
What’s Next for Sapio?
Tweet @JeremyRubin for Alpha access to Sapio!
Interested getting paid to work on it? DM for details...
Sapio Q&A