Skip to content
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

Refactor/email auth simplification #104

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

zkfriendly
Copy link
Contributor

Description

This PR introduces a new simplified EmailAuthSigner contract that provides a signature-like authentication mechanism using emails. Unlike the existing EmailAuth contract which handles nullifiers internally, this new contract is designed to be used like a signature verification mechanism where the calling contract manages its own replay protection.

Key changes:

  • Added new EmailAuthSigner.sol contract
  • Created IEmailAuth.sol interface to standardize common functionality
  • Moved EmailAuthMsg struct to the interface
  • Updated existing contracts to implement the interface
  • Added comprehensive tests for the new signer contract

Type of change

  • New feature (non-breaking change which adds functionality)
  • This change requires a documentation update

How Has This Been Tested?

Added new test files and cases:

  • EmailAuthSigner.t.sol with comprehensive test coverage including:
    • Basic initialization and configuration
    • DKIM registry updates
    • Verifier updates
    • Email authentication
    • Error cases and invalid inputs
    • Upgrade functionality
    • Template ID validation

All existing tests have been updated to work with the interface changes and continue to pass.

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Copy link

openzeppelin-code bot commented Jan 14, 2025

Refactor/email auth simplification

Generated at commit: 5ee69daf5667acf8f11865eca28acc141d1b7296

🚨 Report Summary

Severity Level Results
Contracts Critical
High
Medium
Low
Note
Total
1
1
0
6
28
36
Dependencies Critical
High
Medium
Low
Note
Total
0
0
0
0
0
0

For more details view the full report in OpenZeppelin Code Inspector

@SoraSuegami
Copy link
Contributor

@zkfriendly Thank you for your PR!
Could you minimize duplicated codes in EmailAuthSigner.sol?
I think the EmailAuthSigner can inherit the EmailAuth implementation.
Please feel free to add more functions to EmailAuth if you want/need.
However, it is ideal if all of the existing functions/structs in EmailAuth remain same as the current ones.

@zkfriendly
Copy link
Contributor Author

Thank you for the review.

I think the EmailAuthSigner can inherit the EmailAuth implementation

The main goal of this PR is to provide a simpler primitive that can be used everywhere. For context, there is already an implementation here that doesn’t modify the EmailAuth library; instead, it uses it internally while exposing a much simpler interface. However, after reviewing it, @JohnGuilding suggested that we should aim to simplify the underlying EmailAuth library as well.

Please feel free to add more functions to EmailAuth if you want/need.

Given our goal and the fact that an implementation with a simpler interface already exists, it seems that if cloning and modifying EmailAuth is not an option, we can simply discard this PR and adopt the other implementation that builds upon the existing EmailAuth. cc @JohnGuilding

it is ideal if all of the existing functions/structs in EmailAuth remain same as the current ones.

I agree. Even with the changes in this PR, the functions and structs in EmailAuth remain unchanged; code organization is a bit different to prevent duplicate code, but everything stays the same.

Copy link
Collaborator

@JohnGuilding JohnGuilding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cheers @zkfriendly left some comments.

Let's jump on a call tomorrow if we need to discuss the 1271 signature comments some more

},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why was this needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not strictly needed and can be removed safely. That said, benefits are mainly consistency across environments and corepack compatibility

/// @dev Unlike EmailAuth.sol which handles nullifiers internally, this contract is designed
/// to be used like a signature verification mechanism where the calling contract manages
/// its own replay protection.
contract EmailAuthSigner is
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thoughts an EmailSigner?

/// The templateId of the sign hash command.
uint256 public templateId;

constructor() {}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's a security feature, I think it's worth adding to EmailAuth constructor too

templateId = _templateId;
require(
_dkimRegistryAddr != address(0),
"invalid dkim registry address"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in breakout call, lets move to use custom errors if you're in agreement


// Magic value returned by older versions of EIP1271 when validating data and signature
// bytes4(keccak256("isValidSignature(bytes,bytes)")). Used by Gnosis Safe and others.
bytes4 internal constant EIP1271_MAGIC_VALUE_DATA = 0x20c13b0b;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice yes, we should do that.


contract ERC1271SignatureValidator {
/// Mapping to store if a hash has been signed.
mapping(bytes32 => bool) public isHashSigned;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Where is this updated?
  2. What do you think about verifying proof etc directly in isValidSignature? And state updates can be handled in higher level logic such as how contract signatures work on Safes https://github.com/safe-global/safe-smart-account/blob/1c8b24a0a438e8c2cd089a9d830d1688a47a28d5/contracts/Safe.sol#L140-L142

This way the EmailSigner can be used to verify "signatures" in a single step rather than approve + verify

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this updated?

this part is actually not implemented here yet. And I think your second point is a great idea for how to build this.

/// handling replay protection. The calling contract should implement its own mechanisms
/// to prevent replay attacks, similar to how nonces are used with ECDSA signatures.
/// @param emailAuthMsg The email auth message containing all necessary information for authentication.
function authEmail(EmailAuthMsg memory emailAuthMsg) public {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to above comment on 1271 validation - thinking that verifySignature is executing most of this logic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants