-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAllowedCalldataChecker.sol
58 lines (51 loc) · 2.17 KB
/
AllowedCalldataChecker.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// SPDX-FileCopyrightText: 2024 P2P Validator <[email protected]>
// SPDX-License-Identifier: MIT
pragma solidity 0.8.27;
import "./IAllowedCalldataChecker.sol";
import "./P2pStructs.sol";
/// @dev Error for when the calldata is too short
error AllowedCalldataChecker__DataTooShort();
/// @title AllowedCalldataChecker
/// @author P2P Validator <[email protected]>
/// @notice Abstract contract for checking if a calldata is allowed
abstract contract AllowedCalldataChecker is IAllowedCalldataChecker {
/// @dev Modifier for checking if a calldata is allowed
/// @param _lendingProtocolAddress The address of the lending protocol
/// @param _lendingProtocolCalldata The calldata (encoded signature + arguments) to be passed to the lending protocol
/// @param _functionType Deposit, Withdraw, or None
modifier calldataShouldBeAllowed(
address _lendingProtocolAddress,
bytes calldata _lendingProtocolCalldata,
P2pStructs.FunctionType _functionType
) {
// validate lendingProtocolCalldata for lendingProtocolAddress
bytes4 selector = _getFunctionSelector(_lendingProtocolCalldata);
checkCalldata(
_lendingProtocolAddress,
selector,
_lendingProtocolCalldata[4:],
_functionType
);
_;
}
/// @notice Returns function selector (first 4 bytes of data)
/// @param _data calldata (encoded signature + arguments)
/// @return functionSelector function selector
function _getFunctionSelector(
bytes calldata _data
) private pure returns (bytes4 functionSelector) {
require (_data.length >= 4, AllowedCalldataChecker__DataTooShort());
return bytes4(_data[:4]);
}
/// @notice Checks if the calldata is allowed
/// @param _target The address of the lending protocol
/// @param _selector The selector of the function
/// @param _calldataAfterSelector The calldata after the selector
/// @param _functionType Deposit, Withdraw, or None
function checkCalldata(
address _target,
bytes4 _selector,
bytes calldata _calldataAfterSelector,
P2pStructs.FunctionType _functionType
) public virtual view;
}