-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added MultiChainFlexCallDataValidator which allows a param valu…
…e of callData to be flexible
- Loading branch information
1 parent
737db31
commit 776be11
Showing
7 changed files
with
193 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,5 @@ typechain-types/ | |
# zerodev orchestra | ||
log/ | ||
.envrc | ||
|
||
settings.json |
Submodule FreshCryptoLib
added at
d9bb3b
Submodule I4337
added at
dc6485
Submodule forge-std
updated
31 files
+6 −12 | .github/workflows/ci.yml | |
+3 −1 | .github/workflows/sync.yml | |
+0 −3 | .gitmodules | |
+1 −1 | README.md | |
+3 −3 | foundry.toml | |
+0 −1 | lib/ds-test | |
+1 −1 | package.json | |
+518 −225 | src/StdAssertions.sol | |
+12 −8 | src/StdChains.sol | |
+9 −3 | src/StdInvariant.sol | |
+7 −11 | src/StdJson.sol | |
+201 −106 | src/StdStorage.sol | |
+179 −0 | src/StdToml.sol | |
+1 −1 | src/StdUtils.sol | |
+4 −4 | src/Test.sol | |
+647 −7 | src/Vm.sol | |
+51 −33 | src/mocks/MockERC20.sol | |
+46 −36 | src/mocks/MockERC721.sol | |
+39 −909 | test/StdAssertions.t.sol | |
+33 −26 | test/StdChains.t.sol | |
+19 −11 | test/StdCheats.t.sol | |
+49 −0 | test/StdJson.t.sol | |
+8 −8 | test/StdMath.t.sol | |
+159 −11 | test/StdStorage.t.sol | |
+49 −0 | test/StdToml.t.sol | |
+18 −18 | test/StdUtils.t.sol | |
+3 −3 | test/Vm.t.sol | |
+8 −0 | test/fixtures/test.json | |
+6 −0 | test/fixtures/test.toml | |
+1 −1 | test/mocks/MockERC20.t.sol | |
+1 −1 | test/mocks/MockERC721.t.sol |
Submodule p256-verifier
added at
29475a
Submodule solady
updated
78 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import {ECDSA} from "solady/utils/ECDSA.sol"; | ||
import {MerkleProofLib} from "solady/utils/MerkleProofLib.sol"; | ||
import {IValidator, IHook} from "../interfaces/IERC7579Modules.sol"; | ||
import {PackedUserOperation} from "../interfaces/PackedUserOperation.sol"; | ||
import {IEntryPoint} from "../interfaces/IEntryPoint.sol"; | ||
import { | ||
SIG_VALIDATION_SUCCESS_UINT, | ||
SIG_VALIDATION_FAILED_UINT, | ||
MODULE_TYPE_VALIDATOR, | ||
MODULE_TYPE_HOOK, | ||
ERC1271_MAGICVALUE, | ||
ERC1271_INVALID | ||
} from "../types/Constants.sol"; | ||
|
||
struct ECDSAValidatorStorage { | ||
address owner; | ||
} | ||
|
||
struct FlexCallData { | ||
uint32 offset; | ||
uint32 length; | ||
bytes value; | ||
} | ||
|
||
bytes constant DUMMY_ECDSA_SIG = | ||
hex"fffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c"; | ||
|
||
contract MultiChainFlexCallDataValidator is IValidator, IHook { | ||
event OwnerRegistered(address indexed kernel, address indexed owner); | ||
|
||
mapping(address => ECDSAValidatorStorage) public ecdsaValidatorStorage; | ||
|
||
function onInstall(bytes calldata _data) external payable override { | ||
address owner = address(bytes20(_data[0:20])); | ||
ecdsaValidatorStorage[msg.sender].owner = owner; | ||
emit OwnerRegistered(msg.sender, owner); | ||
} | ||
|
||
function onUninstall(bytes calldata) external payable override { | ||
if (!_isInitialized(msg.sender)) revert NotInitialized(msg.sender); | ||
delete ecdsaValidatorStorage[msg.sender]; | ||
} | ||
|
||
function isModuleType(uint256 typeID) external pure override returns (bool) { | ||
return typeID == MODULE_TYPE_VALIDATOR || typeID == MODULE_TYPE_HOOK; | ||
} | ||
|
||
function isInitialized(address smartAccount) external view override returns (bool) { | ||
return _isInitialized(smartAccount); | ||
} | ||
|
||
function _isInitialized(address smartAccount) internal view returns (bool) { | ||
return ecdsaValidatorStorage[smartAccount].owner != address(0); | ||
} | ||
|
||
function validateUserOp(PackedUserOperation calldata userOp, bytes32 userOpHash) | ||
external | ||
payable | ||
override | ||
returns (uint256) | ||
{ | ||
bytes calldata sig = userOp.signature; | ||
address owner = ecdsaValidatorStorage[msg.sender].owner; | ||
if (sig.length == 65) { | ||
// simple ecdsa verification | ||
if (owner == ECDSA.recover(userOpHash, sig)) { | ||
return SIG_VALIDATION_SUCCESS_UINT; | ||
} | ||
bytes32 ethHash = ECDSA.toEthSignedMessageHash(userOpHash); | ||
address recovered = ECDSA.recover(ethHash, sig); | ||
if (owner != recovered) { | ||
return SIG_VALIDATION_FAILED_UINT; | ||
} | ||
return SIG_VALIDATION_SUCCESS_UINT; | ||
} | ||
bytes memory ecdsaSig = sig[0:65]; | ||
bytes32 merkleRoot = bytes32(sig[65:97]); | ||
// if the signature is a dummy signature, then use dummyUserOpHash instead of real userOpHash | ||
if (keccak256(ecdsaSig) == keccak256(DUMMY_ECDSA_SIG)) { | ||
(bytes32 dummyUserOpHash, bytes32[] memory proof) = abi.decode(sig[97:], (bytes32, bytes32[])); | ||
require(MerkleProofLib.verify(proof, merkleRoot, dummyUserOpHash), "hash is not in proof"); | ||
// otherwise, use real userOpHash | ||
} else { | ||
(bytes32[] memory proof, FlexCallData[] memory flexCallData) = | ||
abi.decode(sig[97:], (bytes32[], FlexCallData[])); | ||
PackedUserOperation memory _userOp = _toMemoryUserOp(userOp); | ||
|
||
_userOp.callData = _replaceCallData(userOp.callData, flexCallData); | ||
bytes32 modifiedUserOpHash = IEntryPoint(0x0000000071727De22E5E9d8BAf0edAc6f37da032).getUserOpHash(_userOp); | ||
require(MerkleProofLib.verify(proof, merkleRoot, modifiedUserOpHash), "hash is not in proof"); | ||
} | ||
// simple ecdsa verification | ||
if (owner == ECDSA.recover(merkleRoot, ecdsaSig)) { | ||
return SIG_VALIDATION_SUCCESS_UINT; | ||
} | ||
bytes32 ethRoot = ECDSA.toEthSignedMessageHash(merkleRoot); | ||
address merkleRecovered = ECDSA.recover(ethRoot, ecdsaSig); | ||
if (owner != merkleRecovered) { | ||
return SIG_VALIDATION_FAILED_UINT; | ||
} | ||
return SIG_VALIDATION_SUCCESS_UINT; | ||
} | ||
|
||
function isValidSignatureWithSender(address, bytes32 hash, bytes calldata sig) | ||
external | ||
view | ||
override | ||
returns (bytes4) | ||
{ | ||
address owner = ecdsaValidatorStorage[msg.sender].owner; | ||
if (sig.length == 65) { | ||
// simple ecdsa verification | ||
if (owner == ECDSA.recover(hash, sig)) { | ||
return ERC1271_MAGICVALUE; | ||
} | ||
bytes32 ethHash = ECDSA.toEthSignedMessageHash(hash); | ||
address recovered = ECDSA.recover(ethHash, sig); | ||
if (owner != recovered) { | ||
return ERC1271_INVALID; | ||
} | ||
return ERC1271_MAGICVALUE; | ||
} | ||
bytes memory ecdsaSig = sig[0:65]; | ||
bytes32 merkleRoot = bytes32(sig[65:97]); | ||
bytes32[] memory proof = abi.decode(sig[97:], (bytes32[])); | ||
require(MerkleProofLib.verify(proof, merkleRoot, hash), "hash is not in proof"); | ||
// simple ecdsa verification | ||
if (owner == ECDSA.recover(merkleRoot, ecdsaSig)) { | ||
return ERC1271_MAGICVALUE; | ||
} | ||
bytes32 ethRoot = ECDSA.toEthSignedMessageHash(merkleRoot); | ||
address merkleRecovered = ECDSA.recover(ethRoot, ecdsaSig); | ||
if (owner != merkleRecovered) { | ||
return ERC1271_INVALID; | ||
} | ||
return ERC1271_MAGICVALUE; | ||
} | ||
|
||
function preCheck(address msgSender, uint256 value, bytes calldata) | ||
external | ||
payable | ||
override | ||
returns (bytes memory) | ||
{ | ||
require(msgSender == ecdsaValidatorStorage[msg.sender].owner, "ECDSAValidator: sender is not owner"); | ||
return hex""; | ||
} | ||
|
||
function postCheck(bytes calldata hookData) external payable override {} | ||
|
||
function _toMemoryUserOp(PackedUserOperation calldata userOp) internal pure returns (PackedUserOperation memory) { | ||
return PackedUserOperation({ | ||
sender: userOp.sender, | ||
nonce: userOp.nonce, | ||
initCode: userOp.initCode, | ||
callData: userOp.callData, | ||
accountGasLimits: userOp.accountGasLimits, | ||
preVerificationGas: userOp.preVerificationGas, | ||
gasFees: userOp.gasFees, | ||
paymasterAndData: userOp.paymasterAndData, | ||
signature: userOp.signature | ||
}); | ||
} | ||
|
||
function _replaceCallData(bytes memory originalCallData, FlexCallData[] memory flexCallDataArray) | ||
internal | ||
pure | ||
returns (bytes memory) | ||
{ | ||
bytes memory modifiedCallData = originalCallData; | ||
for (uint256 i = 0; i < flexCallDataArray.length; i++) { | ||
FlexCallData memory flexData = flexCallDataArray[i]; | ||
require(flexData.offset + flexData.length <= originalCallData.length, "FlexCallData out of bounds"); | ||
// Should not overwrite the first 4 bytes sig of the callData | ||
require(flexData.offset > 4, "FlexCallData offset too small"); | ||
for (uint256 j = 0; j < flexData.length && j < flexData.value.length; j++) { | ||
modifiedCallData[flexData.offset + j] = flexData.value[j]; | ||
} | ||
} | ||
return modifiedCallData; | ||
} | ||
} |