diff --git a/tests/core/contracts/test_contract_init.py b/tests/core/contracts/test_contract_init.py index d50e07f03e..7be650561c 100644 --- a/tests/core/contracts/test_contract_init.py +++ b/tests/core/contracts/test_contract_init.py @@ -1,5 +1,9 @@ import pytest +from eth_utils import ( + to_bytes, +) + from web3.exceptions import ( BadFunctionCallOutput, NameNotFound, @@ -11,12 +15,21 @@ @pytest.fixture -def math_addr(MathContract): +def math_deploy_receipt(MathContract): web3 = MathContract.web3 deploy_txn = MathContract.constructor().transact({'from': web3.eth.coinbase}) deploy_receipt = web3.eth.waitForTransactionReceipt(deploy_txn) assert deploy_receipt is not None - return deploy_receipt['contractAddress'] + return deploy_receipt + + +@pytest.fixture(params=['hex', 'bytes']) +def math_addr(request, math_deploy_receipt): + addr = math_deploy_receipt['contractAddress'] + if request.param == 'bytes': + return to_bytes(hexstr=addr) + else: + return addr def test_contract_with_unset_address(MathContract): diff --git a/tests/core/eth-module/test_eth_contract.py b/tests/core/eth-module/test_eth_contract.py index da23730f88..3cfc9f2d68 100644 --- a/tests/core/eth-module/test_eth_contract.py +++ b/tests/core/eth-module/test_eth_contract.py @@ -3,8 +3,13 @@ Mock, ) +from eth_utils import ( + to_bytes, +) + ABI = [{}] ADDRESS = '0xd3CdA913deB6f67967B99D67aCDFa1712C293601' +BYTES_ADDRESS = to_bytes(hexstr=ADDRESS) NON_CHECKSUM_ADDRESS = '0xd3cda913deb6f67967b99d67acdfa1712c293601' INVALID_CHECKSUM_ADDRESS = '0xd3CDA913deB6f67967B99D67aCDFa1712C293601' @@ -13,6 +18,7 @@ 'args,kwargs,expected', ( ((ADDRESS,), {}, None), + ((BYTES_ADDRESS,), {}, None), ((INVALID_CHECKSUM_ADDRESS,), {}, ValueError), ((NON_CHECKSUM_ADDRESS,), {}, ValueError), ((), {'address': ADDRESS}, None), diff --git a/tests/core/utilities/test_validation.py b/tests/core/utilities/test_validation.py index c50c5db297..618bcb5267 100644 --- a/tests/core/utilities/test_validation.py +++ b/tests/core/utilities/test_validation.py @@ -1,5 +1,9 @@ import pytest +from eth_utils import ( + to_bytes, +) + from web3.exceptions import ( InvalidAddress, ) @@ -64,9 +68,12 @@ ] ADDRESS = '0xd3CdA913deB6f67967B99D67aCDFa1712C293601' +BYTES_ADDRESS = to_bytes(hexstr=ADDRESS) PADDED_ADDRESS = '0x000000000000000000000000d3cda913deb6f67967b99d67acdfa1712c293601' INVALID_CHECKSUM_ADDRESS = '0xd3CDA913deB6f67967B99D67aCDFa1712C293601' NON_CHECKSUM_ADDRESS = '0xd3cda913deb6f67967b99d67acdfa1712c293601' +BYTES_ADDRESS_LEN_LT_20 = bytes(1) * 19 +BYTES_ADDRESS_LEN_GT_20 = bytes(1) * 21 @pytest.mark.parametrize( @@ -78,9 +85,12 @@ (MALFORMED_SELECTOR_COLLISION_ABI, validate_abi, ValueError), (MALFORMED_SIGNATURE_COLLISION_ABI, validate_abi, ValueError), (ADDRESS, validate_address, None), + (BYTES_ADDRESS, validate_address, None), (PADDED_ADDRESS, validate_address, InvalidAddress), (INVALID_CHECKSUM_ADDRESS, validate_address, InvalidAddress), (NON_CHECKSUM_ADDRESS, validate_address, InvalidAddress), + (BYTES_ADDRESS_LEN_LT_20, validate_address, InvalidAddress), + (BYTES_ADDRESS_LEN_GT_20, validate_address, InvalidAddress), ("NotAddress", validate_address, InvalidAddress), (b'not string', validate_address, InvalidAddress), ('bool', validate_abi_type, None), diff --git a/tests/integration/go_ethereum/conftest.py b/tests/integration/go_ethereum/conftest.py index 242c995c2e..c779a6bc96 100644 --- a/tests/integration/go_ethereum/conftest.py +++ b/tests/integration/go_ethereum/conftest.py @@ -7,6 +7,7 @@ from eth_utils import ( is_checksum_address, is_dict, + to_bytes, to_text, ) @@ -172,6 +173,14 @@ def emitter_contract(web3, emitter_contract_factory, geth_fixture_data): return emitter_contract_factory(address=geth_fixture_data['emitter_address']) +@pytest.fixture(scope="module", params=['bytes', 'hex']) +def emitter_contract_address(emitter_contract, request): + if request.param == 'bytes': + return to_bytes(hexstr=emitter_contract.address) + else: + return emitter_contract.address + + @pytest.fixture def unlocked_account(web3, unlockable_account, unlockable_account_pw): web3.personal.unlockAccount(unlockable_account, unlockable_account_pw) @@ -189,6 +198,21 @@ def unlockable_account(web3, coinbase): yield coinbase +@pytest.fixture(params=['bytes', 'hex']) +def unlockable_account_dual_type(unlockable_account, request): + if request.param == 'bytes': + return to_bytes(hexstr=unlockable_account) + else: + return unlockable_account + + +@pytest.yield_fixture +def unlocked_account_dual_type(web3, unlockable_account_dual_type, unlockable_account_pw): + web3.personal.unlockAccount(unlockable_account_dual_type, unlockable_account_pw) + yield unlockable_account_dual_type + web3.personal.lockAccount(unlockable_account_dual_type) + + @pytest.fixture(scope="module") def funded_account_for_raw_txn(geth_fixture_data): account = geth_fixture_data['raw_txn_account'] diff --git a/tests/integration/parity/common.py b/tests/integration/parity/common.py index d5113a9822..3e05c180a3 100644 --- a/tests/integration/parity/common.py +++ b/tests/integration/parity/common.py @@ -196,8 +196,8 @@ def test_trace_block(self, web3, block_with_txn): def test_trace_transaction(self, web3, parity_fixture_data): super().test_trace_transaction(web3, parity_fixture_data) - def test_trace_call(self, web3, math_contract): - super().test_trace_call(web3, math_contract) + def test_trace_call(self, web3, math_contract, math_contract_address): + super().test_trace_call(web3, math_contract, math_contract_address) - def test_eth_call_with_0_result(self, web3, math_contract): - super().test_eth_call_with_0_result(web3, math_contract) + def test_eth_call_with_0_result(self, web3, math_contract, math_contract_address): + super().test_eth_call_with_0_result(web3, math_contract, math_contract_address) diff --git a/tests/integration/parity/conftest.py b/tests/integration/parity/conftest.py index 2d825f0d37..cf09584b1c 100644 --- a/tests/integration/parity/conftest.py +++ b/tests/integration/parity/conftest.py @@ -6,6 +6,7 @@ from eth_utils import ( is_checksum_address, is_dict, + to_bytes, ) from .install_parity import ( @@ -138,11 +139,27 @@ def math_contract(web3, math_contract_factory, parity_fixture_data): return math_contract_factory(address=parity_fixture_data['math_address']) +@pytest.fixture(scope="module", params=['bytes', 'hex']) +def math_contract_address(math_contract, request): + if request.param == 'bytes': + return math_contract.address + else: + return math_contract.address + + @pytest.fixture(scope="module") def emitter_contract(web3, emitter_contract_factory, parity_fixture_data): return emitter_contract_factory(address=parity_fixture_data['emitter_address']) +@pytest.fixture(scope="module", params=['bytes', 'hex']) +def emitter_contract_address(emitter_contract, request): + if request.param == 'bytes': + return to_bytes(hexstr=emitter_contract.address) + else: + return emitter_contract.address + + @pytest.fixture(scope="module") def unlocked_account(web3, unlockable_account, unlockable_account_pw): yield unlockable_account @@ -158,6 +175,19 @@ def unlockable_account(web3, coinbase): yield coinbase +@pytest.fixture(params=['bytes', 'hex']) +def unlockable_account_dual_type(unlockable_account, request): + if request.param == 'bytes': + return to_bytes(hexstr=unlockable_account) + else: + return unlockable_account + + +@pytest.fixture +def unlocked_account_dual_type(unlockable_account_dual_type): + return unlockable_account_dual_type + + @pytest.fixture(scope="module") def funded_account_for_raw_txn(parity_fixture_data): account = parity_fixture_data['raw_txn_account'] diff --git a/tests/integration/test_ethereum_tester.py b/tests/integration/test_ethereum_tester.py index ef495de409..dfb604aca6 100644 --- a/tests/integration/test_ethereum_tester.py +++ b/tests/integration/test_ethereum_tester.py @@ -7,6 +7,7 @@ from eth_utils import ( is_checksum_address, is_dict, + to_bytes, ) from web3 import Web3 @@ -63,6 +64,14 @@ def math_contract(web3, math_contract_factory, math_contract_deploy_txn_hash): return math_contract_factory(contract_address) +@pytest.fixture(scope="module", params=['bytes', 'hex']) +def math_contract_address(math_contract, request): + if request.param == 'bytes': + return math_contract.address + else: + return math_contract.address + + # # Emitter Contract Setup # @@ -81,6 +90,14 @@ def emitter_contract(web3, emitter_contract_factory, emitter_contract_deploy_txn return emitter_contract_factory(contract_address) +@pytest.fixture(scope="module", params=['bytes', 'hex']) +def emitter_contract_address(emitter_contract, request): + if request.param == 'bytes': + return to_bytes(hexstr=emitter_contract.address) + else: + return emitter_contract.address + + @pytest.fixture(scope="module") def empty_block(web3): web3.testing.mine() @@ -151,6 +168,21 @@ def unlocked_account(web3, unlockable_account, unlockable_account_pw): web3.personal.lockAccount(unlockable_account) +@pytest.fixture(params=['bytes', 'hex']) +def unlockable_account_dual_type(unlockable_account, request): + if request.param == 'bytes': + return to_bytes(hexstr=unlockable_account) + else: + return unlockable_account + + +@pytest.fixture +def unlocked_account_dual_type(web3, unlockable_account_dual_type, unlockable_account_pw): + web3.personal.unlockAccount(unlockable_account_dual_type, unlockable_account_pw) + yield unlockable_account_dual_type + web3.personal.lockAccount(unlockable_account_dual_type) + + @pytest.fixture(scope="module") def funded_account_for_raw_txn(web3): account = '0x39EEed73fb1D3855E90Cbd42f348b3D7b340aAA6' diff --git a/web3/utils/module_testing/eth_module.py b/web3/utils/module_testing/eth_module.py index 4ae3baaa6f..bf5d29fb12 100644 --- a/web3/utils/module_testing/eth_module.py +++ b/web3/utils/module_testing/eth_module.py @@ -23,7 +23,7 @@ InvalidAddress, ) -UNKNOWN_ADDRESS = '0xdeadbeef00000000000000000000000000000000' +UNKNOWN_ADDRESS = '0xdEADBEeF00000000000000000000000000000000' UNKNOWN_HASH = '0xdeadbeef00000000000000000000000000000000000000000000000000000000' @@ -153,28 +153,34 @@ def test_eth_getCode(self, web3, math_contract): assert is_string(code) assert len(code) > 2 - def test_eth_sign(self, web3, unlocked_account): - signature = web3.eth.sign(unlocked_account, text='Message tö sign. Longer than hash!') + def test_eth_sign(self, web3, unlocked_account_dual_type): + signature = web3.eth.sign( + unlocked_account_dual_type, text='Message tö sign. Longer than hash!' + ) assert is_bytes(signature) assert len(signature) == 32 + 32 + 1 # test other formats hexsign = web3.eth.sign( - unlocked_account, + unlocked_account_dual_type, hexstr='0x4d6573736167652074c3b6207369676e2e204c6f6e676572207468616e206861736821' ) assert hexsign == signature intsign = web3.eth.sign( - unlocked_account, + unlocked_account_dual_type, 0x4d6573736167652074c3b6207369676e2e204c6f6e676572207468616e206861736821 ) assert intsign == signature - bytessign = web3.eth.sign(unlocked_account, b'Message t\xc3\xb6 sign. Longer than hash!') + bytessign = web3.eth.sign( + unlocked_account_dual_type, b'Message t\xc3\xb6 sign. Longer than hash!' + ) assert bytessign == signature - new_signature = web3.eth.sign(unlocked_account, text='different message is different') + new_signature = web3.eth.sign( + unlocked_account_dual_type, text='different message is different' + ) assert new_signature != signature def test_eth_sendTransaction_addr_checksum_required(self, web3, unlocked_account): @@ -195,10 +201,10 @@ def test_eth_sendTransaction_addr_checksum_required(self, web3, unlocked_account invalid_params = dict(txn_params, **{'to': non_checksum_addr}) web3.eth.sendTransaction(invalid_params) - def test_eth_sendTransaction(self, web3, unlocked_account): + def test_eth_sendTransaction(self, web3, unlocked_account_dual_type): txn_params = { - 'from': unlocked_account, - 'to': unlocked_account, + 'from': unlocked_account_dual_type, + 'to': unlocked_account_dual_type, 'value': 1, 'gas': 21000, 'gasPrice': web3.eth.gasPrice, @@ -232,10 +238,10 @@ def test_eth_sendTransaction_with_nonce(self, web3, unlocked_account): assert txn['gasPrice'] == txn_params['gasPrice'] assert txn['nonce'] == txn_params['nonce'] - def test_eth_replaceTransaction(self, web3, unlocked_account): + def test_eth_replaceTransaction(self, web3, unlocked_account_dual_type): txn_params = { - 'from': unlocked_account, - 'to': unlocked_account, + 'from': unlocked_account_dual_type, + 'to': unlocked_account_dual_type, 'value': 1, 'gas': 21000, 'gasPrice': web3.eth.gasPrice, @@ -252,10 +258,11 @@ def test_eth_replaceTransaction(self, web3, unlocked_account): assert replace_txn['gas'] == 21000 assert replace_txn['gasPrice'] == txn_params['gasPrice'] - def test_eth_replaceTransaction_non_existing_transaction(self, web3, unlocked_account): + def test_eth_replaceTransaction_non_existing_transaction( + self, web3, unlocked_account_dual_type): txn_params = { - 'from': unlocked_account, - 'to': unlocked_account, + 'from': unlocked_account_dual_type, + 'to': unlocked_account_dual_type, 'value': 1, 'gas': 21000, 'gasPrice': web3.eth.gasPrice, @@ -267,10 +274,10 @@ def test_eth_replaceTransaction_non_existing_transaction(self, web3, unlocked_ac ) # auto mine is enabled for this test - def test_eth_replaceTransaction_already_mined(self, web3, unlocked_account): + def test_eth_replaceTransaction_already_mined(self, web3, unlocked_account_dual_type): txn_params = { - 'from': unlocked_account, - 'to': unlocked_account, + 'from': unlocked_account_dual_type, + 'to': unlocked_account_dual_type, 'value': 1, 'gas': 21000, 'gasPrice': web3.eth.gasPrice, @@ -297,10 +304,10 @@ def test_eth_replaceTransaction_incorrect_nonce(self, web3, unlocked_account): with pytest.raises(ValueError): web3.eth.replaceTransaction(txn_hash, txn_params) - def test_eth_replaceTransaction_gas_price_too_low(self, web3, unlocked_account): + def test_eth_replaceTransaction_gas_price_too_low(self, web3, unlocked_account_dual_type): txn_params = { - 'from': unlocked_account, - 'to': unlocked_account, + 'from': unlocked_account_dual_type, + 'to': unlocked_account_dual_type, 'value': 1, 'gas': 21000, 'gasPrice': 10, @@ -529,10 +536,10 @@ def test_eth_getTransactionReceipt_mined(self, web3, block_with_txn, mined_txn_h assert receipt['transactionIndex'] == 0 assert receipt['transactionHash'] == HexBytes(mined_txn_hash) - def test_eth_getTransactionReceipt_unmined(self, web3, unlocked_account): + def test_eth_getTransactionReceipt_unmined(self, web3, unlocked_account_dual_type): txn_hash = web3.eth.sendTransaction({ - 'from': unlocked_account, - 'to': unlocked_account, + 'from': unlocked_account_dual_type, + 'to': unlocked_account_dual_type, 'value': 1, 'gas': 21000, 'gasPrice': web3.eth.gasPrice, @@ -664,7 +671,7 @@ def test_eth_getLogs_with_logs( self, web3, block_with_txn_with_log, - emitter_contract, + emitter_contract_address, txn_hash_with_log): def assert_contains_log(result): @@ -673,7 +680,7 @@ def assert_contains_log(result): assert log_entry['blockNumber'] == block_with_txn_with_log['number'] assert log_entry['blockHash'] == block_with_txn_with_log['hash'] assert log_entry['logIndex'] == 0 - assert is_same_address(log_entry['address'], emitter_contract.address) + assert is_same_address(log_entry['address'], emitter_contract_address) assert log_entry['transactionIndex'] == 0 assert log_entry['transactionHash'] == HexBytes(txn_hash_with_log) @@ -699,7 +706,7 @@ def assert_contains_log(result): # filter with emitter_contract.address filter_params = { "fromBlock": 0, - "address": emitter_contract.address, + "address": emitter_contract_address, } result = web3.eth.getLogs(filter_params) assert_contains_log(result) diff --git a/web3/utils/module_testing/parity_module.py b/web3/utils/module_testing/parity_module.py index 1dda766f4f..c3a6ac39f2 100644 --- a/web3/utils/module_testing/parity_module.py +++ b/web3/utils/module_testing/parity_module.py @@ -39,12 +39,12 @@ def test_trace_transaction(self, web3, parity_fixture_data): trace = web3.parity.traceTransaction(parity_fixture_data['mined_txn_hash']) assert trace[0]['action']['from'] == add_0x_prefix(parity_fixture_data['coinbase']) - def test_trace_call(self, web3, math_contract): + def test_trace_call(self, web3, math_contract, math_contract_address): coinbase = web3.eth.coinbase txn_params = math_contract._prepare_transaction( fn_name='add', fn_args=(7, 11), - transaction={'from': coinbase, 'to': math_contract.address}, + transaction={'from': coinbase, 'to': math_contract_address}, ) trace = web3.parity.traceCall(txn_params) assert trace['stateDiff'] is None @@ -52,12 +52,12 @@ def test_trace_call(self, web3, math_contract): result = hex_to_integer(trace['output']) assert result == 18 - def test_eth_call_with_0_result(self, web3, math_contract): + def test_eth_call_with_0_result(self, web3, math_contract, math_contract_address): coinbase = web3.eth.coinbase txn_params = math_contract._prepare_transaction( fn_name='add', fn_args=(0, 0), - transaction={'from': coinbase, 'to': math_contract.address}, + transaction={'from': coinbase, 'to': math_contract_address}, ) trace = web3.parity.traceCall(txn_params) assert trace['stateDiff'] is None diff --git a/web3/utils/module_testing/personal_module.py b/web3/utils/module_testing/personal_module.py index 7f2768c960..9853399fb6 100644 --- a/web3/utils/module_testing/personal_module.py +++ b/web3/utils/module_testing/personal_module.py @@ -28,21 +28,21 @@ def test_personal_listAccounts(self, web3): in accounts )) - def test_personal_lockAccount(self, web3, unlocked_account): + def test_personal_lockAccount(self, web3, unlockable_account_dual_type): # TODO: how do we test this better? - web3.personal.lockAccount(unlocked_account) + web3.personal.lockAccount(unlockable_account_dual_type) def test_personal_unlockAccount_success(self, web3, - unlockable_account, + unlockable_account_dual_type, unlockable_account_pw): - result = web3.personal.unlockAccount(unlockable_account, unlockable_account_pw) + result = web3.personal.unlockAccount(unlockable_account_dual_type, unlockable_account_pw) assert result is True def test_personal_unlockAccount_failure(self, web3, - unlockable_account): - result = web3.personal.unlockAccount(unlockable_account, 'bad-password') + unlockable_account_dual_type): + result = web3.personal.unlockAccount(unlockable_account_dual_type, 'bad-password') assert result is False def test_personal_newAccount(self, web3): @@ -51,12 +51,12 @@ def test_personal_newAccount(self, web3): def test_personal_sendTransaction(self, web3, - unlockable_account, + unlockable_account_dual_type, unlockable_account_pw): - assert web3.eth.getBalance(unlockable_account) > web3.toWei(1, 'ether') + assert web3.eth.getBalance(unlockable_account_dual_type) > web3.toWei(1, 'ether') txn_params = { - 'from': unlockable_account, - 'to': unlockable_account, + 'from': unlockable_account_dual_type, + 'to': unlockable_account_dual_type, 'gas': 21000, 'value': 1, 'gasPrice': web3.toWei(1, 'gwei'), @@ -64,17 +64,18 @@ def test_personal_sendTransaction(self, txn_hash = web3.personal.sendTransaction(txn_params, unlockable_account_pw) assert txn_hash transaction = web3.eth.getTransaction(txn_hash) - assert transaction['from'] == txn_params['from'] - assert transaction['to'] == txn_params['to'] + + assert is_same_address(transaction['from'], txn_params['from']) + assert is_same_address(transaction['to'], txn_params['to']) assert transaction['gas'] == txn_params['gas'] assert transaction['value'] == txn_params['value'] assert transaction['gasPrice'] == txn_params['gasPrice'] def test_personal_sign_and_ecrecover(self, web3, - unlockable_account, + unlockable_account_dual_type, unlockable_account_pw): message = 'test-web3-personal-sign' - signature = web3.personal.sign(message, unlockable_account, unlockable_account_pw) + signature = web3.personal.sign(message, unlockable_account_dual_type, unlockable_account_pw) signer = web3.personal.ecRecover(message, signature) - assert is_same_address(signer, unlockable_account) + assert is_same_address(signer, unlockable_account_dual_type) diff --git a/web3/utils/rpc_abi.py b/web3/utils/rpc_abi.py index 5145627e3e..ee2e6abdd3 100644 --- a/web3/utils/rpc_abi.py +++ b/web3/utils/rpc_abi.py @@ -24,6 +24,7 @@ FILTER_PARAMS_ABIS = { 'to': 'address', + 'address': 'address' } RPC_ABIS = { @@ -48,7 +49,8 @@ # personal 'personal_sendTransaction': TRANSACTION_PARAMS_ABIS, 'personal_lockAccount': ['address'], - 'personal_unlockAccount': ['address', None, None] + 'personal_unlockAccount': ['address', None, None], + 'personal_sign': [None, 'address', None] }