-
Notifications
You must be signed in to change notification settings - Fork 163
/
ApproveRestrictedWallet.sol
94 lines (81 loc) · 3.5 KB
/
ApproveRestrictedWallet.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;
// Abstract base for common functionality between
// `ApproveRestrictedWallet` and `ApproveRestrictedWallet_Memory`.
abstract contract ApproveRestrictedWalletBase {
address public immutable owner;
mapping (address => bool) public isAllowedSpender;
modifier onlyOwner() {
require(msg.sender == owner, 'only owner');
_;
}
constructor(address owner_) {
owner = owner_;
}
function setAllowedSpender(address spender, bool allowed) external onlyOwner {
isAllowedSpender[spender] = allowed;
}
receive() external payable {}
function exec(address payable callTarget, bytes calldata fnCallData, uint256 callValue)
external payable virtual;
function _exec(address payable callTarget, bytes memory fnCallData, uint256 callValue)
internal
{
(bool s,) = callTarget.call{value: callValue}(fnCallData);
require(s, 'exec failed');
}
}
// A smart wallet that executes arbitrary calls passed in by its owner.
// If an ERC20.approve() call is detected, it will ensure that the spender is on
// the `isAllowedSpender` list before executing it.
contract ApproveRestrictedWallet is ApproveRestrictedWalletBase {
constructor(address owner_) ApproveRestrictedWalletBase(owner_) {}
function exec(address payable callTarget, bytes calldata fnCallData, uint256 callValue)
external payable override
onlyOwner
{
if (bytes4(fnCallData) == IERC20.approve.selector) {
// ABI-decode the remaining bytes of fnCallData as IERC20.approve() parameters
// using a calldata array slice to remove the leading 4 bytes.
(address spender,) = abi.decode(fnCallData[4:], (address, uint256));
require(isAllowedSpender[spender], 'not an allowed spender');
}
_exec(callTarget, fnCallData, callValue);
}
}
// A smart wallet that executes arbitrary calls passed in by its owner.
// If an ERC20.approve() call is detected, it will ensure that the spender is on
// the `isAllowedSpender` list before executing it.
contract ApproveRestrictedWallet_Memory is ApproveRestrictedWalletBase {
constructor(address owner_) ApproveRestrictedWalletBase(owner_) {}
function exec(address payable callTarget, bytes memory fnCallData, uint256 callValue)
external payable override
onlyOwner
{
// Compare the first 4 bytes (selector) of fnCallData.
if (bytes4(fnCallData) == IERC20.approve.selector) {
// Since fnCallData is located in memory, we cannot use calldata slices.
// Modify the array data in-place to shift the start 4 bytes.
bytes32 oldBits;
assembly {
let len := mload(fnCallData)
fnCallData := add(fnCallData, 4)
oldBits := mload(fnCallData)
mstore(fnCallData, sub(len, 4))
}
// ABI-decode fnCallData as IERC20.approve() parameters.
(address spender,) = abi.decode(fnCallData, (address, uint256));
// Undo the array modification.
assembly {
mstore(fnCallData, oldBits)
fnCallData := sub(fnCallData, 4)
}
require(isAllowedSpender[spender], 'not an allowed spender');
}
_exec(callTarget, fnCallData, callValue);
}
}
// Minimal ERC20 interface.
interface IERC20 {
function approve(address spender, uint256 allowance) external returns (bool);
}