Repository navigation
Expand file tree
/
Copy pathIdentityTreeStore.sol
More file actions
253 lines (217 loc) · 8.11 KB
/
Copy pathIdentityTreeStore.sol
File metadata and controls
253 lines (217 loc) · 8.11 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.27;
import {ReverseHashLib} from "../lib/ReverseHashLib.sol";
import {PoseidonUnit2L, PoseidonUnit3L} from "../lib/Poseidon.sol";
import {IState} from "../interfaces/IState.sol";
import {IOnchainCredentialStatusResolver} from "../interfaces/IOnchainCredentialStatusResolver.sol";
import {IRHSStorage} from "../interfaces/IRHSStorage.sol";
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
error NodeNotFound();
error InvalidStateNode();
error InvalidNodeType();
error UnsupportedLength();
/**
* @dev Contract which provides onchain Reverse Hash Service (RHS)
* for checking revocation status of claims.
*/
contract IdentityTreeStore is Initializable, IOnchainCredentialStatusResolver, IRHSStorage {
/**
* @dev Enum for node types
*/
enum NodeType {
Unknown,
Middle,
Leaf,
State,
Empty
}
/**
* @dev Version of contract
*/
string public constant VERSION = "1.1.0";
/**
* @dev Max SMT depth for the CredentialStatus proof
*/
uint256 public constant MAX_SMT_DEPTH = 40;
using ReverseHashLib for ReverseHashLib.Data;
/// @custom:storage-location erc7201:iden3.storage.IdentityTreeStore.ReverseHashLibData
// keccak256(abi.encode(uint256(keccak256("iden3.storage.IdentityTreeStore.ReverseHashLibData")) - 1)) &
// ~bytes32(uint256(0xff));
// solhint-disable-next-line const-name-snakecase
bytes32 private constant ReverseHashLibDataStorageLocation =
0x0f7e3bdc6cc0e880d509aa1f6b8d1a88e5fcb7274e18dfba772424a36fe9b400;
function _getReverseHashLibDataStorage() private pure returns (ReverseHashLib.Data storage $) {
// solhint-disable-next-line no-inline-assembly
assembly {
$.slot := ReverseHashLibDataStorageLocation
}
}
/// @custom:storage-location erc7201:iden3.storage.IdentityTreeStore.Main
struct IdentityTreeStoreMainStorage {
IState _state;
}
// keccak256(abi.encode(uint256(keccak256("iden3.storage.IdentityTreeStore.Main")) - 1)) & ~bytes32(uint256(0xff));
// solhint-disable-next-line const-name-snakecase
bytes32 private constant IdentityTreeStoreMainStorageLocation =
0x95ca427007e091a13a7ccfcb233b8a2ed19d987330a248c445b1b483a35bb800;
/// @dev Get the main storage using assembly to ensure specific storage location
function _getIdentityTreeStoreMainStorage()
private
pure
returns (IdentityTreeStoreMainStorage storage $)
{
// solhint-disable-next-line no-inline-assembly
assembly {
$.slot := IdentityTreeStoreMainStorageLocation
}
}
/**
* @dev Function to call first time for initialization of the proxy.
* @param state The state contract address to be used to check state of the identities
**/
function initialize(address state) public initializer {
IdentityTreeStoreMainStorage storage $its = _getIdentityTreeStoreMainStorage();
ReverseHashLib.Data storage $rhl = _getReverseHashLibDataStorage();
$its._state = IState(state);
$rhl.hashFunction = _hashFunc;
}
/**
* @dev Saves nodes array. Note that each node contains an array itself.
* @param nodes An array of nodes
*/
function saveNodes(uint256[][] memory nodes) external {
return _getReverseHashLibDataStorage().savePreimages(nodes);
}
/**
* @dev Returns a node by its key. Note that a node contains an array.
* @param key The key of the node
* @return The node
*/
function getNode(uint256 key) public view returns (uint256[] memory) {
uint256[] memory preim = _getReverseHashLibDataStorage().getPreimage(key);
if (preim.length == 0) {
revert NodeNotFound();
}
return preim;
}
/**
* @dev Returns revocation status of a claim using given identity id and revocation nonce.
* @param id Issuer's identifier
* @param nonce Revocation nonce
* @return CredentialStatus
*/
function getRevocationStatus(
uint256 id,
uint64 nonce
) external view returns (CredentialStatus memory) {
uint256 state = _getIdentityTreeStoreMainStorage()._state.getStateInfoById(id).state;
return _getRevocationStatusByState(state, nonce);
}
/**
* @dev Returns revocation status of a claim using given identity id, revocation nonce and state
* @param id Issuer's identifier
* @param state of the Issuer
* @param nonce Revocation nonce
* @return CredentialStatus
*/
function getRevocationStatusByIdAndState(
// solhint-disable-next-line no-unused-vars
uint256 id,
uint256 state,
uint64 nonce
) external view returns (CredentialStatus memory) {
return _getRevocationStatusByState(state, nonce);
}
function _getRevocationStatusByState(
uint256 state,
uint64 nonce
) internal view returns (CredentialStatus memory) {
uint256[] memory roots = getNode(state);
if (_nodeType(roots) != NodeType.State) {
revert InvalidStateNode();
}
CredentialStatus memory status = CredentialStatus({
issuer: IdentityStateRoots({
state: state,
claimsTreeRoot: roots[0],
revocationTreeRoot: roots[1],
rootOfRoots: roots[2]
}),
mtp: _getProof(roots[1], nonce)
});
return status;
}
function _getProof(uint256 root, uint256 index) internal view returns (Proof memory) {
uint256[] memory siblings = new uint256[](MAX_SMT_DEPTH);
// Solidity does not guarantee that memory vars are zeroed out
for (uint256 i = 0; i < MAX_SMT_DEPTH; i++) {
siblings[i] = 0;
}
Proof memory proof = Proof({
root: root,
existence: false,
siblings: siblings,
index: index,
value: 0,
auxExistence: false,
auxIndex: 0,
auxValue: 0
});
uint256 nextNodeHash = root;
uint256[] memory children;
ReverseHashLib.Data storage $ = _getReverseHashLibDataStorage();
for (uint256 i = 0; i <= MAX_SMT_DEPTH; i++) {
children = $.hashesToPreimages[nextNodeHash];
NodeType nodeType = _nodeType(children);
if (nodeType == NodeType.Empty) {
break;
}
if (nodeType == NodeType.Leaf) {
if (children[0] == index) {
proof.existence = true;
proof.value = children[1];
break;
}
proof.auxExistence = true;
proof.auxIndex = children[0];
proof.auxValue = children[1];
break;
} else if (nodeType == NodeType.Middle && i < MAX_SMT_DEPTH) {
if ((proof.index >> i) & 1 == 1) {
nextNodeHash = children[1];
proof.siblings[i] = children[0];
} else {
nextNodeHash = children[0];
proof.siblings[i] = children[1];
}
} else {
revert InvalidNodeType();
}
}
return proof;
}
function _nodeType(uint256[] memory node) internal pure returns (NodeType) {
if (node.length == 2) {
return NodeType.Middle;
}
if (node.length == 3 && node[2] == 1) {
return NodeType.Leaf;
}
if (node.length == 3) {
return NodeType.State;
}
if (node.length == 0) {
return NodeType.Empty;
}
return NodeType.Unknown;
}
function _hashFunc(uint256[] memory preimage) internal pure returns (uint256) {
if (preimage.length == 2) {
return PoseidonUnit2L.poseidon([preimage[0], preimage[1]]);
}
if (preimage.length == 3) {
return PoseidonUnit3L.poseidon([preimage[0], preimage[1], preimage[2]]);
}
revert UnsupportedLength();
}
}