def test_auto_gas_computation_when_transacting(web3, STRING_CONTRACT, skip_if_testrpc, wait_for_block): skip_if_testrpc(web3) wait_for_block(web3) StringContract = web3.eth.contract(**STRING_CONTRACT) deploy_txn = StringContract.deploy(args=["Caqalai"]) deploy_receipt = wait_for_transaction_receipt(web3, deploy_txn, 30) assert deploy_receipt is not None string_contract = StringContract(address=deploy_receipt['contractAddress']) gas_estimate = string_contract.estimateGas().setValue( force_bytes("ÄLÄMÖLÖ")) # eth_abi will pass as raw bytes, no encoding # unless we encode ourselves txn_hash = string_contract.transact().setValue(force_bytes("ÄLÄMÖLÖ")) txn_receipt = wait_for_transaction_receipt(web3, txn_hash, 30) assert txn_receipt is not None final_value = string_contract.call().getValue() assert force_bytes(final_value) == force_bytes("ÄLÄMÖLÖ") txn = web3.eth.getTransaction(txn_hash) assert txn['gas'] == gas_estimate + 100000
def _encode_abi(cls, abi, arguments, data=None): argument_types = get_abi_input_types(abi) if not check_if_arguments_can_be_encoded(abi, arguments, {}): raise TypeError( "One or more arguments could not be encoded to the necessary " "ABI type. Expected types are: {0}".format( ', '.join(argument_types), )) try: encoded_arguments = encode_abi( argument_types, force_obj_to_bytes(arguments), ) except EncodingError as e: raise TypeError( "One or more arguments could not be encoded to the necessary " "ABI type: {0}".format(str(e))) if data: return add_0x_prefix( force_bytes(remove_0x_prefix(data)) + force_bytes(remove_0x_prefix(encode_hex(encoded_arguments)))) else: return encode_hex(encoded_arguments)
def test_transacting_with_contract_respects_explicit_gas( web3, STRING_CONTRACT, skip_if_testrpc, wait_for_block): skip_if_testrpc(web3) wait_for_block(web3) StringContract = web3.eth.contract(**STRING_CONTRACT) deploy_txn = StringContract.deploy(arguments=["Caqalai"]) deploy_receipt = wait_for_transaction_receipt(web3, deploy_txn, 30) assert deploy_receipt is not None string_contract = StringContract(address=deploy_receipt['contractAddress']) # eth_abi will pass as raw bytes, no encoding # unless we encode ourselves txn_hash = string_contract.transact({ 'gas': 200000 }).setValue(force_bytes("ÄLÄMÖLÖ")) txn_receipt = wait_for_transaction_receipt(web3, txn_hash, 30) assert txn_receipt is not None final_value = string_contract.call().getValue() assert force_bytes(final_value) == force_bytes("ÄLÄMÖLÖ") txn = web3.eth.getTransaction(txn_hash) assert txn['gas'] == 200000
def _encode_abi(cls, abi, arguments, data=None): argument_types = get_abi_input_types(abi) if not check_if_arguments_can_be_encoded(abi, arguments, {}): raise TypeError( "One or more arguments could not be encoded to the necessary " "ABI type. Expected types are: {0}".format( ', '.join(argument_types), ) ) try: encoded_arguments = encode_abi( argument_types, force_obj_to_bytes(arguments), ) except EncodingError as e: raise TypeError( "One or more arguments could not be encoded to the necessary " "ABI type: {0}".format(str(e)) ) if data: return add_0x_prefix( force_bytes(remove_0x_prefix(data)) + force_bytes(remove_0x_prefix(encode_hex(encoded_arguments))) ) else: return encode_hex(encoded_arguments)
def test_transacting_with_contract_with_string_argument(web3, string_contract): # eth_abi will pass as raw bytes, no encoding # unless we encode ourselves txn_hash = string_contract.transact().setValue(force_bytes("ÄLÄMÖLÖ")) txn_receipt = web3.eth.getTransactionReceipt(txn_hash) assert txn_receipt is not None final_value = string_contract.call().getValue() assert force_bytes(final_value) == force_bytes("ÄLÄMÖLÖ")
def _encodeABI(cls, abi, arguments, data=None): arguent_types = get_abi_input_types(abi) encoded_arguments = encode_abi(arguent_types, force_obj_to_bytes(arguments)) if data: return add_0x_prefix( force_bytes(remove_0x_prefix(data)) + force_bytes(remove_0x_prefix(encode_hex(encoded_arguments))) ) else: return encode_hex(encoded_arguments)
def _encodeABI(cls, abi, arguments, data=None): arguent_types = get_abi_input_types(abi) encoded_arguments = encode_abi(arguent_types, force_obj_to_bytes(arguments)) if data: return add_0x_prefix( force_bytes(remove_0x_prefix(data)) + force_bytes(remove_0x_prefix(encode_hex(encoded_arguments)))) else: return encode_hex(encoded_arguments)
def test_contract_deployment_no_constructor(web3, MathContract, MATH_RUNTIME): deploy_txn = MathContract.deploy() txn_receipt = web3.eth.getTransactionReceipt(deploy_txn) assert txn_receipt is not None assert txn_receipt['contractAddress'] contract_address = txn_receipt['contractAddress'] blockchain_code = web3.eth.getCode(contract_address) assert force_bytes(blockchain_code) == force_bytes(MATH_RUNTIME)
def test_contract_deployment_no_constructor(web3_tester, MathContract, MATH_RUNTIME): deploy_txn = MathContract.deploy() txn_receipt = web3_tester.eth.getTransactionReceipt(deploy_txn) assert txn_receipt is not None assert txn_receipt['contractAddress'] contract_address = txn_receipt['contractAddress'] blockchain_code = web3_tester.eth.getCode(contract_address) assert force_bytes(blockchain_code) == force_bytes(MATH_RUNTIME)
def test_contract_deployment_with_constructor_with_arguments(web3_tester, WithConstructorArgumentsContract, WITH_CONSTRUCTOR_ARGUMENTS_RUNTIME): deploy_txn = WithConstructorArgumentsContract.deploy(arguments=[1234, 'abcd']) txn_receipt = web3_tester.eth.getTransactionReceipt(deploy_txn) assert txn_receipt is not None assert txn_receipt['contractAddress'] contract_address = txn_receipt['contractAddress'] blockchain_code = web3_tester.eth.getCode(contract_address) assert force_bytes(blockchain_code) == force_bytes(WITH_CONSTRUCTOR_ARGUMENTS_RUNTIME)
def test_contract_deployment_with_constructor_without_args(web3_tester, SimpleConstructorContract, SIMPLE_CONSTRUCTOR_RUNTIME): deploy_txn = SimpleConstructorContract.deploy() txn_receipt = web3_tester.eth.getTransactionReceipt(deploy_txn) assert txn_receipt is not None assert txn_receipt['contractAddress'] contract_address = txn_receipt['contractAddress'] blockchain_code = web3_tester.eth.getCode(contract_address) assert force_bytes(blockchain_code) == force_bytes(SIMPLE_CONSTRUCTOR_RUNTIME)
def test_contract_deployment_with_constructor_with_address_argument(web3_tester, WithConstructorAddressArgumentsContract, WITH_CONSTRUCTOR_ADDRESS_RUNTIME): deploy_txn = WithConstructorAddressArgumentsContract.deploy(args=["0x16d9983245de15e7a9a73bc586e01ff6e08de737"]) txn_receipt = web3_tester.eth.getTransactionReceipt(deploy_txn) assert txn_receipt is not None assert txn_receipt['contractAddress'] contract_address = txn_receipt['contractAddress'] blockchain_code = web3_tester.eth.getCode(contract_address) assert force_bytes(blockchain_code) == force_bytes(WITH_CONSTRUCTOR_ADDRESS_RUNTIME)
def test_contract_deployment_with_constructor_without_args( web3, SimpleConstructorContract, SIMPLE_CONSTRUCTOR_RUNTIME): deploy_txn = SimpleConstructorContract.deploy() txn_receipt = web3.eth.getTransactionReceipt(deploy_txn) assert txn_receipt is not None assert txn_receipt['contractAddress'] contract_address = txn_receipt['contractAddress'] blockchain_code = web3.eth.getCode(contract_address) assert force_bytes(blockchain_code) == force_bytes( SIMPLE_CONSTRUCTOR_RUNTIME)
def test_contract_deployment_with_constructor_with_arguments( web3, WithConstructorArgumentsContract, WITH_CONSTRUCTOR_ARGUMENTS_RUNTIME): deploy_txn = WithConstructorArgumentsContract.deploy(args=[1234, 'abcd']) txn_receipt = web3.eth.getTransactionReceipt(deploy_txn) assert txn_receipt is not None assert txn_receipt['contractAddress'] contract_address = txn_receipt['contractAddress'] blockchain_code = web3.eth.getCode(contract_address) assert force_bytes(blockchain_code) == force_bytes( WITH_CONSTRUCTOR_ARGUMENTS_RUNTIME)
def test_contract_deployment_with_constructor_with_address_argument( web3, WithConstructorAddressArgumentsContract, WITH_CONSTRUCTOR_ADDRESS_RUNTIME): deploy_txn = WithConstructorAddressArgumentsContract.deploy( args=["0x16d9983245de15e7a9a73bc586e01ff6e08de737"]) txn_receipt = web3.eth.getTransactionReceipt(deploy_txn) assert txn_receipt is not None assert txn_receipt['contractAddress'] contract_address = txn_receipt['contractAddress'] blockchain_code = web3.eth.getCode(contract_address) assert force_bytes(blockchain_code) == force_bytes( WITH_CONSTRUCTOR_ADDRESS_RUNTIME)
def encode_rpc_request(self, method, params): return force_bytes(json.dumps(force_obj_to_text({ "jsonrpc": "2.0", "method": method, "params": params or [], "id": next(self.request_counter), })))
def test_adding_signature_to_db(chain, signature_db): assert signature_db.call().isKnownSelector(ABI_A_SELECTOR_BYTES) is False assert signature_db.call().isKnownSignature(ABI_A_SIGNATURE) is False create_kwargs = function_definition_to_kwargs(ABI_A) chain.wait.for_receipt( signature_db.transact().addSignature(**create_kwargs)) assert signature_db.call().isKnownSelector(ABI_A_SELECTOR_BYTES) is True assert signature_db.call().isKnownSignature(ABI_A_SIGNATURE) is True assert signature_db.call().numSignatures(ABI_A_SELECTOR_BYTES) == 1 signature_hash = signature_db.call().getSignatureHash( ABI_A_SELECTOR_BYTES, 0) signature_hash_bytes = force_bytes(signature_hash) assert signature_hash_bytes.startswith(ABI_A_SELECTOR_BYTES) signature = signature_db.call().getSignature(signature_hash_bytes) assert signature == ABI_A_SIGNATURE assert signature_db.call().getSignature(ABI_A_SELECTOR_BYTES, 0) == signature assert signature_db.call().getAllSignatureHashes(ABI_A_SELECTOR_BYTES) == [ signature_hash ]
def test_eth_call_with_no_args(web3, wait_for_transaction, MATH_CODE, MATH_RUNTIME): txn_hash = web3.eth.sendTransaction({ "from": web3.eth.coinbase, "data": MATH_CODE, "gas": 3000000, }) wait_for_transaction(web3, txn_hash) txn_receipt = web3.eth.getTransactionReceipt(txn_hash) contract_address = txn_receipt['contractAddress'] assert force_bytes(web3.eth.getCode(contract_address)) == MATH_RUNTIME abi_signature = web3.sha3("return13()", encoding=None)[:10] # sanity assert abi_signature == '0x16216f39' actual_result_hex = web3.eth.call({ "from": web3.eth.coinbase, "to": contract_address, "data": abi_signature, }) actual_result = int(actual_result_hex, 16) assert actual_result == 13
def encode_rpc_request(self, method, params): return force_bytes( json.dumps( force_obj_to_text({ "jsonrpc": "2.0", "method": method, "params": params or [], "id": next(self.request_counter), })))
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 test_eth_sendTransaction_auto_estimates_gas_if_not_provided(web3, wait_for_transaction, MATH_CODE, MATH_RUNTIME): txn_hash = web3.eth.sendTransaction({ "from": web3.eth.coinbase, "data": MATH_CODE, }) wait_for_transaction(web3, txn_hash) txn_receipt = web3.eth.getTransactionReceipt(txn_hash) contract_address = txn_receipt['contractAddress'] assert force_bytes(web3.eth.getCode(contract_address)) == MATH_RUNTIME
def test_eth_sendTransaction_auto_estimates_gas_if_not_provided( web3, wait_for_transaction, MATH_CODE, MATH_RUNTIME): txn_hash = web3.eth.sendTransaction({ "from": web3.eth.coinbase, "data": MATH_CODE, }) wait_for_transaction(web3, txn_hash) txn_receipt = web3.eth.getTransactionReceipt(txn_hash) contract_address = txn_receipt['contractAddress'] assert force_bytes(web3.eth.getCode(contract_address)) == MATH_RUNTIME
def test_eth_sendTransaction_with_data(web3, wait_for_transaction, MATH_CODE, MATH_RUNTIME): txn_hash = web3.eth.sendTransaction({ "from": web3.eth.coinbase, "data": MATH_CODE, "gas": 3000000, }) wait_for_transaction(web3, txn_hash) txn_receipt = web3.eth.getTransactionReceipt(txn_hash) contract_address = txn_receipt['contractAddress'] assert force_bytes(web3.eth.getCode(contract_address)) == MATH_RUNTIME
def test_eth_getCode(web3, wait_for_transaction, MATH_CODE, MATH_RUNTIME): txn_hash = web3.eth.sendTransaction({ "from": web3.eth.coinbase, "data": MATH_CODE, "gas": 3000000, }) wait_for_transaction(web3, txn_hash) txn_receipt = web3.eth.getTransactionReceipt(txn_hash) contract_address = txn_receipt['contractAddress'] assert force_bytes(web3.eth.getCode(contract_address)) == MATH_RUNTIME
def test_eth_call_with_no_args(web3, wait_for_transaction, MATH_CODE, MATH_RUNTIME): txn_hash = web3.eth.sendTransaction({ "from": web3.eth.coinbase, "data": MATH_CODE, "gas": 3000000, }) wait_for_transaction(txn_hash) txn_receipt = web3.eth.getTransactionReceipt(txn_hash) contract_address = txn_receipt['contractAddress'] assert force_bytes(web3.eth.getCode(contract_address)) == MATH_RUNTIME abi_signature = web3.sha3("return13()", encoding=None)[:10] # sanity assert abi_signature == '0x16216f39' actual_result_hex = web3.eth.call({ "from": web3.eth.coinbase, "to": contract_address, "data": abi_signature, }) actual_result = int(actual_result_hex, 16) assert actual_result == 13
def _inner_sign(account, data_to_sign): signature_hash_hex = web3.sha3(encode_hex(data_to_sign)) signature_hash_bytes = decode_hex(signature_hash_hex) private_key = tester.keys[tester.accounts.index(decode_hex(account))] priv_key = PrivateKey(flags=ALL_FLAGS) priv_key.set_raw_privkey(private_key) signature_raw = priv_key.ecdsa_sign_recoverable( signature_hash_bytes, raw=True, digest=sha3_256, ) signature_bytes, rec_id = priv_key.ecdsa_recoverable_serialize(signature_raw) signature = signature_bytes + force_bytes(chr(rec_id)) # Sanity check that the signature is valid. signer_address = force_text(extract_ecdsa_signer( signature_hash_bytes, signature, )) assert signer_address == account return signature
def test_eth_sign(web3_empty, skip_if_testrpc): web3 = web3_empty 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 test_eth_sign(web3_empty, skip_if_testrpc): web3 = web3_empty 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 event_topic(event_signature): from web3.utils.string import force_bytes return force_bytes("0x" + sha3_256(force_bytes(event_signature)).hexdigest())