-
Notifications
You must be signed in to change notification settings - Fork 161
Prototype external storage contract for 7702 CoinbaseSmartWallets #108
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
Draft
amiecorso
wants to merge
1
commit into
coinbase:main
Choose a base branch
from
amiecorso:amie/external-ownership-contract
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity ^0.8.23; | ||
|
|
||
| /// @title External Wallet Storage | ||
| /// @notice Singleton contract for storing wallet state, protected by caller address and code hash | ||
| contract ExternalWalletStorage { | ||
| /// @notice Storage layout used by each wallet instance | ||
| struct WalletStorage { | ||
| uint256 nextOwnerIndex; | ||
| uint256 removedOwnersCount; | ||
| mapping(uint256 index => bytes owner) ownerAtIndex; | ||
| mapping(bytes bytes_ => bool isOwner_) isOwner; | ||
| } | ||
|
|
||
| /// @notice Maps (wallet address, wallet code hash) to wallet storage | ||
| mapping(address => mapping(bytes32 => WalletStorage)) private walletStorage; | ||
|
|
||
| /// @notice Access denied - caller must have expected code hash | ||
| error UnauthorizedAccess(); | ||
|
|
||
| /// @dev Validates caller has expected code hash | ||
| function _validateCaller(bytes32 expectedCodeHash) internal view { | ||
| bytes32 callerCodeHash; | ||
| assembly { | ||
| callerCodeHash := extcodehash(caller()) | ||
| } | ||
|
|
||
| if (callerCodeHash != expectedCodeHash) { | ||
| revert UnauthorizedAccess(); | ||
| } | ||
| } | ||
|
|
||
| /// @notice Gets the next owner index | ||
| function getNextOwnerIndex(bytes32 expectedCodeHash) external view returns (uint256) { | ||
| _validateCaller(expectedCodeHash); | ||
| return walletStorage[msg.sender][expectedCodeHash].nextOwnerIndex; | ||
| } | ||
|
|
||
| /// @notice Gets the removed owners count | ||
| function getRemovedOwnersCount(bytes32 expectedCodeHash) external view returns (uint256) { | ||
| _validateCaller(expectedCodeHash); | ||
| return walletStorage[msg.sender][expectedCodeHash].removedOwnersCount; | ||
| } | ||
|
|
||
| /// @notice Gets owner at index | ||
| function getOwnerAtIndex(bytes32 expectedCodeHash, uint256 index) external view returns (bytes memory) { | ||
| _validateCaller(expectedCodeHash); | ||
| return walletStorage[msg.sender][expectedCodeHash].ownerAtIndex[index]; | ||
| } | ||
|
|
||
| /// @notice Checks if bytes is an owner | ||
| function isOwner(bytes32 expectedCodeHash, bytes calldata ownerBytes) external view returns (bool) { | ||
| _validateCaller(expectedCodeHash); | ||
| return walletStorage[msg.sender][expectedCodeHash].isOwner[ownerBytes]; | ||
| } | ||
|
|
||
| /// @notice Sets the next owner index | ||
| function setNextOwnerIndex(bytes32 expectedCodeHash, uint256 value) external { | ||
| _validateCaller(expectedCodeHash); | ||
| walletStorage[msg.sender][expectedCodeHash].nextOwnerIndex = value; | ||
| } | ||
|
|
||
| /// @notice Sets the removed owners count | ||
| function setRemovedOwnersCount(bytes32 expectedCodeHash, uint256 value) external { | ||
| _validateCaller(expectedCodeHash); | ||
| walletStorage[msg.sender][expectedCodeHash].removedOwnersCount = value; | ||
| } | ||
|
|
||
| /// @notice Sets owner at index | ||
| function setOwnerAtIndex(bytes32 expectedCodeHash, uint256 index, bytes calldata owner) external { | ||
| _validateCaller(expectedCodeHash); | ||
| walletStorage[msg.sender][expectedCodeHash].ownerAtIndex[index] = owner; | ||
| } | ||
|
|
||
| /// @notice Sets isOwner flag | ||
| function setIsOwner(bytes32 expectedCodeHash, bytes calldata ownerBytes, bool value) external { | ||
| _validateCaller(expectedCodeHash); | ||
| walletStorage[msg.sender][expectedCodeHash].isOwner[ownerBytes] = value; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice idea. but needs a small fix to be accepted by the bundler's validation rules:
In order to be associated with the account's address, the last mapping has to be the address, not the first.
so change the mapping to
mapping(bytes32 => mapping(address => WalletStorage))another issue is allow an account to have arbitrary data stored in the external storage, but that is a separate issue.