Skip to content

Commit

Permalink
feat: add _msgSenderOnlyEVCAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
kasperpawlowski committed Oct 16, 2024
1 parent 5025707 commit 3686aea
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/utils/EVCUtil.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,24 @@ abstract contract EVCUtil {
return sender;
}

/// @notice Retrieves the message sender, ensuring it's any EVC account meaning that the execution context is in a
/// standard state (not operator authenticated, not control collateral in progress, not checks in progress).
/// @dev This function must not be used on functions utilized by liquidation flows, i.e. transfer or withdraw.
/// @dev This function must not be used on checkAccountStatus and checkVaultStatus functions.
/// @dev This function can be used on access controlled functions to prevent non-standard authentication paths on
/// the EVC.
/// @return The address of the message sender.
function _msgSenderOnlyEVCAccount() internal view returns (address) {
return _authenticateCallerWithStandardContextState(false);
}

/// @notice Retrieves the message sender, ensuring it's the EVC account owner and that the execution context is in a
/// standard state (not operator authenticated, not control collateral in progress, not checks in progress).
/// @dev It assumes that if the caller is not the EVC, the caller is the account owner.
/// @dev This function must not be used on functions utilized by liquidation flows, i.e. transfer or withdraw.
/// @dev This function must not be used on checkAccountStatus and checkVaultStatus functions.
/// @dev This function can be used on access controlled functions to prevent non-standard authentication paths on
/// the EVC.
/// @return The address of the message sender.
function _msgSenderOnlyEVCAccountOwner() internal view returns (address) {
return _authenticateCallerWithStandardContextState(true);
Expand Down
32 changes: 31 additions & 1 deletion test/unit/EVCUtil/EVCUtil.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ contract EVCClient is EVCUtil {
return _msgSenderForBorrow();
}

function msgSenderOnlyEVCAccount() external view returns (address) {
return _msgSenderOnlyEVCAccount();
}

function msgSenderOnlyEVCAccountOwner() external view returns (address) {
return _msgSenderOnlyEVCAccountOwner();
}
Expand Down Expand Up @@ -195,7 +199,7 @@ contract EVCUtilTest is Test {
evcClient.calledByEVCWithChecksInProgress();
}

function test_calledByEVCAccount_calledByEVCAccountOwner_msgSenderOnlyEVCAccountOwner(
function test_calledByEVCAccount_calledByEVCAccountOwner_msgSenderOnlyEVCAccount_msgSenderOnlyEVCAccountOwner(
address caller,
uint8 id
) external {
Expand All @@ -210,6 +214,9 @@ contract EVCUtilTest is Test {
vm.prank(caller);
evcClient.calledByEVCAccountOwner();

vm.prank(caller);
assertEq(evcClient.msgSenderOnlyEVCAccount(), caller);

vm.prank(caller);
assertEq(evcClient.msgSenderOnlyEVCAccountOwner(), caller);

Expand All @@ -223,6 +230,10 @@ contract EVCUtilTest is Test {
vm.expectRevert(abi.encodeWithSelector(EVCUtil.NotAuthorized.selector));
evcClient.calledByEVCAccountOwner();

vm.prank(address(evc));
vm.expectRevert(abi.encodeWithSelector(EVCUtil.NotAuthorized.selector));
evcClient.msgSenderOnlyEVCAccount();

vm.prank(address(evc));
vm.expectRevert(abi.encodeWithSelector(EVCUtil.NotAuthorized.selector));
evcClient.msgSenderOnlyEVCAccountOwner();
Expand All @@ -238,6 +249,10 @@ contract EVCUtilTest is Test {
vm.expectRevert(abi.encodeWithSelector(EVCUtil.NotAuthorized.selector));
evcClient.calledByEVCAccountOwner();

vm.prank(address(evc));
vm.expectRevert(abi.encodeWithSelector(EVCUtil.NotAuthorized.selector));
evcClient.msgSenderOnlyEVCAccount();

vm.prank(address(evc));
vm.expectRevert(abi.encodeWithSelector(EVCUtil.NotAuthorized.selector));
evcClient.msgSenderOnlyEVCAccountOwner();
Expand All @@ -253,6 +268,10 @@ contract EVCUtilTest is Test {
vm.expectRevert(abi.encodeWithSelector(EVCUtil.NotAuthorized.selector));
evcClient.calledByEVCAccountOwner();

vm.prank(address(evc));
vm.expectRevert(abi.encodeWithSelector(EVCUtil.NotAuthorized.selector));
evcClient.msgSenderOnlyEVCAccount();

vm.prank(address(evc));
vm.expectRevert(abi.encodeWithSelector(EVCUtil.NotAuthorized.selector));
evcClient.msgSenderOnlyEVCAccountOwner();
Expand All @@ -268,6 +287,10 @@ contract EVCUtilTest is Test {
vm.prank(address(evc));
evcClient.calledByEVCAccountOwner();

assertEq(evc.getAccountOwner(caller), address(0));
vm.prank(address(evc));
assertEq(evcClient.msgSenderOnlyEVCAccount(), caller);

assertEq(evc.getAccountOwner(caller), address(0));
vm.prank(address(evc));
assertEq(evcClient.msgSenderOnlyEVCAccountOwner(), caller);
Expand All @@ -285,6 +308,10 @@ contract EVCUtilTest is Test {
vm.prank(address(evc));
evcClient.calledByEVCAccountOwner();

assertEq(evc.getAccountOwner(caller), caller);
vm.prank(address(evc));
assertEq(evcClient.msgSenderOnlyEVCAccount(), caller);

assertEq(evc.getAccountOwner(caller), caller);
vm.prank(address(evc));
assertEq(evcClient.msgSenderOnlyEVCAccountOwner(), caller);
Expand All @@ -298,6 +325,9 @@ contract EVCUtilTest is Test {
vm.expectRevert(abi.encodeWithSelector(EVCUtil.NotAuthorized.selector));
evcClient.calledByEVCAccountOwner();

vm.prank(address(evc));
assertEq(evcClient.msgSenderOnlyEVCAccount(), address(uint160(uint160(caller) ^ id)));

vm.prank(address(evc));
vm.expectRevert(abi.encodeWithSelector(EVCUtil.NotAuthorized.selector));
evcClient.msgSenderOnlyEVCAccountOwner();
Expand Down

0 comments on commit 3686aea

Please sign in to comment.