Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

What is Bitcoin?

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

How Bitcoin works?

Slide 8

Slide 8 text

- 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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Block - Block is a 1MB list of transactions - In block header are some meta informations and POW - It is very hard to create block

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Interact with Bitcoin

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

In Python libraries - jsonrpc_requests - btcpy - python-trezor

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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)

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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)

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

You can do almost everything with those 3 libraries.

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

What is Ethereum?

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

Interact with Ethereum

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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