Python in Blockchain

KaunasPy, November 2018

Jaro Šatkevič - @chompomonim

About Me

Agenda

  • What is Blockchain (main building blocks)
  • Which blockchains actually works.
  • Intro into Bitcoin and Ethereum.
  • Ideas where python is good choice.
  • Libraries and code examples.
  • Actual projects made in python and ideas on where to start from.
  • Where everything is going?

What is blockchain?

Consensus

Programable money

Which of them actually works?

Which of them actually works?

Rest are mostly experiments or PoC

  • EOS - computation platform, not a blockchain
  • Cardano - still in early stage and centralised
  • IOTA - experimental technology, not a blockchain
  • Tron - early beta, not time proven
  • ...

Main parts of Bitcoin

Full node

Mining

Wallet

Bitcoin core

Full node

Ledger of transactions

Mining

creation of blocks with tx

Wallet

view balances, create & sign tx

UTXO    balance

It knows only what it was told about

Online
Service

User

Service

Oracles everywhere

  • Full nodes
  • Mining software
  • Wallets of all kinds
  • Smart contracts
  • Oracles

Is Python good for Blockchain?

Isn't C++, Go, Rust or Java mostly used for blockchain creation?

Yes

Especially for wallets and oracles

Python in Bitcoin

Libs and tools

Libraries

Wallets

Learning materials

Let's generate address

from cryptos import *

btc = Bitcoin(testnet=True)

# Generate private key
priv = sha256('a super cool password')

# Generate public key
pub = btc.privtopub(priv)

# Generate address out of public key
addr = btc.pubtoaddr(pub)

# Our address:'mhdBshFXACyc45nWx2g96tJweuwiThWR1H'

Let's create transaction

...


# Generate destination address
priv2 = sha256('destination address owned by everyone')
pub2 = c.privtopub(priv2)
dest_addr = c.pubtoaddr(pub2)

# Create transaction
inputs = btc.unspent(addr) # we have 710 000 satoshi in inputs
outputs = [
    {'value': 500000, 'address': dest_addr}, 
    {'value': 200000, 'address': addr}
] # 10 000 satoshi left for mining fee

txobj = btc.mktx(inputs, outputs)
tx_obj_signed = btc.sign(tx_obj ,0, priv)
tx = serialize(tx_obj_signed)

# push transaction into blockchain
btc.pushtx(tx)

Python in Ethereum

Python libs and tools

Smart contract

beneficiary: public(address)
auction_start: public(timestamp)
auction_end: public(timestamp)

highest_bidder: public(address)
highest_bid: public(wei_value)

ended: public(bool)

@public
def __init__(_beneficiary: address, _bidding_time: timedelta):
    self.beneficiary = _beneficiary
    self.auction_start = block.timestamp
    self.auction_end = self.auction_start + _bidding_time

@public
@payable
def bid():
    assert block.timestamp < self.auction_end
    assert msg.value > self.highest_bid
    if not self.highest_bid == 0:
        send(self.highest_bidder,self.highest_bid)
    self.highest_bidder = msg.sender
    self.highest_bid = msg.value

@public
def end_auction():
    assert block.timestamp >= self.auction_end

    self.ended = True

    send(self.beneficiary, self.highest_bid)

Let's compile and run it

Mini oracle

import json
import web3

from web3 import Web3, TestRPCProvider
from solc import compile_source
from web3.contract import ConciseContract

w3 = Web3(TestRPCProvider())

contract = w3.eth.contract(abi=abi)
contract_address = '0x85aC27E4124863492DB18EAFBb358102Cad8fc96'
contract_instance = w3.eth.contract(
                        abi=abi, 
                        address=contract_address, 
                        ContractFactoryClass=ConciseContract)


contract_instance.bid(transact={'from': w3.eth.accounts[0], 'value': 10000})
print('Highest bid: {}'.format(contract_instance.highest_bid()))

what's dApps?

Crypto currencies are:

Programmable money

Payment channels, and
Lightning Network are future

Side chains is something I'm watching closely

Blockchain ecosystem needs you

Let's discuss

Jaro Šatkevič - @chompomonim - me@jaro.lt

https://slides.com/jaro/python-in-blockchain

Python in Blockchain: Tools and Possibilities

By Jaro

Python in Blockchain: Tools and Possibilities

  • 1,539