Skip to content

Commit

Permalink
Making OF_MSG_TYPEHASH internal
Browse files Browse the repository at this point in the history
  • Loading branch information
eloi010 committed Nov 22, 2023
1 parent cd53700 commit 5451500
Showing 1 changed file with 83 additions and 83 deletions.
166 changes: 83 additions & 83 deletions contracts/core/base/BaseOpenfortAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ abstract contract BaseOpenfortAccount is
// bytes4(keccak256("executeBatch(address[],uint256[],bytes[])")
bytes4 internal constant EXECUTEBATCH_SELECTOR = 0x47e1da2a;
// keccak256("OpenfortMessage(bytes32 hashedMessage)");
bytes32 private constant OF_MSG_TYPEHASH = 0x57159f03b9efda178eab2037b2ec0b51ce11be0051b8a2a9992c29dc260e4a30;
bytes32 internal constant OF_MSG_TYPEHASH = 0x57159f03b9efda178eab2037b2ec0b51ce11be0051b8a2a9992c29dc260e4a30;

/**
* Struct like ValidationData (from the EIP-4337) - alpha solution - to keep track of session keys' data
Expand Down Expand Up @@ -68,33 +68,40 @@ abstract contract BaseOpenfortAccount is
_disableInitializers();
}

/**
* Require the function call went through EntryPoint or owner
*/
function _requireFromEntryPointOrOwner() internal view virtual {
if (msg.sender != address(entryPoint()) && msg.sender != owner()) {
revert NotOwnerOrEntrypoint();
}
}

/**
* Require the function call went through owner
*/
function _requireFromOwner() internal view {
if (msg.sender != owner()) {
revert NotOwner();
}
}

function owner() public view virtual returns (address);

/**
* Check current account deposit in the entryPoint
*/
function getDeposit() public view virtual returns (uint256) {
function getDeposit() external view virtual returns (uint256) {
return entryPoint().balanceOf(address(this));
}

/*
* @notice See EIP-1271
* Owner and session keys need to sign using EIP712.
*/
function isValidSignature(bytes32 _hash, bytes memory _signature) external view virtual override returns (bytes4) {
bytes32 structHash = keccak256(abi.encode(OF_MSG_TYPEHASH, _hash));
bytes32 digest = _hashTypedDataV4(structHash);
address signer = digest.recover(_signature);
if (owner() == signer) return MAGICVALUE;

SessionKeyStruct storage sessionKey = sessionKeys[signer];
// If the signer is a session key that is still valid
if (
sessionKey.validUntil == 0 || sessionKey.validAfter > block.timestamp
|| sessionKey.validUntil < block.timestamp || sessionKey.limit < 1
) {
return 0xffffffff;
} // Not owner or session key revoked
else if (sessionKey.registrarAddress != owner()) {
return 0xffffffff;
} else {
return MAGICVALUE;
}
}

/*
* @notice Return whether a sessionKey is valid.
*/
Expand Down Expand Up @@ -166,31 +173,6 @@ abstract contract BaseOpenfortAccount is
return false;
}

/*
* @notice See EIP-1271
* Owner and session keys need to sign using EIP712.
*/
function isValidSignature(bytes32 _hash, bytes memory _signature) public view virtual override returns (bytes4) {
bytes32 structHash = keccak256(abi.encode(OF_MSG_TYPEHASH, _hash));
bytes32 digest = _hashTypedDataV4(structHash);
address signer = digest.recover(_signature);
if (owner() == signer) return MAGICVALUE;

SessionKeyStruct storage sessionKey = sessionKeys[signer];
// If the signer is a session key that is still valid
if (
sessionKey.validUntil == 0 || sessionKey.validAfter > block.timestamp
|| sessionKey.validUntil < block.timestamp || sessionKey.limit < 1
) {
return 0xffffffff;
} // Not owner or session key revoked
else if (sessionKey.registrarAddress != owner()) {
return 0xffffffff;
} else {
return MAGICVALUE;
}
}

/**
* Execute a transaction (called directly from owner, or by entryPoint)
*/
Expand Down Expand Up @@ -222,7 +204,7 @@ abstract contract BaseOpenfortAccount is
/**
* Deposit funds for this account in the EntryPoint
*/
function addDeposit() public payable virtual {
function addDeposit() external payable virtual {
entryPoint().depositTo{value: msg.value}(address(this));
}

Expand All @@ -237,42 +219,6 @@ abstract contract BaseOpenfortAccount is
entryPoint().withdrawTo(_withdrawAddress, _amount);
}

/**
* @dev Call a target contract and reverts if it fails.
*/
function _call(address _target, uint256 _value, bytes calldata _calldata) internal virtual {
(bool success, bytes memory result) = _target.call{value: _value}(_calldata);
if (!success) {
assembly {
revert(add(result, 32), mload(result))
}
}
}

/**
* @inheritdoc BaseAccount
*/
function _validateSignature(UserOperation calldata userOp, bytes32 userOpHash)
internal
virtual
override
returns (uint256 validationData)
{
require(entryPoint().getUserOpHash(userOp) == userOpHash, "Calculated userOpHash doesn't match");
bytes32 hash = userOpHash.toEthSignedMessageHash();
address signer = hash.recover(userOp.signature);

// If the userOp was signed by the owner, allow straightaway
if (owner() == signer) return 0;

// Check if the session key is valid according to the data in the userOp
if (isValidSessionKey(signer, userOp.callData)) {
return _packValidationData(false, sessionKeys[signer].validUntil, sessionKeys[signer].validAfter);
}

return SIG_VALIDATION_FAILED;
}

/**
* Register a session key to the account
* @param _key session key to register
Expand All @@ -287,7 +233,7 @@ abstract contract BaseOpenfortAccount is
uint48 _validUntil,
uint48 _limit,
address[] calldata _whitelist
) public virtual {
) external virtual {
_requireFromEntryPointOrOwner();

require(_whitelist.length < 11, "Whitelist too big");
Expand Down Expand Up @@ -328,4 +274,58 @@ abstract contract BaseOpenfortAccount is
emit SessionKeyRevoked(_key);
}
}

/**
* @dev Call a target contract and reverts if it fails.
*/
function _call(address _target, uint256 _value, bytes calldata _calldata) internal virtual {
(bool success, bytes memory result) = _target.call{value: _value}(_calldata);
if (!success) {
assembly {
revert(add(result, 32), mload(result))
}
}
}

/**
* @inheritdoc BaseAccount
*/
function _validateSignature(UserOperation calldata userOp, bytes32 userOpHash)
internal
virtual
override
returns (uint256 validationData)
{
require(entryPoint().getUserOpHash(userOp) == userOpHash, "Calculated userOpHash doesn't match");
bytes32 hash = userOpHash.toEthSignedMessageHash();
address signer = hash.recover(userOp.signature);

// If the userOp was signed by the owner, allow straightaway
if (owner() == signer) return 0;

// Check if the session key is valid according to the data in the userOp
if (isValidSessionKey(signer, userOp.callData)) {
return _packValidationData(false, sessionKeys[signer].validUntil, sessionKeys[signer].validAfter);
}

return SIG_VALIDATION_FAILED;
}

/**
* Require the function call went through EntryPoint or owner
*/
function _requireFromEntryPointOrOwner() internal view virtual {
if (msg.sender != address(entryPoint()) && msg.sender != owner()) {
revert NotOwnerOrEntrypoint();
}
}

/**
* Require the function call went through owner
*/
function _requireFromOwner() internal view {
if (msg.sender != owner()) {
revert NotOwner();
}
}
}

0 comments on commit 5451500

Please sign in to comment.