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

Bitcoin and pycoin

Bitcoin and pycoin

The pycoin library is an open-source Python-based Bitcoin and alt-coin utility library. It includes a couple general-purpose command line tools "ku" for manipulating keys and "tx" for manipulating transactions. This talk will cover bitcoin transactions and investigate some of the features of these command-line tools.

Avatar for Richard Kiss

Richard Kiss

January 11, 2017

Other Decks in Technology

Transcript

  1. Richard Kiss • Python Developer since 1999 • learned about

    bitcoin in 2011 • released pycoin in 2013 • contributed to Mastering Bitcoin book by Andreas M. • worked for ChangeTip, Paxos, a few others
  2. Cryptographic Hash • turns an arbitrary amount of data into

    a fixed amount of data in an unpredictable way • reasonably quick to calculate • can't find an input with a given hash • fixed-length identifier for data blobs
  3. Example Hash: SHA256 • sha256(00) => 6e340b9cffb37a989ca544e6bb780a2c7890 1d3fb33738768511a30617afa01d • sha256(01)

    => 4bf5122f344554c53bde2ebb8cd2b7e3d160 0ad631c385a5d7cce23c7785459a • one bit different input, totally different output
  4. What is a transaction? • "a movement of coins from

    inputs to outputs" • emptying coin rolls (TxIn) into a pile of coins • then rerolling the piles of coins into new coin rolls (TxOut) 10000 unlocked from address A 20000 unlocked from address B 20000 unlocked from address C Lock 39000 to address D Lock 10500 to address E (TxIns) (TxOuts)
  5. What is a UTXO? • Unspent Txransaction Output (or "Spendable")

    • A TxOut that has not been spent yet by its recipient • "a roll of coins that can be spent" • a potential input
  6. UTXO Set • bitcoin nodes maintain a table of every

    TxOut that is available to be spent • inputs of new transactions are compared against this set • a transaction express changes to this database UTXO id Amount 1abcdef,0 1000 satoshis 1abcdef,1 50000 satoshis 1fedcba,0 2947191 satoshis 19de6fab,4 222944 satoshis ... ...
  7. Create a TX Example • I want to spend 50000

    satoshis (quantum units) • find UTXOs that "I control" with total value exceeding that amount • bundle them together on the left side 12500 satoshis 22000 satoshis 20000 satoshis (TxIns) (TxOuts)
  8. Create a TX: Example 2 • send the 50000 satoshis

    to the recipient • put aside some of the extra as change • the rest pays the miner transaction fee (payment) 50000 satoshis (change) 4000 satoshis 12500 satoshis 22000 satoshis 20000 satoshis (TxIns) (TxOuts) (implicit fee of 500 satoshis)
  9. Where do coins come from? • transactions just reassign coins

    from one UTXO to another • a tx consumes spendables and creates new ones • but where do they originally come from? Thin air?
  10. Yes • miners create new blocks • this is a

    very difficult task • "difficult" • each block contains a reward of new coins
  11. UTXO Protections • what does it mean "to control coins"?

    • UTXOs are protected by a script • the script is a "puzzle" written in the bitcoin vm • to spend the money, you need to "solve the puzzle"
  12. Script Types • Pay To Public Key Hash (P2PKH, very

    common) • Pay To Script Hash (P2SH, fairly common) • Pay To Public Key (common but now obsolete) • some other puzzle you can create in script language! (rare)
  13. What is a bitcoin address? • "the location of a

    safe that can hold UTXOs" • anyone who knows the location can put their coins into it • like an email address: anyone who has it can deposit emails into your inbox • send me some spam!! 1KissFDVu2wAYWPRm4UGh5ZCDU9sE9an8T
  14. What is a P2PKH? • most (>90%) transactions are of

    this form • a payment to a bitcoin address that starts with 1 • UTXO protected by a script corresponding to that bitcoin address • digital signature required to release funds
  15. Signatures • most scripts require knowledge of at least one

    secret key to solve them • signatures prove: • you know the secret key that the script expresses • you approve of the transaction • so it approves the transaction without giving away the secret key
  16. What is a secret key? • bitcoin uses ECDSA public

    key crypto, using the secp256k1 curve • in this system, a secret key is just a number! • between 1 and 115792089237316195423570985008687907852 837564279074904382605163141518161494336 (about 1.15x10^77)
  17. How are address created? • create a private key (pick

    a number!) • generate the corresponding public key • hash the public key • encode it into base 58
  18. Create an address (1) • private key: choose a number

    between 1 and 1.15x10^77 • private key: 1 • corresponding public key, formatted as a SEC compressed value, is (in hex) 0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798
  19. Create an address (2) • ripemd160(sha256(0279be667ef9dcbbac55a06295ce 870b07029bfcdb2dce28d959f2815b16f81798)) = 751e76e8199196d454941c45d1b3a323f1433bd6 •

    prepend a magic 00 byte ("bitcoin address") • Base58CheckEncode(00751e76e8199196d454941c4 5d1b3a323f1433bd6) = 1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH
  20. Base 58?? • binary (base 2) 0, 1, 101, 1111011011

    • decimal (base 10): 5, 19, 377, 187910 • hexadecimal (base 16): 0, c, 1f, 7ae7, 4fff971 • base 58: 1jU87esNT8pD
  21. Base58 Encoding • Similar to Base64 Encoding, but with visually-

    similar characters removed • Lowercase L and capital i look similar • o, O, and 0 are similar • 26 lowercase + 26 uppercase + 10 numerals = 62 • remove 0, i, o, and l = 58 characters
  22. Key Management • best practices say don't reuse bitcoin addresses

    • each address has a private key • that could be a lot of secrets to store if you have 1000 addresses...
  23. BIP32 • defines a way to generate one secret, and

    derive other secrets from it • derive as many as you want using "paths" • public and private seeds, 111 characters • xprv9s21ZrQH143K...ujZ2WXGTux1X2k9Krdtq • xpub661MyMwAqR...j82pVShWu9curWmb2zy
  24. BIP32 paths • Paths can be nested, like folders ROOT

    0 1 0 1 2 1LWEWFFQGx6NanoRpw85Rjsupg3Ndts5BM 1MxG8754Rv3HNcx4cAukHEHgNcabaxoYrP 1KhDMM4NA9S7LH9EEbzHm6W39E9PWahdXh 131aXT52YjF83pbEYUX5gywh9cBE5KpWWs 1DaPDpTBQBk9qedW1uqFcFc2sGXzYCmsBG
  25. BIP32 paths • m/0 • m/0/0 • m/1234/1234 • generate

    as many sub-secrets as you need • only need to store the paths from the root node
  26. Hardened BIP32 Paths • Denoted by an apostrophe ' or

    an H • m/0' instead of m/0
 m/0H instead of m/0 • Means "must use PRIVATE seed to create this child" • Useful for generating addresses that can only have come from the owner of the seed
  27. pycoin • general purpose bitcoin library • open source •

    transactions and keys • fully implements the bitcoin scripting vm (!)
  28. Wanna help? contact me here or at http://github.com/richardkiss/pycoin • API

    needs to be improved and standardized • more tests • PEP8 • documentation badly needed!!