Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exploring gas of using oz #25

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 26 additions & 26 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ AddOwnerPublicKeyTest:testRevertsIfAlreadyOwner() (gas: 116301)
AddOwnerPublicKeyTest:testRevertsIfCalledByNonOwner() (gas: 11744)
AddOwnerPublicKeyTest:testSetsIsOwner() (gas: 113775)
AddOwnerPublicKeyTest:testSetsOwnerAtIndex() (gas: 128543)
CoinbaseSmartWallet1271InputGeneratorTest:testGetReplaySafeHashForDeployedAccount() (gas: 308664)
CoinbaseSmartWallet1271InputGeneratorTest:testGetReplaySafeHashForUndeployedAccount() (gas: 291435)
CoinbaseSmartWalletFactoryTest:testDeployDeterministicPassValues() (gas: 267742)
CoinbaseSmartWalletFactoryTest:test_CreateAccount_ReturnsPredeterminedAddress_WhenAccountAlreadyExists() (gas: 287406)
CoinbaseSmartWalletFactoryTest:test_createAccountDeploysToPredeterminedAddress() (gas: 269093)
CoinbaseSmartWalletFactoryTest:test_createAccountSetsOwnersCorrectly() (gas: 277981)
CoinbaseSmartWallet1271InputGeneratorTest:testGetReplaySafeHashForDeployedAccount() (gas: 283108)
CoinbaseSmartWallet1271InputGeneratorTest:testGetReplaySafeHashForUndeployedAccount() (gas: 265831)
CoinbaseSmartWalletFactoryTest:testDeployDeterministicPassValues() (gas: 249076)
CoinbaseSmartWalletFactoryTest:test_CreateAccount_ReturnsPredeterminedAddress_WhenAccountAlreadyExists() (gas: 261498)
wilsoncusack marked this conversation as resolved.
Show resolved Hide resolved
CoinbaseSmartWalletFactoryTest:test_createAccountDeploysToPredeterminedAddress() (gas: 250379)
CoinbaseSmartWalletFactoryTest:test_createAccountSetsOwnersCorrectly() (gas: 259033)
CoinbaseSmartWalletFactoryTest:test_revertsIfNoOwners() (gas: 29214)
ERC1271Test:test_returnsExpectedDomainHashWhenProxy() (gas: 15406)
ERC1271Test:test_returnsExpectedDomainHashWhenProxy() (gas: 13194)
MultiOwnableInitializeTest:testRevertsIfLength32ButLargerThanAddress() (gas: 78927)
MultiOwnableInitializeTest:testRevertsIfLength32NotAddress() (gas: 78908)
MultiOwnableInitializeTest:testRevertsIfLengthNot32Or64() (gas: 101211)
Expand All @@ -28,25 +28,25 @@ RemoveOwnerAtIndexTest:testRevertsIfCalledByNonOwner() (gas: 11332)
RemoveOwnerAtIndexTest:testRevertsIfNoOwnerAtIndex() (gas: 16648)
TestCanSkipChainIdValidation:test_approvedSelectorsReturnTrue() (gas: 15845)
TestCanSkipChainIdValidation:test_otherSelectorsReturnFalse() (gas: 12469)
TestExecuteWithoutChainIdValidation:testExecute() (gas: 424705)
TestExecuteWithoutChainIdValidation:testExecuteBatch() (gas: 729086)
TestExecuteWithoutChainIdValidation:testExecuteBatch(uint256) (runs: 256, μ: 3409748, ~: 3278930)
TestExecuteWithoutChainIdValidation:test__codesize() (gas: 49006)
TestExecuteWithoutChainIdValidation:test__codesize() (gas: 49241)
TestExecuteWithoutChainIdValidation:test_canChangeOwnerWithoutChainId() (gas: 287917)
TestExecuteWithoutChainIdValidation:test_cannotCallExec() (gas: 220047)
TestExecuteWithoutChainIdValidation:test_revertsIfCallerNotEntryPoint() (gas: 8620)
TestExecuteWithoutChainIdValidation:testExecute() (gas: 424839)
TestExecuteWithoutChainIdValidation:testExecuteBatch() (gas: 729083)
TestExecuteWithoutChainIdValidation:testExecuteBatch(uint256) (runs: 256, μ: 3578478, ~: 3434647)
TestExecuteWithoutChainIdValidation:test__codesize() (gas: 49775)
TestExecuteWithoutChainIdValidation:test__codesize() (gas: 50010)
TestExecuteWithoutChainIdValidation:test_canChangeOwnerWithoutChainId() (gas: 288312)
TestExecuteWithoutChainIdValidation:test_cannotCallExec() (gas: 220443)
TestExecuteWithoutChainIdValidation:test_revertsIfCallerNotEntryPoint() (gas: 8598)
TestExecuteWithoutChainIdValidation:test_revertsIfWrongNonceKey() (gas: 62275)
TestExecuteWithoutChainIdValidation:test_revertsWithReservedNonce() (gas: 82302)
TestInitialize:testInitialize() (gas: 21122)
TestInitialize:test_cannotInitImplementation() (gas: 2708253)
TestIsValidSignature:testReturnsInvalidIfPasskeySigButWrongOwnerLength() (gas: 39497)
TestIsValidSignature:testRevertsIfEthereumSignatureButWrongOwnerLength() (gas: 24068)
TestIsValidSignature:testSmartWalletSigner() (gas: 2981137)
TestIsValidSignature:testValidateSignatureWithEOASigner() (gas: 24950)
TestIsValidSignature:testValidateSignatureWithEOASignerFailsWithWrongSigner() (gas: 23880)
TestIsValidSignature:testValidateSignatureWithPasskeySigner() (gas: 421284)
TestIsValidSignature:testValidateSignatureWithPasskeySignerFailsBadOwnerIndex() (gas: 34986)
TestIsValidSignature:testValidateSignatureWithPasskeySignerFailsWithWrongBadSignature() (gas: 428750)
TestUpgradeToAndCall:testUpgradeToAndCall() (gas: 23839)
TestValidateUserOp:testValidateUserOp() (gas: 447113)
TestInitialize:test_cannotInitImplementation() (gas: 2860579)
TestIsValidSignature:testReturnsInvalidIfPasskeySigButWrongOwnerLength() (gas: 42452)
TestIsValidSignature:testRevertsIfEthereumSignatureButWrongOwnerLength() (gas: 24046)
TestIsValidSignature:testSmartWalletSigner() (gas: 3139037)
TestIsValidSignature:testValidateSignatureWithEOASigner() (gas: 25347)
wilsoncusack marked this conversation as resolved.
Show resolved Hide resolved
TestIsValidSignature:testValidateSignatureWithEOASignerFailsWithWrongSigner() (gas: 25481)
TestIsValidSignature:testValidateSignatureWithPasskeySigner() (gas: 421262)
TestIsValidSignature:testValidateSignatureWithPasskeySignerFailsBadOwnerIndex() (gas: 34964)
TestIsValidSignature:testValidateSignatureWithPasskeySignerFailsWithWrongBadSignature() (gas: 428728)
TestUpgradeToAndCall:testUpgradeToAndCall() (gas: 38915)
TestValidateUserOp:testValidateUserOp() (gas: 449222)
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@
[submodule "lib/webauthn-sol"]
path = lib/webauthn-sol
url = https://github.com/base-org/webauthn-sol
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/openzeppelin/openzeppelin-contracts
1 change: 1 addition & 0 deletions lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at dbb610
11 changes: 8 additions & 3 deletions src/CoinbaseSmartWallet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
pragma solidity 0.8.23;

import {Receiver} from "solady/accounts/Receiver.sol";
import {UUPSUpgradeable} from "solady/utils/UUPSUpgradeable.sol";
import {SignatureCheckerLib} from "solady/utils/SignatureCheckerLib.sol";
import {UUPSUpgradeable} from "openzeppelin-contracts/contracts/proxy/utils/UUPSUpgradeable.sol";
import {ERC1967Utils} from "openzeppelin-contracts/contracts/proxy/ERC1967/ERC1967Utils.sol";
import {SignatureChecker} from "openzeppelin-contracts/contracts/utils/cryptography/SignatureChecker.sol";
import {UserOperation, UserOperationLib} from "account-abstraction/interfaces/UserOperation.sol";
import {WebAuthn} from "webauthn-sol/WebAuthn.sol";

Expand Down Expand Up @@ -209,6 +210,10 @@ contract CoinbaseSmartWallet is MultiOwnable, UUPSUpgradeable, Receiver, ERC1271
}
}

function upgradeToAndCall(address newImplementation, bytes memory data) public payable override {
ERC1967Utils.upgradeToAndCall(newImplementation, data);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not actually safe, but OZ is a little annoying to work with here. Doesn't have a clone1967

Copy link
Contributor

@xenoliss xenoliss Mar 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For context, this override is unsafe (and not effective) but temporarily necessary to test gas.

OZ's Clones library deploys 1167 minimal proxies but our smart account implementation is a UUPSUpgradeable contract which, when calling upgradeToAndCall, uses the 1967 standard to check if the IMPLEMENTATION_SLOT is set to __self. This reverts as 1167 does not use IMPLEMENTATION_SLOT.

}

/// @notice Returns the address of the EntryPoint v0.6.
///
/// @return The address of the EntryPoint v0.6
Expand Down Expand Up @@ -302,7 +307,7 @@ contract CoinbaseSmartWallet is MultiOwnable, UUPSUpgradeable, Receiver, ERC1271
owner := mload(add(ownerBytes, 32))
}

return SignatureCheckerLib.isValidSignatureNow(owner, message, sigWrapper.signatureData);
return SignatureChecker.isValidSignatureNow(owner, message, sigWrapper.signatureData);
}

if (ownerBytes.length == 64) {
Expand Down
20 changes: 9 additions & 11 deletions src/CoinbaseSmartWalletFactory.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import {LibClone} from "solady/utils/LibClone.sol";
import {Clones} from "openzeppelin-contracts/contracts/proxy/Clones.sol";
import {CoinbaseSmartWallet} from "./CoinbaseSmartWallet.sol";

/// @title Coinbase Smart Wallet Factory
Expand Down Expand Up @@ -31,24 +31,22 @@ contract CoinbaseSmartWalletFactory {
revert OwnerRequired();
}

(bool alreadyDeployed, address accountAddress) =
LibClone.createDeterministicERC1967(msg.value, implementation, _getSalt(owners, nonce));
bytes32 salt = _getSalt(owners, nonce);
address accountAddress = Clones.predictDeterministicAddress(implementation, salt, address(this));

account = CoinbaseSmartWallet(payable(accountAddress));

if (alreadyDeployed == false) {
account.initialize(owners);
if (address(account).code.length > 0) {
return account;
}

Clones.cloneDeterministic(implementation, salt);
account.initialize{value: msg.value}(owners);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

msg.value kinda has unpredictable behavior here, in that if the account is already deployed, this will just sit on the factory. Maybe should just remove payable on this function

Copy link
Contributor

@xenoliss xenoliss Mar 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about returning the ETH if already existing? Or we could have a similar approach to Oz's one and simply revert if already deployed

}

/// @dev Returns the deterministic address of the account created via `createAccount`.
function getAddress(bytes[] calldata owners, uint256 nonce) external view returns (address predicted) {
predicted = LibClone.predictDeterministicAddress(initCodeHash(), _getSalt(owners, nonce), address(this));
}

/// @dev Returns the initialization code hash of the ERC4337 account (a minimal ERC1967 proxy).
function initCodeHash() public view virtual returns (bytes32 result) {
result = LibClone.initCodeHashERC1967(implementation);
predicted = Clones.predictDeterministicAddress(implementation, _getSalt(owners, nonce), address(this));
}

/// @dev Returns the salt that will be used for deterministic address
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ contract TestExecuteWithoutChainIdValidation is SmartWalletTestBase {
UserOperation memory userOp = _getUserOpWithSignature();
vm.expectEmit(true, true, true, true);
emit IEntryPoint.UserOperationEvent(
entryPoint.getUserOpHash(userOp), userOp.sender, address(0), userOp.nonce, false, 0, 48027
entryPoint.getUserOpHash(userOp), userOp.sender, address(0), userOp.nonce, false, 0, 48423
);
_sendUserOperation(userOp);
}
Expand Down
2 changes: 2 additions & 0 deletions test/CoinbaseSmartWallet/IsValidSignature.t.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {SignatureCheckerLib} from "solady/utils/SignatureCheckerLib.sol";

import "./SmartWalletTestBase.sol";
import "webauthn-sol/../test/Utils.sol";

Expand Down