def parity_export_blocks_process( parity_binary, datadir, chain_config_file_path, parity_port): run_command = ( parity_binary, 'export', 'blocks', os.path.join(datadir, 'blocks_export.rlp'), '--base-path', datadir, '--no-ws', '--no-ui', '--no-warp', '--chain', chain_config_file_path, '--rpcapi', 'all', '--rpcport', parity_port, # '--author', common.COINBASE[2:], ) print(' '.join(run_command)) try: proc = common.get_process(run_command) yield proc finally: common.kill_proc_gracefully(proc) output, errors = proc.communicate() print( "Parity Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( force_text(output), force_text(errors), ) )
def test_admin_setSolc(web3, skip_if_testrpc): skip_if_testrpc(web3) try: solc_path = subprocess.check_output(['which', 'solc']).strip() except subprocess.CalledProcessError: pytest.skip('solc binary not found') solc_version = subprocess.check_output(['solc', '--version']).strip() actual = web3.admin.setSolc(solc_path) assert force_text(solc_version) in actual assert force_text(solc_path) in actual
def get_geth_process(geth_binary, datadir, genesis_file_path, ipc_path, port, networkid, skip_init=False): if not skip_init: init_datadir_command = ( geth_binary, '--datadir', datadir, 'init', genesis_file_path, ) print(' '.join(init_datadir_command)) subprocess.check_output( init_datadir_command, stdin=subprocess.PIPE, stderr=subprocess.PIPE, ) run_geth_command = ( geth_binary, '--datadir', datadir, '--ipcpath', ipc_path, '--nodiscover', '--port', port, '--networkid', networkid, '--etherbase', COINBASE[2:], ) print(' '.join(run_geth_command)) try: proc = get_process(run_geth_command) yield proc finally: kill_proc_gracefully(proc) output, errors = proc.communicate() print("Geth Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( force_text(output), force_text(errors), ))
def make_request(self, method, params): request = self.encode_rpc_request(method, params) self._lock.acquire() try: with get_ipc_socket(self.ipc_path) as sock: sock.sendall(request) # TODO: use a BytesIO object here response_raw = b"" with Timeout(10) as timeout: while True: try: response_raw += sock.recv(4096) except socket.timeout: timeout.sleep(0) continue if response_raw == b"": timeout.sleep(0) else: try: json.loads(force_text(response_raw)) except JSONDecodeError: timeout.sleep(0) continue else: break finally: self._lock.release() return response_raw
def test_dynamic_length_argument_extraction(web3, emitter, wait_for_transaction, emitter_log_topics, emitter_event_ids): string_0 = "this-is-the-first-string-which-exceeds-32-bytes-in-length" string_1 = "this-is-the-second-string-which-exceeds-32-bytes-in-length" txn_hash = emitter.transact().logDynamicArgs(string_0, string_1) txn_receipt = wait_for_transaction(web3, txn_hash) assert len(txn_receipt['logs']) == 1 log_entry = txn_receipt['logs'][0] event_abi = emitter._find_matching_event_abi('LogDynamicArgs') event_topic = emitter_log_topics.LogDynamicArgs assert event_topic in log_entry['topics'] string_0_topic = web3.sha3(string_0, encoding='utf8') assert string_0_topic in log_entry['topics'] event_data = get_event_data(event_abi, log_entry) expected_args = { "arg0": force_text(decode_hex(string_0_topic)), "arg1": string_1, } assert event_data['args'] == expected_args assert event_data['blockHash'] == txn_receipt['blockHash'] assert event_data['blockNumber'] == txn_receipt['blockNumber'] assert event_data['transactionIndex'] == txn_receipt['transactionIndex'] assert event_data['address'] == emitter.address assert event_data['event'] == 'LogDynamicArgs'
def normalize_class_name(value): """ For `type()` calls: * Python 3.4 wants `str` * Python 3.5 doesn't care. """ return force_text(value)
def test_eth_sign(web3, skip_if_testrpc): skip_if_testrpc(web3) private_key_hex = '0x5e95384d8050109aab08c1922d3c230739bc16976553c317e5d0b87b59371f2a' private_key = decode_hex(private_key_hex) # This imports the private key into the running geth instance and unlocks # the account so that it can sign things. # `0xa5df35f30ba0ce878b1061ae086289adff3ba1e0` address = web3.personal.importRawKey(private_key, "password") web3.personal.unlockAccount(address, "password") assert add_0x_prefix(encode_hex(privtoaddr(private_key))) == add_0x_prefix(address) assert address == '0xa5df35f30ba0ce878b1061ae086289adff3ba1e0' # the data to be signed data = b'1234567890abcdefghijklmnopqrstuvwxyz' # the hash of the data `0x089c33d56ed10bd8b571a0f493cedb28db1ae0f40c6cd266243001528c06eab3` data_hash = web3.sha3(data, encoding=None) data_hash_bytes = decode_hex(data_hash) assert force_bytes(data_hash) == sha3(data) priv_key = PrivateKey(flags=ALL_FLAGS) priv_key.set_raw_privkey(private_key) # sanit check the extract_ecdsa_signer function works as expected. vector_sig = priv_key.ecdsa_sign_recoverable(data_hash_bytes, raw=True, digest=sha3_256) vector_sig_bytes, rec_id = priv_key.ecdsa_recoverable_serialize(vector_sig) vector_sig_bytes_full = vector_sig_bytes + force_bytes(chr(rec_id)) vector_address = force_text(extract_ecdsa_signer(data_hash_bytes, vector_sig_bytes_full)) assert vector_address == address # Now have geth sign some data. signature_hex = web3.eth.sign(address, data) signature_bytes = decode_hex(signature_hex) actual_signer = extract_ecdsa_signer(data_hash_bytes, signature_bytes) assert actual_signer == address # Verify the signature against the public key derived from the # original private key. It fails. rec_id = signature_bytes[64] if is_string(rec_id): rec_id = ord(rec_id) recoverable_signature = priv_key.ecdsa_recoverable_deserialize( signature_bytes[:64], rec_id, ) signature = priv_key.ecdsa_recoverable_convert(recoverable_signature) is_valid = priv_key.pubkey.ecdsa_verify( msg=data, raw_sig=signature, digest=sha3_256, ) assert is_valid
def validate_padding_bytes(cls, value, padding_bytes): value_byte_size = cls._get_value_byte_size() padding_size = cls.data_byte_size - value_byte_size if padding_bytes != b'\x00' * padding_size: raise NonEmptyPaddingBytes( "Padding bytes were not empty: {0}".format(force_text(padding_bytes)) )
def get_geth_process(geth_binary, datadir, genesis_file_path, geth_ipc_path, geth_port): init_datadir_command = ( geth_binary, '--datadir', datadir, 'init', genesis_file_path, ) subprocess.check_output( init_datadir_command, stdin=subprocess.PIPE, stderr=subprocess.PIPE, ) run_geth_command = ( geth_binary, '--datadir', datadir, '--ipcpath', geth_ipc_path, '--nodiscover', '--port', geth_port, '--etherbase', COINBASE[2:], ) proc = subprocess.Popen( run_geth_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, ) try: yield proc finally: kill_proc_gracefully(proc) output, errors = proc.communicate() print( "Geth Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( force_text(output), force_text(errors), ) )
def get_geth_process(geth_binary, datadir, genesis_file_path, geth_ipc_path, geth_port): init_datadir_command = ( geth_binary, '--datadir', datadir, 'init', genesis_file_path, ) subprocess.check_output( init_datadir_command, stdin=subprocess.PIPE, stderr=subprocess.PIPE, ) run_geth_command = ( geth_binary, '--datadir', datadir, '--ipcpath', geth_ipc_path, '--nodiscover', '--port', geth_port, '--etherbase', COINBASE[2:], ) proc = subprocess.Popen( run_geth_command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, ) try: yield proc finally: kill_proc_gracefully(proc) output, errors = proc.communicate() print("Geth Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( force_text(output), force_text(errors), ))
def decoder_fn(cls, data): if data == b'\x00': return False elif data == b'\x01': return True else: raise NonEmptyPaddingBytes( "Boolean must be either 0x0 or 0x1. Got: {0}".format(force_text(data)) )
def test_serialization(factories): signature = factories.SignatureFactory() serializer = SignatureSerializer(signature) data = serializer.data assert data['id'] == signature.id assert data['text_signature'] == signature.text_signature assert data['bytes_signature'] == force_text(signature.bytes_signature.bytes4_signature) assert data['hex_signature'] == signature.bytes_signature.get_hex_display()
def validate_padding_bytes(cls, value, padding_bytes): raise NotImplementedError("Must be implemented by subclasses") value_byte_size = cls._get_value_byte_size() padding_size = cls.data_byte_size - value_byte_size if padding_bytes != b'\x00' * padding_size: raise NonEmptyPaddingBytes( "Padding bytes were not empty: {0}".format(force_text(padding_bytes)) )
def normalize_class_name(value): """ For `type()` calls: * Python 2 wants `str` * Python 3.4 wants `str` * Python 3.5 doesn't care. """ if sys.version_info.major == 2: return force_bytes(value) else: return force_text(value)
def isConnected(self): try: response_raw = self.make_request('web3_clientVersion', []) response = json.loads(force_text(response_raw)) except IOError: return False else: assert response['jsonrpc'] == '2.0' assert 'error' not in response return True assert False
def get_process(command_list, terminates=False): proc = subprocess.Popen( command_list, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, bufsize=1, ) if terminates: wait_for_popen(proc, 30) try: yield proc finally: kill_proc_gracefully(proc) output, errors = proc.communicate() print("Parity Process Exited:\n" "stdout:{0}\n\n" "stderr:{1}\n\n".format( force_text(output), force_text(errors), ))
def request_blocking(self, method, params): """ Make a synchronous request using the provider """ response_raw = self.provider.make_request(method, params) if is_string(response_raw): response = json.loads(force_text(response_raw)) elif is_dict(response_raw): response = response_raw if "error" in response: raise ValueError(response["error"]) return response['result']
def test_with_locked_account(project): temp_chain = project.get_chain('temp') account = force_text( create_new_account(temp_chain.geth.data_dir, b'a-test-password')) with temp_chain: web3 = temp_chain.web3 assert account in web3.eth.accounts assert is_account_locked(web3, account) is True assert web3.personal.unlockAccount(account, 'a-test-password') assert is_account_locked(web3, account) is False
def test_with_locked_account(project): temp_chain = project.get_chain('temp') account = force_text( create_new_account(temp_chain.geth.data_dir, b'a-test-password') ) with temp_chain: web3 = temp_chain.web3 assert account in web3.eth.accounts assert is_account_locked(web3, account) is True assert web3.personal.unlockAccount(account, 'a-test-password') assert is_account_locked(web3, account) is False
def get_nested_key(config, key): key_head, _, key_tail = key.rpartition('.') head_getters = (operator.itemgetter(key_part) for key_part in key_head.split('.') if key_part) tail_getter = operator.itemgetter(key_tail) getter_fn = compose( *reversed(tuple(itertools.chain(head_getters, (tail_getter, ))))) try: return getter_fn(config) except TypeError as err: raise KeyError("Error getting nested key {0} from {1}: {2}".format( key, force_text(repr(config)), str(err), ))
def process_strict_type(typ): # Crazy reg expression to separate out base type component (eg. uint), # size (eg. 256, 128x128, none), array component (eg. [], [45], none) regexp = '([a-z]*)([0-9]*x?[0-9]*)((\[[0-9]*\])*)' base, sub, arr, _ = re.match(regexp, force_text(typ)).groups() arrlist = re.findall('\[[0-9]*\]', arr) if len(''.join(arrlist)) != len(arr): raise ValueError("Unknown characters found in array declaration") # Check validity of string type if base == 'string' or base == 'bytes': if not re.match('^[0-9]*$', sub): raise ValueError( "String type must have no suffix or numerical suffix") if sub and int(sub) > 32: raise ValueError("Maximum 32 bytes for fixed-length str or bytes") # Check validity of integer type elif base == 'uint' or base == 'int': if not re.match('^[0-9]+$', sub): raise ValueError("Integer type must have numerical suffix") if 8 > int(sub) or int(sub) > 256: raise ValueError("Integer size out of bounds") if int(sub) % 8 != 0: raise ValueError("Integer size must be multiple of 8") # Check validity of real type elif base == 'ureal' or base == 'real': if not re.match('^[0-9]+x[0-9]+$', sub): raise ValueError( "Real type must have suffix of form <high>x<low>, eg. 128x128") high, low = [int(x) for x in sub.split('x')] if 8 > (high + low) or (high + low) > 256: raise ValueError("Real size out of bounds (max 32 bytes)") if high % 8 != 0 or low % 8 != 0: raise ValueError("Real high/low sizes must be multiples of 8") # Check validity of hash type elif base == 'hash': if not re.match('^[0-9]+$', sub): raise ValueError("Hash type must have numerical suffix") # Check validity of address type elif base == 'address': if sub != '': raise ValueError("Address cannot have suffix") return base, sub, [ast.literal_eval(x) for x in arrlist]
def test_request_account_unlock_with_bad_password(project_dir): project = Project() chain = project.get_chain('temp') # create 3 new accounts account = force_text( create_new_account(chain.geth.data_dir, b'a-test-password')) @click.command() def wrapper(): request_account_unlock(chain, account, None) with chain: assert is_account_locked(chain.web3, account) runner = CliRunner() result = runner.invoke(wrapper, [], input="bad-password\n") assert result.exit_code != 0 assert is_account_locked(chain.web3, account)
def read_data_from_stream(cls, stream): data_length = decode_uint_256(stream) padded_length = ceil32(data_length) data = stream.read(padded_length) if len(data) < padded_length: raise InsufficientDataBytes( "Tried to read {0} bytes. Only got {1} bytes".format( padded_length, len(data), )) padding_bytes = data[data_length:] if padding_bytes != b'\x00' * (padded_length - data_length): raise NonEmptyPaddingBytes( "Padding bytes were not empty: {0}".format( force_text(padding_bytes))) return data[:data_length]
def get_nested_key(config, key): key_head, _, key_tail = key.rpartition('.') head_getters = ( operator.itemgetter(key_part) for key_part in key_head.split('.') if key_part ) tail_getter = operator.itemgetter(key_tail) getter_fn = compose(*reversed(tuple(itertools.chain(head_getters, (tail_getter,))))) try: return getter_fn(config) except TypeError as err: raise KeyError( "Error getting nested key {0} from {1}: {2}".format( key, force_text(repr(config)), str(err), ) )
def decode_rpc_response(self, response): return json.loads(force_text(response))
def second_account(local_chain): # create a new account account = create_new_account(local_chain.geth.data_dir, b'a-test-password') return force_text(account)
def toUtf8(val): return force_text(decode_hex(val))
def is_prefixed(value, prefix): return value.startswith( force_bytes(prefix) if is_bytes(value) else force_text(prefix) )
def _is_prefixed(value, prefix): return value.startswith( force_bytes(prefix) if is_bytes(value) else force_text(prefix))
def test_call_read_string_variable(string_contract): result = string_contract.call().constValue() assert force_text(result) == force_text( "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff" )
def is_predefined_block_number(value): if not is_string(value): return False return force_text(value) in {"latest", "pending", "earliest"}
def is_predefined_block_number(block_number): if not is_string(block_number): return False return force_text(block_number) in {"latest", "pending", "earliest"}
def test_eth_sign(web3, skip_if_testrpc): skip_if_testrpc(web3) private_key_hex = '0x5e95384d8050109aab08c1922d3c230739bc16976553c317e5d0b87b59371f2a' private_key = decode_hex(private_key_hex) # This imports the private key into the running geth instance and unlocks # the account so that it can sign things. # address = '0xa5df35f30ba0ce878b1061ae086289adff3ba1e0' address = web3.personal.importRawKey(private_key, "password") web3.personal.unlockAccount(address, "password") assert add_0x_prefix(encode_hex( privtoaddr(private_key))) == add_0x_prefix(address) assert address == '0xa5df35f30ba0ce878b1061ae086289adff3ba1e0' # the data to be signed data = b'1234567890abcdefghijklmnopqrstuvwxyz' # the hash of the data `0x089c33d56ed10bd8b571a0f493cedb28db1ae0f40c6cd266243001528c06eab3` data_hash = web3.sha3(data, encoding=None) data_hash_bytes = decode_hex(data_hash) assert force_bytes(data_hash) == sha3(data) priv_key = PrivateKey(flags=ALL_FLAGS) priv_key.set_raw_privkey(private_key) # sanit check the extract_ecdsa_signer function works as expected. vector_sig = priv_key.ecdsa_sign_recoverable(data_hash_bytes, raw=True, digest=hashlib.sha3_256) vector_sig_bytes, rec_id = priv_key.ecdsa_recoverable_serialize(vector_sig) vector_sig_bytes_full = vector_sig_bytes + force_bytes(chr(rec_id)) vector_address = force_text( extract_ecdsa_signer(data_hash_bytes, vector_sig_bytes_full)) assert vector_address == address # Now have geth sign some data. signature_hex = web3.eth.sign(address, data) signature_bytes = decode_hex(signature_hex) # geth prefix message before signing geth_prefix_data = eth_message_prefix_hash(web3, data.decode()) actual_signer = extract_ecdsa_signer(force_bytes(geth_prefix_data), signature_bytes) assert actual_signer == address # Verify the signature against the public key derived from the # original private key. It fails. rec_id = signature_bytes[64] if is_string(rec_id): rec_id = ord(rec_id) recoverable_signature = priv_key.ecdsa_recoverable_deserialize( signature_bytes[:64], rec_id, ) signature = priv_key.ecdsa_recoverable_convert(recoverable_signature) is_valid = priv_key.pubkey.ecdsa_verify( msg=data, raw_sig=signature, digest=hashlib.sha3_256, ) assert is_valid
to_dict, ) from web3.utils.caching import ( generate_cache_key, ) @to_dict def shuffle_dict(_dict): keys = list(_dict.keys()) random.shuffle(keys) for key in keys: yield key, _dict[key] encodable_text = st.text().map(lambda v: force_text(force_bytes(v, 'utf8'))) def extend_fn(children): lists_st = st.lists(children) dicts_st = st.dictionaries(encodable_text, children) return lists_st | dicts_st all_st = st.recursive( st.none() | st.integers() | st.booleans() | st.floats() | encodable_text | st.binary(), extend_fn, )
) from web3.utils.caching import ( generate_cache_key, ) @to_dict def shuffle_dict(_dict): keys = list(_dict.keys()) random.shuffle(keys) for key in keys: yield key, _dict[key] encodable_text = st.text().map(lambda v: force_text(force_bytes(v, 'utf8'))) def extend_fn(children): lists_st = st.lists(children) dicts_st = st.dictionaries(encodable_text, children) return lists_st | dicts_st all_st = st.recursive( st.none() | st.integers() | st.booleans() | st.floats() | encodable_text | st.binary(), extend_fn, ) def recursive_shuffle_dict(v):
def test_call_read_string_variable(string_contract): result = string_contract.call().constValue() assert force_text(result) == force_text("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff")