@@ -5,6 +5,24 @@ import {ISignatureTransfer} from "permit2/src/interfaces/ISignatureTransfer.sol"
5
5
import {IOrders} from "../interfaces/IOrders.sol " ;
6
6
7
7
abstract contract UsesPermit2 {
8
+ /// @param permit - the permit2 single token transfer details. includes a `deadline` and an unordered `nonce`.
9
+ /// @param signer - the signer of the permit2 info; the owner of the tokens.
10
+ /// @param signature - the signature over the permit + witness.
11
+ struct Permit2 {
12
+ ISignatureTransfer.PermitTransferFrom permit;
13
+ address owner;
14
+ bytes signature;
15
+ }
16
+
17
+ /// @param permit - the permit2 batch token transfer details. includes a `deadline` and an unordered `nonce`.
18
+ /// @param signer - the signer of the permit2 info; the owner of the tokens.
19
+ /// @param signature - the signature over the permit + witness.
20
+ struct Permit2Batch {
21
+ ISignatureTransfer.PermitBatchTransferFrom permit;
22
+ address owner;
23
+ bytes signature;
24
+ }
25
+
8
26
/// @notice Struct to hold the pre-hashed witness field and the witness type string.
9
27
struct Witness {
10
28
bytes32 witnessHash;
@@ -26,18 +44,22 @@ abstract contract OrdersPermit2 is UsesPermit2 {
26
44
bytes32 constant _OUTPUT_TYPEHASH =
27
45
keccak256 ("Output(address token,uint256 amount,address recipient,uint32 chainId) " );
28
46
29
- /// @param permit - the permit2 batch token transfer details. includes a `deadline` and an unordered `nonce`.
30
- /// @param signer - the signer of the permit2 info; the owner of the tokens.
31
- /// @param signature - the signature over the permit + witness.
32
- struct Permit2Batch {
33
- ISignatureTransfer.PermitBatchTransferFrom permit;
34
- address owner;
35
- bytes signature;
36
- }
37
-
38
47
/// @notice Thrown when a signed Output does not match the corresponding TokenPermissions.
39
48
error OutputMismatch ();
40
49
50
+ /// @notice Encode the Output array according to EIP-712 for use as a permit2 witness.
51
+ /// @param outputs - the Outputs to encode.
52
+ /// @return _witness - the encoded witness field.
53
+ function outputWitness (IOrders.Output[] memory outputs ) public pure returns (Witness memory _witness ) {
54
+ uint256 num = outputs.length ;
55
+ bytes32 [] memory hashes = new bytes32 [](num);
56
+ for (uint256 i = 0 ; i < num; ++ i) {
57
+ hashes[i] = keccak256 (abi.encode (_OUTPUT_TYPEHASH, outputs[i]));
58
+ }
59
+ _witness.witnessHash = keccak256 (abi.encodePacked (hashes));
60
+ _witness.witnessTypeString = _OUTPUT_WITNESS_TYPESTRING;
61
+ }
62
+
41
63
/// @notice Transfer a batch of tokens using permit2.
42
64
/// @param _witness - the hashed witness and its typestring.
43
65
/// @param transferDetails - the TokenPermissions for the transfer, generated based on the use-case (see `_initiateTransferDetails` and `_fillTransferDetails`).
@@ -57,19 +79,6 @@ abstract contract OrdersPermit2 is UsesPermit2 {
57
79
);
58
80
}
59
81
60
- /// @notice Encode the Output array according to EIP-712 for use as a permit2 witness.
61
- /// @param outputs - the Outputs to encode.
62
- /// @return _witness - the encoded witness field.
63
- function outputsWitness (IOrders.Output[] memory outputs ) public pure returns (Witness memory _witness ) {
64
- uint256 num = outputs.length ;
65
- bytes32 [] memory hashes = new bytes32 [](num);
66
- for (uint256 i = 0 ; i < num; ++ i) {
67
- hashes[i] = keccak256 (abi.encode (_OUTPUT_TYPEHASH, outputs[i]));
68
- }
69
- _witness.witnessHash = keccak256 (abi.encodePacked (hashes));
70
- _witness.witnessTypeString = _OUTPUT_WITNESS_TYPESTRING;
71
- }
72
-
73
82
/// @notice transform Output and TokenPermissions structs to TransferDetails structs, for passing to permit2.
74
83
/// @dev always transfers the full permitted amount.
75
84
/// @param outputs - the Outputs to transform.
@@ -142,29 +151,6 @@ abstract contract PassagePermit2 is UsesPermit2 {
142
151
address hostRecipient;
143
152
}
144
153
145
- /// @param permit - the permit2 single token transfer details. includes a `deadline` and an unordered `nonce`.
146
- /// @param signer - the signer of the permit2 info; the owner of the tokens.
147
- /// @param signature - the signature over the permit + witness.
148
- struct Permit2 {
149
- ISignatureTransfer.PermitTransferFrom permit;
150
- address owner;
151
- bytes signature;
152
- }
153
-
154
- /// @notice Transfer tokens using permit2.
155
- /// @param _witness - the hashed witness and its typestring.
156
- /// @param permit2 - the Permit2 information.
157
- function _permitWitnessTransferFrom (Witness memory _witness , Permit2 calldata permit2 ) internal {
158
- ISignatureTransfer (permit2Contract).permitWitnessTransferFrom (
159
- permit2.permit,
160
- _passageTransferDetails (permit2.permit.permitted),
161
- permit2.owner,
162
- _witness.witnessHash,
163
- _witness.witnessTypeString,
164
- permit2.signature
165
- );
166
- }
167
-
168
154
/// @notice Encode & hash the rollupChainId and rollupRecipient for use as a permit2 witness.
169
155
/// @return _witness - the hashed witness and its typestring.
170
156
function enterWitness (uint256 rollupChainId , address rollupRecipient )
@@ -184,16 +170,30 @@ abstract contract PassagePermit2 is UsesPermit2 {
184
170
_witness.witnessTypeString = _EXIT_WITNESS_TYPESTRING;
185
171
}
186
172
187
- /// @notice transform TokenPermissions to TransferDetails, for passing to permit2.
188
- /// @dev always transfers the full permitted amount to address(this).
189
- /// @param permitted - the TokenPermissions to transform.
173
+ /// @notice Transfer tokens using permit2.
174
+ /// @param _witness - the hashed witness and its typestring.
175
+ /// @param permit2 - the Permit2 information.
176
+ function _permitWitnessTransferFrom (Witness memory _witness , Permit2 calldata permit2 ) internal {
177
+ ISignatureTransfer (permit2Contract).permitWitnessTransferFrom (
178
+ permit2.permit,
179
+ _selfTransferDetails (permit2.permit.permitted.amount),
180
+ permit2.owner,
181
+ _witness.witnessHash,
182
+ _witness.witnessTypeString,
183
+ permit2.signature
184
+ );
185
+ }
186
+
187
+ /// @notice Construct TransferDetails transferring a balance to this contract, for passing to permit2.
188
+ /// @dev always transfers the full amount to address(this).
189
+ /// @param amount - the amount to transfer to this contract.
190
190
/// @return transferDetails - the SignatureTransferDetails generated.
191
- function _passageTransferDetails (ISignatureTransfer.TokenPermissions calldata permitted )
191
+ function _selfTransferDetails ( uint256 amount )
192
192
internal
193
193
view
194
194
returns (ISignatureTransfer.SignatureTransferDetails memory transferDetails )
195
195
{
196
196
transferDetails.to = address (this );
197
- transferDetails.requestedAmount = permitted. amount;
197
+ transferDetails.requestedAmount = amount;
198
198
}
199
199
}
0 commit comments