|
| 1 | +const { ethers } = require('hardhat'); |
| 2 | +const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers'); |
| 3 | + |
| 4 | +const { ERC4337Helper } = require('../helpers/erc4337'); |
| 5 | + |
| 6 | +const { shouldBehaveLikeAccountCore, shouldBehaveLikeAccountHolder } = require('./Account.behavior'); |
| 7 | +const { shouldBehaveLikeERC1271 } = require('../utils/cryptography/ERC1271.behavior'); |
| 8 | +const { shouldBehaveLikeERC7821 } = require('./extensions/ERC7821.behavior'); |
| 9 | +const { ZKEmailSigningKey, NonNativeSigner } = require('../helpers/signers'); |
| 10 | + |
| 11 | +const accountSalt = '0x046582bce36cdd0a8953b9d40b8f20d58302bacf3bcecffeb6741c98a52725e2'; // keccak256("[email protected]") |
| 12 | +const selector = '12345'; |
| 13 | +const domainName = 'gmail.com'; |
| 14 | +const publicKeyHash = '0x0ea9c777dc7110e5a9e89b13f0cfc540e3845ba120b2b6dc24024d61488d4788'; |
| 15 | +const emailNullifier = '0x00a83fce3d4b1c9ef0f600644c1ecc6c8115b57b1596e0e3295e2c5105fbfd8a'; |
| 16 | +const templateId = ethers.solidityPackedKeccak256(['string', 'uint256'], ['TEST', 0n]); |
| 17 | + |
| 18 | +const SIGN_HASH_COMMAND = 'signHash'; |
| 19 | + |
| 20 | +async function fixture() { |
| 21 | + // EOAs and environment |
| 22 | + const [admin, beneficiary, other] = await ethers.getSigners(); |
| 23 | + const target = await ethers.deployContract('CallReceiverMockExtended'); |
| 24 | + |
| 25 | + // Registry |
| 26 | + const dkim = await ethers.deployContract('ECDSAOwnedDKIMRegistry'); |
| 27 | + await dkim.initialize(admin, admin); |
| 28 | + await dkim |
| 29 | + .SET_PREFIX() |
| 30 | + .then(prefix => dkim.computeSignedMsg(prefix, domainName, publicKeyHash)) |
| 31 | + .then(message => admin.signMessage(message)) |
| 32 | + .then(signature => dkim.setDKIMPublicKeyHash(selector, domainName, publicKeyHash, signature)); |
| 33 | + |
| 34 | + // Verifier |
| 35 | + const verifier = await ethers.deployContract('ZKEmailVerifierMock'); |
| 36 | + |
| 37 | + // ERC-4337 signer |
| 38 | + const signer = new NonNativeSigner( |
| 39 | + new ZKEmailSigningKey(domainName, publicKeyHash, emailNullifier, accountSalt, templateId), |
| 40 | + ); |
| 41 | + |
| 42 | + // ERC-4337 account |
| 43 | + const helper = new ERC4337Helper(); |
| 44 | + const mock = await helper.newAccount('$AccountZKEmailMock', [accountSalt, dkim.target, verifier.target, templateId]); |
| 45 | + |
| 46 | + const signUserOp = async userOp => { |
| 47 | + // Create email auth message for the user operation hash |
| 48 | + const hash = await userOp.hash(); |
| 49 | + return Object.assign(userOp, { signature: signer.signingKey.sign(hash).serialized }); |
| 50 | + }; |
| 51 | + |
| 52 | + const userOpInvalidSig = async userOp => { |
| 53 | + // Create email auth message for the user operation hash |
| 54 | + const hash = await userOp.hash(); |
| 55 | + const timestamp = Math.floor(Date.now() / 1000); |
| 56 | + const command = SIGN_HASH_COMMAND + ' ' + ethers.toBigInt(hash).toString(); |
| 57 | + const isCodeExist = true; |
| 58 | + const proof = '0x00'; // Mocked in ZKEmailVerifierMock |
| 59 | + |
| 60 | + // Encode the email auth message as the signature |
| 61 | + return ethers.AbiCoder.defaultAbiCoder().encode( |
| 62 | + ['tuple(uint256,bytes[],uint256,tuple(string,bytes32,uint256,string,bytes32,bytes32,bool,bytes))'], |
| 63 | + [ |
| 64 | + [ |
| 65 | + templateId, |
| 66 | + [hash], |
| 67 | + 0, // skippedCommandPrefix |
| 68 | + [domainName, publicKeyHash, timestamp, command, emailNullifier, accountSalt, isCodeExist, proof], |
| 69 | + ], |
| 70 | + ], |
| 71 | + ); |
| 72 | + }; |
| 73 | + |
| 74 | + return { helper, mock, dkim, verifier, target, beneficiary, other, signUserOp, userOpInvalidSig, signer }; |
| 75 | +} |
| 76 | + |
| 77 | +describe('AccountZKEmail', function () { |
| 78 | + beforeEach(async function () { |
| 79 | + Object.assign(this, await loadFixture(fixture)); |
| 80 | + }); |
| 81 | + |
| 82 | + shouldBehaveLikeAccountCore(); |
| 83 | + shouldBehaveLikeAccountHolder(); |
| 84 | + shouldBehaveLikeERC1271({ erc7739: true }); |
| 85 | + shouldBehaveLikeERC7821(); |
| 86 | +}); |
0 commit comments