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

Remove eth-account deprecation warnings #1468

Merged
merged 1 commit into from
Oct 17, 2019
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
1 change: 1 addition & 0 deletions newsfragments/1468.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove deprecation warnings from eth-account dependency upgrade
87 changes: 46 additions & 41 deletions tests/core/eth-module/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from eth_account.messages import (
defunct_hash_message,
encode_defunct,
)
from eth_utils import (
is_checksum_address,
Expand Down Expand Up @@ -96,56 +96,56 @@ def test_eth_account_create_variation(acct):
assert account1 != account2


def test_eth_account_privateKeyToAccount_reproducible(acct, PRIVATE_BYTES):
account1 = acct.privateKeyToAccount(PRIVATE_BYTES)
account2 = acct.privateKeyToAccount(PRIVATE_BYTES)
def test_eth_account_from_key_reproducible(acct, PRIVATE_BYTES):
account1 = acct.from_key(PRIVATE_BYTES)
account2 = acct.from_key(PRIVATE_BYTES)
assert bytes(account1) == PRIVATE_BYTES
assert bytes(account1) == bytes(account2)
assert isinstance(str(account1), str)


def test_eth_account_privateKeyToAccount_diverge(acct, PRIVATE_BYTES, PRIVATE_BYTES_ALT):
account1 = acct.privateKeyToAccount(PRIVATE_BYTES)
account2 = acct.privateKeyToAccount(PRIVATE_BYTES_ALT)
def test_eth_account_from_key_diverge(acct, PRIVATE_BYTES, PRIVATE_BYTES_ALT):
account1 = acct.from_key(PRIVATE_BYTES)
account2 = acct.from_key(PRIVATE_BYTES_ALT)
assert bytes(account2) == PRIVATE_BYTES_ALT
assert bytes(account1) != bytes(account2)


def test_eth_account_privateKeyToAccount_seed_restrictions(acct):
def test_eth_account_from_key_seed_restrictions(acct):
with pytest.raises(ValueError):
acct.privateKeyToAccount(b'')
acct.from_key(b'')
with pytest.raises(ValueError):
acct.privateKeyToAccount(b'\xff' * 31)
acct.from_key(b'\xff' * 31)
with pytest.raises(ValueError):
acct.privateKeyToAccount(b'\xff' * 33)
acct.from_key(b'\xff' * 33)


def test_eth_account_privateKeyToAccount_properties(acct, PRIVATE_BYTES):
account = acct.privateKeyToAccount(PRIVATE_BYTES)
def test_eth_account_from_key_properties(acct, PRIVATE_BYTES):
account = acct.from_key(PRIVATE_BYTES)
assert callable(account.signHash)
assert callable(account.sign_transaction)
assert is_checksum_address(account.address)
assert account.address == '0xa79F6f349C853F9Ea0B29636779ae3Cb4E3BA729'
assert account.privateKey == PRIVATE_BYTES
assert account.key == PRIVATE_BYTES


def test_eth_account_create_properties(acct):
account = acct.create()
assert callable(account.signHash)
assert callable(account.sign_transaction)
assert is_checksum_address(account.address)
assert isinstance(account.privateKey, bytes) and len(account.privateKey) == 32
assert isinstance(account.key, bytes) and len(account.key) == 32


def test_eth_account_recover_transaction_example(acct):
raw_tx_hex = '0xf8640d843b9aca00830e57e0945b2063246f2191f18f2675cedb8b28102e957458018025a00c753084e5a8290219324c1a3a86d4064ded2d15979b1ea790734aaa2ceaafc1a0229ca4538106819fd3a5509dd383e8fe4b731c6870339556a5c06feb9cf330bb' # noqa: E501
from_account = acct.recoverTransaction(raw_tx_hex)
from_account = acct.recover_transaction(raw_tx_hex)
assert from_account == '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'


def test_eth_account_recover_transaction_with_literal(acct):
raw_tx = 0xf8640d843b9aca00830e57e0945b2063246f2191f18f2675cedb8b28102e957458018025a00c753084e5a8290219324c1a3a86d4064ded2d15979b1ea790734aaa2ceaafc1a0229ca4538106819fd3a5509dd383e8fe4b731c6870339556a5c06feb9cf330bb # noqa: E501
from_account = acct.recoverTransaction(raw_tx)
from_account = acct.recover_transaction(raw_tx)
assert from_account == '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'


Expand All @@ -156,8 +156,8 @@ def test_eth_account_recover_message(acct):
'0x3e5bfbbf4d3e39b1a2fd816a7680c19ebebaf3a141b239934ad43cb33fcec8ce',
)
message = "I♥SF"
message_hash = defunct_hash_message(text=message)
from_account = acct.recoverHash(message_hash, vrs=(v, r, s))
message_hash = encode_defunct(text=message)
from_account = acct.recover_message(message_hash, vrs=(v, r, s))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since encode_defunct returns a (signable) message now, the variable name should probably be changed from message_hash. Maybe signable_message, encoded_message or encoded is a reasonable name now.

Another very reasonable option is to rename message => message_text, and message_hash => message. (this might by my favorite)

assert from_account == '0x5ce9454909639D2D17A3F753ce7d93fa0b9aB12E'


Expand All @@ -172,9 +172,9 @@ def test_eth_account_recover_message(acct):
ids=['test_sig_bytes_standard_v', 'test_sig_bytes_chain_naive_v']
)
def test_eth_account_recover_signature_bytes(acct, signature_bytes):
msg_hash = b'\xbb\r\x8a\xba\x9f\xf7\xa1<N,s{i\x81\x86r\x83{\xba\x9f\xe2\x1d\xaa\xdd\xb3\xd6\x01\xda\x00\xb7)\xa1' # noqa: E501
from_account = acct.recoverHash(msg_hash, signature=signature_bytes)
assert from_account == '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'
msg_hash = encode_defunct(b'\xbb\r\x8a\xba\x9f\xf7\xa1<N,s{i\x81\x86r\x83{\xba\x9f\xe2\x1d\xaa\xdd\xb3\xd6\x01\xda\x00\xb7)\xa1') # noqa: E501
from_account = acct.recover_message(msg_hash, signature=signature_bytes)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, msg_hash is an inaccurate variable name now.

assert from_account == '0xb7E7385a15fFd29e349BB409C4c0a7d7469601C7'


def test_eth_account_recover_vrs(acct):
Expand All @@ -183,12 +183,13 @@ def test_eth_account_recover_vrs(acct):
5634810156301565519126305729385531885322755941350706789683031279718535704513,
15655399131600894366408541311673616702363115109327707006109616887384920764603,
)
msg_hash = b'\xbb\r\x8a\xba\x9f\xf7\xa1<N,s{i\x81\x86r\x83{\xba\x9f\xe2\x1d\xaa\xdd\xb3\xd6\x01\xda\x00\xb7)\xa1' # noqa: E501
from_account = acct.recoverHash(msg_hash, vrs=(v, r, s))
assert from_account == '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'

from_account = acct.recoverHash(msg_hash, vrs=map(to_hex, (v, r, s)))
assert from_account == '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'
msg_hash = encode_defunct(b'\xbb\r\x8a\xba\x9f\xf7\xa1<N,s{i\x81\x86r\x83{\xba\x9f\xe2\x1d\xaa\xdd\xb3\xd6\x01\xda\x00\xb7)\xa1') # noqa: E501
from_account = acct.recover_message(msg_hash, vrs=(v, r, s))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here

assert from_account == '0xb7E7385a15fFd29e349BB409C4c0a7d7469601C7'

from_account = acct.recover_message(msg_hash, vrs=map(to_hex, (v, r, s)))
assert from_account == '0xb7E7385a15fFd29e349BB409C4c0a7d7469601C7'


def test_eth_account_recover_vrs_standard_v(acct):
Expand All @@ -197,9 +198,9 @@ def test_eth_account_recover_vrs_standard_v(acct):
5634810156301565519126305729385531885322755941350706789683031279718535704513,
15655399131600894366408541311673616702363115109327707006109616887384920764603,
)
msg_hash = b'\xbb\r\x8a\xba\x9f\xf7\xa1<N,s{i\x81\x86r\x83{\xba\x9f\xe2\x1d\xaa\xdd\xb3\xd6\x01\xda\x00\xb7)\xa1' # noqa: E501
from_account = acct.recoverHash(msg_hash, vrs=(v, r, s))
assert from_account == '0xFeC2079e80465cc8C687fFF9EE6386ca447aFec4'
msg_hash = encode_defunct(b'\xbb\r\x8a\xba\x9f\xf7\xa1<N,s{i\x81\x86r\x83{\xba\x9f\xe2\x1d\xaa\xdd\xb3\xd6\x01\xda\x00\xb7)\xa1') # noqa: E501
from_account = acct.recover_message(msg_hash, vrs=(v, r, s))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

assert from_account == '0xb7E7385a15fFd29e349BB409C4c0a7d7469601C7'


@pytest.mark.parametrize(
Expand Down Expand Up @@ -230,18 +231,22 @@ def test_eth_account_recover_vrs_standard_v(acct):
ids=['web3js_example', '31byte_r_and_s'],
)
def test_eth_account_sign(acct, message, key, expected_bytes, expected_hash, v, r, s, signature):
message_hash = defunct_hash_message(text=message)
assert message_hash == expected_hash
message_hash = encode_defunct(text=message)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

signed_message = Web3.keccak(
b"\x19Ethereum Signed Message:\n" +
bytes(f"{len(message_hash.body)}", encoding='utf-8') + message_hash.body
)
assert signed_message == expected_hash

signed = acct.signHash(message_hash, private_key=key)
signed = acct.sign_message(message_hash, private_key=key)
assert signed.messageHash == expected_hash
assert signed.v == v
assert signed.r == r
assert signed.s == s
assert signed.signature == signature

account = acct.privateKeyToAccount(key)
assert account.signHash(message_hash) == signed
account = acct.from_key(key)
assert account.sign_message(message_hash) == signed


@pytest.mark.parametrize(
Expand Down Expand Up @@ -290,7 +295,7 @@ def test_eth_account_sign_transaction(acct, txn, private_key, expected_raw_tx, t
assert signed.rawTransaction == expected_raw_tx
assert signed.hash == tx_hash

account = acct.privateKeyToAccount(private_key)
account = acct.from_key(private_key)
assert account.sign_transaction(txn) == signed


Expand All @@ -313,8 +318,8 @@ def test_eth_account_sign_transaction_from_eth_test(acct, transaction_info):
assert signed.r == Web3.toInt(hexstr=expected_raw_txn[-130:-66])

# confirm that signed transaction can be recovered to the sender
expected_sender = acct.privateKeyToAccount(key).address
assert acct.recoverTransaction(signed.rawTransaction) == expected_sender
expected_sender = acct.from_key(key).address
assert acct.recover_transaction(signed.rawTransaction) == expected_sender


@pytest.mark.parametrize(
Expand All @@ -324,8 +329,8 @@ def test_eth_account_sign_transaction_from_eth_test(acct, transaction_info):
def test_eth_account_recover_transaction_from_eth_test(acct, transaction):
raw_txn = transaction['signed']
key = transaction['key']
expected_sender = acct.privateKeyToAccount(key).address
assert acct.recoverTransaction(raw_txn) == expected_sender
expected_sender = acct.from_key(key).address
assert acct.recover_transaction(raw_txn) == expected_sender


def test_eth_account_encrypt(acct, web3js_key, web3js_password):
Expand All @@ -340,7 +345,7 @@ def test_eth_account_encrypt(acct, web3js_key, web3js_password):


def test_eth_account_prepared_encrypt(acct, web3js_key, web3js_password):
account = acct.privateKeyToAccount(web3js_key)
account = acct.from_key(web3js_key)
encrypted = account.encrypt(web3js_password)

assert encrypted['address'] == '2c7536e3605d9c16a7a3d7b1898e529396a65c23'
Expand Down
2 changes: 1 addition & 1 deletion tests/core/middleware/test_transaction_signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

KEY_FUNCS = (
eth_keys.keys.PrivateKey,
Account.privateKeyToAccount,
Account.from_key,
HexBytes,
to_hex,
identity,
Expand Down