Skip to content

Commit

Permalink
Merge pull request #1348 from carver/eth-account-upgrade
Browse files Browse the repository at this point in the history
Upgrade eth-account to v0.4
  • Loading branch information
carver authored May 7, 2019
2 parents 519753d + e842007 commit 4180319
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 20 deletions.
37 changes: 23 additions & 14 deletions docs/web3.eth.account.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ is provided by :meth:`w3.eth.sign() <web3.eth.Eth.sign>`.
.. doctest::

>>> from web3.auto import w3
>>> from eth_account.messages import defunct_hash_message
>>> from eth_account.messages import encode_defunct

>>> msg = "I♥SF"
>>> private_key = b"\xb2\\}\xb3\x1f\xee\xd9\x12''\xbf\t9\xdcv\x9a\x96VK-\xe4\xc4rm\x03[6\xec\xf1\xe5\xb3d"
>>> message_hash = defunct_hash_message(text=msg)
>>> signed_message = w3.eth.account.signHash(message_hash, private_key=private_key)
>>> message = encode_defunct(text=msg)
>>> signed_message = w3.eth.account.sign_message(message, private_key=private_key)
>>> signed_message
AttrDict({'messageHash': HexBytes('0x1476abb745d423bf09273f1afd887d951181d25adc66c4834a70491911b7f750'),
'r': 104389933075820307925104709181714897380569894203213074526835978196648170704563,
Expand All @@ -104,15 +104,19 @@ With the original message text and a signature:

.. doctest::

>>> message_hash = defunct_hash_message(text="I♥SF")
>>> w3.eth.account.recoverHash(message_hash, signature=signed_message.signature)
>>> message = encode_defunct(text="I♥SF")
>>> w3.eth.account.recover_message(message, signature=signed_message.signature)
'0x5ce9454909639D2D17A3F753ce7d93fa0b9aB12E'

Verify a Message from message hash
-----------------------------------------------------------

Sometimes you don't have the original message, all you have is the
prefixed & hashed message. To verify it, use:
Sometimes, for historical reasons, you don't have the original message,
all you have is the prefixed & hashed message. To verify it, use:

.. CAUTION:: This method is deprecated, only having a hash typically indicates that
you're using some old kind of mechanism. Expect this method to go away in the
next major version upgrade.

.. doctest::

Expand Down Expand Up @@ -159,13 +163,18 @@ this will prepare it for Solidity:
.. doctest::

>>> from web3 import Web3
>>> from eth_account.messages import defunct_hash_message
>>> from eth_account.messages import encode_defunct, _hash_eip191_message

>>> hex_message = '0x49e299a55346'
>>> hex_signature = '0xe6ca9bba58c88611fad66a6ce8f996908195593807c4b38bd528d2cff09d4eb33e5bfbbf4d3e39b1a2fd816a7680c19ebebaf3a141b239934ad43cb33fcec8ce1c'

# ecrecover in Solidity expects a prefixed & hashed version of the message
>>> message_hash = defunct_hash_message(hexstr=hex_message)
# ecrecover in Solidity expects an encoded version of the message

# - encode the message
>>> message = encode_defunct(hexstr=hex_message)

# - hash the message explicitly
>>> message_hash = _hash_eip191_message(message)

# Remix / web3.js expect the message hash to be encoded to a hex string
>>> hex_message_hash = Web3.toHex(message_hash)
Expand Down Expand Up @@ -226,7 +235,7 @@ with :meth:`~web3.eth.Eth.sendRawTransaction`.
... 'chainId': 1
... }
>>> key = '0x4c0883a69102937d6231471b5dbb6204fe5129617082792ae468d01a3f362318'
>>> signed = w3.eth.account.signTransaction(transaction, key)
>>> signed = w3.eth.account.sign_transaction(transaction, key)
>>> signed.rawTransaction
HexBytes('0xf86a8086d55698372431831e848094f0109fc8df283027b6285cc889f5aa624eac1f55843b9aca008025a009ebb6ca057a0535d6186462bc0b465b561c94a295bdb0621fc19208ab149a9ca0440ffd775ce91a833ab410777204d5341a6f9fa91216a6f3ee2c051fea6a0428')
>>> signed.hash
Expand All @@ -249,8 +258,8 @@ To sign a transaction locally that will invoke a smart contract:

#. Initialize your :meth:`Contract <web3.eth.Eth.contract>` object
#. Build the transaction
#. Sign the transaction, with :meth:`w3.eth.account.signTransaction()
<eth_account.account.Account.signTransaction>`
#. Sign the transaction, with :meth:`w3.eth.account.sign_transaction()
<eth_account.account.Account.sign_transaction>`
#. Broadcast the transaction with :meth:`~web3.eth.Eth.sendRawTransaction`

.. testsetup::
Expand Down Expand Up @@ -287,7 +296,7 @@ To sign a transaction locally that will invoke a smart contract:
'data': '0xa9059cbb000000000000000000000000fb6916095ca1df60bb79ce92ce3ea74c37c5d3590000000000000000000000000000000000000000000000000000000000000001'}

>>> private_key = b"\xb2\\}\xb3\x1f\xee\xd9\x12''\xbf\t9\xdcv\x9a\x96VK-\xe4\xc4rm\x03[6\xec\xf1\xe5\xb3d"
>>> signed_txn = w3.eth.account.signTransaction(unicorn_txn, private_key=private_key)
>>> signed_txn = w3.eth.account.sign_transaction(unicorn_txn, private_key=private_key)
>>> signed_txn.hash
HexBytes('0x4795adc6a719fa64fa21822630c0218c04996e2689ded114b6553cef1ae36618')
>>> signed_txn.rawTransaction
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
include_package_data=True,
install_requires=[
"eth-abi>=2.0.0b6,<3.0.0",
"eth-account>=0.2.1,<0.4.0",
"eth-account>=0.4.0,<0.5.0",
"eth-hash[pycryptodome]>=0.2.0,<1.0.0",
"eth-typing>=2.0.0,<3.0.0",
"eth-utils>=1.4.0,<2.0.0",
Expand Down
11 changes: 8 additions & 3 deletions tests/core/middleware/test_transaction_signing.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import pytest

import eth_account
from eth_account import (
Account,
)
from eth_account.signers.local import (
LocalAccount,
)
import eth_keys
from eth_tester.exceptions import (
ValidationError,
Expand Down Expand Up @@ -48,7 +53,7 @@

KEY_FUNCS = (
eth_keys.keys.PrivateKey,
eth_account.Account.privateKeyToAccount,
Account.privateKeyToAccount,
HexBytes,
to_hex,
identity,
Expand Down Expand Up @@ -188,7 +193,7 @@ def w3():
)
def test_gen_normalized_accounts(key_object):
accounts = gen_normalized_accounts(key_object)
assert all(isinstance(account, eth_account.local.LocalAccount) for account in accounts.values())
assert all(isinstance(account, LocalAccount) for account in accounts.values())


def test_gen_normalized_accounts_type_error(w3):
Expand Down
4 changes: 2 additions & 2 deletions web3/middleware/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from eth_account import (
Account,
)
from eth_account.local import (
from eth_account.signers.local import (
LocalAccount,
)
from eth_keys.datatypes import (
Expand Down Expand Up @@ -63,7 +63,7 @@ def gen_normalized_accounts(val):
def to_account(val):
raise TypeError(
"key must be one of the types: "
"eth_keys.datatype.PrivateKey, eth_account.local.LocalAccount, "
"eth_keys.datatype.PrivateKey, eth_account.signers.local.LocalAccount, "
"or raw private key as a hex string or byte string. "
"Was of type {0}".format(type(val)))

Expand Down

0 comments on commit 4180319

Please sign in to comment.