-
Notifications
You must be signed in to change notification settings - Fork 578
Expand file tree
/
Copy pathAbstractPredicateWrapper.sol
More file actions
293 lines (247 loc) · 10 KB
/
AbstractPredicateWrapper.sol
File metadata and controls
293 lines (247 loc) · 10 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// SPDX-License-Identifier: MIT OR Apache-2.0
pragma solidity >=0.8.0;
/*@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@ HYPERLANE @@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@@
@@@@@@@@@ @@@@@@@@*/
import {AbstractPostDispatchHook} from "../../hooks/libs/AbstractPostDispatchHook.sol";
import {IPostDispatchHook} from "../../interfaces/hooks/IPostDispatchHook.sol";
import {ITokenBridge, ITokenFee, Quote} from "../../interfaces/ITokenBridge.sol";
import {IPredicateWrapper} from "../../interfaces/IPredicateWrapper.sol";
import {Quotes} from "./Quotes.sol";
import {TokenRouter} from "./TokenRouter.sol";
import {TransientStorage} from "../../libs/TransientStorage.sol";
import {PredicateClient} from "@predicate/mixins/PredicateClient.sol";
import {Attestation} from "@predicate/interfaces/IPredicateRegistry.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
/**
* @title AbstractPredicateWrapper
* @author Abacus Works
* @notice Shared base for Predicate-gated warpRoute wrapper contracts.
* Provides the pendingAttestation bypass-prevention mechanism,
* hook implementation, and admin functions common to all wrappers.
*/
abstract contract AbstractPredicateWrapper is
AbstractPostDispatchHook,
PredicateClient,
Ownable,
IPredicateWrapper,
ITokenFee
{
using Quotes for Quote[];
using SafeERC20 for IERC20;
using TransientStorage for bytes32;
// ============ Constants ============
uint8 public constant override hookType =
uint8(IPostDispatchHook.HookTypes.PREDICATE_ROUTER_WRAPPER);
// ============ Immutables ============
/// @notice The underlying warpRoute being wrapped
TokenRouter public immutable warpRoute;
/// @notice The ERC20 token managed by the warpRoute (address(0) for native)
address public immutable token;
/// @notice The local domain ID (cached from warpRoute during construction)
uint32 public immutable localDomain;
// ============ Transient Storage ============
/// @notice Transient flag set before calling the warpRoute, checked in postDispatch
/// @dev Key bypass-prevention: if false in postDispatch, transfer was unauthorized
bytes32 private constant PENDING_ATTESTATION_SLOT =
keccak256("AbstractPredicateWrapper.pendingAttestation");
function pendingAttestation() public view returns (bool) {
return PENDING_ATTESTATION_SLOT.loadBool();
}
// ============ Constructor ============
constructor(
address _warpRoute,
address _registry,
string memory _policyID
) {
if (_warpRoute == address(0))
revert IPredicateWrapper.PredicateRouterWrapper__InvalidWarpRoute();
if (_registry == address(0))
revert IPredicateWrapper.PredicateRouterWrapper__InvalidRegistry();
if (bytes(_policyID).length == 0)
revert IPredicateWrapper.PredicateRouterWrapper__InvalidPolicy();
warpRoute = TokenRouter(_warpRoute);
token = warpRoute.token();
localDomain = warpRoute.localDomain();
_initPredicateClient(_registry, _policyID);
// Infinite approval to warpRoute for token transfers (skip for native)
if (token != address(0)) {
IERC20(token).forceApprove(_warpRoute, type(uint256).max);
}
}
function _pullTokens(
Quote[] memory quotes
) internal virtual returns (uint256 totalNativeRequired) {
totalNativeRequired = Quotes.extract(quotes, address(0));
if (msg.value < totalNativeRequired)
revert IPredicateWrapper
.PredicateRouterWrapper__InsufficientValue();
if (token == address(0)) return totalNativeRequired;
uint256 totalTokenRequired = Quotes.extract(quotes, token);
if (totalTokenRequired > 0) {
IERC20(token).safeTransferFrom(
msg.sender,
address(this),
totalTokenRequired
);
}
}
/// @notice Emits the TransferAuthorized event. Subclasses implement.
function _emitTransferAuthorized(
address sender,
uint32 destination,
bytes32 recipient,
uint256 amount,
string calldata uuid
) internal virtual;
// ============ External Functions ============
/**
* @notice Quotes the fees for a remote transfer by delegating to the underlying warpRoute
*/
function quoteTransferRemote(
uint32 _destination,
bytes32 _recipient,
uint256 _amount
) external view override returns (Quote[] memory quotes) {
return warpRoute.quoteTransferRemote(_destination, _recipient, _amount);
}
/**
* @notice Transfer tokens with Predicate attestation validation
* @param _attestation The Predicate attestation proving compliance
* @param _destination The destination chain domain
* @param _recipient The recipient address on destination (as bytes32)
* @param _amount The amount of tokens to transfer
* @return messageId The Hyperlane message ID
*/
function transferRemoteWithAttestation(
Attestation calldata _attestation,
uint32 _destination,
bytes32 _recipient,
uint256 _amount
) external payable virtual returns (bytes32 messageId) {
bytes memory encodedSigAndArgs = abi.encodeWithSelector(
ITokenBridge.transferRemote.selector,
_destination,
_recipient,
_amount
);
Quote[] memory quotes = warpRoute.quoteTransferRemote(
_destination,
_recipient,
_amount
);
_emitTransferAuthorized(
msg.sender,
_destination,
_recipient,
_amount,
_attestation.uuid
);
return
_executeAttested(
_attestation,
encodedSigAndArgs,
quotes,
_destination
);
}
/**
* @notice Template: authorize → check value → pull tokens → call warpRoute → refund.
* @dev For same-domain transfers, attestation and hook bypass prevention are skipped
* because postDispatch is never called, making enforcement unenforceable.
* @param _attestation Predicate attestation
* @param encodedSigAndArgs ABI-encoded selector + arguments for the warpRoute call
* @param quotes Fee quotes returned by the warpRoute's quote function
* @param _destination The destination domain
* @return messageId Decoded from the warpRoute's return data
*/
function _executeAttested(
Attestation calldata _attestation,
bytes memory encodedSigAndArgs,
Quote[] memory quotes,
uint32 _destination
) internal returns (bytes32 messageId) {
if (pendingAttestation())
revert IPredicateWrapper.PredicateRouterWrapper__ReentryDetected();
if (_destination != localDomain) {
if (
!_authorizeTransaction(
_attestation,
encodedSigAndArgs,
msg.sender,
msg.value
)
)
revert IPredicateWrapper
.PredicateRouterWrapper__AttestationInvalid();
PENDING_ATTESTATION_SLOT.set();
}
uint256 totalNativeRequired = _pullTokens(quotes);
(bool success, bytes memory returnData) = address(warpRoute).call{
value: totalNativeRequired
}(encodedSigAndArgs);
if (!success) {
assembly {
revert(add(returnData, 32), mload(returnData))
}
}
if (pendingAttestation())
revert IPredicateWrapper
.PredicateRouterWrapper__PostDispatchNotExecuted();
uint256 excess = msg.value - totalNativeRequired;
if (excess > 0) {
(bool refundSuccess, ) = msg.sender.call{value: excess}("");
if (!refundSuccess)
revert IPredicateWrapper.PredicateRouterWrapper__RefundFailed();
}
return abi.decode(returnData, (bytes32));
}
// ============ Hook Implementation ============
/// @notice Verifies transfer originated from an attested wrapper call
function _postDispatch(bytes calldata, bytes calldata) internal override {
if (!pendingAttestation())
revert IPredicateWrapper
.PredicateRouterWrapper__UnauthorizedTransfer();
PENDING_ATTESTATION_SLOT.clear();
}
/// @notice No fee — gas fees are paid via the warpRoute's IGP hook
function _quoteDispatch(
bytes calldata,
bytes calldata
) internal pure override returns (uint256) {
return 0;
}
// ============ Admin Functions ============
/// @notice Updates the Predicate policy ID
function setPolicyID(string memory _policyID) external onlyOwner {
if (bytes(_policyID).length == 0)
revert IPredicateWrapper.PredicateRouterWrapper__InvalidPolicy();
_setPolicyID(_policyID);
}
/// @notice Updates the Predicate registry address
function setRegistry(address _registry) external onlyOwner {
if (_registry == address(0))
revert IPredicateWrapper.PredicateRouterWrapper__InvalidRegistry();
_setRegistry(_registry);
}
// ============ ETH Handling ============
/// @notice Accepts ETH refunds from the warpRoute's hook
receive() external payable {}
/// @notice Withdraws trapped ETH to owner
function withdrawETH() external onlyOwner {
uint256 balance = address(this).balance;
(bool success, ) = msg.sender.call{value: balance}("");
if (!success)
revert IPredicateWrapper.PredicateRouterWrapper__WithdrawFailed();
}
}