Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How Bitcoin works

Grant Ammons
February 11, 2014

How Bitcoin works

This is a talk I gave at Philly.rb about Bitcoin. I went into the mechanics of how the protocol works, followed by a real-world example of moving bitcoins between wallets. In one of the transactions, I used the bitcoin-ruby client to create a wallet and send money from it.

Grant Ammons

February 11, 2014
Tweet

More Decks by Grant Ammons

Other Decks in Programming

Transcript

  1. P2P Network Alice Sue Walt Bob Mary Jim Miner Joe

    Miner Dave Miner Amy Miner Tom
  2. Signing Alice I, Bob, am giving alice 5 BTC. 5

    BTC to you, Alice! <signed, Bob> Bob <pubkey> <privkey> <pubkey> <privkey> Mary Jim Walt
  3. > require 'bitcoin'
 => true
 > key = Bitcoin::generate_key
 =>

    ["ab378a05", "04ad070e2c"]
 > pubkey = key[1]
 > address = Bitcoin::pubkey_to_address(pubkey)
 => "14vWNjD38DBxpVcwsJgy4V4URKYoDzaeBB" Boom, new wallet
  4. Transactions 1231 1232 1233 TX id 1234 TX id Inputs:

    Bob:10 BTC Prev output: Tx 1233 Outputs: Bob: 5 BTC Mary: 5 BTC Inputs: John: 7 BTC (Tx 1231) Outputs: Bob: 10 BTC John: 3 BTC (Tx 1232)
  5. P2P Network Alice Sue Walt Bob Mary Jim Miner Joe

    Miner Dave Miner Amy Miner Tom
  6. Proof-of-work require 'digest/md5'
 tx_details = 'hashed tx details'
 nonce =

    0
 hashed = Digest::MD5.hexdigest(tx_details + nonce.to_s)
 while hashed[0..5] != '000000'
 nonce += 1
 hashed = Digest::MD5.hexdigest(tx_details + nonce.to_s)
 end > nonce
 => 8788482
 > hashed
 => "000000513d90dae10c23e4a5e49a00fb"
  7. {
 "hash":"bb8b9b8a57c5ec90e11831a5f8d79128b6a9428bd5605488a8674b36fd9cc6ba",
 "ver":1,
 "vin_sz":1,
 "vout_sz":2,
 "lock_time":0,
 "size":226,
 "in":[
 {
 "prev_out":{


    "hash":"5732fdfb5c319e8836e7d9f64cc1778dd73a1378e3ffed1758394dcc7514f974",
 "n":0
 },
 "scriptSig":"3045022100b8c5056689da7c9e1464b0a..."
 }
 ],
 "out":[
 {
 "value":"3.70000000",
 "scriptPubKey":"OP_DUP OP_HASH160 105cdfe… OP_EQUALVERIFY OP_CHECKSIG"
 },
 {
 "value":"5.00000000",
 "scriptPubKey":"OP_DUP OP_HASH160 a1d5d018… OP_EQUALVERIFY OP_CHECKSIG"
 }
 ]
 }
  8. new_tx = build_tx do |t|
 # input from previous transaction


    t.input do |i|
 i.prev_out prev_tx i.prev_out_index 1
 i.signature_key key
 end
 # sending back to bitcoin-qt t.output do |o|
 o.value 500000000 # 5 BTC
 o.script {|s| s.recipient "mzfBp7iyyAPDGgCxks3DwmzeYF8znMgGvU" }
 end
 end
 puts new_tx.to_json
  9. {
 "hash":"a8ee1848ddb89399c043f1913c4efebfa93325d5899a12eb5ac92c82cdba3103",
 "ver":1,
 "vin_sz":1,
 "vout_sz":1,
 "lock_time":0,
 "size":224,
 "in":[
 {
 "prev_out":{


    "hash":"bb8b9b8a57c5ec90e11831a5f8d79128b6a9428bd5605488a8674b36fd9cc6ba",
 "n":1
 },
 "scriptSig":"304502207659addc..."
 }
 ],
 "out":[
 {
 "value":"5.00000000",
 "scriptPubKey":"OP_DUP OP_HASH160 d1f9008a85f3b5d682bc1558aac95970e46c633b OP_EQUALVERIFY OP_CHECKSIG"
 }
 ]
 }