Skip to content

Commit

Permalink
implement new sign_message fn accepting Any
Browse files Browse the repository at this point in the history
  • Loading branch information
z80dev committed Aug 22, 2023
1 parent 8ebbf51 commit ad20e65
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
13 changes: 8 additions & 5 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, Iterator, List, Optional, Type, Union
from typing import TYPE_CHECKING, Any, Iterator, List, Optional, Type, Union

import click
from eip712.messages import SignableMessage as EIP712SignableMessage
Expand Down Expand Up @@ -54,18 +54,21 @@ def alias(self) -> Optional[str]:
return None

@abstractmethod
def sign_message(self, msg: SignableMessage) -> Optional[MessageSignature]:
def sign_message(self, msg: Any, **signer_options) -> Optional[MessageSignature]:
"""
Sign a message.
Args:
msg (:class:`~ape.types.signatures.SignableMessage`): The message to sign.
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`
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 signed message.
:class:`~ape.types.signatures.MessageSignature` (optional): The signed message.
"""

@abstractmethod
Expand Down Expand Up @@ -494,7 +497,7 @@ class ImpersonatedAccount(AccountAPI):
def address(self) -> AddressType:
return self.raw_address

def sign_message(self, msg: SignableMessage) -> Optional[MessageSignature]:
def sign_message(self, msg: Any, **signer_options) -> Optional[MessageSignature]:
raise NotImplementedError("This account cannot sign messages")

def sign_transaction(self, txn: TransactionAPI, **kwargs) -> Optional[TransactionAPI]:
Expand Down
28 changes: 16 additions & 12 deletions src/ape_accounts/accounts.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import json
from os import environ
from pathlib import Path
from typing import Iterator, Optional
from typing import Any, Iterator, Optional

import click
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 @@ -119,17 +120,20 @@ def delete(self):
self.__decrypt_keyfile(passphrase)
self.keyfile_path.unlink()

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,
r=to_bytes(signed_msg.r),
s=to_bytes(signed_msg.s),
)
def sign_message(self, msg: Any, **signer_options) -> Optional[MessageSignature]:
if isinstance(msg, str):
msg = encode_defunct(text=msg)
if isinstance(msg, SignableMessage):
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,
r=to_bytes(signed_msg.r),
s=to_bytes(signed_msg.s),
)
raise TypeError(f"Can't sign message of type {type(msg)}")

def sign_transaction(self, txn: TransactionAPI, **kwargs) -> Optional[TransactionAPI]:
user_approves = self.__autosign or click.confirm(f"{txn}\n\nSign: ")
Expand Down
21 changes: 12 additions & 9 deletions src/ape_test/accounts.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Iterator, List, Optional
from typing import Any, Iterator, List, Optional

from eth_account import Account as EthAccount
from eth_account.messages import SignableMessage
from eth_account.messages import SignableMessage, encode_defunct
from eth_utils import to_bytes

from ape.api import TestAccountAPI, TestAccountContainerAPI, TransactionAPI
Expand Down Expand Up @@ -101,13 +101,16 @@ def alias(self) -> str:
def address(self) -> AddressType:
return self.network_manager.ethereum.decode_address(self.address_str)

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_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),
)

def sign_transaction(self, txn: TransactionAPI, **kwargs) -> Optional[TransactionAPI]:
# Signs anything that's given to it
Expand Down

0 comments on commit ad20e65

Please sign in to comment.