|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.24; |
| 4 | + |
| 5 | +import {IDKIMRegistry} from "@zk-email/contracts/DKIMRegistry.sol"; |
| 6 | +import {IVerifier} from "@zk-email/email-tx-builder/interfaces/IVerifier.sol"; |
| 7 | +import {EmailAuthMsg} from "@zk-email/email-tx-builder/interfaces/IEmailTypes.sol"; |
| 8 | +import {AbstractSigner} from "./AbstractSigner.sol"; |
| 9 | +import {ZKEmailUtils} from "./ZKEmailUtils.sol"; |
| 10 | + |
| 11 | +/** |
| 12 | + * @dev Implementation of {AbstractSigner} using https://docs.zk.email[ZKEmail] signatures. |
| 13 | + * |
| 14 | + * ZKEmail enables secure authentication and authorization through email messages, leveraging |
| 15 | + * DKIM signatures from a {DKIMRegistry} and zero-knowledge proofs enabled by a {verifier} |
| 16 | + * contract that ensures email authenticity without revealing sensitive information. The DKIM |
| 17 | + * registry is trusted to correctly update DKIM keys, but users can override this behaviour and |
| 18 | + * set their own keys. This contract implements the core functionality for validating email-based |
| 19 | + * signatures in smart contracts. |
| 20 | + * |
| 21 | + * Developers must set the following components during contract initialization: |
| 22 | + * |
| 23 | + * * {accountSalt} - A unique identifier derived from the user's email address and account code. |
| 24 | + * * {DKIMRegistry} - An instance of the DKIM registry contract for domain verification. |
| 25 | + * * {verifier} - An instance of the Verifier contract for zero-knowledge proof validation. |
| 26 | + * * {templateId} - The template ID of the sign hash command, defining the expected format. |
| 27 | + * |
| 28 | + * Example of usage: |
| 29 | + * |
| 30 | + * ```solidity |
| 31 | + * contract MyAccountZKEmail is Account, SignerZKEmail, Initializable { |
| 32 | + * constructor(bytes32 accountSalt, IDKIMRegistry registry, IVerifier verifier, uint256 templateId) { |
| 33 | + * // Will revert if the signer is already initialized |
| 34 | + * _setAccountSalt(accountSalt); |
| 35 | + * _setDKIMRegistry(registry); |
| 36 | + * _setVerifier(verifier); |
| 37 | + * _setTemplateId(templateId); |
| 38 | + * } |
| 39 | + * } |
| 40 | + * ``` |
| 41 | + * |
| 42 | + * IMPORTANT: Avoiding to call {_setAccountSalt}, {_setDKIMRegistry}, {_setVerifier} and {_setTemplateId} |
| 43 | + * either during construction (if used standalone) or during initialization (if used as a clone) may |
| 44 | + * leave the signer either front-runnable or unusable. |
| 45 | + */ |
| 46 | +abstract contract SignerZKEmail is AbstractSigner { |
| 47 | + using ZKEmailUtils for EmailAuthMsg; |
| 48 | + |
| 49 | + bytes32 private _accountSalt; |
| 50 | + IDKIMRegistry private _registry; |
| 51 | + IVerifier private _verifier; |
| 52 | + uint256 private _templateId; |
| 53 | + |
| 54 | + /// @dev Proof verification error. |
| 55 | + error InvalidEmailProof(ZKEmailUtils.EmailProofError err); |
| 56 | + |
| 57 | + /** |
| 58 | + * @dev Unique identifier for owner of this contract defined as a hash of an email address and an account code. |
| 59 | + * |
| 60 | + * An account code is a random integer in a finite scalar field of https://neuromancer.sk/std/bn/bn254[BN254] curve. |
| 61 | + * It is a private randomness to derive a CREATE2 salt of the user's Ethereum address |
| 62 | + * from the email address, i.e., userEtherAddr := CREATE2(hash(userEmailAddr, accountCode)). |
| 63 | + * |
| 64 | + * The account salt is used for: |
| 65 | + * |
| 66 | + * * Privacy: Enables email address privacy on-chain so long as the randomly generated account code is not revealed |
| 67 | + * to an adversary. |
| 68 | + * * Security: Provides a unique identifier that cannot be easily guessed or brute-forced, as it's derived |
| 69 | + * from both the email address and a random account code. |
| 70 | + * * Deterministic Address Generation: Enables the creation of deterministic addresses based on email addresses, |
| 71 | + * allowing users to recover their accounts using only their email. |
| 72 | + */ |
| 73 | + function accountSalt() public view virtual returns (bytes32) { |
| 74 | + return _accountSalt; |
| 75 | + } |
| 76 | + |
| 77 | + /// @dev An instance of the DKIM registry contract. |
| 78 | + /// See https://docs.zk.email/architecture/dkim-verification[DKIM Verification]. |
| 79 | + // solhint-disable-next-line func-name-mixedcase |
| 80 | + function DKIMRegistry() public view virtual returns (IDKIMRegistry) { |
| 81 | + return _registry; |
| 82 | + } |
| 83 | + |
| 84 | + /// @dev An instance of the Verifier contract. |
| 85 | + /// See https://docs.zk.email/architecture/zk-proofs#how-zk-email-uses-zero-knowledge-proofs[ZK Proofs]. |
| 86 | + function verifier() public view virtual returns (IVerifier) { |
| 87 | + return _verifier; |
| 88 | + } |
| 89 | + |
| 90 | + /// @dev The command template of the sign hash command. |
| 91 | + function templateId() public view virtual returns (uint256) { |
| 92 | + return _templateId; |
| 93 | + } |
| 94 | + |
| 95 | + /// @dev Set the {accountSalt}. |
| 96 | + function _setAccountSalt(bytes32 accountSalt_) internal virtual { |
| 97 | + _accountSalt = accountSalt_; |
| 98 | + } |
| 99 | + |
| 100 | + /// @dev Set the {DKIMRegistry} contract address. |
| 101 | + function _setDKIMRegistry(IDKIMRegistry registry_) internal virtual { |
| 102 | + _registry = registry_; |
| 103 | + } |
| 104 | + |
| 105 | + /// @dev Set the {verifier} contract address. |
| 106 | + function _setVerifier(IVerifier verifier_) internal virtual { |
| 107 | + _verifier = verifier_; |
| 108 | + } |
| 109 | + |
| 110 | + /// @dev Set the command's {templateId}. |
| 111 | + function _setTemplateId(uint256 templateId_) internal virtual { |
| 112 | + _templateId = templateId_; |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @dev See {AbstractSigner-_rawSignatureValidation}. Validates a raw signature by: |
| 117 | + * |
| 118 | + * 1. Decoding the email authentication message from the signature |
| 119 | + * 2. Verifying the hash matches the command parameters |
| 120 | + * 3. Checking the template ID matches |
| 121 | + * 4. Validating the account salt |
| 122 | + * 5. Verifying the email proof |
| 123 | + */ |
| 124 | + function _rawSignatureValidation( |
| 125 | + bytes32 hash, |
| 126 | + bytes calldata signature |
| 127 | + ) internal view virtual override returns (bool) { |
| 128 | + // Check if the signature is long enough to contain the EmailAuthMsg |
| 129 | + // The minimum length is 512 bytes (initial part + pointer offsets) |
| 130 | + // - `templateId` is a uint256 (32 bytes). |
| 131 | + // - `commandParams` is a dynamic array of bytes32 (32 bytes offset). |
| 132 | + // - `skippedCommandPrefixSize` is a uint256 (32 bytes). |
| 133 | + // - `proof` is a struct with the following fields (32 bytes offset): |
| 134 | + // - `domainName` is a dynamic string (32 bytes offset). |
| 135 | + // - `publicKeyHash` is a bytes32 (32 bytes). |
| 136 | + // - `timestamp` is a uint256 (32 bytes). |
| 137 | + // - `maskedCommand` is a dynamic string (32 bytes offset). |
| 138 | + // - `emailNullifier` is a bytes32 (32 bytes). |
| 139 | + // - `accountSalt` is a bytes32 (32 bytes). |
| 140 | + // - `isCodeExist` is a boolean, so its length is 1 byte padded to 32 bytes. |
| 141 | + // - `proof` is a dynamic bytes (32 bytes offset). |
| 142 | + // There are 128 bytes for the EmailAuthMsg type and 256 bytes for the proof. |
| 143 | + // Considering all dynamic elements are empty (i.e. `commandParams` = [], `domainName` = "", `maskedCommand` = "", `proof` = []), |
| 144 | + // then we have 128 bytes for the EmailAuthMsg type, 256 bytes for the proof and 4 * 32 for the length of the dynamic elements. |
| 145 | + // So the minimum length is 128 + 256 + 4 * 32 = 512 bytes. |
| 146 | + if (signature.length < 512) return false; |
| 147 | + EmailAuthMsg memory emailAuthMsg = abi.decode(signature, (EmailAuthMsg)); |
| 148 | + return (abi.decode(emailAuthMsg.commandParams[0], (bytes32)) == hash && |
| 149 | + emailAuthMsg.templateId == templateId() && |
| 150 | + emailAuthMsg.proof.accountSalt == accountSalt() && |
| 151 | + emailAuthMsg.isValidZKEmail(DKIMRegistry(), verifier()) == ZKEmailUtils.EmailProofError.NoError); |
| 152 | + } |
| 153 | +} |
0 commit comments