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

Python Libraries for Bitcoin and Ethereum, PyCo...

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

Python Libraries for Bitcoin and Ethereum, PyCon SK 2018

Avatar for Ondrej Sika

Ondrej Sika

March 10, 2018

More Decks by Ondrej Sika

Other Decks in Programming

Transcript

  1. Agenda - What is Bitcoin - What is Ethereum -

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

    Bitcoin, Zcash, Ethereum mining backends - Pythoner since 2011 - Bitcoiner since 2013
  3. What is Bitcoin? - Peer to Peer payment network -

    Decentralized currency - Digital gold - Money of internet - Internet of money
  4. - 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
  5. Transaction - Transaction is signed payment order - Signed by

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

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

    In block header are some meta informations and POW - It is very hard to create block
  8. 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
  9. 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
  10. 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)
  11. 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
  12. 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())
  13. 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())
  14. 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
  15. 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)
  16. 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())
  17. You can do ... - Access Bitcoind - Use wallets

    - Work with transactions - Block parsing You can't do: - mining - you need a special mining HW
  18. 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
  19. web3.py Web3.py is Python implementation of Ethereum JavaScript API. -

    https://github.com/ethereum/web3.py pip install web3
  20. 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
  21. 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; } }
  22. 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()
  23. 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
  24. Summary Python is great tool to hacking Bitcoin & Ethereum

    Python libraries: - jsonrpc_requests - btcpy - python-trezor - web3.py - py-solc
  25. Thank you & Questions Ondrej Sika email: [email protected] web: ondrej-sika.cz

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