1 of 72

Sapio Programming 101

Stateful Smart Contracts for Bitcoin with OP_CTV

2 of 72

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

3 of 72

Background

BIP-119 OP_CTV:

Simple Covenants for Bitcoin

4 of 72

BIP-119 CheckTemplateVerify Summary

  • Proposed Op using a hash to specify exact transaction(s) that can spend a coin when the coin is created
  • Scripts can recursively commit to transactions which contain many outputs that also commit to transactions
  • Many Uses:
    • Enables a congestion control functionality
    • CTV + if/else branches → powerful smart contracts!
    • & more!

This talk is only on Sapio, a CTV contracting language.

5 of 72

What if No CTV?

We have a CTV Emulation Oracle!

CTV is just this but no trusted servers.

6 of 72

This Talk is All About Concrete Examples

7 of 72

My Goals:

Get you excited to use Sapio.

Get feedback on how you’d use it.

Teach you the very basics.

8 of 72

Non Goals:

Cover everything exciting.

This is your 101 class.

9 of 72

Liquid: A Contract Parable

10 of 72

Federation Managed Funds Script

11 <watchman keys> 15 CHECKMULTISIG NOTIF 4032 CSV <backup keys> CHECKMULTISIG ENDIF

11 of 72

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)))

12 of 72

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)))

13 of 72

Federation Managed Funds MiniScript Policy Sapio

14 of 72

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 .

15 of 72

Recap:

  • You can write Scripts in Bitcoin
  • Miniscript + Policy make that easier
  • Sapio adds another layer

16 of 72

Recent Liquid Bug

I

A

M

A

B

L

O

C

K

C

H

A

I

N

S

H

O

R

T

17 of 72

CTV to the Rescue

G

same as before

18 of 72

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

19 of 72

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

20 of 72

11/15

2/3

Peg In

UTXO

Peg Out

UTXO

Backup

2/3

& 4 weeks

11/15

Peg Out

UTXO

Backup

UTXO

21 of 72

11/15

2/3

Peg In

UTXO

Peg Out

UTXO

Backup

2/3

& 4 weeks

11/15

Peg Out

UTXO

Backup

UTXO

22 of 72

23 of 72

24 of 72

D.R.Y. a little harder

don’t repeat yourself,

don’t repeat yourself,

don’t repeat yourself,

don’t repeat yourself...

25 of 72

Why DRY?

26 of 72

Helper Function

27 of 72

Class

Factory

State

Machine

28 of 72

29 of 72

30 of 72

What else...

can we do?

31 of 72

TIC

TAC

TOE

32 of 72

33 of 72

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

34 of 72

Options Contracts, CoinJoins,

Wallet Vaults, Fundraisers,

Lightning Network, Games,

Payment Pools, Wills,

Escrows, BMM Chains,

Congestion Control,

Subscriptions, and more….

35 of 72

So much to do…

so little time

36 of 72

Instead lets go deep!

37 of 72

Boring is the new Exciting:

Fees

38 of 72

Estimation

We can estimate how much fees are needed presently.

But fee rates may change by the time our contract goes out!

39 of 72

Simple Contract

ctv(h)

utxo

1 BTC

Tx h

40 of 72

Simple Contract: CPFP

ctv(h)

utxo

1 BTC

Tx h

Some TX

0.001 fee

0.999 BTC

41 of 72

Simple Contract: CPFP

ctv(h)

utxo

1 BTC

Tx h

Some TX

0.001 fee

2.001 BTC

3 BTC

42 of 72

Simple Contract: Gas/Anchor Output

ctv(h)

utxo

0 BTC

Tx h

1 BTC

43 of 72

Simple Contract: Gas/Anchor Output

ctv(h)

utxo

0

BTC

Tx h

1 BTC

Some TX

0.001 fee

2.001 BTC

2 BTC

44 of 72

Simple Contract: Conditionalizing

ctv(h)

utxo

0

BTC

Tx h

1 BTC

ctv(h)

utxo

1

BTC

Tx h

45 of 72

Simple Contract: Gas/Anchor Input

ctv(h)

utxo

1 BTC

Tx h

0.001 BTC

46 of 72

Simple Contract: Conditionalizing

ctv(h)

utxo

1 BTC

Tx h

0.001 BTC

ctv(h)

utxo

1 BTC

Tx h

47 of 72

How to Pick a Fee Method

  • Does your contract have nodes with relative timelocks?
    • You’ll need an Gas/Anchor In/Out for mempool; or
    • Pay high enough fees
  • Do you need TXID Non-Malleability (e.g., for HTLCS)?
    • Prefer Unconditional Gas/Anchor outputs
  • Don’t care about Malleability?
    • Conditional Gas/Anchor Inputs are Nice!
  • Jeremy’s Favorite?
    • Gas/Anchor outputs

48 of 72

Composability

49 of 72

Sapio is a lot like React

50 of 72

51 of 72

signal

Refundable

UTXO

1 day

refund

UTXO

receive

UTXO

0

0

52 of 72

53 of 72

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

54 of 72

Fees (Again)

55 of 72

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

56 of 72

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

57 of 72

2/3

Peg In

UTXO

Backup

2/3

& 4 weeks

11/15

Peg Out

UTXO

Backup

UTXO

11/15

Refundable

UTXO

0

0

58 of 72

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

59 of 72

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 ⚠

60 of 72

⚠ 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

61 of 72

⚠ 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

62 of 72

⚠ 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

63 of 72

Traits & Composability

64 of 72

65 of 72

66 of 72

67 of 72

Sapio is a lot like React

68 of 72

1000+ Slides on Sapio Contract Design

Left Intentionally Missing

69 of 72

Sapio Programming 102 Syllabus

  • Using the Sapio-UX to develop, debug, & use contracts
  • Contracts
    • Secure Wallet Vaults
    • Congestion Control Contracts
  • Integrating Sapio in Applications, APIs & ABIs
  • Advanced Sapio Programming & Safety Analysis
  • Sapio Compiler Architecture
  • Managing Sapio contract state & Watchtowers

70 of 72

What’s Next for Sapio?

71 of 72

What’s Next for Sapio?

  • Improve Language & Polish for Open Sourcing
  • Developing Library of High Quality Contract Components
  • Improving Developer & Ecosystem Tools
  • Work on CTV Deployment
  • Looking for funding/grants :)

Tweet @JeremyRubin for Alpha access to Sapio!

Interested getting paid to work on it? DM for details...

72 of 72

Sapio Q&A