From cb756adb1586bbf131641ebb48466ab6a2304d0a Mon Sep 17 00:00:00 2001 From: z80 Date: Thu, 21 Sep 2023 16:33:16 -0400 Subject: [PATCH] Revert "implement new sign_message fn accepting Any" This reverts commit ad20e655630d2a1c8804b6e894dc12433a87cbd3. --- src/ape/api/accounts.py | 11 ++++------- src/ape_accounts/accounts.py | 19 ++++--------------- src/ape_test/accounts.py | 22 +++++++++------------- 3 files changed, 17 insertions(+), 35 deletions(-) diff --git a/src/ape/api/accounts.py b/src/ape/api/accounts.py index 9b26dadc15..55a16d3c7e 100644 --- a/src/ape/api/accounts.py +++ b/src/ape/api/accounts.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import TYPE_CHECKING, Any, Iterator, List, Optional, Type, Union +from typing import TYPE_CHECKING, Iterator, List, Optional, Type, Union import click from eip712.messages import EIP712Message @@ -56,18 +56,15 @@ def alias(self) -> Optional[str]: return None @abstractmethod - def sign_message(self, msg: Any, **signer_options) -> Optional[MessageSignature]: + def sign_message(self, msg: SignableMessage) -> Optional[MessageSignature]: """ Sign a message. Args: - msg (Any): The message to sign. Account plugins can handle various types of messages. - For example, :class:`~ape.accounts.LocalAccount` can handle - :class:`~ape.types.signatures.SignableMessage` + msg (:class:`~ape.types.signatures.SignableMessage`): The message to sign. See these `docs `__ # noqa: E501 for more type information on this type. - **signer_options: Additional kwargs given to the signer to modify the signing operation. Returns: :class:`~ape.types.signatures.MessageSignature` (optional): The signature corresponding to the message. @@ -503,7 +500,7 @@ class ImpersonatedAccount(AccountAPI): def address(self) -> AddressType: return self.raw_address - def sign_message(self, msg: Any, **signer_options) -> Optional[MessageSignature]: + def sign_message(self, msg: SignableMessage) -> Optional[MessageSignature]: raise NotImplementedError("This account cannot sign messages") def sign_transaction(self, txn: TransactionAPI, **kwargs) -> Optional[TransactionAPI]: diff --git a/src/ape_accounts/accounts.py b/src/ape_accounts/accounts.py index 305f465c9c..88d58459eb 100644 --- a/src/ape_accounts/accounts.py +++ b/src/ape_accounts/accounts.py @@ -1,12 +1,11 @@ import json from os import environ from pathlib import Path -from typing import Any, Iterator, Optional +from typing import Iterator, Optional import click from eip712.messages import EIP712Message from eth_account import Account as EthAccount -from eth_account.messages import encode_defunct from eth_utils import to_bytes from ethpm_types import HexBytes @@ -121,21 +120,11 @@ def delete(self): self.__decrypt_keyfile(passphrase) self.keyfile_path.unlink() - def sign_message(self, msg: Any, **signer_options) -> Optional[MessageSignature]: - if not isinstance(msg, (SignableMessage, EIP712Message, str)): - logger.warning("Unsupported message type, (type=%r, msg=%r)", type(msg), msg) - return None - - user_approves = self.__autosign or click.confirm(f"Message: {msg}\n\nSign: ") - if isinstance(msg, str): - # Convert str to SignableMessage for handling below - msg = encode_defunct(text=msg) - elif isinstance(msg, EIP712Message): - # Convert EIP712Message to SignableMessage for handling below - msg = msg.signable_message - + def sign_message(self, msg: SignableMessage) -> Optional[MessageSignature]: + user_approves = self.__autosign or click.confirm(f"{msg}\n\nSign: ") if not user_approves: return None + signed_msg = EthAccount.sign_message(msg, self.__key) return MessageSignature( v=signed_msg.v, diff --git a/src/ape_test/accounts.py b/src/ape_test/accounts.py index e66afcfc02..c596b3b165 100644 --- a/src/ape_test/accounts.py +++ b/src/ape_test/accounts.py @@ -1,7 +1,7 @@ -from typing import Any, Iterator, List, Optional +from typing import Iterator, List, Optional from eth_account import Account as EthAccount -from eth_account.messages import SignableMessage, encode_defunct +from eth_account.messages import SignableMessage from eth_utils import to_bytes from ape.api import TestAccountAPI, TestAccountContainerAPI, TransactionAPI @@ -101,17 +101,13 @@ def alias(self) -> str: def address(self) -> AddressType: return self.network_manager.ethereum.decode_address(self.address_str) - def sign_message(self, msg: Any, **signer_options) -> Optional[MessageSignature]: - if isinstance(msg, str): - msg = encode_defunct(text=msg) - if isinstance(msg, SignableMessage): - signed_msg = EthAccount.sign_message(msg, self.private_key) - return MessageSignature( - v=signed_msg.v, - r=to_bytes(signed_msg.r), - s=to_bytes(signed_msg.s), - ) - return None + def sign_message(self, msg: SignableMessage) -> Optional[MessageSignature]: + signed_msg = EthAccount.sign_message(msg, self.private_key) + return MessageSignature( + v=signed_msg.v, + r=to_bytes(signed_msg.r), + s=to_bytes(signed_msg.s), + ) def sign_transaction(self, txn: TransactionAPI, **kwargs) -> Optional[TransactionAPI]: # Signs anything that's given to it