KaunasPy, November 2018
Jaro Šatkevič - @chompomonim
Full node
Mining
Wallet
Bitcoin core
It knows only what it was told about
Online
Service
User
Service
Libraries
Wallets
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'
...
# 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)
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)
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()))
Jaro Šatkevič - @chompomonim - me@jaro.lt
https://slides.com/jaro/python-in-blockchain