Skip to content

Commit

Permalink
Revert "implement new sign_message fn accepting Any"
Browse files Browse the repository at this point in the history
This reverts commit ad20e65.
  • Loading branch information
z80dev committed Sep 21, 2023
1 parent 8435d41 commit cb756ad
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 35 deletions.
11 changes: 4 additions & 7 deletions src/ape/api/accounts.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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 <https://eth-account.readthedocs.io/en/stable/eth_account.html#eth_account.messages.SignableMessage>`__ # 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.
Expand Down Expand Up @@ -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]:
Expand Down
19 changes: 4 additions & 15 deletions src/ape_accounts/accounts.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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,
Expand Down
22 changes: 9 additions & 13 deletions src/ape_test/accounts.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cb756ad

Please sign in to comment.