CS 251 Fall 2021 cs 251 stanford edu
- Slides: 37
CS 251 Fall 2021 (cs 251. stanford. edu) Bitcoin Scripts and Wallets Dan Boneh Note: HW#1 is posted on the course web site. Due Oct. 4.
Recap: the Bitcoin blockchain genesis block H version prev time bits nonce Tx root (4 bytes) H (32 bytes) (4 bytes) (32 bytes) 80 bytes coinbase Tx BH 3 BH 2 BH 1 coinbase Tx prev H prev … Tx root
Tx sequence View the blockchain as a sequence of Tx (append-only) … coinbase Tx Tx cannot be erased: mistaken Tx ⇒ locked or lost of funds
Tx structure (non-coinbase) input[0] input[1] input[2] inputs output[0] output[1] (segwit) witnesses (4 bytes) input: Tx. ID = H(Tx) Tx. ID out-index Script. Sig seq 32 byte hash value Script. PK 8 bytes 4 byte index program ignore (excluding witnesses) output: (part of input) program locktime earliest block # that can include Tx #BTC = value/108
Example null locktime Tx 1: (funding Tx) 2 input value Script. PK UTXO 1 5 value Script. PK 0 UTXO 2 UTXO: unspent Tx output Tx 2: (spending Tx) Tx. ID 1 identifies a UTXO Script. Sig output UTXO 3 UTXO 4 0
Example Tx 1: (funding Tx) 2 input Script. PK UTXO 1 UTXO: unspent Tx output Tx 2: (spending Tx) Tx. ID 1 identifies a UTXO Script. Sig ☓ 5 Script. PK null locktime 0 UTXO 2 output UTXO 3 UTXO 4 0
Validating Tx 2 program from funding Tx: under what conditions can UTXO be spent Miners check (for each input): 1. The program Script. Sig | Script. PK returns true 2. Tx. ID | index is in the current UTXO set program from spending Tx: proof that conditions are met 3. sum input values ≥ sum output values After Tx 2 is posted, miners remove UTXO 2 from UTXO set
Transaction types: (1) P 2 PKH pay to public key hash Alice want to pay Bob 5 BTC: • step 1: Bob generates sig key pair (pk. B, sk. B) �Gen() • step 2: Bob computes his Bitcoin address as addr. B �H(pk. B) • step 3: Bob sends addr. B to Alice • step 4: Alice posts Tx: Point to Alice’s UTXO Script. PKB: input 7 BTC UTXOB for Bob 5 Script. PKB UTXOA for Alice (change) 2 Script. PKA DUP HASH 256 <addr. B> EQVERIFY CHECKSIG 0
Transaction types: (1) P 2 PKH pay to public key hash “input” contains Script. Sig that authorizes spending Alice’s UTXO • example: Script. Sig contains Alice’s signature on Tx � miners cannot change Script. PKB (will invalidate Alice’s signature) Point to Alice’s UTXO Script. PKB: input 7 BTC UTXOB for Bob 5 Script. PKB UTXOA for Alice (change) 2 Script. PKA DUP HASH 256 <addr. B> EQVERIFY CHECKSIG 0
Transaction types: (1) P 2 PKH Later, when Bob wants to spend his UTXO: Txspend: Tx. ID 0 points to UTXOB Script. Sig. B <sig> <pk. B> output create a Txspend output 0 (authorizes spending UTXOB) <sig> = Sign(sk. B, Tx) where Tx = (Txspend excluding all Script. Sigs) (SIGHASH_ALL) Miners validate that Script. Sig. B | Script. PKB returns true
Transaction types: (2) P 2 SH: pay to script hash (pre Seg. Wit in 2017) Payer specifies a redeem script (instead of just pkhash) Usage: (1) Bob publishes hash(redeem script) �Bitcoint addr. (2) Alice sends funds to that address in funding Tx (3) Bob can spend UTXO if he can satisfy the script Script. PK in UTXO: HASH 160 <H(redeem script)> EQUAL Script. Sig to spend: <sig 1> <sig 2> … <sign> <redeem script> payer can specify complex conditions for when UTXO can be spent
P 2 SH Miner verifies: (1) <Script. Sig> Script. PK = true �spending Tx gave correct script (2) Script. Sig = true �script is satisfied
Example P 2 SH: multisig Goal: spending a UTXO requires t-out-of-n signatures Redeem script for 2 -out-of-3: (chosen by payer) <2> <PK 1> <PK 2> <PK 3> <3> CHECKMULTISIG threshold hash gives P 2 SH address Script. Sig to spend: (by payee) <0> <sig 1> <sig 3> <redeem script> (in the clear)
Abstractly … Multisig address: addr = H(PK 1, PK 2, PK 3, 2 -of-3) Tx 1: (funding Tx) Tx 2: (spending Tx) input 7 BTC 5 addr UTXOB for Bob 2 Script. PKA 0 UTXOA for Alice (change) input: UTXO, sig 1, sig 3, PK 1, PK 2, PK 3, 2 -of-3 output 0
Example Bitcoin scripts
Protecting assets with a co-signatory Alice stores her funds in UTXOs for addr = 2 -of-2(PKA, PKS) PKA Alice spending Tx PKS is this Alice custody server yep, it’s me post Tx with <sig. A> <sig. S> on Tx ⇒ theft of Alice’s SKA does not compromise BTC
Escrow service Alice wants to buy a backpack for 0. 1�from merchant Bob Goal: Alice only pays after backpack arrives, but can’t not pay addr = 2 -of-3(PKA, PKB, PKJ) post payment of 0. 11� to addr (UTXOA) Alice PKA want backpack for 0. 1� once see Tx on chain mail backpack arrives send <sig. A> on Tx: UTXOA�(PKB: 0. 1, PKA: 0. 01) Bob Judge PKB PKJ redeem using <sig. A> <sig. B> on Tx
Escrow service: a dispute (1) Backpack never arrives: (Bob at fault) Alice gets her funds back with help of Judge and a Tx: ( UTXOA �PKA , sig. A, sig. Judge ) [2 -out-of-3] (2) Alice never sends sig. A: (Alice at fault) Bob gets paid with help of Judge as a Tx: ( UTXOA �PKB , sig. B, sig. Judge ) [2 -out-of-3] (3) Both are at fault: Judge publishes <sig. Judge> on Tx: ( UTXOA �PKA: 0. 05, PKB: 0. 05, PKJ: 0. 01 ) Now either Alice or Bob can execute this Tx.
Cross Chain Atomic Swap Alice has 5 BTC, Bob has 2 LTC (Lite. Coin). They want to swap. Want a sequence of Tx on the Bitcoin and Litecoin chains s. t. : • either success: Alice has 2 LTC and Bob has 5 BTX, • or failure: no funds move. Swap cannot get stuck halfway. Goal: design a sequence of Tx to do this. solution: programming proj #1 ex 4.
Managing crypto assets: Wallets
Managing secret keys Users can have many PK/SK: • one per Bitcoin address, Ethereum address, … Wallets: • Generates PK/SK, and stores SK, • Post and verify Tx, • Show balances
Managing lots of secret keys Types of wallets: • cloud (e. g. , Coinbase): cloud holds secret keys … like a bank. • laptop/phone: Electrum, Meta. Mask, … client stores secret keys • hardware: Trezor, Ledger, … • paper: print all sk on paper • brain: memorize sk (bad idea) • Hybrid: non-custodial cloud wallet (using threshold signatures) Not your keys, not your coins … but lose key ⇒ lose funds
Simplified Payment Verification (SPV) How does a client wallet display Alice’s current balances? • Laptop/phone wallet needs to verify an incoming payment • Goal: do so w/o downloading entire blockchain (366 GB) SPV: block header Tx root (1) download all block headers (56 MB) (2) Tx download: • wallet �server: list of my wallet addrs (Bloom filter) • server �wallet: Tx involving addresses + Merkle proof to block header.
Simplified Payment Verification (SPV) Problems: (1) Security: are BH the ones on the blockchain? Can server omit Tx? • Electrum: download block headers from ten random servers, optionally, also from a trusted full node. List of servers: electrum. org/#community (2) Privacy: remote server can test if an addr belongs to wallet We will see better light client designs later in the course (e. g. Celo)
Hardware wallet: Ledger, Trezor, … End user can have lots of secret keys. How to store them ? ? ? Hardware wallet (e. g. , Ledger Nano X) - connects to laptop or phone wallet using Bluetooth or USB - manages many secret keys - Bolos OS: each coin type is an app on top of OS - PIN to unlock HW (up to 48 digits) - screen and buttons to verify and confirm Tx
Hardware wallet: backup • ECDSA public key
On Ledger When initializing ledger: • user asked to write down the 24 words • each word encodes 11 bits (24 × 11 = 268 bits) • list of 2048 words in different languages (BIP 39)
Example: English word list ⋮ save list of 24 words
Crypto Steel Careful with unused letters …
On Ledger When initializing ledger: • user asked to write down the 24 words • each word encodes 11 bits (24 × 11 = 268 bits) • list of 2048 words in different languages (BIP 39) Beware of “pre-initialized HW wallet” • 2018: funds transferred to wallet promptly stolen
How to securely check balances? •
Idea 2: (used in HD wallets) • kpub does not reveal sk 1, sk 2, … computed from kpub
Paper wallet (be careful when generating) Bitcoin address = base 58(hash(PK)) base 58 = a-z. A-Z 0 -9 without {0, O, l, 1} signing key (cleartext)
Managing crypto assets in the cloud How exchanges store assets
Hot/cold storage Coinbase: holds customer assets Design: 98% of assets (SK) are held in cold storage (98%) hot wallet (2%) SKhot 2% of assets customers
Problems Can’t prove ownership of assets in cold storage, without accessing cold storage: • To prove ownership (e. g. , in audit or in a proof of solvency) • To participate in proof-of-stake consensus Solutions: • Keep everything in hot wallet (e. g, Anchorage) • Proxy keys: keys that prove ownership of assets, but cannot spend assets
END OF LECTURE Next lecture: consensus
- Cs 251 stanford
- Cs251 stanford solutions
- Stanford cs 251
- Highwire stanford edu
- Cs 3214 fall 2021
- Ucla fall 2021
- Cos 318 princeton
- Cos318 fall 2021
- Tgfoa fall conference 2021
- Edu.sharif.edu
- A vida tem tristezas mil hinário
- Cs 251
- Anupam saxena
- 15-251
- 251 del 2000
- 15 251
- 15-251
- Aae 251
- Aecp program army
- Edu.ro programe scolare 2020-2021
- Advantages of micro teaching
- Karel the robot learns java
- Stanford
- Stanford modellen
- Sherlock stanford
- Mark kasevich
- Susie phillips stanford cheerleader
- Stanford 10 test
- Subtes binet
- Hussein al baya
- Stanford exploration project
- Ann dohn stanford
- Cs321 stanford
- Cs 148
- Abhinav agarwal stanford
- Diego ongaro stanford
- Cs155
- Stanford prison experiment right to withdraw