|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.23; |
| 3 | + |
| 4 | +import {Test} from "forge-std/Test.sol"; |
| 5 | +import {EIP7702Proxy} from "../../src/EIP7702Proxy.sol"; |
| 6 | +import {CoinbaseSmartWallet} from "../../lib/smart-wallet/src/CoinbaseSmartWallet.sol"; |
| 7 | +import {ECDSA} from "openzeppelin-contracts/contracts/utils/cryptography/ECDSA.sol"; |
| 8 | + |
| 9 | +/** |
| 10 | + * @title CoinbaseImplementationTest |
| 11 | + * @dev Tests specific to the CoinbaseSmartWallet implementation |
| 12 | + */ |
| 13 | +contract CoinbaseImplementationTest is Test { |
| 14 | + uint256 constant _EOA_PRIVATE_KEY = 0xA11CE; |
| 15 | + address payable _eoa; |
| 16 | + |
| 17 | + uint256 constant _NEW_OWNER_PRIVATE_KEY = 0xB0B; |
| 18 | + address payable _newOwner; |
| 19 | + |
| 20 | + CoinbaseSmartWallet wallet; |
| 21 | + CoinbaseSmartWallet implementation; |
| 22 | + EIP7702Proxy proxy; |
| 23 | + bytes4 initSelector; |
| 24 | + |
| 25 | + bytes4 constant ERC1271_MAGIC_VALUE = 0x1626ba7e; |
| 26 | + bytes4 constant ERC1271_FAIL_VALUE = 0xffffffff; |
| 27 | + |
| 28 | + function setUp() public { |
| 29 | + // Set up test accounts |
| 30 | + _eoa = payable(vm.addr(_EOA_PRIVATE_KEY)); |
| 31 | + _newOwner = payable(vm.addr(_NEW_OWNER_PRIVATE_KEY)); |
| 32 | + |
| 33 | + // Deploy Coinbase implementation |
| 34 | + implementation = new CoinbaseSmartWallet(); |
| 35 | + initSelector = CoinbaseSmartWallet.initialize.selector; |
| 36 | + |
| 37 | + // Deploy and setup proxy |
| 38 | + proxy = new EIP7702Proxy(address(implementation), initSelector); |
| 39 | + bytes memory proxyCode = address(proxy).code; |
| 40 | + vm.etch(_eoa, proxyCode); |
| 41 | + |
| 42 | + // Initialize with Coinbase implementation |
| 43 | + bytes memory initArgs = _createInitArgs(_newOwner); |
| 44 | + bytes memory signature = _signInitData(_EOA_PRIVATE_KEY, initArgs); |
| 45 | + EIP7702Proxy(_eoa).initialize(initArgs, signature); |
| 46 | + |
| 47 | + wallet = CoinbaseSmartWallet(payable(_eoa)); |
| 48 | + } |
| 49 | + |
| 50 | + // ======== Utility Functions ======== |
| 51 | + /** |
| 52 | + * @dev Creates initialization arguments for CoinbaseSmartWallet |
| 53 | + * @param owner Address to set as the initial owner |
| 54 | + * @return Encoded initialization arguments for CoinbaseSmartWallet |
| 55 | + */ |
| 56 | + function _createInitArgs( |
| 57 | + address owner |
| 58 | + ) internal pure returns (bytes memory) { |
| 59 | + bytes[] memory owners = new bytes[](1); |
| 60 | + owners[0] = abi.encode(owner); |
| 61 | + return abi.encode(owners); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @dev Signs initialization data for CoinbaseSmartWallet that will be verified by the proxy |
| 66 | + * @param signerPk Private key of the signer |
| 67 | + * @param initArgs Initialization arguments to sign |
| 68 | + * @return Signature bytes |
| 69 | + */ |
| 70 | + function _signInitData( |
| 71 | + uint256 signerPk, |
| 72 | + bytes memory initArgs |
| 73 | + ) internal view returns (bytes memory) { |
| 74 | + bytes32 initHash = keccak256(abi.encode(proxy, initArgs)); |
| 75 | + (uint8 v, bytes32 r, bytes32 s) = vm.sign(signerPk, initHash); |
| 76 | + return abi.encodePacked(r, s, v); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @dev Helper to create ECDSA signatures |
| 81 | + * @param pk Private key to sign with |
| 82 | + * @param hash Message hash to sign |
| 83 | + * @return signature Encoded signature bytes |
| 84 | + */ |
| 85 | + function _sign( |
| 86 | + uint256 pk, |
| 87 | + bytes32 hash |
| 88 | + ) internal pure returns (bytes memory signature) { |
| 89 | + (uint8 v, bytes32 r, bytes32 s) = vm.sign(pk, hash); |
| 90 | + return abi.encodePacked(r, s, v); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @dev Creates a signature from a wallet owner for CoinbaseSmartWallet validation |
| 95 | + * @param message Message to sign |
| 96 | + * @param smartWallet Address of the wallet contract |
| 97 | + * @param ownerPk Private key of the owner |
| 98 | + * @param ownerIndex Index of the owner in the wallet's owner list |
| 99 | + * @return Wrapped signature bytes |
| 100 | + */ |
| 101 | + function _createOwnerSignature( |
| 102 | + bytes32 message, |
| 103 | + address smartWallet, |
| 104 | + uint256 ownerPk, |
| 105 | + uint256 ownerIndex |
| 106 | + ) internal view returns (bytes memory) { |
| 107 | + bytes32 replaySafeHash = CoinbaseSmartWallet(payable(smartWallet)) |
| 108 | + .replaySafeHash(message); |
| 109 | + bytes memory signature = _sign(ownerPk, replaySafeHash); |
| 110 | + return _applySignatureWrapper(ownerIndex, signature); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @dev Wraps a signature with owner index for CoinbaseSmartWallet validation |
| 115 | + * @param ownerIndex Index of the owner in the wallet's owner list |
| 116 | + * @param signatureData Raw signature bytes to wrap |
| 117 | + * @return Encoded signature wrapper |
| 118 | + */ |
| 119 | + function _applySignatureWrapper( |
| 120 | + uint256 ownerIndex, |
| 121 | + bytes memory signatureData |
| 122 | + ) internal pure returns (bytes memory) { |
| 123 | + return |
| 124 | + abi.encode( |
| 125 | + CoinbaseSmartWallet.SignatureWrapper(ownerIndex, signatureData) |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + // ======== Tests ======== |
| 130 | + function test_initialize_setsOwner() public { |
| 131 | + assertTrue( |
| 132 | + wallet.isOwnerAddress(_newOwner), |
| 133 | + "New owner should be owner after initialization" |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + function test_isValidSignature_succeeds_withValidOwnerSignature( |
| 138 | + bytes32 message |
| 139 | + ) public { |
| 140 | + assertTrue( |
| 141 | + wallet.isOwnerAddress(_newOwner), |
| 142 | + "New owner should be owner after initialization" |
| 143 | + ); |
| 144 | + assertEq( |
| 145 | + wallet.ownerAtIndex(0), |
| 146 | + abi.encode(_newOwner), |
| 147 | + "Owner at index 0 should be new owner" |
| 148 | + ); |
| 149 | + |
| 150 | + bytes memory signature = _createOwnerSignature( |
| 151 | + message, |
| 152 | + address(wallet), |
| 153 | + _NEW_OWNER_PRIVATE_KEY, |
| 154 | + 0 // First owner |
| 155 | + ); |
| 156 | + |
| 157 | + bytes4 result = wallet.isValidSignature(message, signature); |
| 158 | + assertEq( |
| 159 | + result, |
| 160 | + ERC1271_MAGIC_VALUE, |
| 161 | + "Should accept valid contract owner signature" |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + function test_execute_transfersEth_whenCalledByOwner( |
| 166 | + address recipient, |
| 167 | + uint256 amount |
| 168 | + ) public { |
| 169 | + vm.assume(recipient != address(0)); |
| 170 | + assumeNotPrecompile(recipient); |
| 171 | + assumePayable(recipient); |
| 172 | + vm.assume(amount > 0 && amount <= 100 ether); |
| 173 | + |
| 174 | + vm.deal(address(_eoa), amount); |
| 175 | + vm.deal(recipient, 0); |
| 176 | + |
| 177 | + vm.prank(_newOwner); |
| 178 | + wallet.execute( |
| 179 | + payable(recipient), |
| 180 | + amount, |
| 181 | + "" // empty calldata for simple transfer |
| 182 | + ); |
| 183 | + |
| 184 | + assertEq( |
| 185 | + recipient.balance, |
| 186 | + amount, |
| 187 | + "Coinbase wallet execute should transfer ETH" |
| 188 | + ); |
| 189 | + } |
| 190 | + |
| 191 | + function test_upgradeToAndCall_reverts_whenCalledByNonOwner( |
| 192 | + address nonOwner |
| 193 | + ) public { |
| 194 | + vm.assume(nonOwner != address(0)); |
| 195 | + vm.assume(nonOwner != _newOwner); // Ensure caller isn't the actual owner |
| 196 | + |
| 197 | + address newImpl = address(new CoinbaseSmartWallet()); |
| 198 | + |
| 199 | + vm.prank(nonOwner); |
| 200 | + vm.expectRevert(); // Coinbase wallet specific access control |
| 201 | + wallet.upgradeToAndCall(newImpl, ""); |
| 202 | + } |
| 203 | + |
| 204 | + function test_initialize_reverts_whenCalledTwice() public { |
| 205 | + bytes memory initArgs = _createInitArgs(_newOwner); |
| 206 | + bytes memory signature = _signInitData(_EOA_PRIVATE_KEY, initArgs); |
| 207 | + |
| 208 | + // Try to initialize again |
| 209 | + vm.expectRevert(CoinbaseSmartWallet.Initialized.selector); |
| 210 | + EIP7702Proxy(_eoa).initialize(initArgs, signature); |
| 211 | + } |
| 212 | +} |
0 commit comments