-
-
Notifications
You must be signed in to change notification settings - Fork 131
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
feat(AccountAPI): sign raw hash32 #1966
Conversation
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.
The UX is better with this being in a separate method rather than relying on size-checking and prompts.
src/ape_accounts/accounts.py
Outdated
display_msg = f"Signing raw bytes: '{msg.hex()}'" | ||
|
||
if sign_raw_hash := ( | ||
len(msg) == 32 and not click.confirm("Sign using EIP-191? (Select 'y' if unsure)") |
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.
Using click
-isms outside of a CLI is a bit strange, I know we are already doing it elsewhere in this calss, but it'd be nice to keep prompting in the CLI components.
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.
This class needs to prompt the user directly to unlock or approve actions, so seems okay to me
Another reason a separate method might be better: Accounts that don't support this will be more obvious. Otherwise by the same rationale, we'd have to warn or raise exceptions in accounts trying to sign a bytes value of a certain length, which assumes stuff about the signer's intent. By having separate methods, the intent is clear, and when the account does not support such thing, it can raise a Just something I was thinking about ... |
I'm on board with this change, also makes it easier to roll out |
df3c28c
to
1045ec5
Compare
Refactored in fc4b250 as a separate method with |
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.
Looks good!
Some small feedback if you'll indulge, but LGTM either way
:class:`~ape.types.signatures.MessageSignature` (optional): | ||
The signature corresponding to the message. | ||
""" | ||
raise NotImplementedError( |
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.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Perform recovery using EIP-191 signed message check. If set False, then will attempt | |
Perform recovery using EIP-191 signed message check. If False, then will attempt |
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
small perf, maybe, for short-circuit eval on the or
stmt
elif isinstance(data, bytes) and (len(data) != 32 or recover_using_eip191): | |
elif isinstance(data, bytes) and (recover_using_eip191 or len(data) != 32): |
as checking a bool is prolly faster than a length check
What I did
Add a new path to
AccountAPI
called.sign_raw_msghash
that allows signing a raw 32 byte hash without treating it as an EIP191SignableMessage
typefixes: #1962
How I did it
I needed this for work I was doing on ape safe, but it is a really dangerous thing to allow, so I added a warning message and additionally disallowed autosigning for it. For
AccountAPI.recover_message
I added an additional kwargrecover_using_eip191=True
that can be set false if you want to opt-in for that behavior for verifying a signature.How to verify it
Try the experience locally, I have added tests for it. Ultimately, when installled alongside the delegate feature for ape safe (ApeWorX/ape-safe#41) you should now be able to use it to directly sign something with the API
Checklist