Skip to content

Commit

Permalink
Minor optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
eloi010 committed Nov 14, 2023
1 parent 9c439f2 commit 1b682d7
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions contracts/core/BaseOpenfortAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ abstract contract BaseOpenfortAccount is
bytes4 internal constant EXECUTE_SELECTOR = 0xb61d27f6;
// bytes4(keccak256("executeBatch(address[],uint256[],bytes[])")
bytes4 internal constant EXECUTEBATCH_SELECTOR = 0x47e1da2a;
uint48 internal constant DEFAULT_LIMIT = 100;

/**
* Struct like ValidationData (from the EIP-4337) - alpha solution - to keep track of session keys' data
Expand Down Expand Up @@ -108,19 +107,18 @@ abstract contract BaseOpenfortAccount is
*/
function isValidSessionKey(address _sessionKey, bytes calldata callData) public returns (bool) {
SessionKeyStruct storage sessionKey = sessionKeys[_sessionKey];
// If the signer is a session key that is still valid
if (sessionKey.validUntil == 0) {
return false;
} // Not owner or session key revoked
// If not owner and the session key is revoked, return false
if (sessionKey.validUntil == 0) return false;

// If the signer is a session key that is still valid
// Let's first get the selector of the function that the caller is using
bytes4 funcSelector =
callData[0] | (bytes4(callData[1]) >> 8) | (bytes4(callData[2]) >> 16) | (bytes4(callData[3]) >> 24);

if (funcSelector == EXECUTE_SELECTOR) {
if (sessionKey.limit == 0) {
return false;
} // Limit of transactions per sessionKey reached
// Limit of transactions per sessionKey reached
if (sessionKey.limit == 0) return false;
// Deduct one use of the limit for the given session key
unchecked {
sessionKey.limit = sessionKey.limit - 1;
}
Expand All @@ -145,17 +143,14 @@ abstract contract BaseOpenfortAccount is
return false; // All other cases, deny
} else if (funcSelector == EXECUTEBATCH_SELECTOR) {
(address[] memory toContracts,,) = abi.decode(callData[4:], (address[], uint256[], bytes[]));
if (sessionKey.limit < toContracts.length || toContracts.length > 9) {
return false;
} // Limit of transactions per sessionKey reached
// Check if limit of transactions per sessionKey reached
if (sessionKey.limit < toContracts.length || toContracts.length > 9) return false;
unchecked {
sessionKey.limit = sessionKey.limit - SafeCastUpgradeable.toUint48(toContracts.length);
}

// Check if it is a masterSessionKey
if (sessionKey.masterSessionKey) {
return true;
}
// Check if it is a masterSessionKey (no whitelist applies)
if (sessionKey.masterSessionKey) return true;

for (uint256 i = 0; i < toContracts.length;) {
if (toContracts[i] == address(this)) {
Expand Down

0 comments on commit 1b682d7

Please sign in to comment.