-
Notifications
You must be signed in to change notification settings - Fork 0
/
CollectionRegistry.sol
78 lines (67 loc) · 2.47 KB
/
CollectionRegistry.sol
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
pragma solidity ^0.5.0;
contract CollectionRegistry {
address public manufacturer;
string public name;
uint public maxItems;
address[] public maintainers;
item[] public items;
event itemAdded(address publicKey, string identifier, string metaData);
struct item {
address publicKey;
string identifier;
string metaData;
}
modifier deployConditions(string memory _name, address[] memory _maintainers) {
require(_maintainers.length > 0, "maintainers list mustn't be empty");
require(bytes(_name).length > 0, "name may not be empty");
_;
}
modifier maintainer() {
bool isMaintainer = false;
for (uint i = 0; i < maintainers.length; i++) {
if (maintainers[i] == msg.sender) {
isMaintainer = true;
}
}
require(isMaintainer, "not a maintainer");
_;
}
modifier itemConditions(address _publicKey, string memory _identifier) {
if (maxItems != 0) {
require(items.length < maxItems, "collection reached max amount of items");
}
require(bytes(_identifier).length > 0, "identifier mustn't be empty");
for (uint i = 0; i < items.length; i++) {
if (items[i].publicKey == _publicKey) {
revert("item already exists in collection");
}
if (keccak256(abi.encodePacked(items[i].identifier)) == keccak256(abi.encodePacked(_identifier))) {
revert("item already exists in collection");
}
}
_;
}
constructor(string memory _name, uint _maxItems, address[] memory _maintainers) public deployConditions(_name, _maintainers) {
manufacturer = msg.sender;
name = _name;
maxItems = _maxItems;
maintainers = _maintainers;
}
function addItem(address _publicKey, string memory _identifier, string memory _metaData)
public maintainer itemConditions(_publicKey, _identifier) {
item memory newItem;
newItem.publicKey = _publicKey;
newItem.identifier = _identifier;
newItem.metaData = _metaData;
items.push(newItem);
emit itemAdded(_publicKey, _identifier, _metaData);
}
function checkItem(address _publicKey) public view returns (bool) {
for (uint i = 0; i < items.length; i++) {
if (items[i].publicKey == _publicKey) {
return true;
}
}
return false;
}
}