|
1 | 1 | // SPDX-License-Identifier: UNLICENSED |
2 | 2 | pragma solidity ^0.8.13; |
3 | 3 |
|
4 | | -contract ScheduledTxModule {} |
| 4 | +import {ISafe} from "./interfaces/ISafe.sol"; |
| 5 | + |
| 6 | +/** |
| 7 | + * @title ScheduledTxModule |
| 8 | + * @notice A Safe module that allows scheduling transactions with time-based execution windows |
| 9 | + * @dev Uses EIP-712 signatures to authorize scheduled transactions that can only be executed within specified time windows |
| 10 | + */ |
| 11 | +contract ScheduledTxModule { |
| 12 | + /** |
| 13 | + * @notice EIP-712 type hash for scheduled transaction permits |
| 14 | + * @dev Used to generate the struct hash for signature verification |
| 15 | + */ |
| 16 | + bytes32 public immutable PERMIT_TYPEHASH = keccak256( |
| 17 | + "ScheduledTxModule(address to,uint256 value,bytes data,uint256 nonce,uint64 executeAfter,uint64 deadline)" |
| 18 | + ); |
| 19 | + |
| 20 | + /** |
| 21 | + * @notice Tracks which nonces have been used for each Safe |
| 22 | + * @dev Mapping from Safe address to nonce to execution status |
| 23 | + */ |
| 24 | + mapping(address safe => mapping(uint256 nonce => bool used)) public nonces; |
| 25 | + |
| 26 | + /** |
| 27 | + * @notice Thrown when attempting to execute a transaction after its deadline |
| 28 | + */ |
| 29 | + error TransactionExpired(); |
| 30 | + |
| 31 | + /** |
| 32 | + * @notice Thrown when attempting to reuse a nonce that has already been executed |
| 33 | + */ |
| 34 | + error AlreadyExecuted(); |
| 35 | + |
| 36 | + /** |
| 37 | + * @notice Thrown when attempting to execute a transaction before its executeAfter timestamp |
| 38 | + */ |
| 39 | + error TooEarly(); |
| 40 | + |
| 41 | + /** |
| 42 | + * @notice Thrown when the module transaction execution fails |
| 43 | + */ |
| 44 | + error ModuleTxFailed(); |
| 45 | + |
| 46 | + /** |
| 47 | + * @notice Generates the EIP-712 domain separator for this contract |
| 48 | + * @dev The domain separator includes the contract name, version, chain ID, and address |
| 49 | + * @return The EIP-712 domain separator hash |
| 50 | + */ |
| 51 | + function getDomainSeparator() private view returns (bytes32) { |
| 52 | + return keccak256( |
| 53 | + abi.encode( |
| 54 | + keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"), |
| 55 | + keccak256(bytes("ScheduledTxModule")), |
| 56 | + keccak256(bytes("1")), |
| 57 | + block.chainid, |
| 58 | + address(this) |
| 59 | + ) |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @notice Executes a scheduled transaction on behalf of a Safe |
| 65 | + * @dev Validates timing constraints, signature, and marks nonce as used before execution |
| 66 | + * @param safe The address of the Safe contract that will execute the transaction |
| 67 | + * @param to The destination address for the transaction |
| 68 | + * @param value The amount of ETH to send with the transaction (in wei) |
| 69 | + * @param data The calldata to send with the transaction |
| 70 | + * @param nonce A unique identifier for this scheduled transaction (prevents replay attacks) |
| 71 | + * @param executeAfter The earliest timestamp when this transaction can be executed |
| 72 | + * @param deadline The latest timestamp when this transaction can be executed |
| 73 | + * @param signatures EIP-712 signature(s) from Safe owner(s) authorizing this transaction |
| 74 | + * @custom:reverts TooEarly if current timestamp is before executeAfter |
| 75 | + * @custom:reverts TransactionExpired if current timestamp is after deadline |
| 76 | + * @custom:reverts AlreadyExecuted if this nonce has already been used for this Safe |
| 77 | + * @custom:reverts ModuleTxFailed if the Safe transaction execution fails |
| 78 | + */ |
| 79 | + function execute( |
| 80 | + address safe, |
| 81 | + address to, |
| 82 | + uint256 value, |
| 83 | + bytes memory data, |
| 84 | + uint256 nonce, |
| 85 | + uint64 executeAfter, |
| 86 | + uint64 deadline, |
| 87 | + bytes memory signatures |
| 88 | + ) external { |
| 89 | + require(block.timestamp <= deadline, TransactionExpired()); |
| 90 | + require(block.timestamp >= executeAfter, TooEarly()); |
| 91 | + require(nonces[safe][nonce] == false, AlreadyExecuted()); |
| 92 | + |
| 93 | + nonces[safe][nonce] = true; |
| 94 | + |
| 95 | + bytes32 signatureData = keccak256(abi.encode(PERMIT_TYPEHASH, to, value, data, nonce, executeAfter, deadline)); |
| 96 | + |
| 97 | + bytes32 hash = keccak256(abi.encodePacked("\x19\x01", getDomainSeparator(), signatureData)); |
| 98 | + |
| 99 | + ISafe(payable(safe)).checkSignatures(hash, abi.encodePacked(signatureData), signatures); |
| 100 | + |
| 101 | + require(ISafe(payable(safe)).execTransactionFromModule(to, value, data, 0), ModuleTxFailed()); |
| 102 | + } |
| 103 | +} |
0 commit comments