// SPDX-License-Identifier: LGPL-3.0-only /* solhint-disable one-contract-per-file */ pragma solidity >=0.8.0; import {IAccount} from "@account-abstraction/contracts/interfaces/IAccount.sol"; import {UserOperationLib} from "@account-abstraction/contracts/core/UserOperationLib.sol"; import {PackedUserOperation} from "@account-abstraction/contracts/interfaces/PackedUserOperation.sol"; import {_packValidationData} from "@account-abstraction/contracts/core/Helpers.sol"; contract SafeMock { address public singleton; address public owner; address public fallbackHandler; mapping(address => bool) public modules; constructor() { owner = msg.sender; } function setup(address _fallbackHandler, address _module) public virtual { require(owner == address(0), "Already setup"); owner = msg.sender; fallbackHandler = _fallbackHandler; modules[_module] = true; } function _signatureSplit(bytes memory signature) internal pure returns (uint8 v, bytes32 r, bytes32 s) { // solhint-disable-next-line no-inline-assembly assembly ("memory-safe") { r := mload(add(signature, 0x20)) s := mload(add(signature, 0x40)) v := byte(0, mload(add(signature, 0x60))) } } function checkSignatures(bytes32 dataHash, bytes memory data, bytes memory signature) public view { require(dataHash == keccak256(data), "Invalid data hash"); uint8 v; bytes32 r; bytes32 s; (v, r, s) = _signatureSplit(signature); require( // The original safe contract requires the V value to be > 30 for EIP-191 signed messages // The canonical eth V value is 27 or 28, so by adding 4 to it we get a value > 30 // And when we verify the signature we subtract 4 from it owner == ecrecover(keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", dataHash)), v - 4, r, s), "Invalid signature" ); } function execTransactionFromModule( address payable to, uint256 value, bytes calldata data, uint8 operation ) external returns (bool success) { require(modules[msg.sender], "not executing that"); if (operation == 1) (success, ) = to.delegatecall(data); else (success, ) = to.call{value: value}(data); } function execTransactionFromModuleReturnData( address payable to, uint256 value, bytes calldata data, uint8 operation ) external returns (bool success, bytes memory returnData) { require(modules[msg.sender], "not executing that"); if (operation == 1) (success, returnData) = to.delegatecall(data); else (success, returnData) = to.call{value: value}(data); } // solhint-disable-next-line payable-fallback,no-complex-fallback fallback() external payable { // solhint-disable-next-line no-inline-assembly assembly ("memory-safe") { let handler := sload(fallbackHandler.slot) if iszero(handler) { return(0, 0) } let ptr := mload(0x40) calldatacopy(ptr, 0, calldatasize()) // The msg.sender address is shifted to the left by 12 bytes to remove the padding // Then the address without padding is stored right after the calldata mstore(add(ptr, calldatasize()), shl(96, caller())) // Add 20 bytes for the address appended add the end let success := call(gas(), handler, 0, ptr, add(calldatasize(), 20), 0, 0) returndatacopy(ptr, 0, returndatasize()) if iszero(success) { revert(ptr, returndatasize()) } return(ptr, returndatasize()) } } receive() external payable {} } contract Safe4337Mock is SafeMock, IAccount { using UserOperationLib for PackedUserOperation; bytes32 private constant DOMAIN_SEPARATOR_TYPEHASH = 0x47e79534a245952e8b16893a336b85a3d9ea9fa8c573f3d803afb92a79469218; bytes32 private constant SAFE_OP_TYPEHASH = 0xc03dfc11d8b10bf9cf703d558958c8c42777f785d998c62060d85a4f0ef6ea7f; /** * @dev A structure used internally for manually encoding a Safe operation for when computing the EIP-712 struct hash. */ struct EncodedSafeOpStruct { bytes32 typeHash; address safe; uint256 nonce; bytes32 initCodeHash; bytes32 callDataHash; uint128 verificationGasLimit; uint128 callGasLimit; uint256 preVerificationGas; uint128 maxPriorityFeePerGas; uint128 maxFeePerGas; bytes32 paymasterAndDataHash; uint48 validAfter; uint48 validUntil; address entryPoint; } address public immutable SUPPORTED_ENTRYPOINT; constructor(address entryPoint) { SUPPORTED_ENTRYPOINT = entryPoint; } /** * @notice Validates the call is initiated by the entry point. */ modifier onlySupportedEntryPoint() { require(msg.sender == SUPPORTED_ENTRYPOINT, "Unsupported entry point"); _; } /// @dev Validates user operation provided by the entry point /// @inheritdoc IAccount function validateUserOp( PackedUserOperation calldata userOp, bytes32, uint256 missingAccountFunds ) external onlySupportedEntryPoint returns (uint256 validationData) { // We check the execution function signature to make sure the entryPoint can't call any other function // and make sure the execution of the user operation is handled by the module require( this.executeUserOp.selector == bytes4(userOp.callData) || this.executeUserOpWithErrorString.selector == bytes4(userOp.callData), "Unsupported execution function id" ); validationData = _validateSignatures(userOp); if (missingAccountFunds != 0) { (bool success, ) = SUPPORTED_ENTRYPOINT.call{value: missingAccountFunds}(""); success; } return 0; } /// @notice Executes user operation provided by the entry point /// @dev Reverts if unsuccessful /// @param to Destination address of the user operation. /// @param value Ether value of the user operation. /// @param data Data payload of the user operation. /// @param operation Operation type of the user operation. function executeUserOp(address to, uint256 value, bytes memory data, uint8 operation) external onlySupportedEntryPoint { bool success; if (operation == 1) (success, ) = to.delegatecall(data); else (success, ) = to.call{value: value}(data); require(success, "Execution failed"); } /// @notice Executes user operation provided by the entry point /// @dev Reverts if unsuccessful and bubbles up the error message /// @param to Destination address of the user operation. /// @param value Ether value of the user operation. /// @param data Data payload of the user operation. /// @param operation Operation type of the user operation. function executeUserOpWithErrorString(address to, uint256 value, bytes memory data, uint8 operation) external onlySupportedEntryPoint { bool success; bytes memory returnData; if (operation == 1) (success, returnData) = to.delegatecall(data); else (success, returnData) = to.call{value: value}(data); require(success, string(returnData)); } function domainSeparator() public view returns (bytes32) { return keccak256(abi.encode(DOMAIN_SEPARATOR_TYPEHASH, block.chainid, this)); } function chainId() public view returns (uint256) { return block.chainid; } /** * @notice Returns the 32-byte Safe operation hash to be signed by owners for the specified ERC-4337 user operation. * @dev The Safe operation timestamps are pre-pended to the signature bytes as `abi.encodePacked(validAfter, validUntil, signatures)`. * @param userOp The ERC-4337 user operation. * @return operationHash Operation hash. */ function getOperationHash(PackedUserOperation calldata userOp) external view returns (bytes32 operationHash) { (bytes memory operationData, , , ) = _getSafeOp(userOp); operationHash = keccak256(operationData); } /** * @dev Validates that the user operation is correctly signed. Reverts if signatures are invalid. * @param userOp User operation struct. * @return validationData An integer indicating the result of the validation. */ function _validateSignatures(PackedUserOperation calldata userOp) internal view returns (uint256 validationData) { (bytes memory operationData, uint48 validAfter, uint48 validUntil, bytes calldata signatures) = _getSafeOp(userOp); checkSignatures(keccak256(operationData), operationData, signatures); validationData = _packValidationData(false, validUntil, validAfter); } /** * @dev Decodes an ERC-4337 user operation into a Safe operation. * @param userOp The ERC-4337 user operation. * @return operationData Encoded EIP-712 Safe operation data bytes used for signature verification. * @return validAfter The timestamp the user operation is valid from. * @return validUntil The timestamp the user operation is valid until. * @return signatures The Safe owner signatures extracted from the user operation. */ function _getSafeOp( PackedUserOperation calldata userOp ) internal view returns (bytes memory operationData, uint48 validAfter, uint48 validUntil, bytes calldata signatures) { // Extract additional Safe operation fields from the user operation signature which is encoded as: // `abi.encodePacked(validAfter, validUntil, signatures)` { bytes calldata sig = userOp.signature; validAfter = uint48(bytes6(sig[0:6])); validUntil = uint48(bytes6(sig[6:12])); signatures = sig[12:]; } // It is important that **all** user operation fields are represented in the `SafeOp` data somehow, to prevent // user operations from being submitted that do not fully respect the user preferences. The only exception is // the `signature` bytes. Note that even `initCode` needs to be represented in the operation data, otherwise // it can be replaced with a more expensive initialization that would charge the user additional fees. { // In order to work around Solidity "stack too deep" errors related to too many stack variables, manually // encode the `SafeOp` fields into a memory `struct` for computing the EIP-712 struct-hash. This works // because the `EncodedSafeOpStruct` struct has no "dynamic" fields so its memory layout is identical to the // result of `abi.encode`-ing the individual fields. EncodedSafeOpStruct memory encodedSafeOp = EncodedSafeOpStruct({ typeHash: SAFE_OP_TYPEHASH, safe: userOp.sender, nonce: userOp.nonce, initCodeHash: keccak256(userOp.initCode), callDataHash: keccak256(userOp.callData), verificationGasLimit: uint128(userOp.unpackVerificationGasLimit()), callGasLimit: uint128(userOp.unpackCallGasLimit()), preVerificationGas: userOp.preVerificationGas, maxPriorityFeePerGas: uint128(userOp.unpackMaxPriorityFeePerGas()), maxFeePerGas: uint128(userOp.unpackMaxFeePerGas()), paymasterAndDataHash: keccak256(userOp.paymasterAndData), validAfter: validAfter, validUntil: validUntil, entryPoint: SUPPORTED_ENTRYPOINT }); bytes32 safeOpStructHash; // solhint-disable-next-line no-inline-assembly assembly ("memory-safe") { // Since the `encodedSafeOp` value's memory layout is identical to the result of `abi.encode`-ing the // individual `SafeOp` fields, we can pass it directly to `keccak256`. Additionally, there are 13 // 32-byte fields to hash, for a length of `14 * 32 = 448` bytes. safeOpStructHash := keccak256(encodedSafeOp, 448) } operationData = abi.encodePacked(bytes1(0x19), bytes1(0x01), domainSeparator(), safeOpStructHash); } } }