|
| 1 | +// SPDX-License-Identifier: LGPL-3.0-only |
| 2 | +pragma solidity ^0.8.30; |
| 3 | + |
| 4 | +/** |
| 5 | + * @title BasicSmartAccount - This contract support batch execution of transactions. |
| 6 | + * The only storage is a nonce to prevent replay attacks. |
| 7 | + * The contract is intended to be used with EIP-7702 where EOA delegates to this contract. |
| 8 | + */ |
| 9 | +contract BasicSmartAccount { |
| 10 | + struct Storage { |
| 11 | + uint256 nonce; |
| 12 | + } |
| 13 | + |
| 14 | + // Reserve a unique storage slot for the nonce. |
| 15 | + // * keccak256("BasicSmartAccount") & (~0xff) |
| 16 | + bytes32 private constant _STORAGE = |
| 17 | + 0xbdfee0231e0903cde9ca6fd75d08a500062dc3d87718f712bc6958ed69761700; |
| 18 | + |
| 19 | + // Domain typehash for EIP712 message. |
| 20 | + // * keccak256("EIP712Domain(uint256 chainId,address verifyingContract)"); |
| 21 | + bytes32 private constant _DOMAIN_TYPEHASH = |
| 22 | + 0x47e79534a245952e8b16893a336b85a3d9ea9fa8c573f3d803afb92a79469218; |
| 23 | + |
| 24 | + // The struct typehash for the EIP712 message. |
| 25 | + // * keccak256("HandleOps(bytes32 data,uint256 nonce)") |
| 26 | + bytes32 private constant _HANDLEOPS_TYPEHASH = |
| 27 | + 0x4f8bb4631e6552ac29b9d6bacf60ff8b5481e2af7c2104fe0261045fa6988111; |
| 28 | + |
| 29 | + address private immutable ENTRY_POINT; |
| 30 | + |
| 31 | + error InvalidSignature(); |
| 32 | + |
| 33 | + /** |
| 34 | + * @dev Sends multiple transactions with signature validation and reverts all if one fails. |
| 35 | + * @param userOps Encoded User Ops. |
| 36 | + * @param r The r part of the signature. |
| 37 | + * @param vs The v and s part of the signature. |
| 38 | + */ |
| 39 | + function handleOps( |
| 40 | + bytes memory userOps, |
| 41 | + uint256 r, |
| 42 | + uint256 vs |
| 43 | + ) public payable { |
| 44 | + Storage storage $ = _storage(); |
| 45 | + uint256 nonce = $.nonce; |
| 46 | + |
| 47 | + // Calculate the hash of transactions data and nonce for signature verification |
| 48 | + bytes32 domainSeparator = keccak256( |
| 49 | + abi.encode(_DOMAIN_TYPEHASH, block.chainid, address(this)) |
| 50 | + ); |
| 51 | + |
| 52 | + bytes32 structHash = keccak256( |
| 53 | + abi.encode(_HANDLEOPS_TYPEHASH, keccak256(userOps), nonce) |
| 54 | + ); |
| 55 | + bytes32 digest = keccak256( |
| 56 | + abi.encodePacked("\x19\x01", domainSeparator, structHash) |
| 57 | + ); |
| 58 | + |
| 59 | + // Verify the signature of EIP712 message |
| 60 | + require(_isValidSignature(digest, r, vs), InvalidSignature()); |
| 61 | + |
| 62 | + // Update nonce for the sender to prevent replay attacks |
| 63 | + unchecked { |
| 64 | + $.nonce = nonce + 1; |
| 65 | + } |
| 66 | + |
| 67 | + /* solhint-disable no-inline-assembly */ |
| 68 | + assembly ("memory-safe") { |
| 69 | + let length := mload(userOps) |
| 70 | + let i := 0x20 |
| 71 | + for { |
| 72 | + |
| 73 | + } lt(i, length) { |
| 74 | + |
| 75 | + } { |
| 76 | + let to := shr(0x60, mload(add(userOps, i))) |
| 77 | + let value := mload(add(userOps, add(i, 0x14))) |
| 78 | + let dataLength := mload(add(userOps, add(i, 0x34))) |
| 79 | + let data := add(userOps, add(i, 0x54)) |
| 80 | + let success := call(gas(), to, value, data, dataLength, 0, 0) |
| 81 | + |
| 82 | + if eq(success, 0) { |
| 83 | + returndatacopy(0, 0, returndatasize()) |
| 84 | + revert(0, returndatasize()) |
| 85 | + } |
| 86 | + i := add(i, add(0x54, dataLength)) |
| 87 | + } |
| 88 | + } |
| 89 | + /* solhint-enable no-inline-assembly */ |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @dev Validates the signature by extracting `v` and `s` from `vs` and using `ecrecover`. |
| 94 | + * @param hash The hash of the signed data. |
| 95 | + * @param r The r part of the signature. |
| 96 | + * @param vs The v and s part of the signature combined. |
| 97 | + * @return bool True if the signature is valid, false otherwise. |
| 98 | + */ |
| 99 | + function _isValidSignature( |
| 100 | + bytes32 hash, |
| 101 | + uint256 r, |
| 102 | + uint256 vs |
| 103 | + ) internal view returns (bool) { |
| 104 | + unchecked { |
| 105 | + uint256 v = (vs >> 255) + 27; |
| 106 | + uint256 s = vs & |
| 107 | + 0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff; |
| 108 | + |
| 109 | + return |
| 110 | + address(this) == |
| 111 | + ecrecover(hash, uint8(v), bytes32(r), bytes32(s)); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + function _storage() private pure returns (Storage storage $) { |
| 116 | + assembly ("memory-safe") { |
| 117 | + $.slot := _STORAGE |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + function getNonce() external view returns (uint256) { |
| 122 | + return _storage().nonce; |
| 123 | + } |
| 124 | +} |
0 commit comments