$30 off During Our Annual Pro Sale. View Details »

Python Libraries for Bitcoin and Ethereum, PyCon SK 2018

Python Libraries for Bitcoin and Ethereum, PyCon SK 2018

Ondrej Sika

March 10, 2018
Tweet

More Decks by Ondrej Sika

Other Decks in Programming

Transcript

  1. Python Libraries
    for Bitcoin & Ethereum
    Ondrej Sika
    [email protected]
    PyCon SK 2018,
    Bratislava SK, 10. 3. 2018

    View Slide

  2. Agenda
    - What is Bitcoin
    - What is Ethereum
    - RPC, Web3
    - Bitcoin libraries
    - Ethereum libraries

    View Slide

  3. Ondrej Sika
    - Software engineer at Slush Pool (mining)
    - Bitcoin, Zcash, Ethereum mining backends
    - Pythoner since 2011
    - Bitcoiner since 2013

    View Slide

  4. View Slide

  5. What is Bitcoin?

    View Slide

  6. What is Bitcoin?
    - Peer to Peer payment network
    - Decentralized currency
    - Digital gold
    - Money of internet
    - Internet of money

    View Slide

  7. How Bitcoin works?

    View Slide

  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

    View Slide

  9. Transaction
    - Transaction is signed payment order
    - Signed by sender's private key
    - To receiver address (public key (hash))

    View Slide

  10. Blockchain
    - Blockchain is distributed ledger which stores all
    transactions in chronological order
    - Transactions in blockchain are stored in blocks

    View Slide

  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

    View Slide

  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

    View Slide

  13. HD Wallets
    - Hierarchical deterministic wallets
    - Support in Trezor, Electrum

    View Slide

  14. Interact with Bitcoin

    View Slide

  15. Bitcoind
    Bitcoin daemon
    - Full Bitcoin node
    - Require Blockchain (160 GB)
    - JSON RPC API (HTTP)
    - CLI

    View Slide

  16. Bitcoind CLI
    bitcoin-cli getnewaddress
    3Kyvxpx3B55q6Hx7JYJahLNi16cEfFajZ5
    bitcoin-cli dumpprivkey 3Ky..jZ5
    L1XxeiQ3Y4LpeWCSEZonew7oBZoEgxWuDFK6cH9rmjfM
    Y5FbhZfn

    View Slide

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

    View Slide

  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

    View Slide

  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)

    View Slide

  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

    View Slide

  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())

    View Slide

  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())

    View Slide

  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

    View Slide

  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)

    View Slide

  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())

    View Slide

  26. You can do almost
    everything with those 3
    libraries.

    View Slide

  27. You can do ...
    - Access Bitcoind
    - Use wallets
    - Work with transactions
    - Block parsing
    You can't do:
    - mining - you need a special mining HW

    View Slide

  28. View Slide

  29. What is Ethereum?

    View Slide

  30. What is Ethereum?
    - Also currency, network, …
    - Decentralized computer
    - Smart contract platform

    View Slide

  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

    View Slide

  32. Interact with Ethereum

    View Slide

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

    View Slide

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

    View Slide

  35. web3.py
    Web3.py is Python implementation of Ethereum
    JavaScript API.
    - https://github.com/ethereum/web3.py
    pip install web3

    View Slide

  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'))

    View Slide

  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

    View Slide

  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; }
    }

    View Slide

  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[':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()

    View Slide

  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

    View Slide

  41. Summary
    Python is great tool to hacking Bitcoin & Ethereum
    Python libraries:
    - jsonrpc_requests
    - btcpy
    - python-trezor
    - web3.py
    - py-solc

    View Slide

  42. Thank you & Questions
    Ondrej Sika
    email: [email protected]
    web: ondrej-sika.cz
    twitter: @ondrejsika
    linkedin: /in/ondrejsika/
    Slides: https://sika.link/pyconsk2018

    View Slide