Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add warnings for web3.sha3, middleware_stack, and python 3.5 #1020

Merged
merged 6 commits into from
Sep 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ Fetch various data about the current state of the ERC20 contract.
>>> erc20.functions.decimals().call()
0
>>> erc20.functions.totalSupply().call()
2038
2039

Get the balance of an account address.

Expand Down
2 changes: 1 addition & 1 deletion tests/core/contracts/test_extracting_event_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_dynamic_length_argument_extraction(web3,
event_topic = emitter_log_topics.LogDynamicArgs
assert event_topic in log_entry['topics']

string_0_topic = web3.sha3(text=string_0)
string_0_topic = web3.keccak(text=string_0)
assert string_0_topic in log_entry['topics']

event_data = get_event_data(event_abi, log_entry)
Expand Down
2 changes: 1 addition & 1 deletion tests/core/contracts/test_extracting_event_data_old.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ def test_dynamic_length_argument_extraction(web3,
event_topic = emitter_log_topics.LogDynamicArgs
assert event_topic in log_entry['topics']

string_0_topic = web3.sha3(text=string_0)
string_0_topic = web3.keccak(text=string_0)
assert string_0_topic in log_entry['topics']

event_data = get_event_data(event_abi, log_entry)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@
from web3 import Web3


@pytest.mark.parametrize(
'message, digest',
[
('cowmö', HexBytes('0x0f355f04c0a06eebac1d219b34c598f85a1169badee164be8a30345944885fe8')),
('', HexBytes('0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')),
],
)
def test_keccak_text(message, digest):
assert Web3.keccak(text=message) == digest


@pytest.mark.parametrize(
'message, digest',
[
Expand All @@ -17,7 +28,33 @@
],
)
def test_sha3_text(message, digest):
assert Web3.sha3(text=message) == digest
with pytest.deprecated_call():
assert Web3.sha3(text=message) == digest


@pytest.mark.parametrize(
'hexstr, digest',
[
(
'0x636f776dc3b6',
HexBytes('0x0f355f04c0a06eebac1d219b34c598f85a1169badee164be8a30345944885fe8')
),
(
'636f776dc3b6',
HexBytes('0x0f355f04c0a06eebac1d219b34c598f85a1169badee164be8a30345944885fe8')
),
(
'0x',
HexBytes('0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')
),
(
'',
HexBytes('0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470')
),
],
)
def test_keccak_hexstr(hexstr, digest):
assert Web3.keccak(hexstr=hexstr) == digest


@pytest.mark.parametrize(
Expand All @@ -42,7 +79,8 @@ def test_sha3_text(message, digest):
],
)
def test_sha3_hexstr(hexstr, digest):
assert Web3.sha3(hexstr=hexstr) == digest
with pytest.deprecated_call():
assert Web3.sha3(hexstr=hexstr) == digest


@pytest.mark.parametrize(
Expand All @@ -53,9 +91,9 @@ def test_sha3_hexstr(hexstr, digest):
(-1, ValueError),
],
)
def test_sha3_primitive_invalid(primitive, exception):
def test_keccak_primitive_invalid(primitive, exception):
with pytest.raises(exception):
Web3.sha3(primitive)
Web3.keccak(primitive)


@pytest.mark.parametrize(
Expand All @@ -71,8 +109,8 @@ def test_sha3_primitive_invalid(primitive, exception):
),
],
)
def test_sha3_primitive(primitive, digest):
assert Web3.sha3(primitive) == digest
def test_keccak_primitive(primitive, digest):
assert Web3.keccak(primitive) == digest


@pytest.mark.parametrize(
Expand All @@ -83,17 +121,17 @@ def test_sha3_primitive(primitive, digest):
{'text': '', 'hexstr': '0x'},
],
)
def test_sha3_raise_if_primitive_and(kwargs):
def test_keccak_raise_if_primitive_and(kwargs):
# must not set more than one input
with pytest.raises(TypeError):
Web3.sha3('', **kwargs)
Web3.keccak('', **kwargs)


def test_sha3_raise_if_hexstr_and_text():
def test_keccak_raise_if_hexstr_and_text():
with pytest.raises(TypeError):
Web3.sha3(hexstr='0x', text='')
Web3.keccak(hexstr='0x', text='')


def test_sha3_raise_if_no_args():
def test_keccak_raise_if_no_args():
with pytest.raises(TypeError):
Web3.sha3()
Web3.keccak()
12 changes: 6 additions & 6 deletions tests/ens/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def ens_setup():
ens_key = accounts.pop()

# create ENS contract
eth_labelhash = w3.sha3(text='eth')
eth_labelhash = w3.keccak(text='eth')
eth_namehash = bytes32(0x93cdeb708b7545dc668eb9280176169d1c33cfd8ed6f04690a0bcc88a93fc4ae)
resolver_namehash = bytes32(0xfdd5d5de6dd63db72bbc2d487944ba13bf775b50a80805fe6fcaba9b0fba88f5)
reverse_tld_namehash = bytes32(0xa097f6721ce401e757d1223a763fef49b8b5f90bb18567ddb86fd205dff71d34) # noqa: E501
Expand All @@ -126,7 +126,7 @@ def ens_setup():

ens_contract.functions.setSubnodeOwner(
eth_namehash,
w3.sha3(text='resolver'),
w3.keccak(text='resolver'),
ens_key
).transact({'from': ens_key})

Expand Down Expand Up @@ -173,13 +173,13 @@ def ens_setup():
# set 'addr.reverse' to resolve to reverse registrar
ens_contract.functions.setSubnodeOwner(
b'\0' * 32,
w3.sha3(text='reverse'),
w3.keccak(text='reverse'),
ens_key
).transact({'from': ens_key})

ens_contract.functions.setSubnodeOwner(
reverse_tld_namehash,
w3.sha3(text='addr'),
w3.keccak(text='addr'),
ens_key
).transact({'from': ens_key})

Expand All @@ -196,7 +196,7 @@ def ens_setup():
# set owner of tester.eth to an account controlled by tests
ens_contract.functions.setSubnodeOwner(
eth_namehash,
w3.sha3(text='tester'),
w3.keccak(text='tester'),
w3.eth.accounts[2] # note that this does not have to be the default, only in the list
).transact({'from': ens_key})

Expand All @@ -210,7 +210,7 @@ def ens_setup():
# make the reverse registrar the owner of the 'addr.reverse' name
ens_contract.functions.setSubnodeOwner(
reverse_tld_namehash,
w3.sha3(text='addr'),
w3.keccak(text='addr'),
reverse_registrar.address
).transact({'from': ens_key})

Expand Down
11 changes: 10 additions & 1 deletion web3/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
import pkg_resources
import sys
import warnings

if (3, 5) <= sys.version_info < (3, 6):
warnings.warn(
"Support for Python 3.5 will be removed in web3.py v5",
category=DeprecationWarning,
stacklevel=2)

if sys.version_info < (3, 5):
raise EnvironmentError("Python 3.5 or above is required")
raise EnvironmentError(
"Python 3.5 or above is required. "
"Note that support for Python 3.5 will be remove in web3.py v5")

from eth_account import Account # noqa: E402
from web3.main import Web3 # noqa: E402
Expand Down
17 changes: 12 additions & 5 deletions web3/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from_wei,
is_address,
is_checksum_address,
keccak,
keccak as eth_utils_keccak,
remove_0x_prefix,
to_checksum_address,
to_wei,
Expand Down Expand Up @@ -51,6 +51,7 @@
)
from web3.utils.decorators import (
combomethod,
deprecated_for,
)
from web3.utils.empty import empty
from web3.utils.encoding import (
Expand Down Expand Up @@ -132,16 +133,22 @@ def providers(self, providers):
self.manager.providers = providers

@staticmethod
@deprecated_for("This method has been renamed to keccak")
@apply_to_return_value(HexBytes)
def sha3(primitive=None, text=None, hexstr=None):
return Web3.keccak(primitive, text, hexstr)

@staticmethod
@apply_to_return_value(HexBytes)
def keccak(primitive=None, text=None, hexstr=None):
if isinstance(primitive, (bytes, int, type(None))):
input_bytes = to_bytes(primitive, hexstr=hexstr, text=text)
return keccak(input_bytes)
return eth_utils_keccak(input_bytes)

raise TypeError(
"You called sha3 with first arg %r and keywords %r. You must call it with one of "
"these approaches: sha3(text='txt'), sha3(hexstr='0x747874'), "
"sha3(b'\\x74\\x78\\x74'), or sha3(0x747874)." % (
"You called keccak with first arg %r and keywords %r. You must call it with one of "
"these approaches: keccak(text='txt'), keccak(hexstr='0x747874'), "
"keccak(b'\\x74\\x78\\x74'), or keccak(0x747874)." % (
primitive,
{'text': text, 'hexstr': hexstr}
)
Expand Down