|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.28; |
| 3 | + |
| 4 | +import {IERC7779} from '../interfaces/IERC7779.sol'; |
| 5 | + |
| 6 | +// Note: To inherit from when we are ready to utilize 7702 fully. |
| 7 | + |
| 8 | +abstract contract ERC7779Adapter is IERC7779 { |
| 9 | + error NonAuthorizedOnRedelegationCaller(); |
| 10 | + |
| 11 | + // keccak256(abi.encode(uint256(keccak256(bytes("InteroperableDelegatedAccount.ERC.Storage"))) - |
| 12 | + // 1)) & ~bytes32(uint256(0xff)); |
| 13 | + bytes32 internal constant ERC7779_STORAGE_BASE = 0xc473de86d0138e06e4d4918a106463a7cc005258d2e21915272bcb4594c18900; |
| 14 | + |
| 15 | + struct ERC7779Storage { |
| 16 | + bytes32[] storageBases; |
| 17 | + } |
| 18 | + /* |
| 19 | + * @dev Externally shares the storage bases that has been used throughout the account. |
| 20 | + * Majority of 7702 accounts will have their distinctive storage base to reduce the |
| 21 | + chance of storage collision. |
| 22 | + * This allows the external entities to know what the storage base is of the account. |
| 23 | + * Wallets willing to redelegate already-delegated accounts should call |
| 24 | + accountStorageBase() to check if it confirms with the account it plans to redelegate. |
| 25 | + * |
| 26 | + * The bytes32 array should be stored at the storage slot: |
| 27 | + keccak(keccak('InteroperableDelegatedAccount.ERC.Storage')-1) & ~0xff |
| 28 | + * This is an append-only array so newly redelegated accounts should not overwrite the |
| 29 | + storage at this slot, but just append their base to the array. |
| 30 | + * This append operation should be done during the initialization of the account. |
| 31 | + */ |
| 32 | + |
| 33 | + function accountStorageBases() external view returns (bytes32[] memory) { |
| 34 | + ERC7779Storage storage $; |
| 35 | + assembly { |
| 36 | + $.slot := ERC7779_STORAGE_BASE |
| 37 | + } |
| 38 | + return $.storageBases; |
| 39 | + } |
| 40 | + |
| 41 | + function _addStorageBase(bytes32 storageBase) internal { |
| 42 | + ERC7779Storage storage $; |
| 43 | + assembly { |
| 44 | + $.slot := ERC7779_STORAGE_BASE |
| 45 | + } |
| 46 | + $.storageBases.push(storageBase); |
| 47 | + } |
| 48 | + |
| 49 | + /* |
| 50 | + * @dev Function called before redelegation. |
| 51 | + * This function should prepare the account for a delegation to a different |
| 52 | + implementation. |
| 53 | + * This function could be triggered by the new wallet that wants to redelegate an already |
| 54 | + delegated EOA. |
| 55 | + * It should uninitialize storages if needed and execute wallet-specific logic to prepare |
| 56 | + for redelegation. |
| 57 | + * msg.sender should be the owner of the account. |
| 58 | + */ |
| 59 | + function onRedelegation() external returns (bool) { |
| 60 | + require(msg.sender == address(this), NonAuthorizedOnRedelegationCaller()); |
| 61 | + _onRedelegation(); |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + /// @dev This function is called before redelegation. |
| 66 | + /// @dev Account should override this function to implement the specific logic. |
| 67 | + function _onRedelegation() internal virtual; |
| 68 | +} |
0 commit comments