-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathIdentityUtilities.sol
More file actions
311 lines (283 loc) · 9.76 KB
/
IdentityUtilities.sol
File metadata and controls
311 lines (283 loc) · 9.76 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.27;
import { IIdentityUtilities } from "./interface/IIdentityUtilities.sol";
import { AccessControlUpgradeable } from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import { UUPSUpgradeable } from "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol";
import { IIdentity } from "./interface/IIdentity.sol";
import { IClaimIssuer } from "./interface/IClaimIssuer.sol";
/**
* @title IdentityUtilities
* @notice Contract for registering and retrieving structured topic schemas using encoded string arrays.
* @dev Inherits from AccessControl and supports UUPS upgrades. Topics define field names and types
* using ABI-encoded `string[]` arrays.
*/
contract IdentityUtilities is
IIdentityUtilities,
AccessControlUpgradeable,
UUPSUpgradeable
{
/// @notice Role identifier for accounts allowed to manage topics
bytes32 public constant TOPIC_MANAGER_ROLE =
keccak256("TOPIC_MANAGER_ROLE");
/// @dev Mapping from topic ID to TopicInfo struct
mapping(uint256 => TopicInfo) private _topics;
/// @notice Disables initializers on the implementation contract
constructor() {
_disableInitializers();
}
/**
* @notice Initializes the contract and sets the admin and topic manager roles.
* @param admin Address to receive DEFAULT_ADMIN_ROLE and TOPIC_MANAGER_ROLE
*/
function initialize(address admin) external initializer {
__AccessControl_init();
_grantRole(DEFAULT_ADMIN_ROLE, admin);
_grantRole(TOPIC_MANAGER_ROLE, admin);
}
/**
* @inheritdoc IIdentityUtilities
*/
function addTopic(
uint256 topicId,
string calldata name,
bytes calldata encodedFieldNames,
bytes calldata encodedFieldTypes
) external override onlyRole(TOPIC_MANAGER_ROLE) {
require(bytes(name).length > 0, "Empty topic name");
require(
_topics[topicId].encodedFieldNames.length == 0,
"Topic already exists"
);
_validateFieldArrays(encodedFieldNames, encodedFieldTypes);
_topics[topicId] = TopicInfo({
name: name,
encodedFieldNames: encodedFieldNames,
encodedFieldTypes: encodedFieldTypes
});
emit TopicAdded(topicId, name, encodedFieldNames, encodedFieldTypes);
}
/**
* @inheritdoc IIdentityUtilities
*/
function updateTopic(
uint256 topicId,
string calldata name,
bytes calldata encodedFieldNames,
bytes calldata encodedFieldTypes
) external override onlyRole(TOPIC_MANAGER_ROLE) {
require(
_topics[topicId].encodedFieldNames.length != 0,
"Topic does not exist"
);
require(bytes(name).length > 0, "Empty topic name");
_validateFieldArrays(encodedFieldNames, encodedFieldTypes);
_topics[topicId] = TopicInfo({
name: name,
encodedFieldNames: encodedFieldNames,
encodedFieldTypes: encodedFieldTypes
});
emit TopicUpdated(topicId, name, encodedFieldNames, encodedFieldTypes);
}
/**
* @inheritdoc IIdentityUtilities
*/
function removeTopic(
uint256 topicId
) external override onlyRole(TOPIC_MANAGER_ROLE) {
require(
_topics[topicId].encodedFieldNames.length != 0,
"Topic does not exist"
);
delete _topics[topicId];
emit TopicRemoved(topicId);
}
/**
* @inheritdoc IIdentityUtilities
*/
function getTopic(
uint256 topicId
) external view override returns (TopicInfo memory) {
return _topics[topicId];
}
/**
* @inheritdoc IIdentityUtilities
*/
function getSchema(
uint256 topicId
)
external
view
override
returns (string[] memory fieldNames, string[] memory fieldTypes)
{
if (_topics[topicId].encodedFieldNames.length == 0) {
return (new string[](0), new string[](0));
}
fieldNames = abi.decode(_topics[topicId].encodedFieldNames, (string[]));
fieldTypes = abi.decode(_topics[topicId].encodedFieldTypes, (string[]));
}
/**
* @notice Returns decoded field names for a given topic ID
* @param topicId The ID of the topic
* @return string[] Array of field names
*/
function getFieldNames(
uint256 topicId
) external view returns (string[] memory) {
if (_topics[topicId].encodedFieldNames.length == 0) {
return new string[](0);
}
return abi.decode(_topics[topicId].encodedFieldNames, (string[]));
}
/**
* @notice Returns decoded field types for a given topic ID
* @param topicId The ID of the topic
* @return string[] Array of field types
*/
function getFieldTypes(
uint256 topicId
) external view returns (string[] memory) {
if (_topics[topicId].encodedFieldTypes.length == 0) {
return new string[](0);
}
return abi.decode(_topics[topicId].encodedFieldTypes, (string[]));
}
/**
* @notice Returns an array of TopicInfo structs for the given topic IDs
* @param topicIds Array of topic IDs to get TopicInfo structs for
* @return TopicInfo[] Array of TopicInfo structs corresponding to the input topic IDs
*/
function getTopicInfos(
uint256[] calldata topicIds
) external view returns (TopicInfo[] memory) {
TopicInfo[] memory topics = new TopicInfo[](topicIds.length);
for (uint256 i = 0; i < topicIds.length; i++) {
topics[i] = _topics[topicIds[i]];
}
return topics;
}
/**
* @notice Gets comprehensive claim information for an identity across multiple topics
* @param identity The identity contract address
* @param topicIds Array of topic IDs to check
* @return result Array of claim information structs
*/
function getClaimsWithTopicInfo(
address identity,
uint256[] calldata topicIds
) external view returns (ClaimInfo[] memory result) {
uint256 totalClaims = _countTotalClaims(identity, topicIds);
result = new ClaimInfo[](totalClaims);
uint256 resultIndex = 0;
for (uint256 i = 0; i < topicIds.length; i++) {
uint256 topicId = topicIds[i];
TopicInfo memory topicInfo = _topics[topicId];
bytes32[] memory claimIds = IIdentity(identity).getClaimIdsByTopic(
topicId
);
for (uint256 j = 0; j < claimIds.length; j++) {
result[resultIndex] = _buildClaimInfo(
identity,
topicId,
topicInfo,
claimIds[j]
);
resultIndex++;
}
}
}
/**
* @dev Required override for UUPS upgradability authorization
* @param newImplementation Address of the new implementation
*/
function _authorizeUpgrade(
address newImplementation
) internal override onlyRole(DEFAULT_ADMIN_ROLE) {}
function _countTotalClaims(
address identity,
uint256[] calldata topicIds
) internal view returns (uint256 total) {
for (uint256 i = 0; i < topicIds.length; i++) {
bytes32[] memory claimIds = IIdentity(identity).getClaimIdsByTopic(
topicIds[i]
);
total += claimIds.length;
}
}
function _isClaimValid(
address identity,
uint256 topicId,
address issuer,
bytes memory signature,
bytes memory data
) internal view returns (bool) {
if (issuer == address(0) || issuer.code.length == 0) return false;
try
IClaimIssuer(issuer).isClaimValid(
IIdentity(identity),
topicId,
signature,
data
)
returns (bool valid) {
return valid;
} catch {
return false;
}
}
function _buildClaimInfo(
address identity,
uint256 topicId,
TopicInfo memory topicInfo,
bytes32 claimId
) internal view returns (ClaimInfo memory info) {
(
,
// topic - not used
uint256 scheme,
address issuer,
bytes memory signature,
bytes memory data,
string memory uri
) = IIdentity(identity).getClaim(claimId);
bool isValid = _isClaimValid(
identity,
topicId,
issuer,
signature,
data
);
info = ClaimInfo({
topic: topicInfo,
isValid: isValid,
claimId: claimId,
scheme: scheme,
issuer: issuer,
signature: signature,
data: data,
uri: uri
});
}
/**
* @dev Validates that encoded field names/types match in length and content.
* @param encodedNames ABI-encoded string[] of field names
* @param encodedTypes ABI-encoded string[] of field types
*/
function _validateFieldArrays(
bytes memory encodedNames,
bytes memory encodedTypes
) internal pure {
string[] memory names = abi.decode(encodedNames, (string[]));
string[] memory types_ = abi.decode(encodedTypes, (string[]));
require(
names.length == types_.length,
"Field name/type count mismatch"
);
for (uint256 i = 0; i < names.length; i++) {
require(bytes(names[i]).length > 0, "Empty field name");
require(bytes(types_[i]).length > 0, "Empty field type");
}
}
/// @dev Reserved storage space to allow future layout changes
uint256[50] private __gap; // solhint-disable-line ordering
}