def setUp(self): s = t.state() code = open('sponsor.sol').read() self.c = t.state().abi_contract(code, language='solidity') self.alice_priv = t.k0 self.alice_addr = utils.privtoaddr(self.alice_priv) self.bob_priv = t.k1 self.bob_addr = utils.privtoaddr(self.bob_priv) # wget -qO- https://www.realitykeys.com/api/v1/runkeeper/new --post-data="user_id=29908850&activity=running&measurement=total_distance&comparison=ge&goal=4000&settlement_date=2015-12-23&objection_period_secs=604800&accept_terms_of_service=current&use_existing=1" before_event = json.loads('{"no_pubkey": "", "comparison": null, "measurement": "total_distance", "activity_open_datetime": "2015-12-02 06:35:05", "settlement_date": "2015-12-03", "objection_period_secs": 0, "human_resolution_scheduled_datetime": null, "value": null, "machine_resolution_winner": "No", "id": 6206, "evaluation_method": null, "machine_resolution_value": "246.210454708", "is_user_authenticated": true, "objection_fee_satoshis_paid": 0, "objection_fee_satoshis_due": 1000000, "machine_resolution_scheduled_datetime": "2015-12-03 00:00:00", "user_id": "29908850", "goal": null, "created_datetime": "2015-12-02 06:35:05", "winner": null, "user_profile": "edochan", "winner_value": null, "source": "runkeeper", "yes_pubkey": "", "signature_v2": {"signed_hash": null, "base_unit": 1000000000000000000, "signed_value": null, "sig_der": null, "ethereum_address": "6fde387af081c37d9ffa762b49d340e6ae213395", "fact_hash": "634ab403c50fec82f39e8c4057ea29105708967d386c7f58325a28db333bc418", "sig_r": null, "sig_s": null, "pubkey": "02e577bc17badf301e14d86c33d76be5c3b82a0c416fc93cd124d612761191ec21", "sig_v": null}, "activity_closed_datetime": null, "activity": "walking", "human_resolution_value": null, "user_name": ["edochan"], "winner_privkey": null}') sig_data = before_event['signature_v2'] rk_addr = sig_data['ethereum_address'] event_hash = sig_data['fact_hash'] base_unit = sig_data['base_unit'] # This happens to be the same as the base unit, but there is no deep meaning behind this. ETH_TO_WEI = 1000000000000000000000 self.pledge_id = self.c.add_pledge( self.bob_addr, decode_hex(event_hash), base_unit, 100, # You have to walk at least 100 meters before we pay int(0.01 * ETH_TO_WEI / 100), # We'll give you 0.01 eth per 100 meters (0.0001 eth per meter) after that rk_addr, sender=self.alice_priv, value=int(0.04 * ETH_TO_WEI), # We'll fund you 0.04 eth );
def init_system(genesis, key): code = serpent.compile(open('serp/root.se').read()) tx_make_root = transactions.Transaction.contract(0, 0, 10**12, 10000, code).sign(key) root_contract = processblock.apply_tx(genesis, tx_make_root) root_hash = root_contract.encode('hex') f = lambda x: write_owner(root_hash, x) map(f, ['serp/data.se', 'serp/tag.se', 'serp/users.se', 'serp/currency.se']) code = serpent.compile(open('serp/data.se').read()) tx_make_data = transactions.Transaction.contract(1, 0, 10**12, 10000, code).sign(key) code = serpent.compile(open('serp/tag.se').read()) tx_make_tag = transactions.Transaction.contract(2, 0, 10**12, 10000, code).sign(key) code = serpent.compile(open('serp/users.se').read()) tx_make_users = transactions.Transaction.contract(3, 0, 10**12, 10000, code).sign(key) code = serpent.compile(open('serp/currency.se').read()) tx_make_currency = transactions.Transaction.contract( 4, 0, 10**12, 10000, code).sign(key) data_contract = processblock.apply_tx(genesis, tx_make_data) tag_contract = processblock.apply_tx(genesis, tx_make_tag) users_contract = processblock.apply_tx(genesis, tx_make_users) currency_contract = processblock.apply_tx(genesis, tx_make_currency) d_contract = data_contract.encode('hex') t_contract = tag_contract.encode('hex') u_contract = users_contract.encode('hex') c_contract = currency_contract.encode('hex') #init root tx_init_root = transactions.Transaction( 5, 0, 10**12, 10000, root_contract, serpent.encode_datalist( [d_contract, t_contract, u_contract, c_contract])).sign(key) ans = processblock.apply_tx(genesis, tx_init_root) adresses = { root_hash: 'root', d_contract: 'data', t_contract: 'tags', u_contract: 'users', utils.privtoaddr(key): 'me', c_contract: 'currency' } return adresses
def check(c, owner=None, secret=None, H_secret=None, H_msg=None): if owner: # Check if owner right. assert hex(gs(c, "owner"))[2:-1] == utils.privtoaddr(owner) if gs(c, "commit") == 0: assert gs(c, "commit_release") == 0 else: assert gs(c, "commit_release") != 0 if H_secret: assert sha3([secret])%STRIP == H_secret assert gs(c, "commit") % STRIP == H_secret if H_msg: assert gs(c, "commit_release") % STRIP == H_msg check_meddling(c, owner, secret) if owner: check_wrong(c, owner)
import requests import sys import time from uuid import uuid4 from pyethereum import utils import serpent JSONRPC_URL = "http://127.0.0.1:8080" DEFAULT_GAS = 10000 GAS_PRICE = 10 * 10**12 DEFAULT_KEY = '0x' + utils.sha3("cow").encode( 'hex') # part of the Genesis block DEFAULT_ADDRESS = '0x' + utils.privtoaddr( DEFAULT_KEY[2:]) # cd2a3d9f938e13cd947ec05abc7fe734df8dd826 # FIXME using cow address doesn't work DEFAULT_ADDRESS = '0x8928602aaee4d7cec275e0da580805f6949cfe98' class ApiException(Exception): def __init__(self, code, message): self.code = code self.message = message def __str__(self): return "code=%d, message=\"%s\"" % (self.code, self.message) class Api(object): def __init__(self, jsonrpc_url=JSONRPC_URL):
def accounts(): k = u.sha3('cow') v = u.privtoaddr(k) k2 = u.sha3('horse') v2 = u.privtoaddr(k2) return k, v, k2, v2
from pyethereum import tester, utils key = 'steve' s = tester.state() registry = s.abi_contract('name_registry.se') public_key0 = utils.privtoaddr(tester.k0) public_key1 = utils.privtoaddr(tester.k1) registry.register(key, sender=tester.k0) registry.set_value(key, 'rules', sender=tester.k0) registry.register(key, sender=tester.k1) registry.set_value(key, 'stinks', sender=tester.k1) print(key) print(str(registry.value(key)))
import serpent, json, random from pyethereum import transactions, blocks, processblock,utils NUM_ACCOUNTS=4 root_code = serpent.compile(open('zeroid.se').read()) root_key = utils.sha3('cow') root_addr = utils.privtoaddr(root_key) keys = {} for x in range(NUM_ACCOUNTS): key = utils.sha3(str(x+4)) addr = utils.privtoaddr(key) keys[addr] = key endowment = {root_addr: 10**18} for x in keys: endowment[x] = 10**18 genesis = blocks.genesis(endowment) tx1 = transactions.contract(0, 10**12, 100000, 0, root_code).sign(root_key) result, contract = processblock.apply_transaction(genesis, tx1) nonce=1 for address in keys:
o = c.add_player(value=1000, sender=tester.k1) print("Player 2 Added: {}\n").format(o) ##################################### SETUP COMMITMENTS ######################################## choice = ["rock", "paper", "scissors"] tobytearr = lambda n, L: [] if L == 0 else tobytearr(n / 256, L - 1 ) + [n % 256] choice1 = 0x01 nonce1 = 0x01 ch1 = ''.join(map(chr, tobytearr(choice1, 32))) no1 = ''.join(map(chr, tobytearr(nonce1, 32))) print("Player one chooses {} which is: {}").format(choice1, choice[choice1]) k0_pub_addr_hex = utils.privtoaddr(tester.k0) ## Prepare and pad the address k0_pub_addr = ''.join(map(chr, tobytearr(long(k0_pub_addr_hex, 16), 32))) ## Now use it for the commitment s1 = ''.join([k0_pub_addr, ch1, no1]) comm1 = utils.sha3(s1) choice2 = 0x02 nonce2 = 0x01 ch2 = ''.join(map(chr, tobytearr(choice2, 32))) no2 = ''.join(map(chr, tobytearr(nonce2, 32))) print("Player two chooses {} which is: {}\n").format(choice2, choice[choice2]) k1_pub_addr_hex = utils.privtoaddr(tester.k1)
def new_user(brain_pass): key = utils.sha3(brain_pass) addr = utils.privtoaddr(key) return key, addr
def mk_acc(n): out={"priv":utils.sha3("brainwallet"+str(n))} out["pub"]=b.privtopub(out["priv"]) out["addr"]=int(utils.privtoaddr(out["priv"]), 16) return(out)
if contract.storage[1] < MAXIMUM_TICKETS: return(2) if block.timestamp - contract.storage[2] < 100: return(2) #start drawing t = block.prevhash%MAXIMUM_TICKETS send(1000, contract.storage[3+t+1], MAXIMUM_TICKETS*(10**15)) return (3+t+1) ''' import serpent from pyethereum import transactions, blocks, processblock, utils import time code = serpent.compile(lottery) key = utils.sha3('cow') addr = utils.privtoaddr(key) key2 = utils.sha3('cow2') addr2 = utils.privtoaddr(key2) key_host = utils.sha3('host') add_host = utils.privtoaddr(key_host) #initialize the block genesis = blocks.genesis({addr: 10**18, addr2: 10**18, add_host: 10**18}) #This is to initialize the contract tx1 = transactions.contract(0,10**12,10000,0,code).sign(key_host) result, contract = processblock.apply_transaction(genesis, tx1) #start buying tickets #nonce, gasprice, startgas, to, value, data
def mk_acc(n): out = {"priv": utils.sha3("brainwallet" + str(n))} out["pub"] = b.privtopub(out["priv"]) out["addr"] = int(utils.privtoaddr(out["priv"]), 16) return (out)
import serpent, json, random from pyethereum import transactions, blocks, processblock, utils NUM_ACCOUNTS = 4 root_code = serpent.compile(open('zeroid.se').read()) root_key = utils.sha3('cow') root_addr = utils.privtoaddr(root_key) keys = {} for x in range(NUM_ACCOUNTS): key = utils.sha3(str(x + 4)) addr = utils.privtoaddr(key) keys[addr] = key endowment = {root_addr: 10**18} for x in keys: endowment[x] = 10**18 genesis = blocks.genesis(endowment) tx1 = transactions.contract(0, 10**12, 100000, 0, root_code).sign(root_key) result, contract = processblock.apply_transaction(genesis, tx1) nonce = 1 for address in keys:
def __init__(self, name, genesis=None): self.private_key = utils.sha3(name) self.addr = utils.privtoaddr(self.private_key) self.genesis = genesis or blocks.genesis( {self.addr: self.START_BALANCE})
import serpent from pyethereum import transactions, blocks, processblock, utils import bitcoin key = utils.sha3('aimfesidfd') addr = utils.privtoaddr(key) def pad32(n): if type(n) == str: h = n.encode('hex') else: h = "%02x" % n l = len(h) return "0" * (32 - l) + h nargs = pad32(1) d0 = pad32('hi') print nargs, d0 msg_hash = utils.sha3(nargs + d0) v, r, s = bitcoin.ecdsa_raw_sign(msg_hash, key) pubkey = bitcoin.privkey_to_pubkey(key) verified = bitcoin.ecdsa_raw_verify(msg_hash, (v, r, s), pubkey) gen = blocks.genesis({addr: 10**18}) print serpent.compile_to_assembly(open("DAOist frame.se").read()) DAOcode = serpent.compile(open("DAOist frame.se").read()) DAOcontract = transactions.contract(0, 1, 10**12, 100, DAOcode)
import time import serpent from pyethereum import transactions, blocks, processblock, utils import bitcoin key = utils.sha3('cow') # generate private key using 'brain wallet' seed (should be high entropy) addr = utils.privtoaddr(key) # get address from private key gen = blocks.genesis({addr: 10**60}) assembly = serpent.compile_to_assembly(open('tester.se').read()) print assembly code = serpent.assemble(assembly) print code msg_hash = utils.sha3('heres a message') v, r, s = bitcoin.ecdsa_raw_sign(msg_hash, key) pub = bitcoin.privkey_to_pubkey(key) verified = bitcoin.ecdsa_raw_verify(msg_hash, (v, r, s), pub) print verified tx_make_root = transactions.contract(0,10,10**30, 10**30, code).sign(key) success, root_contract = processblock.apply_tx(gen, tx_make_root) #tx_init_root = transactions.Transaction(1, 100, 10**40, root_contract, 0, serpent.encode_datalist([msg_hash, v, r, s])).sign(key) #tx_init_root = transactions.Transaction(1, 100, 10**40, root_contract, 0, serpent.encode_datalist(['hi', 'bye'])).sign(key)
def init_system(genesis, key): code = serpent.compile(open('serp/root.se').read()) tx_make_root = transactions.Transaction.contract(0,0,10**12, 10000, code).sign(key) root_contract = processblock.apply_tx(genesis, tx_make_root) root_hash = root_contract.encode('hex') f = lambda x: write_owner(root_hash, x) map(f, ['serp/data.se', 'serp/tag.se', 'serp/users.se', 'serp/currency.se']) code = serpent.compile(open('serp/data.se').read()) tx_make_data = transactions.Transaction.contract(1,0,10**12, 10000, code).sign(key) code = serpent.compile(open('serp/tag.se').read()) tx_make_tag = transactions.Transaction.contract(2,0,10**12, 10000, code).sign(key) code = serpent.compile(open('serp/users.se').read()) tx_make_users = transactions.Transaction.contract(3,0,10**12, 10000, code).sign(key) code = serpent.compile(open('serp/currency.se').read()) tx_make_currency = transactions.Transaction.contract(4,0,10**12, 10000, code).sign(key) data_contract = processblock.apply_tx(genesis, tx_make_data) tag_contract = processblock.apply_tx(genesis, tx_make_tag) users_contract = processblock.apply_tx(genesis, tx_make_users) currency_contract = processblock.apply_tx(genesis, tx_make_currency) d_contract = data_contract.encode('hex') t_contract = tag_contract.encode('hex') u_contract = users_contract.encode('hex') c_contract = currency_contract.encode('hex') #init root tx_init_root = transactions.Transaction(5, 0, 10**12, 10000, root_contract, serpent.encode_datalist([d_contract, t_contract, u_contract, c_contract])).sign(key) ans = processblock.apply_tx(genesis, tx_init_root) adresses = {root_hash:'root', d_contract:'data', t_contract:'tags', u_contract:'users', utils.privtoaddr(key):'me', c_contract:'currency'} return adresses
o = c.add_player(value=1000, sender=tester.k1) print("Player 2 Added: {}\n").format(o) ##################################### SETUP COMMITMENTS ######################################## choice = ["rock", "paper", "scissors"] tobytearr = lambda n, L: [] if L == 0 else tobytearr(n / 256, L - 1)+[n % 256] choice1 = 0x01 nonce1 = 0x01 ch1 = ''.join(map(chr, tobytearr(choice1, 32))) no1 = ''.join(map(chr, tobytearr(nonce1, 32))) print("Player one chooses {} which is: {}").format(choice1, choice[choice1]) k0_pub_addr_hex = utils.privtoaddr(tester.k0) ## Prepare and pad the address k0_pub_addr = ''.join(map(chr, tobytearr(long(k0_pub_addr_hex,16),32))) ## Now use it for the commitment s1 = ''.join([k0_pub_addr, ch1, no1]) comm1 = utils.sha3(s1) choice2 = 0x02 nonce2 = 0x01 ch2 = ''.join(map(chr, tobytearr(choice2, 32))) no2 = ''.join(map(chr, tobytearr(nonce2, 32))) print("Player two chooses {} which is: {}\n").format(choice2, choice[choice2]) k1_pub_addr_hex = utils.privtoaddr(tester.k1)
def __init__(self, name, genesis=None): self.private_key = utils.sha3(name) self.addr = utils.privtoaddr(self.private_key) self.genesis = genesis or blocks.genesis({self.addr: self.START_BALANCE})
def step_impl(context): context.key = utils.sha3('cows') context.addr = utils.privtoaddr(context.key) context.gen = blocks.genesis({context.addr: 10**60})
if not self.storage[key]: self.storage[key] = value return(1) else: return(-1) def get(key): #Returns -1 if not registered, returns value if registered if not self.storage[key]: return(-1) else: return(self.storage[key]) ''' #Create public key public_k1 = utils.privtoaddr(tester.k1) #Generate state and add contract to block chain s = tester.state() print("Tester state created") c = s.abi_contract(serpent_code) print("Code added to block chain") #Test contract o = c.get("Bob") if o == -1: print("No value has been stored at key \"Bob\"") else: print("The value stored with key \"Bob\" is " + str(o)) o = c.register("Bob", 10)
import time import serpent from pyethereum import transactions, blocks, processblock, utils import bitcoin key = utils.sha3( 'cow' ) # generate private key using 'brain wallet' seed (should be high entropy) addr = utils.privtoaddr(key) # get address from private key gen = blocks.genesis({addr: 10**60}) assembly = serpent.compile_to_assembly(open('tester.se').read()) print assembly code = serpent.assemble(assembly) print code msg_hash = utils.sha3('heres a message') v, r, s = bitcoin.ecdsa_raw_sign(msg_hash, key) pub = bitcoin.privkey_to_pubkey(key) verified = bitcoin.ecdsa_raw_verify(msg_hash, (v, r, s), pub) print verified tx_make_root = transactions.contract(0, 10, 10**30, 10**30, code).sign(key) success, root_contract = processblock.apply_tx(gen, tx_make_root) #tx_init_root = transactions.Transaction(1, 100, 10**40, root_contract, 0, serpent.encode_datalist([msg_hash, v, r, s])).sign(key) #tx_init_root = transactions.Transaction(1, 100, 10**40, root_contract, 0, serpent.encode_datalist(['hi', 'bye'])).sign(key) tx_init_root = transactions.Transaction( 1, 100, 10**40, root_contract, 0, serpent.encode_datalist([2, '139dcd5cc79e260272e05147c349ab5f2db3f102',
def accounts(): k = utils.sha3('cow') v = utils.privtoaddr(k) k2 = utils.sha3('horse') v2 = utils.privtoaddr(k2) return k, v, k2, v2
def new_config(data_dir=None): cfg = _get_default_config() if not data_dir: tempfile.mktemp() cfg.set('misc', 'data_dir', data_dir) return cfg ### From here is pasted together from earlier version of pyetherem import serpent from pyethereum import transactions, blocks, processblock, utils #processblock.print_debug = 1 from pyethereum import slogging slogging.set_level('eth.tx',"DEBUG") code = serpent.compile(namecoin_code) key = utils.sha3('cow') addr = utils.privtoaddr(key) genesis = blocks.genesis(new_db(), { addr: 10**18 }) tx1 = transactions.contract(nonce=0,gasprice=10**12,startgas=10000,endowment=0,code=code).sign(key) result, contract = processblock.apply_transaction(genesis,tx1) print genesis.to_dict() tx2 = transactions.Transaction(nonce=1,gasprice=10**12,startgas=10000,to=contract,value=0, data=serpent.encode_abi(0,1,45)).sign(key) result, ans = processblock.apply_transaction(genesis,tx2) serpent.decode_datalist(ans) #print genesis.to_dict()
def transfer(addr, value): #We are going to max out debt at 1000 credits per person if self.storage[msg.sender] - value < -1000: return(-1) else: #If they have not exceeded their debt limit, we do the transaction self.storage[msg.sender] -= value self.storage[addr] += value return(1) #Simply return the balance at that address def balance(addr): return(self.storage[addr]) ''' public_k0 = utils.privtoaddr(tester.k0) public_k1 = utils.privtoaddr(tester.k1) s = tester.state() c = s.abi_contract(serpent_code) o = c.balance(public_k0) print("tester.k0's current balance is " + str(o)) o = c.balance(public_k1) print("tester.k1's current balance is " + str(o)) o = c.transfer(public_k0, 500, sender=tester.k1) if o == 1: print("500 credits sent to tester_k1 from tester_k0") else:
def __init__(self, secret): self.key = utils.sha3(secret) self.address = utils.privtoaddr(self.key)
import json import requests import sys import time from uuid import uuid4 from pyethereum import utils import serpent JSONRPC_URL = "http://127.0.0.1:8080" DEFAULT_GAS = 10000 GAS_PRICE = 10 * 10 ** 12 DEFAULT_KEY = "0x" + utils.sha3("cow").encode("hex") # part of the Genesis block DEFAULT_ADDRESS = "0x" + utils.privtoaddr(DEFAULT_KEY[2:]) # cd2a3d9f938e13cd947ec05abc7fe734df8dd826 # FIXME using cow address doesn't work DEFAULT_ADDRESS = "0x8928602aaee4d7cec275e0da580805f6949cfe98" class ApiException(Exception): def __init__(self, code, message): self.code = code self.message = message def __str__(self): return 'code=%d, message="%s"' % (self.code, self.message) class Api(object): def __init__(self, jsonrpc_url=JSONRPC_URL):
''' import serpent from pyethereum import transactions, blocks, processblock, utils import time import sys #require command line args if len(sys.argv) < 3: print("Usage: %s [price] [recipient: transcation success: 2, transcation deny: 3] [sender: transcation success: 2, transcation deny: 3]" %sys.argv[0]) sys.exit(1) code = serpent.compile(escrow) sender_key = utils.sha3('sender') sender_addr = utils.privtoaddr(sender_key) recipient_key = utils.sha3('recipient') recipient_addr = utils.privtoaddr(recipient_key) host_key = utils.sha3('host') host_addr = utils.privtoaddr(host_key) #initialize the block genesis = blocks.genesis({sender_addr: 10**18, recipient_addr: 10**18, host_addr: 10**18}) #initialize the contract tx1 = transactions.contract(0, 10**12, 10000, 0, code).sign(host_key) result, contract = processblock.apply_transaction(genesis, tx1) #execute escrow transaction #nonce, gasprice, startgas, to, value, data price = int(sys.argv[1]) #user supplied price