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

Python Libraries for Bitcoin and Ethereum, PyCon SK 2018

Python Libraries for Bitcoin and Ethereum, PyCon SK 2018

Ondrej Sika

March 10, 2018

More Decks by Ondrej Sika

Other Decks in Programming


  1. Python Libraries for Bitcoin & Ethereum Ondrej Sika ondrej@ondrejsika.com PyCon

    SK 2018, Bratislava SK, 10. 3. 2018
  2. Agenda - What is Bitcoin - What is Ethereum -

    RPC, Web3 - Bitcoin libraries - Ethereum libraries
  3. Ondrej Sika - Software engineer at Slush Pool (mining) -

    Bitcoin, Zcash, Ethereum mining backends - Pythoner since 2011 - Bitcoiner since 2013
  4. None
  5. What is Bitcoin?

  6. What is Bitcoin? - Peer to Peer payment network -

    Decentralized currency - Digital gold - Money of internet - Internet of money
  7. How Bitcoin works?

  8. - Wallet is a pair of private and public keys

    - Public key (hash) is your address for receiving money - Private key is for sending your money - Wallets can be mobile apps (eg. Mycelium), desktop apps (eg. Electrum) or hardware wallets (eg. Trezor) Wallet
  9. Transaction - Transaction is signed payment order - Signed by

    sender's private key - To receiver address (public key (hash))
  10. Blockchain - Blockchain is distributed ledger which stores all transactions

    in chronological order - Transactions in blockchain are stored in blocks
  11. Block - Block is a 1MB list of transactions -

    In block header are some meta informations and POW - It is very hard to create block
  12. Proof of Work (POW) - Piece of data which is

    hard to find (time-consuming and costly) - Producing this proof is mining - Way how to slow down block generation - DDOS protection
  13. HD Wallets - Hierarchical deterministic wallets - Support in Trezor,

  14. Interact with Bitcoin

  15. Bitcoind Bitcoin daemon - Full Bitcoin node - Require Blockchain

    (160 GB) - JSON RPC API (HTTP) - CLI
  16. Bitcoind CLI bitcoin-cli getnewaddress 3Kyvxpx3B55q6Hx7JYJahLNi16cEfFajZ5 bitcoin-cli dumpprivkey 3Ky..jZ5 L1XxeiQ3Y4LpeWCSEZonew7oBZoEgxWuDFK6cH9rmjfM Y5FbhZfn

  17. In Python libraries - jsonrpc_requests - btcpy - python-trezor

  18. jsonrpc_requests - Just JSON RPC wrapper for Python - Require

    running bitcoind for every operation - Everything is stored on bitcoind (wallets, …) - https://github.com/gciotta/jsonrpc-requests pip install jsonrpc_requests
  19. jsonrpc_requests example from jsonrpc_requests import Server bitcoind = Server('http://chains.bo:8332', auth=('btcrpc',

    'btc')) address = bitcoind.getnewaddress() privkey = bitcoind.dumpprivkey(address) print(address) print(privkey)
  20. btcpy - Maintained bitcoin library - Support segwit addresses (new

    format) - Implement Wallets, Transactions (doesn't require bitcoind) - https://github.com/chainside/btcpy pip install chainside-btcpy
  21. btcpy - Wallets from btcpy.setup import setup setup('mainnet') from btcpy.structs.crypto

    import PrivateKey privkey = PrivateKey.from_wif('L1Xx..Zfn') pubkey = privkey.pub() print(pubkey.to_address()) print(pubkey.to_segwit_address())
  22. btcpy - HD Wallets from btcpy.setup import setup setup('mainnet') from

    btcpy.structs.hd import ExtendedPrivateKey xprv = ExtendedPrivateKey.decode('xprv9s2..X73') print(xprv.key.pub().to_address()) print(xprv.key.pub().to_segwit_address())
  23. python-trezor - Communicate with Trezor HW - Support all trezor

    API - Wallets, Transactions - Trezor management (eg. pin change) - https://github.com/trezor/python-trezor sudo apt-get install python3-dev git \ cython3 libusb-1.0-0-dev libudev-dev \ pip install trezor
  24. python-trezor from trezorlib.client import TrezorClient from trezorlib.transport import get_transport client

    = TrezorClient(get_transport()) bip32_path = client.expand_path("44'/0'/0'/0/0") address = client.get_address('Bitcoin', bip32_path) print(address) bip32_path = client.expand_path("44'/0'/0'/0/1") address = client.get_address('Bitcoin', bip32_path) print(address)
  25. Trezor XPUB with btcpy from btcpy.setup import setup setup('mainnet') from

    btcpy.structs.hd import ExtendedPublicKey xpub = ExtendedPublicKey.decode('xpub6Cn..QfL') print(xpub.derive('./0/0').key.to_address()) print(xpub.derive('./0/1').key.to_address())
  26. You can do almost everything with those 3 libraries.

  27. You can do ... - Access Bitcoind - Use wallets

    - Work with transactions - Block parsing You can't do: - mining - you need a special mining HW
  28. None
  29. What is Ethereum?

  30. What is Ethereum? - Also currency, network, … - Decentralized

    computer - Smart contract platform
  31. What is a smart contract? Smart contrat are scripts which

    can hold any informations, run any code (supported by Turing-complete EVM - ethereum virtual machine). Smart contract are commanded by users and also by other smart contracts. You can use smart contract for: - Crowdfundings - Property registries - Company shares
  32. Interact with Ethereum

  33. jsonrpc_requests You can also use requests, because Ethereum has JSON

    RPC interface.
  34. jsonrpc_requests example from jsonrpc_requests import Server eth = Server('http://eth.xsika.cz:8545') print(eth.eth_blockNumber())

  35. web3.py Web3.py is Python implementation of Ethereum JavaScript API. -

    https://github.com/ethereum/web3.py pip install web3
  36. web3.py example from web3 import Web3, HTTPProvider w3 = Web3(HTTPProvider('http://eth.xsika.cz:8545'))

    print(w3.eth.blockNumber) print(w3.personal.newAccount('x'))
  37. py-solc Python wrapper around solc - solidity compiler. - https://github.com/ethereum/py-solc

    - http://solidity.readthedocs.io/en/latest/installing-solidit y.html - How to install solc pip install py-solc
  38. greeter.sol pragma solidity ^0.4.0; contract Greeter { string public greeting;

    function Greeter() { greeting = 'Hello'; } function setGreeting(string _greeting) public { greeting = _greeting; } function greet() constant returns (string) { return greeting; } }
  39. py-solc example from web3 import Web3, HTTPProvider from solc import

    compile_source with open('greeter.sol', 'r') as f: src = f.read() compiled_sol = compile_source(src) contract_interface = compiled_sol['<stdin>:Greeter'] w3 = Web3(HTTPProvider('http://eth.xsika.cz:8545')) con = w3.eth.contract(abi=contract_interface['abi'], bytecode=contract_interface['bin']) tx_id = con.deploy()
  40. Interact with contract ... print('Contract value: {}'.format(con.greet()) # Prints: Contract

    value: Hello cont.setGreeting('Nihao') print('Contract value:{}'.format(con.greet())) # Prints: Contract value: Nihao
  41. Summary Python is great tool to hacking Bitcoin & Ethereum

    Python libraries: - jsonrpc_requests - btcpy - python-trezor - web3.py - py-solc
  42. Thank you & Questions Ondrej Sika email: ondrej@ondrejsika.com web: ondrej-sika.cz

    twitter: @ondrejsika linkedin: /in/ondrejsika/ Slides: https://sika.link/pyconsk2018