Skip to content

Instantly share code, notes, and snippets.

@Sjors
Last active March 24, 2018 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sjors/09c13dc440d453356db29a7e20951800 to your computer and use it in GitHub Desktop.
Save Sjors/09c13dc440d453356db29a7e20951800 to your computer and use it in GitHub Desktop.
Anatomy of a bitcoin transaction

footer: 2018-03-24 - Code Block London - sjors@sprovoost.nl - @provoost on Twitter slidenumbers: true autoscale: false

Bitcoin's Moving Parts

  • Coins
  • Digital signatures
  • Chain of blocks
  • Proof of work
  • Censorship resistance
  • Anatomy of a Transaction

The Problem

Alice emails Bob: "I give you 2 BTC"

  1. Why is it worth anything?
  2. What did Bob get that Alice gave up?

^ 1. Because people are willing to exchange it for money and other things of value. ^ 2. A coin

[.build-lists: true]


Coins

  • Alice has two 1.5 BTC coins1

  • Alice emails Bob:

    I spend my coins, and create:

    • 2 BTC coin for Bob; and
    • 1 BTC coin for Alice

The Problem

Alice emails Bob: "I give you 2 BTC"

  1. Why is it worth anything?
  2. What did Bob get that Alice gave up?
  3. How does Alice prove she owned the coin?

Digital signatures

  • Private key: big random number
  • Public key: point on curve2

inline

  • private key can encrypt or sign message

^ In Bitcoin the message is public, so only signed, not encrypted

^ Exchanging public keys can be a hassle, e.g. SSL certificate chains or GPG key signing parties...


Digital signatures

  • Coin is signed message: "Only Alice can spend me" 3
  • Who's Alice?
  • Bitcoin address derived from public key 4
  • Alice is whoever can reveal public key and sign a message with private key

The Problem

Alice emails Bob: "I give you 2 BTC"

  1. Why is it worth anything?
  2. What did Bob get that Alice gave up?
  3. How does Alice prove she owned the coin?
  4. What if Alice sends Carol the same coins?

^: 4. Instead of email, let's use a blockchain


[fit] The Blockchain


A Blockchain is a chain of blocks -- Peter Todd


A Blockchain is a database with virtue -- Chris DeRose


The Blockchain

Things we care about to solve problem (4):

  • Publish all transactions, and in which order
  • Ensure everyone can see all transactions

right, 50%

^ Merkle trees: each block tied to its transactions, and the previous block

^ Image Source: https://blog.bitmex.com/graphical-illustration-of-a-bitcoin-block/


The Problem

Alice publishes: "I give Bob my 2 BTC"

  1. Why is it worth anything?
  2. What did Bob get that Alice gave up?
  3. How does Alice prove she owned the coin?
  4. What if Alice sends Carol the same coins?
  • it's on the blockchain so everyone can see it
  • what if there's many different blockchains?

The Problem

Alice publishes: "I give Bob my 2 BTC"

  1. Why is it worth anything?
  2. What did Bob get that Alice gave up?
  3. How does Alice prove she owned the coin?
  4. What if Alice sends Carol the same coins?
  • it's on the blockchain so everyone can see it
  • what if there's many different blockchains?

Solution 1: use a regulator

  • declare existence of conflicting blockchains fraud
  • give regulator(s) access to your database
  • add additional crypto magic
  • brand it "blockchain inspired technology"

Solution 2: Proof-of-Work

Convince someone to:

  • do useless work
  • which uniquely commits to transaction data
  • in exchange for coins.

Throw dice on a piece of paper with the transaction list.


^ Imagine seeing the list of transactions in the video background


Proof-of-Work

  • sha("000001 | Alice sends Bob 2 BTC, etc") = 0fed9a90
  • sha("000002 | Alice sends Bob 2 BTC, etc") = e7c54529
  • sha("000003 | Alice sends Bob 2 BTC, etc") = 6c48ab21
  • sha("855453 | Alice sends Bob 2 BTC, etc") = 000005e6
  • N leading zeros -> X kWh * £0.10 -> £... per block 5
  • Miner creates coin out of thin air which Alice & Bob consider valid

^ Difficulty adjusted so it's always ~10 mins

^ Uselessness is a feature. Specialized (ASIC) hardware can't be used for anything else, unlike CPU's which a government could have a large supply off that normally does other things.


The Problem

Alice publishes: "I give Bob my 2 BTC", miner burns electricity to attest this.

  1. Why is it worth anything?
  2. What did Bob get that Alice gave up?
  3. How does Alice prove she owned the coin?
  4. What if Alice sends Carol the same coins?
  • it's on the blockchain so everyone can see it
  • what if there's many different blockchains?

^ Many different blockchains require bribing miner.


The Problem

Alice publishes: "I give Bob my 2 BTC", miner burns electricity to attest this.

  1. Why is it worth anything?
  2. What did Bob get that Alice gave up?
  3. How does Alice prove she owned the coin?
  4. What if Alice sends Carol the same coins?
  5. What if someone doesn't like Bob?6

Censorship resistance

  • Miners compete, fees offer extra incentive
  • P2P: transactions and blocks route around censorship
  • Fungability: all transacions (should) look the same
  • Lot's of problems left to solve

inline


Anatomy of a Bitcoin Transaction

Alice publishes: "I give Bob my 2 BTC"

  • Bob is whoever can reveal public key corresponding to Bob's address and sign a message with private key:

Alice publishes: OP_DUP OP_HASH160 <Bob's address> OP_EQUALVERIFY OP_CHECKSIG

Bob spends: <Bob's signature><Bob's pubkey>

^ Remember that Bitcoin address is derived from public key


Anatomy of a Bitcoin Transaction

Alice publishes: "I give Bob my 2 BTC"

  • Bob is whoever can reveal public key corresponding to Bob's address and sign a message with private key:

Alice publishes: OP_DUP OP_HASH160 <Bob's address> OP_EQUALVERIFY OP_CHECKSIG

Bob spends: <Bob's signature><Bob's pubkey>


Script stack

  • <Bob's signature>
  • <Bob's pubkey>
  • OP_DUP
  • OP_HASH160
  • <Bob's address>
  • OP_EQUALVERIFY
  • OP_CHECKSIG

Script stack

  • <Bob's signature>
  • <Bob's pubkey>
  • <Bob's pubkey>
  • OP_HASH160
  • <Bob's address>
  • OP_EQUALVERIFY
  • OP_CHECKSIG

Script stack

  • <Bob's signature>
  • <Bob's pubkey>
  • <Bob's address>
  • <Bob's address>
  • OP_EQUALVERIFY
  • OP_CHECKSIG

Script stack

  • <Bob's signature>
  • <Bob's pubkey>
  • OP_CHECKSIG

Script stack

  • true

Other opcodes

right, 80%

  • OP_CHECKMULTISIG : N of M sigs
  • OP_CHECKLOCKTIMEVERIFY: HODL
  • OP_IF / OP_ELSE
  • OP_RETURN: 80 bytes spam 7
  • OP_NOP: does nothing (yet!)

Thanks

Slides: slideshare.net/provoost

Blog: medium.com/provoost-on-crypto

PGP: ED9B DF7A D6A5 5E23 2E84 5242 57FF 9BDB CC30 1009

Footnotes

  1. UTXO: Unspent Transaction (tx) Output

  2. secp256k1

  3. The new coins say "Only Bob can spend me" and the change says "Only Alice can spend me".

  4. Q&A tip: why not just use the public key as a Bitcoin address?

  5. Any scarce resource will do, but the simplest known combination with the right properties is electricity + specialized chips + hashing.

  6. i.e. wants to stop the transaction

  7. e.g. Rare Pepe trades: CryptoKitties, but with frogs

Display the source blob
Display the rendered blob
Raw
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment