Skip to content
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
12 changes: 12 additions & 0 deletions script/DeployCaveatEnforcers.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import { ERC721BalanceGteEnforcer } from "../src/enforcers/ERC721BalanceGteEnfor
import { ERC721TransferEnforcer } from "../src/enforcers/ERC721TransferEnforcer.sol";
import { ERC1155BalanceGteEnforcer } from "../src/enforcers/ERC1155BalanceGteEnforcer.sol";
import { ExactCalldataEnforcer } from "../src/enforcers/ExactCalldataEnforcer.sol";
import { ExactExecutionBatchEnforcer } from "../src/enforcers/ExactExecutionBatchEnforcer.sol";
import { ExactCalldataBatchEnforcer } from "../src/enforcers/ExactCalldataBatchEnforcer.sol";
import { ExactExecutionEnforcer } from "../src/enforcers/ExactExecutionEnforcer.sol";
import { IdEnforcer } from "../src/enforcers/IdEnforcer.sol";
import { LimitedCallsEnforcer } from "../src/enforcers/LimitedCallsEnforcer.sol";
import { NativeBalanceGteEnforcer } from "../src/enforcers/NativeBalanceGteEnforcer.sol";
Expand Down Expand Up @@ -103,6 +106,15 @@ contract DeployCaveatEnforcers is Script {
deployedAddress = address(new ExactCalldataEnforcer{ salt: salt }());
console2.log("ExactCalldataEnforcer: %s", deployedAddress);

deployedAddress = address(new ExactCalldataBatchEnforcer{ salt: salt }());
console2.log("ExactCalldataBatchEnforcer: %s", deployedAddress);

deployedAddress = address(new ExactExecutionBatchEnforcer{ salt: salt }());
console2.log("ExactExecutionBatchEnforcer: %s", deployedAddress);

deployedAddress = address(new ExactExecutionEnforcer{ salt: salt }());
console2.log("ExactExecutionEnforcer: %s", deployedAddress);

deployedAddress = address(new IdEnforcer{ salt: salt }());
console2.log("IdEnforcer: %s", deployedAddress);

Expand Down
64 changes: 64 additions & 0 deletions src/enforcers/ExactCalldataBatchEnforcer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: MIT AND Apache-2.0
pragma solidity 0.8.23;

import { ExecutionLib } from "@erc7579/lib/ExecutionLib.sol";
import { ModeLib } from "@erc7579/lib/ModeLib.sol";

import { CaveatEnforcer } from "./CaveatEnforcer.sol";
import { ModeCode, Execution } from "../utils/Types.sol";

/**
* @title ExactCalldataBatchEnforcer
* @notice Ensures that the provided batch execution calldata matches exactly the expected calldata for each execution.
* @dev This caveat enforcer operates only in batch execution mode.
*/
contract ExactCalldataBatchEnforcer is CaveatEnforcer {
using ExecutionLib for bytes;
using ModeLib for ModeCode;

////////////////////////////// Public Methods //////////////////////////////

/**
* @notice Validates that each execution's calldata in the batch matches the expected calldata.
* @param _terms The encoded expected Executions.
* @param _mode The execution mode, which must be batch.
* @param _executionCallData The batch execution calldata.
*/
function beforeHook(
bytes calldata _terms,
bytes calldata,
ModeCode _mode,
bytes calldata _executionCallData,
bytes32,
address,
address
)
public
pure
override
onlyBatchExecutionMode(_mode)
{
Execution[] calldata executions_ = _executionCallData.decodeBatch();
Execution[] memory termsExecutions_ = getTermsInfo(_terms);

// Validate that the number of executions matches the number of expected calldata
require(executions_.length == termsExecutions_.length, "ExactCalldataBatchEnforcer:invalid-batch-size");

// Check each execution's calldata matches exactly
for (uint256 i = 0; i < executions_.length; i++) {
require(
keccak256(termsExecutions_[i].callData) == keccak256(executions_[i].callData),
"ExactCalldataBatchEnforcer:invalid-calldata"
);
}
}

/**
* @notice Extracts the expected executions from the provided terms.
* @param _terms The encoded expected Executions.
* @return executions_ Array of expected Executions.
*/
function getTermsInfo(bytes calldata _terms) public pure returns (Execution[] memory executions_) {
executions_ = _terms.decodeBatch();
}
}
62 changes: 62 additions & 0 deletions src/enforcers/ExactExecutionBatchEnforcer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: MIT AND Apache-2.0
pragma solidity 0.8.23;

import { ExecutionLib } from "@erc7579/lib/ExecutionLib.sol";
import { ModeLib } from "@erc7579/lib/ModeLib.sol";

import { CaveatEnforcer } from "./CaveatEnforcer.sol";
import { ModeCode, Execution } from "../utils/Types.sol";

/**
* @title ExactExecutionBatchEnforcer
* @notice Ensures that each execution in the batch matches exactly with the expected execution (target, value, and calldata).
* @dev This caveat enforcer operates only in batch execution mode.
*/
contract ExactExecutionBatchEnforcer is CaveatEnforcer {
using ExecutionLib for bytes;
using ModeLib for ModeCode;

////////////////////////////// Public Methods //////////////////////////////

/**
* @notice Validates that each execution in the batch matches exactly with the expected execution.
* @param _terms The encoded expected Executions.
* @param _mode The execution mode, which must be batch.
* @param _executionCallData The batch execution calldata.
*/
function beforeHook(
bytes calldata _terms,
bytes calldata,
ModeCode _mode,
bytes calldata _executionCallData,
bytes32,
address,
address
)
public
pure
override
onlyBatchExecutionMode(_mode)
{
Execution[] calldata executions_ = _executionCallData.decodeBatch();
Execution[] memory termsExecutions_ = getTermsInfo(_terms);

// Validate that the number of executions matches
require(executions_.length == termsExecutions_.length, "ExactExecutionBatchEnforcer:invalid-batch-size");

// Encode both sets of executions and compare the hashes
require(
keccak256(abi.encode(executions_)) == keccak256(abi.encode(termsExecutions_)),
"ExactExecutionBatchEnforcer:invalid-execution"
);
}

/**
* @notice Extracts the expected executions from the provided terms.
* @param _terms The encoded expected Executions.
* @return executions_ Array of expected Executions.
*/
function getTermsInfo(bytes calldata _terms) public pure returns (Execution[] memory executions_) {
executions_ = _terms.decodeBatch();
}
}
59 changes: 59 additions & 0 deletions src/enforcers/ExactExecutionEnforcer.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: MIT AND Apache-2.0
pragma solidity 0.8.23;

import { ExecutionLib } from "@erc7579/lib/ExecutionLib.sol";
import { ModeLib } from "@erc7579/lib/ModeLib.sol";

import { CaveatEnforcer } from "./CaveatEnforcer.sol";
import { ModeCode, Execution } from "../utils/Types.sol";

/**
* @title ExactExecutionEnforcer
* @notice Ensures that the provided execution matches exactly with the expected execution (target, value, and calldata).
* @dev This caveat enforcer operates only in single execution mode.
*/
contract ExactExecutionEnforcer is CaveatEnforcer {
using ExecutionLib for bytes;
using ModeLib for ModeCode;

////////////////////////////// Public Methods //////////////////////////////

/**
* @notice Validates that the execution matches exactly with the expected execution.
* @param _terms The encoded expected Execution.
* @param _mode The execution mode, which must be single.
* @param _executionCallData The execution calldata.
*/
function beforeHook(
bytes calldata _terms,
bytes calldata,
ModeCode _mode,
bytes calldata _executionCallData,
bytes32,
address,
address
)
public
pure
override
onlySingleExecutionMode(_mode)
{
// Decode execution data
(address execTarget_, uint256 execValue_, bytes calldata execCallData_) = _executionCallData.decodeSingle();

require(
address(bytes20(_terms[0:20])) == execTarget_ && uint256(bytes32(_terms[20:52])) == execValue_
&& keccak256(_terms[52:]) == keccak256(execCallData_),
"ExactExecutionEnforcer:invalid-execution"
);
}

/**
* @notice Extracts the expected execution from the provided terms.
* @param _terms The encoded expected Execution.
* @return execution_ The expected Execution.
*/
function getTermsInfo(bytes calldata _terms) public pure returns (Execution memory execution_) {
(execution_.target, execution_.value, execution_.callData) = _terms.decodeSingle();
}
}
Loading