|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import "./erc6551-utils/ERC6551AccountLib.sol"; |
| 5 | +import "./erc6551-utils/IERC6551Account.sol"; |
| 6 | + |
| 7 | +import "../eip/interface/IERC721.sol"; |
| 8 | +import "../smart-wallet/non-upgradeable/Account.sol"; |
| 9 | + |
| 10 | +contract TokenBoundAccount is Account, IERC6551Account { |
| 11 | + using ECDSA for bytes32; |
| 12 | + |
| 13 | + /*/////////////////////////////////////////////////////////////// |
| 14 | + Events |
| 15 | + //////////////////////////////////////////////////////////////*/ |
| 16 | + |
| 17 | + event TokenBoundAccountCreated(address indexed account, bytes indexed data); |
| 18 | + |
| 19 | + /*/////////////////////////////////////////////////////////////// |
| 20 | + Constructor |
| 21 | + //////////////////////////////////////////////////////////////*/ |
| 22 | + |
| 23 | + /** |
| 24 | + * @notice Executes once when a contract is created to initialize state variables |
| 25 | + * |
| 26 | + * @param _entrypoint - 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789 |
| 27 | + * @param _factory - The factory contract address to issue token Bound accounts |
| 28 | + * |
| 29 | + */ |
| 30 | + constructor(IEntryPoint _entrypoint, address _factory) Account(_entrypoint, _factory) { |
| 31 | + _disableInitializers(); |
| 32 | + } |
| 33 | + |
| 34 | + receive() external payable override(IERC6551Account, Account) {} |
| 35 | + |
| 36 | + /// @notice Returns whether a signer is authorized to perform transactions using the wallet. |
| 37 | + function isValidSigner(address _signer, UserOperation calldata) public view override returns (bool) { |
| 38 | + return (owner() == _signer); |
| 39 | + } |
| 40 | + |
| 41 | + /// @notice See EIP-1271 |
| 42 | + function isValidSignature(bytes32 _hash, bytes memory _signature) |
| 43 | + public |
| 44 | + view |
| 45 | + virtual |
| 46 | + override |
| 47 | + returns (bytes4 magicValue) |
| 48 | + { |
| 49 | + address signer = _hash.recover(_signature); |
| 50 | + |
| 51 | + if (owner() == signer) { |
| 52 | + magicValue = MAGICVALUE; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + function owner() public view returns (address) { |
| 57 | + (uint256 chainId, address tokenContract, uint256 tokenId) = ERC6551AccountLib.token(); |
| 58 | + |
| 59 | + if (chainId != block.chainid) return address(0); |
| 60 | + |
| 61 | + return IERC721(tokenContract).ownerOf(tokenId); |
| 62 | + } |
| 63 | + |
| 64 | + function executeCall( |
| 65 | + address to, |
| 66 | + uint256 value, |
| 67 | + bytes calldata data |
| 68 | + ) external payable onlyAdminOrEntrypoint returns (bytes memory result) { |
| 69 | + return _call(to, value, data); |
| 70 | + } |
| 71 | + |
| 72 | + /// @notice Withdraw funds for this account from Entrypoint. |
| 73 | + function withdrawDepositTo(address payable withdrawAddress, uint256 amount) public virtual override { |
| 74 | + require(owner() == msg.sender, "Account: not NFT owner"); |
| 75 | + entryPoint().withdrawTo(withdrawAddress, amount); |
| 76 | + } |
| 77 | + |
| 78 | + function token() |
| 79 | + external |
| 80 | + view |
| 81 | + returns ( |
| 82 | + uint256 chainId, |
| 83 | + address tokenContract, |
| 84 | + uint256 tokenId |
| 85 | + ) |
| 86 | + { |
| 87 | + return ERC6551AccountLib.token(); |
| 88 | + } |
| 89 | + |
| 90 | + function nonce() external view returns (uint256) { |
| 91 | + return getNonce(); |
| 92 | + } |
| 93 | + |
| 94 | + /*/////////////////////////////////////////////////////////////// |
| 95 | + Internal Functions |
| 96 | + //////////////////////////////////////////////////////////////*/ |
| 97 | + |
| 98 | + function _call( |
| 99 | + address _target, |
| 100 | + uint256 value, |
| 101 | + bytes memory _calldata |
| 102 | + ) internal virtual override returns (bytes memory result) { |
| 103 | + bool success; |
| 104 | + (success, result) = _target.call{ value: value }(_calldata); |
| 105 | + if (!success) { |
| 106 | + assembly { |
| 107 | + revert(add(result, 32), mload(result)) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + /*/////////////////////////////////////////////////////////////// |
| 113 | + Modifiers |
| 114 | + //////////////////////////////////////////////////////////////*/ |
| 115 | + |
| 116 | + modifier onlyAdminOrEntrypoint() override { |
| 117 | + require(msg.sender == address(entryPoint()) || msg.sender == owner(), "Account: not admin or EntryPoint."); |
| 118 | + _; |
| 119 | + } |
| 120 | +} |
0 commit comments