-
Notifications
You must be signed in to change notification settings - Fork 0
/
Existingsystem.sol
237 lines (198 loc) · 7.63 KB
/
Existingsystem.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
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
pragma solidity >=0.4.14 <0.6.0;
pragma experimental ABIEncoderV2;
//import './DateTime.sol';
//import './HexUtils.sol';
import './strings.sol';
// import './FogDeviceRegistration.sol';
contract FogBlockchainAuthentication{
using strings for *;
string public s;
address public owner;
struct Authentication {
address requesteddeviceid;
string deviceinfo;
string previousblock;
address accessdeviceid;
string permissions;
// string location;
uint timelease;
}
mapping (address => Authentication) fogdeviceauth;
// string regname;
// address regdeviceid; string regdeviceconfiguration;
// uint32 regmanufacturerid;string reglocations;
// string regipfsusercollagepicaddress;
string ipfsusercollagepicaddress;
struct FogDevice {
string modelname;
address deviceid;
uint32 manufacturerid;
// string locations;
string usercollagepicaddress;
string deviceconfiguration;
int authcoins;
int registertime;
}
// Stored devices
mapping (address => FogDevice) fogdevices;
// byte32 s4;
event Log(string text1,string text);
event Logs(string text1,uint32 text);
event Logint(string text1,int text);
event Log(string text);
event Logs(uint32 text);
event Logint(int text);
function sendImageHash(string ipfshash) public {
ipfsusercollagepicaddress = ipfshash;
}
function getImageHash() public returns (string x) {
return ipfsusercollagepicaddress;
}
function register(string name, string deviceconfiguration,uint32 manufacturerid,string ipfsusercollagepicaddress)
public returns (bool res) {
FogDevice dev = fogdevices[msg.sender];
// Return false if already registered.
if ( dev.deviceid != 0x0) {
return false;
}
dev.modelname = name;
dev.deviceid = msg.sender;
dev.registertime = 100000;
dev.manufacturerid = manufacturerid;
dev.deviceconfiguration=deviceconfiguration;
dev.usercollagepicaddress=ipfsusercollagepicaddress;
//dev.locations=locations;
dev.authcoins=100;
fogdevices[msg.sender] = dev;
// s4=sha3(dev.modelname, dev.deviceid,dev.manufacturerid,dev.deviceconfiguration,dev.usercollagepicaddress);
Log(name,"Registered Successfully");
return true;
}
function unregister() public returns (bool res) {
FogDevice dev = fogdevices[msg.sender];
delete dev.modelname;
dev.deviceid = 0x0;
dev.registertime = 0;
dev.deviceconfiguration = "";
dev.usercollagepicaddress="";
dev.authcoins=0;
Log(dev.modelname,"UnRegistered Successfully");
return true;
}
function getDevInfo(address devid) public returns (string name, address deviceid, string deviceconfiguration,uint32 manufacturerid,string ipfsusercollagepicaddress,int authcoins) {
FogDevice dev = fogdevices[devid];
name = dev.modelname;
deviceid = dev.deviceid;
deviceconfiguration = dev.deviceconfiguration;
manufacturerid=dev.manufacturerid;
//locations=dev.locations;
ipfsusercollagepicaddress=dev.usercollagepicaddress;
authcoins=dev.authcoins;
Log(name,"Device Registered Successfully");
return(name,deviceid,deviceconfiguration,manufacturerid,ipfsusercollagepicaddress,authcoins);
}
function remove() public returns (bool res) {
if (msg.sender == owner) {
suicide(owner);
Log("Contract","Smart contract suicide Successfully");
}
}
//string public fdlocation;
function checkAuthentication(address addr,string name, string deviceconfiguration,uint32 manufacturerid,string ipfsusercollagepicaddress, uint leasetime)
public returns (bool res) {
var(regname,regdeviceid,regdeviceconfiguration,regmanufacturerid,regipfsusercollagepicaddress,regauthcoins)= getDevInfo(addr);
Log("regname ",regname);
Log("name",name);
Logs("regmanufacturerid",regmanufacturerid);
Logs("manufacturerid",manufacturerid);
Log("regipfsusercollagepicaddress",regipfsusercollagepicaddress);
Log("ipfsusercollagepicaddress",ipfsusercollagepicaddress);
Logint("regauthcoins",regauthcoins);
if ( keccak256(regname)==keccak256(name) && regmanufacturerid==manufacturerid && keccak256(regipfsusercollagepicaddress) == keccak256(ipfsusercollagepicaddress) && regauthcoins>1)
{
// var s = reglocations.toSlice();
// var spliinal= //reglocations.toSlice().find(locations.toSlice());
//bytes memory tempEmptyStringTest = //bytes(spliinal.toString()); // Uses memory
regauthcoins=regauthcoins-1; giveAccesstoFD(addr,deviceconfiguration,manufacturerid,"Write",leasetime);
Log("Access granted Successfully");
return true;
}
// emptyStringTest is not an empty string
}
// var delim = ":".toSlice();
// if(parts[i].toString()=="locations")
// {
// bool success= RequestFogdevice(name,locations);
// if(success==true)
// {
// giveAccesstoFD(addr,deviceconfiguration,manufacturerid,"Write");
// }
// }
}
//feed.info.value(10).gas(800)();
//string name, string deviceconfiguration,uint32 manufacturerid,string locations,string ipfsusercollagepicaddress
Log("Access restricted");
regauthcoins=regauthcoins-50;
return false;
}
function giveAccesstoFD(address requesteddeviceid,
string deviceinfo,
address accessdeviceid,
string permissions, uint leaseTime) expire(msg.sender) public returns (bool)
{
Authentication devauth = fogdeviceauth[msg.sender];
// Return false if already registered.
devauth.requesteddeviceid=requesteddeviceid;
devauth.deviceinfo=deviceinfo;
devauth.accessdeviceid=accessdeviceid;
devauth.permissions=permissions;
devauth.timelease=block.timestamp + leaseTime;
fogdeviceauth[msg.sender] = devauth;
return true;
}
function getAccessfo(address devid) public returns (address requesteddeviceid,
string deviceinfo,
address accessdeviceid,
string permissions,
uint leaseTime) {
Authentication devauth = fogdeviceauth[msg.sender];
requesteddeviceid= devauth.requesteddeviceid;
deviceinfo=devauth.deviceinfo;
accessdeviceid=devauth.accessdeviceid;
permissions=devauth.permissions;
//location=devauth.location;
leaseTime=devauth.timelease-now;
}
function autoDisconnect() public returns (bool)
{
removeAccesstoFD();
}
modifier expire(address _addr) {
if (fogdeviceauth[_addr].timelease >= block.timestamp) {
Authentication devauth = fogdeviceauth[msg.sender];
// Return false if already registered.
devauth.requesteddeviceid=0x0;
devauth.deviceinfo="";
devauth.previousblock="";
devauth.accessdeviceid=0x0;
devauth.permissions="";
// devauth.location="";
devauth.timelease=0;
}
_;
}
function removeAccesstoFD() public returns (bool)
{
Authentication devauth = fogdeviceauth[msg.sender];
// Return false if already registered.
devauth.requesteddeviceid=0x0;
devauth.deviceinfo="";
devauth.previousblock="";
devauth.accessdeviceid=0x0;
devauth.permissions="";
// devauth.location="";
devauth.timelease=0;
fogdeviceauth[msg.sender] = devauth;
return true;
}
}