Skip to content

Commit

Permalink
refactor: remove domain separator as it can be precomputed
Browse files Browse the repository at this point in the history
  • Loading branch information
Zer0dot committed Oct 7, 2024
1 parent fed7217 commit 69ff270
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/account/ModularAccountBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -400,10 +400,6 @@ abstract contract ModularAccountBase is
super.upgradeToAndCall(newImplementation, data);
}

function domainSeparator() public view returns (bytes32) {
return keccak256(abi.encode(_DOMAIN_SEPARATOR_TYPEHASH, block.chainid, address(this)));
}

function isValidSignature(bytes32 hash, bytes calldata signature) public view returns (bytes4) {
ModuleEntity sigValidation = ModuleEntity.wrap(bytes24(signature));
signature = signature[24:];
Expand Down Expand Up @@ -558,7 +554,7 @@ abstract contract ModularAccountBase is
)
);

bytes32 typedDataHash = MessageHashUtils.toTypedDataHash(domainSeparator(), structHash);
bytes32 typedDataHash = MessageHashUtils.toTypedDataHash(_domainSeparator(), structHash);

if (_isValidSignature(sigValidation, typedDataHash, sig) != _1271_MAGIC_VALUE) {
revert DeferredInstallSignatureInvalid();
Expand Down Expand Up @@ -820,6 +816,10 @@ abstract contract ModularAccountBase is
}
}

function _domainSeparator() internal view returns (bytes32) {
return keccak256(abi.encode(_DOMAIN_SEPARATOR_TYPEHASH, block.chainid, address(this)));
}

function _isValidSignature(ModuleEntity sigValidation, bytes32 hash, bytes calldata signature)
internal
view
Expand Down
2 changes: 1 addition & 1 deletion src/account/SemiModularAccountBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ abstract contract SemiModularAccountBase is ModularAccountBase {
/// but the original hash should be passed to the external-facing function for 1271 validation.
function replaySafeHash(bytes32 hash) public view virtual returns (bytes32) {
return MessageHashUtils.toTypedDataHash({
domainSeparator: domainSeparator(),
domainSeparator: _domainSeparator(),
structHash: _hashStructReplaySafeHash(hash)
});
}
Expand Down
12 changes: 4 additions & 8 deletions test/utils/ModuleSignatureUtils.sol
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,7 @@ contract ModuleSignatureUtils {
bytes32 domainSeparator;

// Needed for initCode txs
if (address(account).code.length > 0) {
domainSeparator = account.domainSeparator();
} else {
domainSeparator = _computeDomainSeparatorNotDeployed(account);
}
domainSeparator = _computeDomainSeparator(account);

bytes32 structHash = keccak256(
abi.encode(
Expand All @@ -208,8 +204,8 @@ contract ModuleSignatureUtils {
return replaySafeHash;
}

function _computeDomainSeparatorNotDeployed(ModularAccount account) internal view returns (bytes32) {
bytes32 domainSeparatorTypehash = 0x47e79534a245952e8b16893a336b85a3d9ea9fa8c573f3d803afb92a79469218;
function _computeDomainSeparator(ModularAccount account) internal view returns (bytes32) {
bytes32 domainSeparatorTypehash = keccak256("EIP712Domain(uint256 chainId,address verifyingContract)");
return keccak256(abi.encode(domainSeparatorTypehash, block.chainid, address(account)));
}

Expand All @@ -225,7 +221,7 @@ contract ModuleSignatureUtils {
// for SMA, the domain separator used for the deferred validation installation is the same as the one
// used to compute the replay safe hash.
return MessageHashUtils.toTypedDataHash({
domainSeparator: _computeDomainSeparatorNotDeployed(account),
domainSeparator: _computeDomainSeparator(account),
structHash: _hashStructReplaySafeHash(typedDataHash)
});
}
Expand Down

0 comments on commit 69ff270

Please sign in to comment.