Skip to content

Commit c8913cc

Browse files
committed
minor refactor
1 parent 3ea69d2 commit c8913cc

File tree

2 files changed

+52
-52
lines changed

2 files changed

+52
-52
lines changed

src/Orders.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ abstract contract OrderDestination is IOrders, OrdersPermit2 {
5151
function fillPermit2(Output[] memory outputs, OrdersPermit2.Permit2Batch calldata permit2) external {
5252
// transfer all tokens to the Output recipients via permit2 (includes check on nonce & deadline)
5353
_permitWitnessTransferFrom(
54-
outputsWitness(outputs), _fillTransferDetails(outputs, permit2.permit.permitted), permit2
54+
outputWitness(outputs), _fillTransferDetails(outputs, permit2.permit.permitted), permit2
5555
);
5656

5757
// emit
@@ -126,7 +126,7 @@ abstract contract OrderOrigin is IOrders, OrdersPermit2 {
126126
) external {
127127
// transfer all tokens to the tokenRecipient via permit2 (includes check on nonce & deadline)
128128
_permitWitnessTransferFrom(
129-
outputsWitness(outputs), _initiateTransferDetails(tokenRecipient, permit2.permit.permitted), permit2
129+
outputWitness(outputs), _initiateTransferDetails(tokenRecipient, permit2.permit.permitted), permit2
130130
);
131131

132132
// emit

src/permit2/UsesPermit2.sol

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@ import {ISignatureTransfer} from "permit2/src/interfaces/ISignatureTransfer.sol"
55
import {IOrders} from "../interfaces/IOrders.sol";
66

77
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+
826
/// @notice Struct to hold the pre-hashed witness field and the witness type string.
927
struct Witness {
1028
bytes32 witnessHash;
@@ -26,18 +44,22 @@ abstract contract OrdersPermit2 is UsesPermit2 {
2644
bytes32 constant _OUTPUT_TYPEHASH =
2745
keccak256("Output(address token,uint256 amount,address recipient,uint32 chainId)");
2846

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-
3847
/// @notice Thrown when a signed Output does not match the corresponding TokenPermissions.
3948
error OutputMismatch();
4049

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+
4163
/// @notice Transfer a batch of tokens using permit2.
4264
/// @param _witness - the hashed witness and its typestring.
4365
/// @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 {
5779
);
5880
}
5981

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-
7382
/// @notice transform Output and TokenPermissions structs to TransferDetails structs, for passing to permit2.
7483
/// @dev always transfers the full permitted amount.
7584
/// @param outputs - the Outputs to transform.
@@ -142,29 +151,6 @@ abstract contract PassagePermit2 is UsesPermit2 {
142151
address hostRecipient;
143152
}
144153

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-
168154
/// @notice Encode & hash the rollupChainId and rollupRecipient for use as a permit2 witness.
169155
/// @return _witness - the hashed witness and its typestring.
170156
function enterWitness(uint256 rollupChainId, address rollupRecipient)
@@ -184,16 +170,30 @@ abstract contract PassagePermit2 is UsesPermit2 {
184170
_witness.witnessTypeString = _EXIT_WITNESS_TYPESTRING;
185171
}
186172

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.
190190
/// @return transferDetails - the SignatureTransferDetails generated.
191-
function _passageTransferDetails(ISignatureTransfer.TokenPermissions calldata permitted)
191+
function _selfTransferDetails(uint256 amount)
192192
internal
193193
view
194194
returns (ISignatureTransfer.SignatureTransferDetails memory transferDetails)
195195
{
196196
transferDetails.to = address(this);
197-
transferDetails.requestedAmount = permitted.amount;
197+
transferDetails.requestedAmount = amount;
198198
}
199199
}

0 commit comments

Comments
 (0)