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

Replace attrdict with NamedTuple #76

Merged
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
23 changes: 1 addition & 22 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,7 @@ logs
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff:
.idea/workspace.xml
.idea/tasks.xml
.idea/dictionaries
.idea/vcs.xml
.idea/jsLibraryMappings.xml

# Sensitive or high-churn files:
.idea/dataSources.ids
.idea/dataSources.xml
.idea/dataSources.local.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml

# Gradle:
.idea/gradle.xml
.idea/libraries

# Mongo Explorer plugin:
.idea/mongoSettings.xml
.idea

# VIM temp files
*.swp
Expand Down
2 changes: 1 addition & 1 deletion docs/eth_account.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Account

See :doc:`eth_account.signers` for alternative signers.

AttributeDict
SignedTransaction & SignedMessage
-----------------------------------

.. automodule:: eth_account.datastructures
Expand Down
48 changes: 25 additions & 23 deletions eth_account/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@
vrs_from,
)
from eth_account.datastructures import (
AttributeDict,
SignedMessage,
SignedTransaction,
)
from eth_account.hdaccount import (
ETHEREUM_DEFAULT_PATH,
Expand Down Expand Up @@ -517,7 +518,7 @@ def sign_message(self, signable_message: SignableMessage, private_key):
:param private_key: the key to sign the message with
:type private_key: hex str, bytes, int or :class:`eth_keys.datatypes.PrivateKey`
:returns: Various details about the signature - most importantly the fields: v, r, and s
:rtype: ~eth_account.datastructures.AttributeDict
:rtype: ~eth_account.datastructures.SignedMessage
Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting that the doctests (which were supposedly checked recently) didn't catch that the returned value changes below.

I would expect there to be a number of places in the docs where this would change.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just spent 1,5 hour to depuzzle this and here's the best I know so far:

  1. We never had any doctest running in this repo so far. I wasn't able to find a single CI run that ran any doctests (even though they exist)

  2. Then I found Doctests for Account and other general doc cleanup #94 which seems to fix this (it reports running 50 tests)

I'd happily rebase this on top of #94 but in case #94 needs more time to finish it should be easy to rebase that one on top of these changes, too.

Copy link
Collaborator

Choose a reason for hiding this comment

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

🤦 I totally lost track of #94. I just updated and it should be ready to merge. @cburgdorf I'm happy to pull your changes into #94, or vice versa, whatever is easier! Just let me know.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kclowes I don't have merge rights in this repo. If you have merge rights then feel free to merge both PRs in any preferred order.


.. doctest:: python

Expand All @@ -531,12 +532,13 @@ def sign_message(self, signable_message: SignableMessage, private_key):
>>> # If you're curious about the internal fields of SignableMessage, take a look at EIP-191, linked above # noqa: E501
>>> key = "0xb25c7db31feed9122727bf0939dc769a96564b2de4c4726d035b36ecf1e5b364"
>>> Account.sign_message(msghash, key)
AttrDict({'messageHash':
HexBytes('0x1476abb745d423bf09273f1afd887d951181d25adc66c4834a70491911b7f750'),
'r': 104389933075820307925104709181714897380569894203213074526835978196648170704563,
's': 28205917190874851400050446352651915501321657673772411533993420917949420456142,
'v': 28,
'signature': HexBytes('...')})
SignedMessage(messageHash=HexBytes('0x1476abb745d423bf09273f1afd887d951181d25adc66c4834a70491911b7f750'),
r=104389933075820307925104709181714897380569894203213074526835978196648170704563,
s=28205917190874851400050446352651915501321657673772411533993420917949420456142,
v=28,
signature=HexBytes('0xe6ca9bba58c88611fad66a6ce8f996908195593807c4b38bd528d2cff09d4eb33e5bfbbf4d3e39b1a2fd816a7680c19ebebaf3a141b239934ad43cb33fcec8ce1c'))



.. _EIP-191: https://eips.ethereum.org/EIPS/eip-191
"""
Expand All @@ -563,7 +565,7 @@ def signHash(self, message_hash, private_key):
:type private_key: hex str, bytes, int or :class:`eth_keys.datatypes.PrivateKey`
:returns: Various details about the signature - most
importantly the fields: v, r, and s
:rtype: ~eth_account.datastructures.AttributeDict
:rtype: ~eth_account.datastructures.SignedMessage
"""
warnings.warn(
"signHash is deprecated in favor of sign_message",
Expand All @@ -580,13 +582,13 @@ def _sign_hash(self, message_hash, private_key):
key = self._parsePrivateKey(private_key)

(v, r, s, eth_signature_bytes) = sign_message_hash(key, msg_hash_bytes)
return AttributeDict({
'messageHash': msg_hash_bytes,
'r': r,
's': s,
'v': v,
'signature': HexBytes(eth_signature_bytes),
})
return SignedMessage(
messageHash=msg_hash_bytes,
r=r,
s=s,
v=v,
signature=HexBytes(eth_signature_bytes),
)

@combomethod
def signTransaction(self, transaction_dict, private_key):
Expand Down Expand Up @@ -666,13 +668,13 @@ def sign_transaction(self, transaction_dict, private_key):

transaction_hash = keccak(rlp_encoded)

return AttributeDict({
'rawTransaction': HexBytes(rlp_encoded),
'hash': HexBytes(transaction_hash),
'r': r,
's': s,
'v': v,
})
return SignedTransaction(
rawTransaction=HexBytes(rlp_encoded),
hash=HexBytes(transaction_hash),
r=r,
s=s,
v=v,
)

@combomethod
def _parsePrivateKey(self, key):
Expand Down
58 changes: 33 additions & 25 deletions eth_account/datastructures.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
from attrdict import (
AttrDict,
from typing import (
NamedTuple,
)

from hexbytes import (
HexBytes,
)


def __getitem__(self, index):
try:
return tuple.__getitem__(self, index)
except TypeError:
return getattr(self, index)


class SignedTransaction(NamedTuple):
rawTransaction: HexBytes
hash: HexBytes
r: int
s: int
v: int

def __getitem__(self, index):
return __getitem__(self, index)


class SignedMessage(NamedTuple):
messageHash: HexBytes
r: int
s: int
v: int
signature: HexBytes

class AttributeDict(AttrDict):
"""
See `AttrDict docs <https://github.com/bcj/AttrDict#attrdict-1>`_

This class differs only in that it is made immutable. This immutability
is **not** a security guarantee. It is only a style-check convenience.
"""
def __setitem__(self, attr, val):
raise TypeError(
'This data is immutable -- create a copy instead of modifying. '
'For example, AttributeDict(old, replace_key=replace_val).'
)

def _repr_pretty_(self, builder, cycle):
"""
Custom pretty output for the IPython console
"""
builder.text(self.__class__.__name__ + "(")
if cycle:
builder.text("<cycle>")
else:
builder.pretty(dict(self))
builder.text(")")
def __getitem__(self, index):
return __getitem__(self, index)
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
url='https://github.com/ethereum/eth-account',
include_package_data=True,
install_requires=[
"attrdict>=2.0.0,<3",
"bitarray>=1.2.1,<1.3.0",
"eth-abi>=2.0.0b7,<3",
"eth-keyfile>=0.5.0,<0.6.0",
Expand Down
21 changes: 10 additions & 11 deletions tests/core/test_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ def test_sign_message_against_sign_hash_as_text(keyed_acct, message_text):
# sign via message
signable_message = encode_defunct(text=message_text)
signed_via_message = keyed_acct.sign_message(signable_message)

assert signed_via_hash == signed_via_message


Expand Down Expand Up @@ -374,11 +373,11 @@ def test_sign_message_against_sign_hash_as_hex(keyed_acct, message_bytes):
def test_eth_account_sign(acct, message, key, expected_bytes, expected_hash, v, r, s, signature):
signable = encode_defunct(text=message)
signed = acct.sign_message(signable, private_key=key)
assert signed.messageHash == expected_hash
assert signed.v == v
assert signed.r == r
assert signed.s == s
assert signed.signature == signature
assert signed.messageHash == signed['messageHash'] == expected_hash
assert signed.v == signed['v'] == v
assert signed.r == signed['r'] == r
assert signed.s == signed['s'] == s
assert signed.signature == signed['signature'] == signature

account = acct.from_key(key)
assert account.sign_message(signable) == signed
Expand Down Expand Up @@ -481,11 +480,11 @@ def test_eth_long_account_address_sign_data_with_intended_validator(acct, messag
)
def test_eth_account_sign_transaction(acct, txn, private_key, expected_raw_tx, tx_hash, r, s, v):
signed = acct.sign_transaction(txn, private_key)
assert signed.r == r
assert signed.s == s
assert signed.v == v
assert signed.rawTransaction == expected_raw_tx
assert signed.hash == tx_hash
assert signed.r == signed['r'] == r
assert signed.s == signed['s'] == s
assert signed.v == signed['v'] == v
assert signed.rawTransaction == signed['rawTransaction'] == expected_raw_tx
assert signed.hash == signed['hash'] == tx_hash

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