-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathModuleHooks.sol
More file actions
146 lines (128 loc) · 4.84 KB
/
ModuleHooks.sol
File metadata and controls
146 lines (128 loc) · 4.84 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// SPDX-License-Identifier: Apache-2.0
pragma solidity 0.8.27;
import "./interfaces/IModuleHooks.sol";
import "./ModuleSelfAuth.sol";
import "./ModuleStorage.sol";
import "./ModuleERC165.sol";
import "../../interfaces/receivers/IERC1155Receiver.sol";
import "../../interfaces/receivers/IERC721Receiver.sol";
import "../../interfaces/receivers/IERC223Receiver.sol";
contract ModuleHooks is IERC1155Receiver, IERC721Receiver, IModuleHooks, ModuleERC165, ModuleSelfAuth {
// Randomly generated to avoid collisions, with:
// xxd -len 32 -plain -cols 32 /dev/urandom
bytes32 private constant HOOKS_KEY = bytes32(0x5f198cb61cfcc209f357b4ede4ad2218c53e3b4cb7e8fa1e8b0ec5e3951acbaa);
/**
* @notice Reads the implementation hook of a signature
* @param _signature Signature function
* @return The address of the implementation hook, address(0) if none
*/
function readHook(bytes4 _signature) external override view returns (address) {
return _readHook(_signature);
}
/**
* @notice Adds a new hook to handle a given function selector
* @param _signature Signature function linked to the hook
* @param _implementation Hook implementation contract
* @dev Can't overwrite hooks that are part of the mainmodule (those defined below)
*/
function addHook(bytes4 _signature, address _implementation) external override onlySelf {
require(_readHook(_signature) == address(0), "ModuleHooks#addHook: HOOK_ALREADY_REGISTERED");
_writeHook(_signature, _implementation);
}
/**
* @notice Removes a registered hook
* @param _signature Signature function linked to the hook
* @dev Can't remove hooks that are part of the mainmodule (those defined below)
* without upgrading the wallet
*/
function removeHook(bytes4 _signature) external override onlySelf {
require(_readHook(_signature) != address(0), "ModuleHooks#removeHook: HOOK_NOT_REGISTERED");
_writeHook(_signature, address(0));
}
/**
* @notice Reads the implementation hook of a signature
* @param _signature Signature function
* @return The address of the implementation hook, address(0) if none
*/
function _readHook(bytes4 _signature) private view returns (address) {
return address(uint160(uint256(ModuleStorage.readBytes32Map(HOOKS_KEY, _signature))));
}
/**
* @notice Writes the implementation hook of a signature
* @param _signature Signature function
* @param _implementation Hook implementation contract
*/
function _writeHook(bytes4 _signature, address _implementation) private {
ModuleStorage.writeBytes32Map(HOOKS_KEY, _signature, bytes32(uint256(uint160(_implementation))));
}
/**
* @notice Handle the receipt of a single ERC1155 token type.
* @return `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))`
*/
function onERC1155Received(
address,
address,
uint256,
uint256,
bytes calldata
) external override returns (bytes4) {
return ModuleHooks.onERC1155Received.selector;
}
/**
* @notice Handle the receipt of multiple ERC1155 token types.
* @return `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))`
*/
function onERC1155BatchReceived(
address,
address,
uint256[] calldata,
uint256[] calldata,
bytes calldata
) external override returns (bytes4) {
return ModuleHooks.onERC1155BatchReceived.selector;
}
/**
* @notice Handle the receipt of a single ERC721 token.
* @return `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`
*/
function onERC721Received(address, address, uint256, bytes calldata) external override returns (bytes4) {
return ModuleHooks.onERC721Received.selector;
}
/**
* @notice Routes fallback calls through hooks
*/
fallback() external payable {
if (msg.data.length >= 4) {
address target = _readHook(msg.sig);
if (target != address(0)) {
(bool success, bytes memory result) = target.delegatecall(msg.data);
assembly {
if iszero(success) {
revert(add(result, 0x20), mload(result))
}
return(add(result, 0x20), mload(result))
}
}
}
}
/**
* @notice Allows the wallet to receive ETH
*/
receive() external payable { }
/**
* @notice Query if a contract implements an interface
* @param _interfaceID The interface identifier, as specified in ERC-165
* @return `true` if the contract implements `_interfaceID`
*/
function supportsInterface(bytes4 _interfaceID) public override virtual pure returns (bool) {
if (
_interfaceID == type(IModuleHooks).interfaceId ||
_interfaceID == type(IERC1155Receiver).interfaceId ||
_interfaceID == type(IERC721Receiver).interfaceId ||
_interfaceID == type(IERC223Receiver).interfaceId
) {
return true;
}
return super.supportsInterface(_interfaceID);
}
}