-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
New eth.sign and recovery hash generator #301
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d40ad04
toBytes is more semantically correct than toAscii
carver 3dc88df
Mark to/fromAscii deprecated, with new decorator
carver a2469c5
new toText() & toBytes(), stop coercing to text
carver da16080
Move toBytes & toText to web3.utils.encoding
carver 11d6c89
Reuse toBytes logic in sha3
carver b4da21c
toDecimal: add hexstr, deprecate hex autodetection
carver c7a98fd
Deprecate from_decimal
carver 2b56e2c
Replace from_ascii/to_ascii with to_hex/to_bytes
carver 79f4455
Consistent hexstr/text in conversions, trim hex
carver 0309997
eth.sign: hex and text messages, get recovery hash
carver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# coding=utf-8 | ||
|
||
import pytest | ||
import sys | ||
|
||
|
||
@pytest.mark.skipif(sys.version_info.major < 3, reason="requires python 3") | ||
@pytest.mark.parametrize( | ||
'message, expected', | ||
[ | ||
( | ||
'Message tö sign. Longer than hash!', | ||
'0x10c7cb57942998ab214c062e7a57220a174aacd80418cead9f90ec410eacada1', | ||
), | ||
( | ||
# Intentionally sneaky: message is a hexstr interpreted as text | ||
'0x4d6573736167652074c3b6207369676e2e204c6f6e676572207468616e206861736821', | ||
'0x6192785e9ad00100e7332ff585824b65eafa30bc8f1265cf86b5368aa3ab5d56', | ||
), | ||
] | ||
) | ||
def test_recovery_message_text_hash(web3, message, expected): | ||
assert web3.eth._recoveryMessageHash(text=message) == expected | ||
|
||
|
||
@pytest.mark.skipif(sys.version_info.major < 3, reason="requires python 3") | ||
@pytest.mark.parametrize( | ||
'message, expected', | ||
[ | ||
( | ||
'0x4d6573736167652074c3b6207369676e2e204c6f6e676572207468616e206861736821', | ||
'0x10c7cb57942998ab214c062e7a57220a174aacd80418cead9f90ec410eacada1', | ||
), | ||
( | ||
'0x29d9f7d6a1d1e62152f314f04e6bd4300ad56fd72102b6b83702869a089f470c', | ||
'0xe709159ef0e6323c705786fc50e47a8143812e9f82f429e585034777c7bf530b', | ||
), | ||
] | ||
) | ||
def test_recovery_message_hexstr_hash(web3, message, expected): | ||
assert web3.eth._recoveryMessageHash(hexstr=message) == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
# coding=utf-8 | ||
|
||
from __future__ import unicode_literals | ||
|
||
import pytest | ||
import sys | ||
|
||
from web3 import Web3 | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'val, expected', | ||
( | ||
(b'\x01', b'\x01'), | ||
(b'\xff', b'\xff'), | ||
(b'\x00', b'\x00'), | ||
(0x01, b'\x01'), | ||
(0xFF, b'\xff'), | ||
(0, b'\x00'), | ||
(256, b'\x01\x00'), | ||
(True, b'\x01'), | ||
(False, b'\x00'), | ||
) | ||
) | ||
def test_to_bytes_primitive(val, expected): | ||
assert Web3.toBytes(val) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'val, expected', | ||
( | ||
('0x', b''), | ||
('0x0', b'\x00'), | ||
('0x1', b'\x01'), | ||
('0', b'\x00'), | ||
('1', b'\x01'), | ||
('0xFF', b'\xff'), | ||
('0x100', b'\x01\x00'), | ||
('0x0000', b'\x00\x00'), | ||
) | ||
) | ||
def test_to_bytes_hexstr(val, expected): | ||
assert Web3.toBytes(hexstr=val) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'val, expected', | ||
( | ||
('cowmö', b'cowm\xc3\xb6'), | ||
('', b''), | ||
) | ||
) | ||
def test_to_bytes_text(val, expected): | ||
assert Web3.toBytes(text=val) == expected | ||
|
||
|
||
def test_to_text_identity(): | ||
if sys.version_info.major < 3: | ||
with pytest.raises(NotImplementedError): | ||
Web3.toText(text='') | ||
else: | ||
assert Web3.toText(text='pass-through') == 'pass-through' | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'val, expected', | ||
( | ||
(b'', ''), | ||
('0x', ''), | ||
(b'cowm\xc3\xb6', 'cowmö'), | ||
('0x636f776dc3b6', 'cowmö'), | ||
(0x636f776dc3b6, 'cowmö'), | ||
('0xa', '\n'), | ||
) | ||
) | ||
def test_to_text(val, expected): | ||
if sys.version_info.major < 3: | ||
with pytest.raises(NotImplementedError): | ||
Web3.toText(val) | ||
else: | ||
assert Web3.toText(val) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'val, expected', | ||
( | ||
('0x', ''), | ||
('0xa', '\n'), | ||
('0x636f776dc3b6', 'cowmö'), | ||
) | ||
) | ||
def test_to_text_hexstr(val, expected): | ||
if sys.version_info.major < 3: | ||
with pytest.raises(NotImplementedError): | ||
Web3.toText(hexstr=val) | ||
else: | ||
assert Web3.toText(hexstr=val) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'val, expected', | ||
( | ||
(b'\x00', 0), | ||
(b'\x01', 1), | ||
(b'\x00\x01', 1), | ||
(b'\x01\x00', 256), | ||
('255', 255), | ||
('-1', -1), | ||
(True, 1), | ||
(False, 0), | ||
# Deprecated: | ||
('0x0', 0), | ||
('0x1', 1), | ||
('0x01', 1), | ||
('0x10', 16), | ||
) | ||
) | ||
def test_to_decimal(val, expected): | ||
if isinstance(val, bytes) and bytes == str: | ||
pytest.skip("Python 3 is required to pass in bytes") | ||
assert Web3.toDecimal(val) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'val, expected', | ||
( | ||
('0', 0), | ||
('-1', -1), | ||
('255', 255), | ||
) | ||
) | ||
def test_to_decimal_text(val, expected): | ||
if isinstance(val, bytes) and bytes == str: | ||
pytest.skip("Python 3 is required to pass in bytes") | ||
assert Web3.toDecimal(text=val) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'val, expected', | ||
( | ||
('0x0', 0), | ||
('0x1', 1), | ||
('0x01', 1), | ||
('0x10', 16), | ||
('0', 0), | ||
('1', 1), | ||
('01', 1), | ||
('10', 16), | ||
) | ||
) | ||
def test_to_decimal_hexstr(val, expected): | ||
assert Web3.toDecimal(hexstr=val) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'val, expected', | ||
( | ||
(b'\x00', '0x0'), | ||
(b'\x01', '0x1'), | ||
(b'\x10', '0x10'), | ||
(b'\x01\x00', '0x100'), | ||
(b'', '0x'), | ||
(0, '0x0'), | ||
(1, '0x1'), | ||
(16, '0x10'), | ||
(256, '0x100'), | ||
(False, '0x0'), | ||
(True, '0x1'), | ||
) | ||
) | ||
def test_to_hex(val, expected): | ||
assert Web3.toHex(val) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'val, expected', | ||
( | ||
('', '0x'), | ||
('cowmö', '0x636f776dc3b6'), | ||
) | ||
) | ||
def test_to_hex_text(val, expected): | ||
assert Web3.toHex(text=val) == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
'val, expected', | ||
( | ||
('0x0', '0x0'), | ||
('0x1', '0x1'), | ||
('0x01', '0x1'), | ||
('0x10', '0x10'), | ||
) | ||
) | ||
def test_to_hex_cleanup_only(val, expected): | ||
assert Web3.toHex(hexstr=val) == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't need to be cleaned up here, but it might be wise for us to remove the use of
web3.toHex
here in favor of using theeth_utils.encode_hex
function so as to not make our tests dependent on this API.