def generate_feed(block_number, nonce, ask_for_1000, bid_for_1000, private_key): hexX = h2b("%0.64X" % block_number) hexY = h2b("%0.64X" % nonce) hexZ = h2b("%0.64X" % ask_for_1000) hexW = h2b("%0.64X" % bid_for_1000) msg = hexX + hexY + hexZ + hexW msghash = b2h(utils.sha3(msg)) V, R, S = bitcoin.ecdsa_raw_sign(msghash, private_key) # print("V R S") R = utils.int_to_hex(R) S = utils.int_to_hex(S) # print("0x%x" % V, R, S) dic = {} dic['status'] = "success" data = {} data['block_number'] = block_number data['nonce'] = nonce data['bid_for_1000'] = bid_for_1000 data['ask_for_1000'] = ask_for_1000 data['message'] = "0x" + b2h(msg) data['hash'] = "0x" + msghash data['signer'] = "0x" + b2h(utils.privtoaddr(private_key)) data['v'] = V data['r'] = R data['s'] = S dic['data'] = data print(json.dumps(dic, indent=4, sort_keys=True))
def generate_genesis(path=None, num_participants=1): privkeys = [ utils.sha3(utils.to_string(i)) for i in range(num_participants) ] addrs = [utils.privtoaddr(k) for k in privkeys] deposit_sizes = [i * 500 + 500 for i in range(num_participants)] randaos = [RandaoManager(utils.sha3(k)) for k in privkeys] validators = [(generate_validation_code(a), ds * 10**18, r.get(9999), a) for a, ds, r in zip(addrs, deposit_sizes, randaos)] s = make_casper_genesis(validators=validators, alloc={a: { 'balance': 10**18 } for a in addrs}, timestamp=int(time.time()), epoch_length=100) genesis_hash = apply_const_message( s, sender=casper_config['METROPOLIS_ENTRY_POINT'], to=casper_config['METROPOLIS_BLOCKHASH_STORE'], data=utils.encode_int32(0)) genesis_number = call_casper(s, 'getBlockNumber') print('genesis block hash: %s' % utils.encode_hex(genesis_hash)) print('genesis block number: %d' % genesis_number) print('%d validators: %r' % (num_participants, [utils.encode_hex(a) for a in addrs])) snapshot = s.to_snapshot() header = s.prev_headers[0] genesis = { "nonce": "0x" + utils.encode_hex(header.nonce), "difficulty": utils.int_to_hex(header.difficulty), "mixhash": "0x" + utils.encode_hex(header.mixhash), "coinbase": "0x" + utils.encode_hex(header.coinbase), "timestamp": utils.int_to_hex(header.timestamp), "parentHash": "0x" + utils.encode_hex(header.prevhash), "extraData": "0x" + utils.encode_hex(header.extra_data), "gasLimit": utils.int_to_hex(header.gas_limit), "alloc": snapshot["alloc"] } if path: with open(path, 'w') as f: json.dump(genesis, f, sort_keys=False, indent=4, separators=(',', ': ')) print('casper genesis generated') else: return genesis
def run_genesis_test(params, mode): params = copy.deepcopy(params) if 'difficulty' not in params: params['difficulty'] = int_to_hex(2**34) if 'mixhash' not in params: params['mixhash'] = '0x' + '0' * 64 if 'nonce' not in params: params['nonce'] = '0x0000000000000042' if 'timestamp' not in params: params['timestamp'] = int_to_hex(5000) if 'parentHash' not in params: params['parentHash'] = '0x' + '0' * 64 if 'gasLimit' not in params: params['gasLimit'] = int_to_hex(5000) if 'extraData' not in params: params['extraData'] = '0x' if 'coinbase' not in params: params['coinbase'] = '0x' + '3' * 40 x = time.time() b = blocks.genesis( EphemDB(), start_alloc=params['alloc'], difficulty=parse_int_or_hex(params['difficulty']), timestamp=parse_int_or_hex(params['timestamp']), extra_data=decode_hex(remove_0x_head(params['extraData'])), gas_limit=parse_int_or_hex(params['gasLimit']), mixhash=decode_hex(remove_0x_head(params['mixhash'])), prevhash=decode_hex(remove_0x_head(params['parentHash'])), coinbase=decode_hex(remove_0x_head(params['coinbase'])), nonce=decode_hex(remove_0x_head(params['nonce']))) assert b.difficulty == parse_int_or_hex(params['difficulty']) assert b.timestamp == parse_int_or_hex(params['timestamp']) assert b.extra_data == decode_hex(remove_0x_head(params['extraData'])) assert b.gas_limit == parse_int_or_hex(params['gasLimit']) assert b.mixhash == decode_hex(remove_0x_head(params['mixhash'])) assert b.prevhash == decode_hex(remove_0x_head(params['parentHash'])) assert b.nonce == decode_hex(remove_0x_head(params['nonce'])) print 9 if mode == FILL: params['result'] = encode_hex(rlp.encode(b)) return params elif mode == VERIFY: assert params['result'] == encode_hex(rlp.encode(b)) elif mode == TIME: return {'creation': time.time() - x}
def run_genesis_test(params, mode): params = copy.deepcopy(params) if 'difficulty' not in params: params['difficulty'] = int_to_hex(2 ** 34) if 'mixhash' not in params: params['mixhash'] = '0x' + '0' * 64 if 'nonce' not in params: params['nonce'] = '0x0000000000000042' if 'timestamp' not in params: params['timestamp'] = int_to_hex(5000) if 'parentHash' not in params: params['parentHash'] = '0x' + '0' * 64 if 'gasLimit' not in params: params['gasLimit'] = int_to_hex(5000) if 'extraData' not in params: params['extraData'] = '0x' if 'coinbase' not in params: params['coinbase'] = '0x' + '3' * 40 x = time.time() b = blocks.genesis(EphemDB(), start_alloc=params['alloc'], difficulty=parse_int_or_hex(params['difficulty']), timestamp=parse_int_or_hex(params['timestamp']), extra_data=decode_hex(remove_0x_head(params['extraData'])), gas_limit=parse_int_or_hex(params['gasLimit']), mixhash=decode_hex(remove_0x_head(params['mixhash'])), prevhash=decode_hex(remove_0x_head(params['parentHash'])), coinbase=decode_hex(remove_0x_head(params['coinbase'])), nonce=decode_hex(remove_0x_head(params['nonce']))) assert b.difficulty == parse_int_or_hex(params['difficulty']) assert b.timestamp == parse_int_or_hex(params['timestamp']) assert b.extra_data == decode_hex(remove_0x_head(params['extraData'])) assert b.gas_limit == parse_int_or_hex(params['gasLimit']) assert b.mixhash == decode_hex(remove_0x_head(params['mixhash'])) assert b.prevhash == decode_hex(remove_0x_head(params['parentHash'])) assert b.nonce == decode_hex(remove_0x_head(params['nonce'])) print(9) if mode == FILL: params['result'] = encode_hex(rlp.encode(b)) return params elif mode == VERIFY: assert params['result'] == encode_hex(rlp.encode(b)) elif mode == TIME: return { 'creation': time.time() - x }
def ecdsa(order, priv_key): hashed_order = keccak_256( address(order['makerAddress']) + uint(order['makerAmount']) + address(order['makerToken']) + address(order['takerAddress']) + uint(order['takerAmount']) + address(order['takerToken']) + uint(order['nonce']) + uint(order['expiration'])) magic = keccak_256(b'\x19Ethereum Signed Message:\n32' + hashed_order.digest()) v, r, s = ecdsa_raw_sign(magic.hexdigest(), hexadecimal.decode_hex(priv_key)) r = utils.int_to_hex(r) s = utils.int_to_hex(s) # Ensuring that the parameters are correct length if len(r) < 66: r = b'0x0' + r[2:] if len(s) < 66: diff = 66 - len(s) s = '0x' + '0' * diff + s[2:] return v, r, s
def new_game(event_obj): print("\n----------- New Game -------------\n") msg = event_obj['args']['seed'] print("Seed is: 0x" + utils.encode_hex(msg)) print("Let's sign it!\n") v, r, s = ecdsa_raw_sign_with_random_k(msg, key) while not is_casino_win(s): print("[-] Bad signature :( Let's resign\n") v, r, s = ecdsa_raw_sign_with_random_k(msg, key) print("[+] Good signature found! ") print("s :", utils.int_to_hex(s)) tx = contract_instance.transact({ 'from': web3.eth.coinbase }).confirm(msg, v, utils.encode_int32(r), utils.encode_int32(s)) print("Send it by : 0x" + utils.encode_hex(tx)) time.sleep(2)
def int_to_hex(value): if value == 0: return hex(0) return ethereum_utils.int_to_hex(value)