-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProductTracker.sol
163 lines (127 loc) · 5.79 KB
/
ProductTracker.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
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
pragma solidity ^0.4.21;
contract ProductTracker {
struct Producer {
string signature;
string fullName;
string phoneNumber;
string eMail;
string uuid;
}
struct Product {
string producerName;
string prunerName;
string producingArea;
string harvestDate;
string shippingDate;
string receptionDate;
string issuanceDate;
string inspectionDate1;
string inspectionDate2;
string inspectionDate3;
string pesticideUse;
bool initialized;
}
Producer public registeredProducers;
address[] public producersAccounts;
mapping(address => Producer) public producers;
mapping(string => Product) private productStore;
mapping(address => mapping(string => bool)) private walletStore;
event ProducerHistory(address indexed producer);
event ProducerInfo(address producerAddress, string fullName, string phoneNumber, string eMail, string uuid);
event ProductCreate(address account, string uuid, string producerName);
event RejectCreate(address account, string uuid, string message);
event ProductTransfer(address from, address to, string uuid);
event RejectTransfer(address from, address to, string uuid, string message);
event DigitalSignature(address addr);
function addProducer(string _signature, string _fullName, string _phoneNumber, string _eMail, string _uuid) public {
Producer storage producer = producers[msg.sender];
for(uint i = 0; i < producersAccounts.length; i ++){
if(msg.sender == producersAccounts[i]){
producersAccounts[i] = producersAccounts[producersAccounts.length - 1];
delete producersAccounts[i];
delete producers[msg.sender];
producersAccounts.length --;
}
}
producer.signature = _signature;
producer.fullName = _fullName;
producer.phoneNumber = _phoneNumber;
producer.eMail = _eMail;
producer.uuid = _uuid;
producersAccounts.push(msg.sender) - 1;
emit ProducerHistory(msg.sender);
emit ProducerInfo(msg.sender, _fullName, _phoneNumber, _eMail, _uuid);
}
function removeProducer(address _producerAddress) public {
// Delete producer from struct and mapping
for(uint i = 0; i < producersAccounts.length; i ++){
if(_producerAddress == producersAccounts[i]){
producersAccounts[i] = producersAccounts[producersAccounts.length - 1];
delete producersAccounts[i];
delete producers[_producerAddress];
producersAccounts.length --;
}
}
emit ProducerHistory(_producerAddress);
}
function getAllProducers() view public returns(address[]) {
return producersAccounts;
}
function getOneProducer(address _address) view public returns (string, string, string, string, string) {
return (producers[_address].signature, producers[_address].fullName, producers[_address].phoneNumber, producers[_address].eMail, producers[_address].uuid);
}
function producersNumber() view public returns (uint) {
return producersAccounts.length;
}
function createProduct(string uuid,
string producerName,
string prunerName,
string producingArea,
string harvestDate,
string shippingDate,
string receptionDate,
string issuanceDate,
string inspectionDate1,
string inspectionDate2,
string inspectionDate3,
string pesticideUse) public {
if(productStore[uuid].initialized) {
emit RejectCreate(msg.sender, uuid, "A product with this UUID already exists.");
return;
}
productStore[uuid] = Product(producerName, prunerName, producingArea, harvestDate, shippingDate, receptionDate, issuanceDate, inspectionDate1, inspectionDate2, inspectionDate3, pesticideUse, true);
walletStore[msg.sender][uuid] = true;
emit ProductCreate(msg.sender, uuid, producerName);
}
function transferProduct(address to, string uuid) public {
if(!productStore[uuid].initialized) {
emit RejectTransfer(msg.sender, to, uuid, "No product with this UUID exists");
return;
}
if(!walletStore[msg.sender][uuid]) {
emit RejectTransfer(msg.sender, to, uuid, "Sender does not own this product.");
return;
}
walletStore[msg.sender][uuid] = false;
walletStore[to][uuid] = true;
emit ProductTransfer(msg.sender, to, uuid);
}
function getProductByUUID1(string uuid) public constant returns (string, string, string, string, string) {
return (productStore[uuid].producerName, productStore[uuid].prunerName, productStore[uuid].producingArea, productStore[uuid].harvestDate, productStore[uuid].shippingDate);
}
function getProductByUUID2(string uuid) public constant returns (string, string, string, string, string, string) {
return (productStore[uuid].receptionDate, productStore[uuid].issuanceDate, productStore[uuid].inspectionDate1, productStore[uuid].inspectionDate2, productStore[uuid].inspectionDate3, productStore[uuid].pesticideUse);
}
function isOwnerOf(address owner, string uuid) public constant returns (bool) {
if(walletStore[owner][uuid]) {
return true;
}
return false;
}
// Find out if the producer info is signed
function isSigned(address _addr, bytes32 hash, uint8 v, bytes32 r, bytes32 s) constant returns(bool) {
bytes memory prefix = "\x19Ethereum Signed Message:\n32";
bytes32 prefixedHash = keccak256(prefix, hash);
return ecrecover(prefixedHash, v, r, s) == (_addr);
}
}