caelum0x 94c26e9496 feat: QIE Forge — clean-room AMM + lending vault, SDK, and CLI for QIE
Initial build for QIE Mainnet Hackathon 3.0 (DeFi & Financial
Infrastructure track). Includes:

- packages/contracts: constant-product AMM (factory/pair/router),
  over-collateralized lending vault, and an oracle adapter wrapping
  QIE's real deployed price-feed contracts. 25/25 Foundry tests pass
  locally. Clean-room: no existing DEX/lending protocol source was
  read or copied.
- packages/core: TypeScript SDK (viem-based) for QIE's chain, the
  real (corrected) oracle ABI, a fail-closed QUSDC helper, a
  Blockscout explorer client, and QIE Wallet connection constants.
  Live-verified against real testnet (chain 1983) and mainnet
  (chain 1990) RPCs and the real oracle contracts.
- packages/cli: a small CLI built on @qie-forge/core.

Real findings from building this, documented in README.md and
docs/STATUS.md: the previously-documented testnet RPC URL is dead
(replacement found and verified live); QIE's own oracle docs describe
a getPrice(string) function that reverts on the real deployed
contracts, which actually implement a Chainlink AggregatorV3-style
interface (verified live via raw eth_call); the oracle is deployed on
Mainnet only, not Testnet.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-26 23:34:51 +03:00

661 lines
30 KiB
Solidity

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;
// Parts of this test file are adapted from Adam Egyed (@adamegyed) proof of concept available at:
// https://github.com/adamegyed/erc7579-execute-vulnerability/tree/4589a30ff139e143d6c57183ac62b5c029217a90
//
// solhint-disable no-console
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {ECDSA} from "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import {MessageHashUtils} from "@openzeppelin/contracts/utils/cryptography/MessageHashUtils.sol";
import {PackedUserOperation, IAccount, IEntryPoint} from "@openzeppelin/contracts/interfaces/IERC4337.sol";
import {ERC4337Utils} from "@openzeppelin/contracts/account/utils/ERC4337Utils.sol";
import {
ERC7579Utils,
Mode,
CallType,
ExecType,
ModeSelector,
ModePayload,
Execution
} from "@openzeppelin/contracts/account/utils/draft-ERC7579Utils.sol";
import {Test, Vm, console} from "forge-std/Test.sol";
contract SampleAccount is IAccount, Ownable {
using ECDSA for *;
using MessageHashUtils for *;
using ERC4337Utils for *;
using ERC7579Utils for *;
event Log(bool duringValidation, Execution[] calls);
error UnsupportedCallType(CallType callType);
constructor(address initialOwner) Ownable(initialOwner) {}
function validateUserOp(
PackedUserOperation calldata userOp,
bytes32 userOpHash,
uint256 missingAccountFunds
) external override returns (uint256 validationData) {
require(msg.sender == address(ERC4337Utils.ENTRYPOINT_V07), "only from EP");
// Check signature
if (userOpHash.toEthSignedMessageHash().recover(userOp.signature) != owner()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
// If this is an execute call with a batch operation, log the call details from the calldata
if (bytes4(userOp.callData[0x00:0x04]) == this.execute.selector) {
(CallType callType, , , ) = Mode.wrap(bytes32(userOp.callData[0x04:0x24])).decodeMode();
if (callType == ERC7579Utils.CALLTYPE_BATCH) {
// Remove the selector
bytes calldata params = userOp.callData[0x04:];
// Use the same vulnerable assignment technique here, but assert afterwards that the checks aren't
// broken here by comparing to the result of `abi.decode(...)`.
bytes calldata executionCalldata;
assembly ("memory-safe") {
let dataptr := add(params.offset, calldataload(add(params.offset, 0x20)))
executionCalldata.offset := add(dataptr, 32)
executionCalldata.length := calldataload(dataptr)
}
// Check that this decoding step is done correctly.
(, bytes memory executionCalldataMemory) = abi.decode(params, (bytes32, bytes));
require(
keccak256(executionCalldata) == keccak256(executionCalldataMemory),
"decoding during validation failed"
);
// Now, we know that we have `bytes calldata executionCalldata` as would be decoded by the solidity
// builtin decoder for the `execute` function.
// This is where the vulnerability from ExecutionLib results in a different result between validation
// and execution.
emit Log(true, executionCalldata.decodeBatch());
}
}
if (missingAccountFunds > 0) {
(bool success, ) = payable(msg.sender).call{value: missingAccountFunds}("");
success; // Silence warning. The entrypoint should validate the result.
}
return ERC4337Utils.SIG_VALIDATION_SUCCESS;
}
function execute(Mode mode, bytes calldata executionCalldata) external payable {
require(msg.sender == address(this) || msg.sender == address(ERC4337Utils.ENTRYPOINT_V07), "not auth");
(CallType callType, ExecType execType, , ) = mode.decodeMode();
// check if calltype is batch or single
if (callType == ERC7579Utils.CALLTYPE_SINGLE) {
executionCalldata.execSingle(execType);
} else if (callType == ERC7579Utils.CALLTYPE_BATCH) {
executionCalldata.execBatch(execType);
emit Log(false, executionCalldata.decodeBatch());
} else if (callType == ERC7579Utils.CALLTYPE_DELEGATECALL) {
executionCalldata.execDelegateCall(execType);
} else {
revert UnsupportedCallType(callType);
}
}
}
contract ERC7579UtilsTest is Test {
using MessageHashUtils for *;
using ERC4337Utils for *;
using ERC7579Utils for *;
address private _owner;
uint256 private _ownerKey;
address private _account;
address private _beneficiary;
address private _recipient1;
address private _recipient2;
constructor() {
// EntryPoint070
vm.etch(
0x0000000071727De22E5E9d8BAf0edAc6f37da032,
vm.readFileBinary("node_modules/hardhat-predeploy/bin/0x0000000071727De22E5E9d8BAf0edAc6f37da032.bytecode")
);
// SenderCreator070
vm.etch(
0xEFC2c1444eBCC4Db75e7613d20C6a62fF67A167C,
vm.readFileBinary("node_modules/hardhat-predeploy/bin/0xEFC2c1444eBCC4Db75e7613d20C6a62fF67A167C.bytecode")
);
// signing key
(_owner, _ownerKey) = makeAddrAndKey("owner");
// ERC-4337 account
_account = address(new SampleAccount(_owner));
vm.deal(_account, 1 ether);
// other
_beneficiary = makeAddr("beneficiary");
_recipient1 = makeAddr("recipient1");
_recipient2 = makeAddr("recipient2");
}
function testExecuteBatchDecodeCorrectly() public {
Execution[] memory calls = new Execution[](2);
calls[0] = Execution({target: _recipient1, value: 1 wei, callData: ""});
calls[1] = Execution({target: _recipient2, value: 1 wei, callData: ""});
PackedUserOperation[] memory userOps = new PackedUserOperation[](1);
userOps[0] = PackedUserOperation({
sender: _account,
nonce: 0,
initCode: "",
callData: abi.encodeCall(
SampleAccount.execute,
(
ERC7579Utils.encodeMode(
ERC7579Utils.CALLTYPE_BATCH,
ERC7579Utils.EXECTYPE_DEFAULT,
ModeSelector.wrap(0x00),
ModePayload.wrap(0x00)
),
ERC7579Utils.encodeBatch(calls)
)
),
accountGasLimits: _packGas(500_000, 500_000),
preVerificationGas: 0,
gasFees: _packGas(1, 1),
paymasterAndData: "",
signature: ""
});
(uint8 v, bytes32 r, bytes32 s) = vm.sign(
_ownerKey,
this.hashUserOperation(userOps[0]).toEthSignedMessageHash()
);
userOps[0].signature = abi.encodePacked(r, s, v);
vm.recordLogs();
ERC4337Utils.ENTRYPOINT_V07.handleOps(userOps, payable(_beneficiary));
assertEq(_recipient1.balance, 1 wei);
assertEq(_recipient2.balance, 1 wei);
_collectAndPrintLogs(false);
}
function testExecuteBatchDecodeEmpty() public {
bytes memory fakeCalls = abi.encodePacked(
uint256(1), // Length of execution[]
uint256(0x20), // offset
uint256(uint160(_recipient1)), // target
uint256(1), // value: 1 wei
uint256(0x60), // offset of data
uint256(0) // length of
);
PackedUserOperation[] memory userOps = new PackedUserOperation[](1);
userOps[0] = PackedUserOperation({
sender: _account,
nonce: 0,
initCode: "",
callData: abi.encodeCall(
SampleAccount.execute,
(
ERC7579Utils.encodeMode(
ERC7579Utils.CALLTYPE_BATCH,
ERC7579Utils.EXECTYPE_DEFAULT,
ModeSelector.wrap(0x00),
ModePayload.wrap(0x00)
),
abi.encodePacked(
uint256(0x70) // fake offset pointing to paymasterAndData
)
)
),
accountGasLimits: _packGas(500_000, 500_000),
preVerificationGas: 0,
gasFees: _packGas(1, 1),
paymasterAndData: abi.encodePacked(address(0), fakeCalls),
signature: ""
});
(uint8 v, bytes32 r, bytes32 s) = vm.sign(
_ownerKey,
this.hashUserOperation(userOps[0]).toEthSignedMessageHash()
);
userOps[0].signature = abi.encodePacked(r, s, v);
vm.expectRevert(
abi.encodeWithSelector(
IEntryPoint.FailedOpWithRevert.selector,
0,
"AA23 reverted",
abi.encodeWithSelector(ERC7579Utils.ERC7579DecodingError.selector)
)
);
ERC4337Utils.ENTRYPOINT_V07.handleOps(userOps, payable(_beneficiary));
_collectAndPrintLogs(false);
}
function testExecuteBatchDecodeDifferent() public {
bytes memory execCallData = abi.encodePacked(
uint256(0x20), // offset pointing to the next segment
uint256(5), // Length of execution[]
uint256(0), // offset of calls[0], and target (!!)
uint256(0x20), // offset of calls[1], and value (!!)
uint256(0), // offset of calls[2], and rel offset of data (!!)
uint256(0) // offset of calls[3].
// There is one more to read by the array length, but it's not present here. This will be
// paymasterAndData.length during validation, pointing to an all-zero call.
// During execution, the offset will be 0, pointing to a call with value.
);
PackedUserOperation[] memory userOps = new PackedUserOperation[](1);
userOps[0] = PackedUserOperation({
sender: _account,
nonce: 0,
initCode: "",
callData: abi.encodePacked(
SampleAccount.execute.selector,
ERC7579Utils.encodeMode(
ERC7579Utils.CALLTYPE_BATCH,
ERC7579Utils.EXECTYPE_DEFAULT,
ModeSelector.wrap(0x00),
ModePayload.wrap(0x00)
),
uint256(0x5c), // offset pointing to the next segment
uint224(type(uint224).max), // Padding to align the `bytes` types
// type(uint256).max, // unknown padding
uint256(execCallData.length), // Length of the data
execCallData
),
accountGasLimits: _packGas(500_000, 500_000),
preVerificationGas: 0,
gasFees: _packGas(1, 1),
paymasterAndData: abi.encodePacked(uint256(0), uint256(0)), // padding length to create an offset
signature: ""
});
(uint8 v, bytes32 r, bytes32 s) = vm.sign(
_ownerKey,
this.hashUserOperation(userOps[0]).toEthSignedMessageHash()
);
userOps[0].signature = abi.encodePacked(r, s, v);
vm.expectRevert(
abi.encodeWithSelector(
IEntryPoint.FailedOpWithRevert.selector,
0,
"AA23 reverted",
abi.encodeWithSelector(ERC7579Utils.ERC7579DecodingError.selector)
)
);
ERC4337Utils.ENTRYPOINT_V07.handleOps(userOps, payable(_beneficiary));
_collectAndPrintLogs(true);
}
function testExecuteBatchDecodeWrappingPointer() public {
// The entry offset wraps around 2**256 to a location before the batch. During execution, `executionCalldata`
// is the last buffer in calldata, and the entry resolves to target `address(0)` (i.e. the account itself)
// with calldata read further in the buffer. During validation, the same buffer sits inside the user
// operation, and the entry resolves to a target read from the user operation header.
bytes memory payload = abi.encodeCall(
SampleAccount.execute,
(
ERC7579Utils.encodeMode(
ERC7579Utils.CALLTYPE_SINGLE,
ERC7579Utils.EXECTYPE_DEFAULT,
ModeSelector.wrap(0x00),
ModePayload.wrap(0x00)
),
abi.encodePacked(_recipient2, uint256(0.5 ether)) // single execution, see {ERC7579Utils-encodeSingle}
)
);
// Execution: `executionCalldata` starts at 100, so the array starts at 164 and the entry at 2**256 - 27.
// The entry's calldata offset is read at 37, which spans the `bytes` offset (0x40) and the high byte of the
// `bytes` length, giving 0x4000. The entry's calldata length is then at 2**256 - 27 + 0x4000 = 16357.
bytes memory execCallData = abi.encodePacked(
uint256(0x20), // offset pointing to the next segment
uint256(1), // Length of execution[]
type(uint256).max - 191 + 1, // offset of calls[0], relative to the array start (wraps)
new bytes(16357 - 100 - 0x60), // padding up to the calldata of calls[0], as resolved during execution
uint256(payload.length), // Length of the calldata of calls[0]
payload
);
PackedUserOperation[] memory userOps = new PackedUserOperation[](1);
userOps[0] = PackedUserOperation({
sender: _account,
nonce: 0,
initCode: "",
callData: abi.encodeCall(
SampleAccount.execute,
(
ERC7579Utils.encodeMode(
ERC7579Utils.CALLTYPE_BATCH,
ERC7579Utils.EXECTYPE_DEFAULT,
ModeSelector.wrap(0x00),
ModePayload.wrap(0x00)
),
execCallData
)
),
accountGasLimits: _packGas(500_000, 500_000),
preVerificationGas: 0,
gasFees: _packGas(1, 1),
paymasterAndData: "",
signature: ""
});
(uint8 v, bytes32 r, bytes32 s) = vm.sign(
_ownerKey,
this.hashUserOperation(userOps[0]).toEthSignedMessageHash()
);
userOps[0].signature = abi.encodePacked(r, s, v);
vm.expectRevert(
abi.encodeWithSelector(
IEntryPoint.FailedOpWithRevert.selector,
0,
"AA23 reverted",
abi.encodeWithSelector(ERC7579Utils.ERC7579DecodingError.selector)
)
);
ERC4337Utils.ENTRYPOINT_V07.handleOps(userOps, payable(_beneficiary));
assertEq(_recipient2.balance, 0);
}
// BAD: buffer empty
function testDecodeBatchEmptyBuffer() public {
_testDecodeBatch("", false);
}
// BAD: buffer too short
function testDecodeBatchBufferTooShort() public {
_testDecodeBatch(abi.encodePacked(uint128(0)), false);
}
// GOOD
function testDecodeBatchZero() public {
_testDecodeBatch(abi.encode(0), true);
// Note: Solidity also supports this even though it's odd. Offset 0 means array is at the same location, which
// is interpreted as an array of length 0, which doesn't require any more data
// solhint-disable-next-line var-name-mixedcase
uint256[] memory _1 = abi.decode(abi.encode(0), (uint256[]));
_1;
}
// BAD: offset is out of bounds
function testDecodeBatchOffsetOutOfBound() public {
_testDecodeBatch(abi.encode(1), false);
}
// GOOD
function testDecodeBatchEmptyArray() public {
_testDecodeBatch(abi.encode(32, 0), true);
}
// BAD: reported array length extends beyond bounds
function testDecodeBatchLengthExtendsOutOfBound() public {
_testDecodeBatch(abi.encode(32, 1), false);
}
// GOOD
function testDecodeBatchFirstBytes() public view {
// 0000000000000000000000000000000000000000000000000000000000000020 (32) offset
// 0000000000000000000000000000000000000000000000000000000000000001 ( 1) array length
// 0000000000000000000000000000000000000000000000000000000000000020 (32) element 0 offset
// 000000000000000000000000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (recipient) target for element #0
// 000000000000000000000000000000000000000000000000000000000000002a (42) value for element #0
// 0000000000000000000000000000000000000000000000000000000000000060 (96) offset to calldata for element #0
// 000000000000000000000000000000000000000000000000000000000000000c (12) length of the calldata for element #0
// 48656c6c6f20576f726c64210000000000000000000000000000000000000000 (..) buffer for the calldata for element #0
assertEq(
bytes("Hello World!"),
this.callDecodeBatchAndGetFirstBytes(
abi.encode(32, 1, 32, _recipient1, 42, 96, 12, bytes12("Hello World!"))
)
);
}
// BAD: caught when decoding (used to only revert when the caller dereferenced the element)
function testDecodeBatchDeepOutOfBound1() public {
// This is invalid, the first element of the array points is out of bounds
//
// 0000000000000000000000000000000000000000000000000000000000000020 (32) offset
// 0000000000000000000000000000000000000000000000000000000000000001 ( 1) array length
// 0000000000000000000000000000000000000000000000000000000000000000 ( 0) element 0 offset
// <missing element>
_testDecodeBatch(abi.encode(32, 1, 0), false);
}
// BAD: caught when decoding (used to only revert when the caller dereferenced the element)
function testDecodeBatchDeepOutOfBound2() public {
// This is invalid, the first element of the array points is out of bounds
//
// 0000000000000000000000000000000000000000000000000000000000000020 (32) offset
// 0000000000000000000000000000000000000000000000000000000000000001 ( 1) array length
// 0000000000000000000000000000000000000000000000000000000000000020 (32) element 0 offset
// <missing element>
_testDecodeBatch(abi.encode(32, 1, 32), false);
}
function testDecodeBatchDeepOutOfBound3() public {
// This is invalid: the first item is partly out of bounds.
//
// 0000000000000000000000000000000000000000000000000000000000000020 (32) offset
// 0000000000000000000000000000000000000000000000000000000000000001 ( 1) array length
// 0000000000000000000000000000000000000000000000000000000000000020 (32) element 0 offset
// 000000000000000000000000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (recipient) target for element #0
// <missing data>
_testDecodeBatch(abi.encode(32, 1, 32, _recipient1), false);
}
function testDecodeBatchDeepOutOfBound4() public {
// This is invalid: the bytes field of the first element of the array is out of bounds
//
// 0000000000000000000000000000000000000000000000000000000000000020 (32) offset
// 0000000000000000000000000000000000000000000000000000000000000001 ( 1) array length
// 0000000000000000000000000000000000000000000000000000000000000020 (32) element 0 offset
// 000000000000000000000000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (recipient) target for element #0
// 000000000000000000000000000000000000000000000000000000000000002a (42) value for element #0
// 0000000000000000000000000000000000000000000000000000000000000060 (96) offset to calldata for element #0
// <missing data>
_testDecodeBatch(abi.encode(32, 1, 32, _recipient1, 42, 96), false);
}
// BAD: the offset of the first element wraps around, pointing before the buffer
function testDecodeBatchItemOffsetUnderflow() public {
// 0000000000000000000000000000000000000000000000000000000000000020 (32) offset
// 0000000000000000000000000000000000000000000000000000000000000001 ( 1) array length
// ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa0 (-96) element 0 offset
//
// Offsets are relative to the beginning of the array's elements, and are added to that base pointer
// without any overflow check. Here the first element resolves to 32 bytes before the buffer.
_testDecodeBatch(abi.encode(32, 1, type(uint256).max - 95), false);
}
// BAD: the offset of the first element wraps around to the start of the buffer
function testDecodeBatchElementOffsetWraps() public {
// 0000000000000000000000000000000000000000000000000000000000000020 (32) offset
// 0000000000000000000000000000000000000000000000000000000000000001 ( 1) array length
// ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc0 (-64) element 0 offset
_testDecodeBatch(abi.encode(32, 1, type(uint256).max - 0x3f), false);
}
// BAD: the offset of the first element wraps around, pointing past the end of msg.data
function testDecodeBatchItemOffsetOverflow() public {
_testDecodeBatch(abi.encode(32, 1, 1 << 255), false);
}
// BAD: the offset of the first element's calldata wraps around, pointing before the buffer
function testDecodeBatchItemCalldataOffsetUnderflow() public {
// 0000000000000000000000000000000000000000000000000000000000000020 (32) offset
// 0000000000000000000000000000000000000000000000000000000000000001 ( 1) array length
// 0000000000000000000000000000000000000000000000000000000000000020 (32) element 0 offset
// 000000000000000000000000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (recipient) target for element #0
// 000000000000000000000000000000000000000000000000000000000000002a (42) value for element #0
// ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80 (-128) offset to calldata for element #0
//
// The calldata of the first element resolves to the 32 bytes that precede the buffer (the ABI length
// slot of `executionCalldata`), so its content is read from outside of the buffer.
_testDecodeBatch(abi.encode(32, 1, 32, _recipient1, 42, type(uint256).max - 127), false);
}
// BAD: the offset of the calldata of the first element wraps around to a location before the element
function testDecodeBatchCallDataOffsetWraps() public {
// 0000000000000000000000000000000000000000000000000000000000000020 (32) offset
// 0000000000000000000000000000000000000000000000000000000000000001 ( 1) array length
// 0000000000000000000000000000000000000000000000000000000000000020 (32) element 0 offset
// 000000000000000000000000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (recipient) target for element #0
// 000000000000000000000000000000000000000000000000000000000000002a (42) value for element #0
// ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe0 (-32) offset to calldata for element #0
_testDecodeBatch(abi.encode(32, 1, 32, _recipient1, 42, type(uint256).max - 0x1f), false);
}
// BAD: the first element points before the buffer, while its calldata points back inside the buffer
function testDecodeBatchItemBeforeBuffer() public {
// 0000000000000000000000000000000000000000000000000000000000000040 (64) offset
// 00000000000000000000000000000000000000000000000000000000000000a0 (160) (unused by the array)
// 0000000000000000000000000000000000000000000000000000000000000001 ( 1) array length
// ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80 (-128) element 0 offset
// 0000000000000000000000000000000000000000000000000000000000000000 ( 0) (unused by the array)
//
// The first element resolves to the 32 bytes that precede the buffer (the ABI length slot of
// `executionCalldata`), so its target is read from outside of the buffer. Its offset to calldata is the
// second word of the buffer, which points to a valid empty calldata at the end of the buffer.
_testDecodeBatch(abi.encode(64, 160, 1, type(uint256).max - 127, 0), false);
}
// BAD: the calldata of the first element starts in the buffer, but its content extends past the buffer
function testDecodeBatchItemCalldataExtendsOutOfBound() public {
// 0000000000000000000000000000000000000000000000000000000000000020 (32) offset
// 0000000000000000000000000000000000000000000000000000000000000001 ( 1) array length
// 0000000000000000000000000000000000000000000000000000000000000020 (32) element 0 offset
// 000000000000000000000000xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx (recipient) target for element #0
// 000000000000000000000000000000000000000000000000000000000000002a (42) value for element #0
// 0000000000000000000000000000000000000000000000000000000000000060 (96) offset to calldata for element #0
// 0000000000000000000000000000000000000000000000000000000000000040 (64) length of calldata for element #0
// <missing data>
//
// When the buffer is followed by other data in msg.data, the content of the calldata is read from there.
_testDecodeBatch(abi.encode(32, 1, 32, _recipient1, 42, 96, 64), false);
}
// GOOD
function testDecodeBatchEncoded() public view {
Execution[] memory executionBatch = new Execution[](2);
executionBatch[0] = Execution({target: _recipient1, value: 1, callData: bytes("Hello World!")});
executionBatch[1] = Execution({target: _recipient2, value: 2, callData: ""});
bytes memory encoded = ERC7579Utils.encodeBatch(executionBatch);
assertEq(bytes("Hello World!"), this.callDecodeBatchAndGetFirstBytes(encoded));
assertEq(bytes("Hello World!"), this.callDecodeBatchAndGetFirstBytesWithCalldata(encoded, new bytes(256)));
}
// Decoding must not depend on what follows `executionCalldata` in calldata, including with offsets that wrap.
function testFuzzDecodeBatchSameWithCalldata(
int256 elementOffset,
int256 callDataOffset,
uint256 callDataLength
) public view {
bytes memory encoded = abi.encode(
32,
1,
bound(elementOffset, -0x100, 0x100),
_recipient1,
42,
bound(callDataOffset, -0x100, 0x100),
bound(callDataLength, 0, 0x40),
bytes32("Hello World!")
);
(bool success, bytes memory result) = address(this).staticcall(
abi.encodeCall(this.callDecodeBatchAndGetFirstBytes, (encoded))
);
(bool successWithCalldata, bytes memory resultWithCalldata) = address(this).staticcall(
abi.encodeCall(this.callDecodeBatchAndGetFirstBytesWithCalldata, (encoded, new bytes(256)))
);
assertEq(success, successWithCalldata);
assertEq(result, resultWithCalldata);
}
function _testDecodeBatch(bytes memory encoded, bool valid) private {
if (!valid) vm.expectRevert(ERC7579Utils.ERC7579DecodingError.selector);
this.callDecodeBatch(encoded);
if (!valid) vm.expectRevert(ERC7579Utils.ERC7579DecodingError.selector);
this.callDecodeBatchWithCalldata(encoded, new bytes(256));
}
function callDecodeBatch(bytes calldata executionCalldata) public pure {
ERC7579Utils.decodeBatch(executionCalldata);
}
function callDecodeBatchWithCalldata(bytes calldata executionCalldata, bytes calldata) public pure {
ERC7579Utils.decodeBatch(executionCalldata);
}
function callDecodeBatchAndGetFirstBytes(bytes calldata executionCalldata) public pure returns (bytes calldata) {
return ERC7579Utils.decodeBatch(executionCalldata)[0].callData;
}
function callDecodeBatchAndGetFirstBytesWithCalldata(
bytes calldata executionCalldata,
bytes calldata
) public pure returns (bytes calldata) {
return ERC7579Utils.decodeBatch(executionCalldata)[0].callData;
}
function hashUserOperation(PackedUserOperation calldata useroperation) public view returns (bytes32) {
return useroperation.hash(address(ERC4337Utils.ENTRYPOINT_V07));
}
function _collectAndPrintLogs(bool includeTotalValue) internal view {
Vm.Log[] memory logs = vm.getRecordedLogs();
for (uint256 i = 0; i < logs.length; i++) {
if (logs[i].emitter == _account) {
_printDecodedCalls(logs[i].data, includeTotalValue);
}
}
}
function _printDecodedCalls(bytes memory logData, bool includeTotalValue) internal pure {
(bool duringValidation, Execution[] memory calls) = abi.decode(logData, (bool, Execution[]));
console.log(
string.concat(
"Batch execute contents, as read during ",
duringValidation ? "validation" : "execution",
": "
)
);
console.log(" Execution[] length: %s", calls.length);
uint256 totalValue = 0;
for (uint256 i = 0; i < calls.length; ++i) {
console.log(string.concat(" calls[", vm.toString(i), "].target = ", vm.toString(calls[i].target)));
console.log(string.concat(" calls[", vm.toString(i), "].value = ", vm.toString(calls[i].value)));
console.log(string.concat(" calls[", vm.toString(i), "].data = ", vm.toString(calls[i].callData)));
totalValue += calls[i].value;
}
if (includeTotalValue) {
console.log(" Total value: %s", totalValue);
}
}
function _packGas(uint256 upper, uint256 lower) internal pure returns (bytes32) {
return bytes32(uint256((upper << 128) | uint128(lower)));
}
}