From 98628e4de68dc5b74af1f29b8c04207a0f423c0a Mon Sep 17 00:00:00 2001 From: highskore Date: Fri, 6 Dec 2024 17:15:26 +0100 Subject: [PATCH 1/6] feat(SafeHelpers): add formatERC1271Hash --- src/Interfaces.sol | 3 ++- src/test/helpers/SafeHelpers.sol | 34 ++++++++++++++++++++++++- src/test/helpers/interfaces/IEIP712.sol | 6 +++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 src/test/helpers/interfaces/IEIP712.sol diff --git a/src/Interfaces.sol b/src/Interfaces.sol index 2eb5a176..4a769cbe 100644 --- a/src/Interfaces.sol +++ b/src/Interfaces.sol @@ -4,7 +4,7 @@ pragma solidity >=0.8.23 <0.9.0; /* solhint-disable no-unused-import */ /*////////////////////////////////////////////////////////////// - ERCs + ERCs/EIPs //////////////////////////////////////////////////////////////*/ import { IERC1271, EIP1271_MAGIC_VALUE } from "./module-bases/interfaces/IERC1271.sol"; @@ -14,6 +14,7 @@ import { IERC3156FlashLender, IERC3156FlashBorrower } from "./module-bases/interfaces/Flashloan.sol"; +import { IEIP712 } from "./test/helpers/interfaces/IEIP712.sol"; /*////////////////////////////////////////////////////////////// MODULES diff --git a/src/test/helpers/SafeHelpers.sol b/src/test/helpers/SafeHelpers.sol index 531e7c8c..3f861393 100644 --- a/src/test/helpers/SafeHelpers.sol +++ b/src/test/helpers/SafeHelpers.sol @@ -23,13 +23,21 @@ import { ISafe7579Launchpad } from "../../accounts/safe/interfaces/ISafe7579Laun import { IERC7579Account } from "../../accounts/common/interfaces/IERC7579Account.sol"; import { IAccountFactory } from "../../accounts/factory/interface/IAccountFactory.sol"; import { IAccountModulesPaginated } from "./interfaces/IAccountModulesPaginated.sol"; -import { IERC1271, EIP1271_MAGIC_VALUE } from "../../Interfaces.sol"; +import { IERC1271, EIP1271_MAGIC_VALUE, IEIP712 } from "../../Interfaces.sol"; // Utils import { startPrank, stopPrank } from "../utils/Vm.sol"; /// @notice Helper functions for the Safe7579 implementation contract SafeHelpers is HelperBase { + /*////////////////////////////////////////////////////////////////////////// + CONSTANTS + //////////////////////////////////////////////////////////////////////////*/ + + /// @notice The typehash for EIP712 Safe messages + bytes32 constant SAFE_MSG_TYPEHASH = + 0x60b3cbf8b4a223d68d641b3b6ddf9a298e7f33710cf3d3a9d1146b5a6150fbca; + /*////////////////////////////////////////////////////////////////////////// EXECUTIONS //////////////////////////////////////////////////////////////////////////*/ @@ -316,6 +324,30 @@ contract SafeHelpers is HelperBase { ) == EIP1271_MAGIC_VALUE; } + /// @notice Format a Safe compatible hash for an account instance + /// @param instance AccountInstance the account instance to format the hash for + /// @param hash bytes32 the hash to format + /// @return bytes32 the formatted hash + function formatERC1271Hash( + AccountInstance memory instance, + address, + bytes32 hash + ) + public + virtual + override + deployAccountForAction(instance) + returns (bytes32) + { + bytes memory messageData = abi.encodePacked( + bytes1(0x19), + bytes1(0x01), + IEIP712(instance.account).domainSeparator(), + keccak256(abi.encodePacked(SAFE_MSG_TYPEHASH, abi.encode(keccak256(abi.encode(hash))))) + ); + return keccak256(messageData); + } + /// @notice Format a ERC1271 signature for an account /// @param validator address the address of the validator /// @param signature bytes the signature to format diff --git a/src/test/helpers/interfaces/IEIP712.sol b/src/test/helpers/interfaces/IEIP712.sol new file mode 100644 index 00000000..6aa52f3e --- /dev/null +++ b/src/test/helpers/interfaces/IEIP712.sol @@ -0,0 +1,6 @@ +// SPDX-License-Identifier: MIT +pragma solidity >=0.8.23 <0.9.0; + +interface IEIP712 { + function domainSeparator() external view returns (bytes32); +} From 82f7348925eee91fa6e4a3310e738a29df4aa4b0 Mon Sep 17 00:00:00 2001 From: highskore Date: Fri, 6 Dec 2024 17:32:05 +0100 Subject: [PATCH 2/6] chore: rename eip712 to erc712 --- src/Interfaces.sol | 2 +- .../IEIP712.sol => module-bases/interfaces/IERC712.sol} | 2 +- src/test/helpers/SafeHelpers.sol | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) rename src/{test/helpers/interfaces/IEIP712.sol => module-bases/interfaces/IERC712.sol} (86%) diff --git a/src/Interfaces.sol b/src/Interfaces.sol index 4a769cbe..bc6f3b2e 100644 --- a/src/Interfaces.sol +++ b/src/Interfaces.sol @@ -14,7 +14,7 @@ import { IERC3156FlashLender, IERC3156FlashBorrower } from "./module-bases/interfaces/Flashloan.sol"; -import { IEIP712 } from "./test/helpers/interfaces/IEIP712.sol"; +import { IERC712 } from "./module-bases/interfaces/IERC712.sol"; /*////////////////////////////////////////////////////////////// MODULES diff --git a/src/test/helpers/interfaces/IEIP712.sol b/src/module-bases/interfaces/IERC712.sol similarity index 86% rename from src/test/helpers/interfaces/IEIP712.sol rename to src/module-bases/interfaces/IERC712.sol index 6aa52f3e..9b88f2c7 100644 --- a/src/test/helpers/interfaces/IEIP712.sol +++ b/src/module-bases/interfaces/IERC712.sol @@ -1,6 +1,6 @@ // SPDX-License-Identifier: MIT pragma solidity >=0.8.23 <0.9.0; -interface IEIP712 { +interface IERC712 { function domainSeparator() external view returns (bytes32); } diff --git a/src/test/helpers/SafeHelpers.sol b/src/test/helpers/SafeHelpers.sol index 3f861393..9885677a 100644 --- a/src/test/helpers/SafeHelpers.sol +++ b/src/test/helpers/SafeHelpers.sol @@ -23,7 +23,7 @@ import { ISafe7579Launchpad } from "../../accounts/safe/interfaces/ISafe7579Laun import { IERC7579Account } from "../../accounts/common/interfaces/IERC7579Account.sol"; import { IAccountFactory } from "../../accounts/factory/interface/IAccountFactory.sol"; import { IAccountModulesPaginated } from "./interfaces/IAccountModulesPaginated.sol"; -import { IERC1271, EIP1271_MAGIC_VALUE, IEIP712 } from "../../Interfaces.sol"; +import { IERC1271, EIP1271_MAGIC_VALUE, IERC712 } from "../../Interfaces.sol"; // Utils import { startPrank, stopPrank } from "../utils/Vm.sol"; @@ -342,7 +342,7 @@ contract SafeHelpers is HelperBase { bytes memory messageData = abi.encodePacked( bytes1(0x19), bytes1(0x01), - IEIP712(instance.account).domainSeparator(), + IERC712(instance.account).domainSeparator(), keccak256(abi.encodePacked(SAFE_MSG_TYPEHASH, abi.encode(keccak256(abi.encode(hash))))) ); return keccak256(messageData); From aecb25e4933b2cbbb352b0879b06d96e9ca26022 Mon Sep 17 00:00:00 2001 From: highskore Date: Fri, 6 Dec 2024 17:37:11 +0100 Subject: [PATCH 3/6] fix: revert if validator is installed and non-zero --- src/test/helpers/SafeHelpers.sol | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/test/helpers/SafeHelpers.sol b/src/test/helpers/SafeHelpers.sol index 9885677a..adb82917 100644 --- a/src/test/helpers/SafeHelpers.sol +++ b/src/test/helpers/SafeHelpers.sol @@ -330,7 +330,7 @@ contract SafeHelpers is HelperBase { /// @return bytes32 the formatted hash function formatERC1271Hash( AccountInstance memory instance, - address, + address validator, bytes32 hash ) public @@ -339,6 +339,13 @@ contract SafeHelpers is HelperBase { deployAccountForAction(instance) returns (bytes32) { + // Revert if validator is not 0x0 or the validator is installed + if ( + validator != address(0x0) + && isModuleInstalled(instance, MODULE_TYPE_VALIDATOR, validator, "") + ) { + revert("formatERC1271Hash: validator is installed"); + } bytes memory messageData = abi.encodePacked( bytes1(0x19), bytes1(0x01), From f8460fe374eaa7a9e2ba54f391718f304a72bafc Mon Sep 17 00:00:00 2001 From: highskore Date: Fri, 6 Dec 2024 17:38:07 +0100 Subject: [PATCH 4/6] chore: fix comment --- src/test/helpers/SafeHelpers.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/helpers/SafeHelpers.sol b/src/test/helpers/SafeHelpers.sol index adb82917..e01471cc 100644 --- a/src/test/helpers/SafeHelpers.sol +++ b/src/test/helpers/SafeHelpers.sol @@ -339,7 +339,7 @@ contract SafeHelpers is HelperBase { deployAccountForAction(instance) returns (bytes32) { - // Revert if validator is not 0x0 or the validator is installed + // Revert if validator is not 0x0 and the validator is installed if ( validator != address(0x0) && isModuleInstalled(instance, MODULE_TYPE_VALIDATOR, validator, "") From 0675f1296715048a0742e61a8b0ad89ebc6826c8 Mon Sep 17 00:00:00 2001 From: highskore Date: Fri, 6 Dec 2024 17:39:30 +0100 Subject: [PATCH 5/6] chore: remove redundant check --- src/test/helpers/SafeHelpers.sol | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/test/helpers/SafeHelpers.sol b/src/test/helpers/SafeHelpers.sol index e01471cc..492111ff 100644 --- a/src/test/helpers/SafeHelpers.sol +++ b/src/test/helpers/SafeHelpers.sol @@ -340,10 +340,7 @@ contract SafeHelpers is HelperBase { returns (bytes32) { // Revert if validator is not 0x0 and the validator is installed - if ( - validator != address(0x0) - && isModuleInstalled(instance, MODULE_TYPE_VALIDATOR, validator, "") - ) { + if (isModuleInstalled(instance, MODULE_TYPE_VALIDATOR, validator, "")) { revert("formatERC1271Hash: validator is installed"); } bytes memory messageData = abi.encodePacked( From 413fd4b77c10486538cc5903c5e130955e0acd62 Mon Sep 17 00:00:00 2001 From: highskore Date: Fri, 6 Dec 2024 17:39:49 +0100 Subject: [PATCH 6/6] chore: fix comment [ci skip] --- src/test/helpers/SafeHelpers.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/helpers/SafeHelpers.sol b/src/test/helpers/SafeHelpers.sol index 492111ff..8186a055 100644 --- a/src/test/helpers/SafeHelpers.sol +++ b/src/test/helpers/SafeHelpers.sol @@ -339,7 +339,7 @@ contract SafeHelpers is HelperBase { deployAccountForAction(instance) returns (bytes32) { - // Revert if validator is not 0x0 and the validator is installed + // Revert if validator is installed if (isModuleInstalled(instance, MODULE_TYPE_VALIDATOR, validator, "")) { revert("formatERC1271Hash: validator is installed"); }