From f31e3d14589735ae4cea61be102670e5d493905e Mon Sep 17 00:00:00 2001 From: Paco Date: Wed, 29 May 2024 04:29:41 +0800 Subject: [PATCH 1/2] fix eip712 encoding --- contracts/erc7683/ERC7683Across.sol | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/contracts/erc7683/ERC7683Across.sol b/contracts/erc7683/ERC7683Across.sol index dfc5c5a12..d0aaa3cc0 100644 --- a/contracts/erc7683/ERC7683Across.sol +++ b/contracts/erc7683/ERC7683Across.sol @@ -52,16 +52,29 @@ library ERC7683Permit2Lib { "AcrossOrderData orderData)", ACROSS_ORDER_DATA_TYPE ); + bytes internal constant PERMIT2_CROSS_CHAIN_ORDER_TYPE = + abi.encodePacked( + ACROSS_ORDER_DATA_TYPE, + "CrossChainOrder(", + "address settlerContract,", + "address swapper,", + "uint256 nonce,", + "uint32 originChainId,", + "uint32 initiateDeadline,", + "uint32 fillDeadline,", + "AcrossOrderData orderData)", + TOKEN_PERMISSIONS_TYPE + ); bytes32 internal constant CROSS_CHAIN_ORDER_TYPE_HASH = keccak256(CROSS_CHAIN_ORDER_TYPE); string private constant TOKEN_PERMISSIONS_TYPE = "TokenPermissions(address token,uint256 amount)"; string internal constant PERMIT2_ORDER_TYPE = - string(abi.encodePacked("CrossChainOrder witness)", CROSS_CHAIN_ORDER_TYPE, TOKEN_PERMISSIONS_TYPE)); + string(abi.encodePacked("CrossChainOrder witness)", PERMIT2_CROSS_CHAIN_ORDER_TYPE)); // Hashes an order to get an order hash. Needed for permit2. function hashOrder(CrossChainOrder memory order, bytes32 orderDataHash) internal pure returns (bytes32) { return keccak256( - abi.encodePacked( + abi.encode( CROSS_CHAIN_ORDER_TYPE_HASH, order.settlementContract, order.swapper, @@ -77,7 +90,7 @@ library ERC7683Permit2Lib { function hashOrderData(AcrossOrderData memory orderData) internal pure returns (bytes32) { return keccak256( - abi.encodePacked( + abi.encode( ACROSS_ORDER_DATA_TYPE_HASH, orderData.inputToken, orderData.inputAmount, @@ -86,7 +99,7 @@ library ERC7683Permit2Lib { orderData.destinationChainId, orderData.recipient, orderData.exclusivityDeadline, - orderData.message + keccak256(orderData.message) ) ); } From e2d406fa86e1dc2bdcb1131753d41b5479125487 Mon Sep 17 00:00:00 2001 From: Paco Date: Fri, 28 Jun 2024 10:50:58 +0800 Subject: [PATCH 2/2] reduce duplicate code --- contracts/erc7683/ERC7683Across.sol | 31 +++++++++++++---------------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/contracts/erc7683/ERC7683Across.sol b/contracts/erc7683/ERC7683Across.sol index ec37e1405..b9601d8fa 100644 --- a/contracts/erc7683/ERC7683Across.sol +++ b/contracts/erc7683/ERC7683Across.sol @@ -49,26 +49,23 @@ library ERC7683Permit2Lib { "uint32 originChainId,", "uint32 initiateDeadline,", "uint32 fillDeadline,", - "AcrossOrderData orderData)", - ACROSS_ORDER_DATA_TYPE + "AcrossOrderData orderData)" ); - bytes internal constant PERMIT2_CROSS_CHAIN_ORDER_TYPE = - abi.encodePacked( - ACROSS_ORDER_DATA_TYPE, - "CrossChainOrder(", - "address settlementContract,", - "address swapper,", - "uint256 nonce,", - "uint32 originChainId,", - "uint32 initiateDeadline,", - "uint32 fillDeadline,", - "AcrossOrderData orderData)", - TOKEN_PERMISSIONS_TYPE - ); - bytes32 internal constant CROSS_CHAIN_ORDER_TYPE_HASH = keccak256(CROSS_CHAIN_ORDER_TYPE); + + bytes internal constant CROSS_CHAIN_ORDER_EIP712_TYPE = + abi.encodePacked(CROSS_CHAIN_ORDER_TYPE, ACROSS_ORDER_DATA_TYPE); + bytes32 internal constant CROSS_CHAIN_ORDER_TYPE_HASH = keccak256(CROSS_CHAIN_ORDER_EIP712_TYPE); + string private constant TOKEN_PERMISSIONS_TYPE = "TokenPermissions(address token,uint256 amount)"; string internal constant PERMIT2_ORDER_TYPE = - string(abi.encodePacked("CrossChainOrder witness)", PERMIT2_CROSS_CHAIN_ORDER_TYPE)); + string( + abi.encodePacked( + "CrossChainOrder witness)", + ACROSS_ORDER_DATA_TYPE, + CROSS_CHAIN_ORDER_TYPE, + TOKEN_PERMISSIONS_TYPE + ) + ); // Hashes an order to get an order hash. Needed for permit2. function hashOrder(CrossChainOrder memory order, bytes32 orderDataHash) internal pure returns (bytes32) {