Skip to content

Commit

Permalink
Nicer errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ScreamingHawk committed Sep 11, 2023
1 parent e003d71 commit 0fef464
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
13 changes: 8 additions & 5 deletions contracts/hooks/EIP4337Hook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,24 @@ contract EIP4337Hook is IEIP4337Hook, ModuleNonce {
}

modifier onlyEntrypoint() {
require(msg.sender == entrypoint, 'EIP4337Hook: only 4337 or self'); //FIXME error obj
if (msg.sender != entrypoint) {
revert InvalidCaller();
}
_;
}

/**
* Allow the EIP-4337 entrypoint to execute a transaction on the wallet.
* @dev This function does not validate as the entrypoint is trusted to have called validateUserOp.
* @dev Failure handling done by ModuleCalls.
* @notice This functions is only callable by the Entrypoint.
*/
function eip4337SelfExecute(IModuleCalls.Transaction[] calldata txs) external payable onlyEntrypoint {
// Self execute
(bool success, ) = payable(address(this)).call{value: msg.value}(
abi.encodeWithSelector(IModuleCalls.selfExecute.selector, txs)
);
//FIXME Required? selfexecute should revert if it fails
require(success, 'call failed'); // FIXME Better error?
(success);
}

/**
Expand All @@ -48,8 +50,9 @@ contract EIP4337Hook is IEIP4337Hook, ModuleNonce {
bytes32 userOpHash,
uint256 missingAccountFunds
) external onlyEntrypoint returns (uint256 validationData) {
// Check nonce
_validateNonce(userOp.nonce); //FIXME Sequence space encoding is diff to EIP-4337 encoding
// Check nonce.
// Note Sequence space encoding is diff to EIP-4337 encoding.
_validateNonce(userOp.nonce);

// Check signature
bytes32 ethHash = keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", userOpHash));
Expand Down
9 changes: 8 additions & 1 deletion contracts/hooks/interfaces/IEIP4337Hook.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@ pragma solidity 0.8.18;
import "../../modules/commons/interfaces/IModuleCalls.sol";
import "./IAccount.sol";

interface IEIP4337HookErrors {

// Thrown when not called by the entrypoint.
error InvalidCaller();

}

/**
* An extension to EIP-4337 that includes a self execute function.
*/
interface IEIP4337Hook is IAccount {
interface IEIP4337Hook is IAccount, IEIP4337HookErrors {

/**
* @notice Allow wallet owner to execute an action.
Expand Down
22 changes: 20 additions & 2 deletions foundry_test/hooks/EIP4337Hook.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
pragma solidity 0.8.18;

import 'contracts/hooks/EIP4337Hook.sol';
import 'contracts/hooks/interfaces/IEIP4337Hook.sol';
import 'contracts/modules/commons/ModuleHooks.sol';
import 'contracts/modules/MainModule.sol';
import 'contracts/modules/MainModuleUpgradable.sol';
import 'contracts/Factory.sol';

import 'foundry_test/base/AdvTest.sol';

contract EIP4337HookTest is AdvTest {
contract EIP4337HookTest is AdvTest, IEIP4337HookErrors {
MainModule private template;
EIP4337Hook private wallet;

Expand Down Expand Up @@ -40,7 +41,24 @@ contract EIP4337HookTest is AdvTest {
uint256 value;
}

function test_execute_sendEth(ToVal memory sendTx) external {
function test_4337execute_invalidCaller(ToVal memory sendTx, address sender) external {
vm.assume(sender != ENTRYPOINT);
IModuleCalls.Transaction[] memory txs = new IModuleCalls.Transaction[](1);
txs[0] = IModuleCalls.Transaction({
delegateCall: false,
revertOnError: false,
gasLimit: 0,
target: sendTx.target,
value: sendTx.value,
data: ''
});

vm.expectRevert(InvalidCaller.selector);
vm.prank(sender);
wallet.eip4337SelfExecute(txs);
}

function test_4337execute_sendEth(ToVal memory sendTx) external {
vm.assume(sendTx.target.code.length == 0); // Non contract
uint256 walletBal = address(wallet).balance;
vm.assume(sendTx.value <= walletBal);
Expand Down

0 comments on commit 0fef464

Please sign in to comment.