-
-
Notifications
You must be signed in to change notification settings - Fork 133
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
feat(AccountAPI): sign raw hash32 #1966
Merged
fubuloubu
merged 4 commits into
ApeWorX:main
from
fubuloubu:feat/AccountAPI/sign-raw-hash32
Mar 21, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fc4b250
feat(AccountAPI): allow signing raw 32 byte hash
fubuloubu 1045ec5
test: add test for raw hash signing
fubuloubu 3054309
fix(typing): use HexBytes type from `eth-pydantic-types` consistently
fubuloubu 0af512a
docs: add docstring for `AccountAPI.sign_raw_msghash`
fubuloubu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -6,7 +6,7 @@ | |||||
from eip712.messages import SignableMessage as EIP712SignableMessage | ||||||
from eth_account import Account | ||||||
from eth_account.messages import encode_defunct | ||||||
from hexbytes import HexBytes | ||||||
from eth_pydantic_types import HexBytes | ||||||
|
||||||
from ape.api.address import BaseAddress | ||||||
from ape.api.transactions import ReceiptAPI, TransactionAPI | ||||||
|
@@ -57,6 +57,23 @@ def alias(self) -> Optional[str]: | |||||
""" | ||||||
return None | ||||||
|
||||||
def sign_raw_msghash(self, msghash: HexBytes) -> Optional[MessageSignature]: | ||||||
""" | ||||||
Sign a raw message hash. | ||||||
|
||||||
Args: | ||||||
msghash (:class:`~eth_pydantic_types.HexBytes`): | ||||||
The message hash to sign. Plugins may or may not support this operation. | ||||||
Default implementation is to raise ``NotImplementedError``. | ||||||
|
||||||
Returns: | ||||||
:class:`~ape.types.signatures.MessageSignature` (optional): | ||||||
The signature corresponding to the message. | ||||||
""" | ||||||
raise NotImplementedError( | ||||||
f"Raw message signing is not supported by '{self.__class__.__name__}'" | ||||||
) | ||||||
|
||||||
@abstractmethod | ||||||
def sign_message(self, msg: Any, **signer_options) -> Optional[MessageSignature]: | ||||||
""" | ||||||
|
@@ -295,8 +312,9 @@ def declare(self, contract: "ContractContainer", *args, **kwargs) -> ReceiptAPI: | |||||
|
||||||
def check_signature( | ||||||
self, | ||||||
data: Union[SignableMessage, TransactionAPI, str, EIP712Message, int], | ||||||
data: Union[SignableMessage, TransactionAPI, str, EIP712Message, int, bytes], | ||||||
signature: Optional[MessageSignature] = None, # TransactionAPI doesn't need it | ||||||
recover_using_eip191: bool = True, | ||||||
) -> bool: | ||||||
""" | ||||||
Verify a message or transaction was signed by this account. | ||||||
|
@@ -307,6 +325,10 @@ def check_signature( | |||||
signature (Optional[:class:`~ape.types.signatures.MessageSignature`]): | ||||||
The signature to check. Defaults to ``None`` and is not needed when the first | ||||||
argument is a transaction class. | ||||||
recover_using_eip191 (bool): | ||||||
Perform recovery using EIP-191 signed message check. If set False, then will attempt | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
recovery as raw hash. `data`` must be a 32 byte hash if this is set False. | ||||||
Defaults to ``True``. | ||||||
|
||||||
Returns: | ||||||
bool: ``True`` if the data was signed by this account. ``False`` otherwise. | ||||||
|
@@ -315,6 +337,8 @@ def check_signature( | |||||
data = encode_defunct(text=data) | ||||||
elif isinstance(data, int): | ||||||
data = encode_defunct(hexstr=HexBytes(data).hex()) | ||||||
elif isinstance(data, bytes) and (len(data) != 32 or recover_using_eip191): | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. small perf, maybe, for short-circuit eval on the
Suggested change
as checking a bool is prolly faster than a length check |
||||||
data = encode_defunct(data) | ||||||
elif isinstance(data, EIP712Message): | ||||||
data = data.signable_message | ||||||
if isinstance(data, (SignableMessage, EIP712SignableMessage)): | ||||||
|
@@ -329,6 +353,9 @@ def check_signature( | |||||
elif isinstance(data, TransactionAPI): | ||||||
return self.address == Account.recover_transaction(data.serialize_transaction()) | ||||||
|
||||||
elif isinstance(data, bytes) and len(data) == 32 and not recover_using_eip191: | ||||||
return self.address == Account._recover_hash(data, vrs=signature) | ||||||
|
||||||
else: | ||||||
raise AccountsError(f"Unsupported message type: {type(data)}.") | ||||||
|
||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion: The
@raises_not_implemented
decorator does exactly this! same messaging.