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

Update OpenZeppelin libraries #5

Merged
merged 9 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "packages/contracts/lib/forge-std"]
path = packages/contracts/lib/forge-std
url = https://github.com/foundry-rs/forge-std
2 changes: 1 addition & 1 deletion packages/contracts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ $ yarn test
Run integration tests

Before running integration tests, you need to make a `packages/contracts/test/build_integration` directory, download the zip file from the following link, and place its unziped directory under that directory.
https://drive.google.com/file/d/1ky3XyabnBFwcyBoWBimhoePT9kbFyEBR/view?usp=sharing
https://drive.google.com/file/d/1waD-GzzpCmo3xN4rdDqQ2A2B7id1JR7V/view?usp=sharing

Then, move `email_auth.zkey` and `email_auth.wasm` in the unzipped directory `params` to `build_integration`.

Expand Down
1 change: 1 addition & 0 deletions packages/contracts/lib/forge-std
Submodule forge-std added at bb4cee
9 changes: 3 additions & 6 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
"lint": "solhint 'src/**/*.sol'"
},
"dependencies": {
"@openzeppelin/contracts": "^4.9.2",
"@openzeppelin/contracts-upgradeable": "^4.9.2",
"@uniswap/v3-core": "https://github.com/Uniswap/v3-core#0.8",
"@uniswap/v3-periphery": "https://github.com/Uniswap/v3-periphery.git",
"@zk-email/contracts": "^4.1.0",
"accountabstraction": "eth-infinitism/account-abstraction#v0.6.0",
"@openzeppelin/contracts": "^5.0.0",
"@openzeppelin/contracts-upgradeable": "^5.0.0",
"@zk-email/contracts": "^6.0.0",
"solady": "^0.0.123"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/remappings.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@openzeppelin/=../../node_modules/@openzeppelin
@openzeppelin/contracts-upgradeable/=../../node_modules/@openzeppelin/contracts-upgradeable
@zk-email/=../../node_modules/@zk-email
@uniswap/=../../node_modules/@uniswap
forge-std/=../../node_modules/forge-std/src
Expand Down
15 changes: 10 additions & 5 deletions packages/contracts/src/EmailAccountRecovery.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
pragma solidity ^0.8.12;

import "./EmailAuth.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/utils/Create2.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";

Expand Down Expand Up @@ -63,7 +62,10 @@ abstract contract EmailAccountRecovery {
type(ERC1967Proxy).creationCode,
abi.encode(
emailAuthImplementation(),
abi.encodeCall(EmailAuth.initialize, (accountSalt))
abi.encodeCall(
EmailAuth.initialize,
(address(this), accountSalt)
)
)
)
)
Expand Down Expand Up @@ -107,7 +109,10 @@ abstract contract EmailAccountRecovery {
address guardian = computeEmailAuthAddress(
emailAuthMsg.proof.accountSalt
);
require(!Address.isContract(guardian), "guardian is already deployed");
require(
address(guardian).code.length == 0,
"guardian is already deployed"
);
uint templateId = computeAcceptanceTemplateId(templateIdx);
require(templateId == emailAuthMsg.templateId, "invalid template id");
require(emailAuthMsg.proof.isCodeExist == true, "isCodeExist is false");
Expand All @@ -119,7 +124,7 @@ abstract contract EmailAccountRecovery {
emailAuthImplementation(),
abi.encodeCall(
EmailAuth.initialize,
(emailAuthMsg.proof.accountSalt)
(address(this), emailAuthMsg.proof.accountSalt)
)
);
EmailAuth guardianEmailAuth = EmailAuth(address(proxy));
Expand Down Expand Up @@ -157,7 +162,7 @@ abstract contract EmailAccountRecovery {
address guardian = computeEmailAuthAddress(
emailAuthMsg.proof.accountSalt
);
require(Address.isContract(guardian), "guardian is not deployed");
require(address(guardian).code.length > 0, "guardian is not deployed");
uint templateId = uint256(
keccak256(
abi.encode(
Expand Down
12 changes: 9 additions & 3 deletions packages/contracts/src/EmailAuth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@ contract EmailAuth is OwnableUpgradeable, UUPSUpgradeable {
constructor() {}

/// @notice Initialize the contract
function initialize(bytes32 _accountSalt) public initializer {
__Ownable_init();
function initialize(
address _initialOwner,
bytes32 _accountSalt
) public initializer {
__Ownable_init(_initialOwner);
accountSalt = _accountSalt;
timestampCheckEnabled = true;
}
Expand Down Expand Up @@ -59,7 +62,10 @@ contract EmailAuth is OwnableUpgradeable, UUPSUpgradeable {
function getSubjectTemplate(
uint _templateId
) public view returns (string[] memory) {
require(subjectTemplates[_templateId].length > 0, "template id not exists");
require(
subjectTemplates[_templateId].length > 0,
"template id not exists"
);
return subjectTemplates[_templateId];
}

Expand Down
16 changes: 11 additions & 5 deletions packages/contracts/src/libraries/SubjectUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ library SubjectUtils {
string public constant DECIMALS_MATCHER = "{decimals}";
string public constant ETH_ADDR_MATCHER = "{ethAddr}";

function addressToChecksumHexString(address addr) internal pure returns (string memory) {
function addressToChecksumHexString(
address addr
) internal pure returns (string memory) {
string memory lowerCaseAddrWithOx = Strings.toHexString(addr);

bytes memory lowerCaseAddr = new bytes(40); // Remove 0x added by the OZ lib
Expand All @@ -24,7 +26,9 @@ library SubjectUtils {
}

// Hash of lowercase addr
uint256 lowerCaseHash = uint256(keccak256(abi.encodePacked(lowerCaseAddr)));
uint256 lowerCaseHash = uint256(
keccak256(abi.encodePacked(lowerCaseAddr))
);

// Result hex = 42 chars with 0x prefix
bytes memory result = new bytes(42);
Expand Down Expand Up @@ -56,7 +60,9 @@ library SubjectUtils {

/// @notice Convert bytes to hex string without 0x prefix
/// @param data bytes to convert
function bytesToHexString(bytes memory data) public pure returns (string memory) {
function bytesToHexString(
bytes memory data
) public pure returns (string memory) {
bytes memory hexChars = "0123456789abcdef";
bytes memory hexString = new bytes(2 * data.length);

Expand All @@ -70,7 +76,7 @@ library SubjectUtils {
}

/// @notice Calculate the expected subject.
/// @param subjectParams Params to be used in the subject
/// @param subjectParams Params to be used in the subject
/// @param template Template to be used for the subject
function computeExpectedSubject(
bytes[] memory subjectParams,
Expand Down Expand Up @@ -99,7 +105,7 @@ library SubjectUtils {
subjectParams[nextParamIndex],
(int256)
);
stringParam = Strings.toString(param);
stringParam = Strings.toStringSigned(param);
} else if (Strings.equal(template[i], DECIMALS_MATCHER)) {
uint256 param = abi.decode(
subjectParams[nextParamIndex],
Expand Down
15 changes: 10 additions & 5 deletions packages/contracts/src/utils/ECDSAOwnedDKIMRegistry.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;

import "@openzeppelin/contracts/utils/Strings.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@zk-email/contracts/DKIMRegistry.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import {MessageHashUtils} from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";

/// @title ECDSAOwnedDKIMRegistry
/// @notice A DKIM Registry that could be updated by predefined ECDSA signer
Expand All @@ -18,7 +19,7 @@ contract ECDSAOwnedDKIMRegistry is IDKIMRegistry {
string public constant REVOKE_PREFIX = "REVOKE:";

constructor(address _signer) {
dkimRegistry = new DKIMRegistry();
dkimRegistry = new DKIMRegistry(address(this));
signer = _signer;
}

Expand Down Expand Up @@ -53,7 +54,9 @@ contract ECDSAOwnedDKIMRegistry is IDKIMRegistry {
domainName,
publicKeyHash
);
bytes32 digest = bytes(signedMsg).toEthSignedMessageHash();
bytes32 digest = MessageHashUtils.toEthSignedMessageHash(
bytes(signedMsg)
);
address recoveredSigner = digest.recover(signature);
require(recoveredSigner == signer, "Invalid signature");

Expand Down Expand Up @@ -84,7 +87,9 @@ contract ECDSAOwnedDKIMRegistry is IDKIMRegistry {
domainName,
publicKeyHash
);
bytes32 digest = bytes(signedMsg).toEthSignedMessageHash();
bytes32 digest = MessageHashUtils.toEthSignedMessageHash(
bytes(signedMsg)
);
address recoveredSigner = digest.recover(signature);
require(recoveredSigner == signer, "Invalid signature");

Expand Down
8 changes: 4 additions & 4 deletions packages/contracts/src/utils/Groth16Verifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ contract Groth16Verifier {
uint256 constant gammax2 = 10857046999023057135944570762232829481370756359578518086990519993285655852781;
uint256 constant gammay1 = 4082367875863433681332203403145435568316851327593401208105741076214120093531;
uint256 constant gammay2 = 8495653923123431417604973247489272438418190587263600148770280649306958101930;
uint256 constant deltax1 = 4648268560658283857862580267414600988863509998549932461710092005137935910849;
uint256 constant deltax2 = 17002085987812032414533534878472954255275695125541817856525001058669417074740;
uint256 constant deltay1 = 14350701953093407925913536369212657176263226209656730697674193748288897497533;
uint256 constant deltay2 = 18380647281531473181154662941267515134112351439925402835187150916909153931146;
uint256 constant deltax1 = 8669958621598211614523288487212013709697484330995020679718658681228237042515;
uint256 constant deltax2 = 20454902724738874023254735802726569431511469610593615972101503188096009710272;
uint256 constant deltay1 = 15734859514659546211155613710618764479812042975409274787015906938438731725279;
uint256 constant deltay2 = 4568434731332300359441355722270230945101674782479620800555660846836059432281;


uint256 constant IC0x = 11875865670464336529510676641721437876098964006709650423001784788588843034166;
Expand Down
5 changes: 2 additions & 3 deletions packages/contracts/test/EmailAccountRecovery.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@ contract EmailAccountRecoveryTest is SimpleWallet, Test {

function setUpForInternal() public {
vm.startPrank(deployer);
initialize(address(0x0), address(0x0), address(0x0));
vm.deal(address(this), 1 ether);
initialize(msg.sender, address(0x0), address(0x0), address(0x0));
vm.stopPrank();
}

function setUpForPublic() public {
vm.startPrank(deployer);
this.initialize(address(0x0), address(0x0), address(0x0));
this.initialize(deployer, address(0x0), address(0x0), address(0x0));
vm.deal(address(this), 1 ether);
vm.stopPrank();
}
Expand Down
32 changes: 21 additions & 11 deletions packages/contracts/test/Integration.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import "forge-std/console.sol";
contract IntegrationTest is Test {
using Strings for *;
using console for *;
using ECDSA for *;

EmailAuth emailAuth;
Verifier verifier;
Expand Down Expand Up @@ -51,7 +50,9 @@ contract IntegrationTest is Test {
domainName,
publicKeyHash
);
bytes32 digest = bytes(signedMsg).toEthSignedMessageHash();
bytes32 digest = MessageHashUtils.toEthSignedMessageHash(
bytes(signedMsg)
);
(uint8 v, bytes32 r, bytes32 s) = vm.sign(1, digest);
bytes memory signature = abi.encodePacked(r, s, v);
dkim.setDKIMPublicKeyHash(
Expand All @@ -76,6 +77,7 @@ contract IntegrationTest is Test {
address(simpleWalletImpl),
abi.encodeWithSelector(
simpleWalletImpl.initialize.selector,
signer,
address(verifier),
address(dkim),
address(emailAuthImpl)
Expand All @@ -102,7 +104,7 @@ contract IntegrationTest is Test {
console.log("SimpleWallet is at ", address(simpleWallet));
assertEq(
address(simpleWallet),
0x3d3955aAe5Bdf9E6547A140Baad4BC57Fa4EBA17
0x3Bb7f1A59bDE3B61a0d537723E4e27D022489635
);
address simpleWalletOwner = simpleWallet.owner();

Expand Down Expand Up @@ -135,7 +137,7 @@ contract IntegrationTest is Test {
emailProof.publicKeyHash = bytes32(vm.parseUint(pubSignals[9]));
emailProof.timestamp = vm.parseUint(pubSignals[11]);
emailProof
.maskedSubject = "Accept guardian request for 0x3d3955aAe5Bdf9E6547A140Baad4BC57Fa4EBA17";
.maskedSubject = "Accept guardian request for 0x3Bb7f1A59bDE3B61a0d537723E4e27D022489635";
emailProof.emailNullifier = bytes32(vm.parseUint(pubSignals[10]));
emailProof.accountSalt = bytes32(vm.parseUint(pubSignals[32]));
accountSalt = emailProof.accountSalt;
Expand All @@ -160,9 +162,7 @@ contract IntegrationTest is Test {

// Call handleAcceptance -> GuardianStatus.ACCEPTED
bytes[] memory subjectParamsForAcceptance = new bytes[](1);
subjectParamsForAcceptance[0] = abi.encode(
address(simpleWallet)
);
subjectParamsForAcceptance[0] = abi.encode(address(simpleWallet));
EmailAuthMsg memory emailAuthMsg = EmailAuthMsg({
templateId: simpleWallet.computeAcceptanceTemplateId(templateIdx),
subjectParams: subjectParamsForAcceptance,
Expand Down Expand Up @@ -203,10 +203,13 @@ contract IntegrationTest is Test {
emailProof.publicKeyHash = bytes32(vm.parseUint(pubSignals[9]));
emailProof.timestamp = vm.parseUint(pubSignals[11]);
emailProof
.maskedSubject = "Set the new signer of 0x3d3955aAe5Bdf9E6547A140Baad4BC57Fa4EBA17 to 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720"; // 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720 is account 9
.maskedSubject = "Set the new signer of 0x3Bb7f1A59bDE3B61a0d537723E4e27D022489635 to 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720"; // 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720 is account 9
emailProof.emailNullifier = bytes32(vm.parseUint(pubSignals[10]));
emailProof.accountSalt = bytes32(vm.parseUint(pubSignals[32]));
require(emailProof.accountSalt == accountSalt, "accountSalt should be the same");
require(
emailProof.accountSalt == accountSalt,
"accountSalt should be the same"
);
emailProof.isCodeExist = vm.parseUint(pubSignals[33]) == 1;
emailProof.proof = proofToBytes(
string.concat(
Expand Down Expand Up @@ -238,7 +241,11 @@ contract IntegrationTest is Test {
});
simpleWallet.handleRecovery(emailAuthMsg, templateIdx);
require(simpleWallet.isRecovering(), "isRecovering should be set");
require(simpleWallet.newSignerCandidate() == 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720, "newSignerCandidate should be set");
require(
simpleWallet.newSignerCandidate() ==
0xa0Ee7A142d267C1f36714E4a8F75612F20a79720,
"newSignerCandidate should be set"
);
require(simpleWallet.timelock() > 0, "timelock should be set");
require(
simpleWallet.owner() == simpleWalletOwner,
Expand All @@ -251,7 +258,10 @@ contract IntegrationTest is Test {
simpleWallet.completeRecovery();
console.log("simpleWallet owner: ", simpleWallet.owner());
require(!simpleWallet.isRecovering(), "isRecovering should be reset");
require(simpleWallet.newSignerCandidate() == address(0), "newSignerCandidate should be reset");
require(
simpleWallet.newSignerCandidate() == address(0),
"newSignerCandidate should be reset"
);
require(simpleWallet.timelock() == 0, "timelock should be reset");
require(
simpleWallet.owner() == 0xa0Ee7A142d267C1f36714E4a8F75612F20a79720,
Expand Down
Loading
Loading