-
Notifications
You must be signed in to change notification settings - Fork 161
Expand file tree
/
Copy pathExternalWalletStorage.sol
More file actions
80 lines (67 loc) · 3.16 KB
/
ExternalWalletStorage.sol
File metadata and controls
80 lines (67 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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;
}
}