Skip to content

Commit 3ea69d2

Browse files
committed
refactor: generate witness as public field
1 parent e88165b commit 3ea69d2

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed

src/Orders.sol

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ abstract contract OrderDestination is IOrders, OrdersPermit2 {
5050
/// @custom:emits Filled
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)
53-
_permitWitnessTransferFrom(outputs, _fillTransferDetails(outputs, permit2.permit.permitted), permit2);
53+
_permitWitnessTransferFrom(
54+
outputsWitness(outputs), _fillTransferDetails(outputs, permit2.permit.permitted), permit2
55+
);
5456

5557
// emit
5658
emit Filled(outputs);
@@ -123,7 +125,9 @@ abstract contract OrderOrigin is IOrders, OrdersPermit2 {
123125
OrdersPermit2.Permit2Batch calldata permit2
124126
) external {
125127
// transfer all tokens to the tokenRecipient via permit2 (includes check on nonce & deadline)
126-
_permitWitnessTransferFrom(outputs, _initiateTransferDetails(tokenRecipient, permit2.permit.permitted), permit2);
128+
_permitWitnessTransferFrom(
129+
outputsWitness(outputs), _initiateTransferDetails(tokenRecipient, permit2.permit.permitted), permit2
130+
);
127131

128132
// emit
129133
emit Order(permit2.permit.deadline, _inputs(permit2.permit.permitted), outputs);

src/Passage.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ contract Passage is PassagePermit2 {
109109
public
110110
{
111111
// transfer tokens to this contract via permit2
112-
_permitWitnessTransferFrom(_enterWitness(rollupChainId, rollupRecipient), true, permit2);
112+
_permitWitnessTransferFrom(enterWitness(rollupChainId, rollupRecipient), permit2);
113113
// check and emit
114114
_enterToken(rollupChainId, rollupRecipient, permit2.permit.permitted.token, permit2.permit.permitted.amount);
115115
}
@@ -197,7 +197,7 @@ contract RollupPassage is PassagePermit2 {
197197
/// @custom:emits ExitToken
198198
function exitTokenPermit2(address hostRecipient, PassagePermit2.Permit2 calldata permit2) public {
199199
// transfer tokens to this contract
200-
_permitWitnessTransferFrom(_exitWitness(hostRecipient), false, permit2);
200+
_permitWitnessTransferFrom(exitWitness(hostRecipient), permit2);
201201
// burn and emit
202202
_exitToken(hostRecipient, permit2.permit.permitted.token, permit2.permit.permitted.amount);
203203
}

src/permit2/UsesPermit2.sol

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

77
abstract contract UsesPermit2 {
8+
/// @notice Struct to hold the pre-hashed witness field and the witness type string.
9+
struct Witness {
10+
bytes32 witnessHash;
11+
string witnessTypeString;
12+
}
13+
814
/// @notice The Permit2 contract address.
915
address immutable permit2Contract;
1016

@@ -33,34 +39,35 @@ abstract contract OrdersPermit2 is UsesPermit2 {
3339
error OutputMismatch();
3440

3541
/// @notice Transfer a batch of tokens using permit2.
36-
/// @param outputs - the Outputs for the witness field.
42+
/// @param _witness - the hashed witness and its typestring.
3743
/// @param transferDetails - the TokenPermissions for the transfer, generated based on the use-case (see `_initiateTransferDetails` and `_fillTransferDetails`).
3844
/// @param permit2 - the Permit2Batch information.
3945
function _permitWitnessTransferFrom(
40-
IOrders.Output[] memory outputs,
46+
Witness memory _witness,
4147
ISignatureTransfer.SignatureTransferDetails[] memory transferDetails,
4248
Permit2Batch calldata permit2
4349
) internal {
4450
ISignatureTransfer(permit2Contract).permitWitnessTransferFrom(
4551
permit2.permit,
4652
transferDetails,
4753
permit2.owner,
48-
_witness(outputs),
49-
_OUTPUT_WITNESS_TYPESTRING,
54+
_witness.witnessHash,
55+
_witness.witnessTypeString,
5056
permit2.signature
5157
);
5258
}
5359

5460
/// @notice Encode the Output array according to EIP-712 for use as a permit2 witness.
5561
/// @param outputs - the Outputs to encode.
56-
/// @return witness - the encoded witness field.
57-
function _witness(IOrders.Output[] memory outputs) internal pure returns (bytes32 witness) {
62+
/// @return _witness - the encoded witness field.
63+
function outputsWitness(IOrders.Output[] memory outputs) public pure returns (Witness memory _witness) {
5864
uint256 num = outputs.length;
5965
bytes32[] memory hashes = new bytes32[](num);
6066
for (uint256 i = 0; i < num; ++i) {
6167
hashes[i] = keccak256(abi.encode(_OUTPUT_TYPEHASH, outputs[i]));
6268
}
63-
witness = keccak256(abi.encodePacked(hashes));
69+
_witness.witnessHash = keccak256(abi.encodePacked(hashes));
70+
_witness.witnessTypeString = _OUTPUT_WITNESS_TYPESTRING;
6471
}
6572

6673
/// @notice transform Output and TokenPermissions structs to TransferDetails structs, for passing to permit2.
@@ -145,29 +152,36 @@ abstract contract PassagePermit2 is UsesPermit2 {
145152
}
146153

147154
/// @notice Transfer tokens using permit2.
148-
/// @param witness - the pre-hashed witness field.
155+
/// @param _witness - the hashed witness and its typestring.
149156
/// @param permit2 - the Permit2 information.
150-
function _permitWitnessTransferFrom(bytes32 witness, bool isEnter, Permit2 calldata permit2) internal {
157+
function _permitWitnessTransferFrom(Witness memory _witness, Permit2 calldata permit2) internal {
151158
ISignatureTransfer(permit2Contract).permitWitnessTransferFrom(
152159
permit2.permit,
153160
_passageTransferDetails(permit2.permit.permitted),
154161
permit2.owner,
155-
witness,
156-
isEnter ? _ENTER_WITNESS_TYPESTRING : _EXIT_WITNESS_TYPESTRING,
162+
_witness.witnessHash,
163+
_witness.witnessTypeString,
157164
permit2.signature
158165
);
159166
}
160167

161168
/// @notice Encode & hash the rollupChainId and rollupRecipient for use as a permit2 witness.
162-
/// @return witness - the encoded witness field.
163-
function _enterWitness(uint256 rollupChainId, address rollupRecipient) internal pure returns (bytes32 witness) {
164-
witness = keccak256(abi.encode(_ENTER_WITNESS_TYPEHASH, EnterWitness(rollupChainId, rollupRecipient)));
169+
/// @return _witness - the hashed witness and its typestring.
170+
function enterWitness(uint256 rollupChainId, address rollupRecipient)
171+
public
172+
pure
173+
returns (Witness memory _witness)
174+
{
175+
_witness.witnessHash =
176+
keccak256(abi.encode(_ENTER_WITNESS_TYPEHASH, EnterWitness(rollupChainId, rollupRecipient)));
177+
_witness.witnessTypeString = _ENTER_WITNESS_TYPESTRING;
165178
}
166179

167180
/// @notice Hash the hostRecipient for use as a permit2 witness.
168-
/// @return witness - the encoded witness field.
169-
function _exitWitness(address hostRecipient) internal pure returns (bytes32 witness) {
170-
witness = keccak256(abi.encode(_EXIT_WITNESS_TYPEHASH, ExitWitness(hostRecipient)));
181+
/// @return _witness - the hashed witness and its typestring.
182+
function exitWitness(address hostRecipient) public pure returns (Witness memory _witness) {
183+
_witness.witnessHash = keccak256(abi.encode(_EXIT_WITNESS_TYPEHASH, ExitWitness(hostRecipient)));
184+
_witness.witnessTypeString = _EXIT_WITNESS_TYPESTRING;
171185
}
172186

173187
/// @notice transform TokenPermissions to TransferDetails, for passing to permit2.

test/Passage.t.sol

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ contract PassageTest is Test {
2121
uint256 gas = 10_000_000;
2222
uint256 maxFeePerGas = 50;
2323

24-
uint256 tokenAdminKey = 123;
25-
2624
event Enter(uint256 indexed rollupChainId, address indexed rollupRecipient, uint256 amount);
2725

2826
event EnterToken(

0 commit comments

Comments
 (0)