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
16 changes: 16 additions & 0 deletions src/Kernel.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ contract Kernel is EIP712, Compatibility, KernelStorage {
default { return(0, returndatasize()) }
}
}

/// @notice Executes a function call to an external contract with delegatecall
/// @param to The address of the target contract
/// @param data The call data to be sent
function executeDelegateCall(address to, bytes memory data) external payable {
if (msg.sender != address(entryPoint) && msg.sender != address(this) && !_checkCaller()) {
revert NotAuthorizedCaller();
}
assembly {
let success := delegatecall(gas(), to, add(data, 0x20), mload(data), 0, 0)
returndatacopy(0, 0, returndatasize())
switch success
case 0 { revert(0, returndatasize()) }
default { return(0, returndatasize()) }
}
}

/// @notice Executes a function call to an external contract batched
/// @param calls The calls to be executed, in order
Expand Down
18 changes: 17 additions & 1 deletion test/foundry/KernelTestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,24 @@ abstract contract KernelTestBase is Test {
kernel.execute(validCallers[i], 0, "", Operation.Call);
}
}


function test_external_call_execute_delegatecall_success() external {
address[] memory validCallers = getOwners();
for (uint256 i = 0; i < validCallers.length; i++) {
vm.prank(validCallers[i]);
kernel.executeDelegateCall(validCallers[i], "");
}
}
function test_external_call_execute_delegatecall_fail() external {
address[] memory validCallers = getOwners();
for (uint256 i = 0; i < validCallers.length; i++) {
vm.prank(address(uint160(validCallers[i]) + 1));
vm.expectRevert();
kernel.executeDelegateCall(validCallers[i], "");
}
}

function test_external_call_execute_delegatecall_option_fail() external {
address[] memory validCallers = getOwners();
for (uint256 i = 0; i < validCallers.length; i++) {
vm.prank(validCallers[i]);
Expand Down