-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
294 lines (253 loc) · 11.7 KB
/
server.js
File metadata and controls
294 lines (253 loc) · 11.7 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
const toRau = require("iotex-antenna/lib/account/utils").toRau;
const Contract = require("iotex-antenna/lib/contract/contract").Contract
const turf = require('./modules/turfModules')
const buffer = require('@turf/buffer')
const express = require('express');
const bodyParser = require('body-parser');
const server = express();
const path = require('path');
const Web3 = require('web3');
const Antenna = require('iotex-antenna')
const VEHICLE_REGISTER_ABI = require('./src/pages/vehicle-registration/ABI')
const DID_REGISTER_ABI = require('./src/pages/did-registration/did-contract-details').abi
const ZONE_REGISTER_ABI = require('./src/pages/jurisdiction-registry/zone-contract-details.js').abi
const ZONE_REGISTER_ADDRESS = require('./src/pages/jurisdiction-registry/zone-contract-details.js').address
const axios = require('axios').default
const generateRandomRoute = require('./modules/generateRandomRoute')
const fetchDIDsAndGeometries = require('./modules/fetchDIDsAndGeometries');
const addGeometriesToDidDocs = require('./modules/addGeometriesToDidDocs')
const mapboxtoken = 'pk.eyJ1IjoiamdqYW1lcyIsImEiOiJjazd5cHlucXUwMDF1M2VtZzM1bjVwZ2hnIn0.Oavbw2oHnexn0hiVOoZwuA'
// Fetch registered zones from Zone Registry
var samplePoints = require('./data/samplePoints.json');
const samplePolygons = require('./data/samplePolygons.json');
const sampleJurisdictionDIDdocs = require('./data/sampleZoneDids.json')
// const sampleVehicles = require('./data/sampleVehicles.json')
let turfPolygons = []
let VEHICLE_REGISTER_ADDRESS = "io10m9n3kge7l9es3n4raq90m3thtr7futpp0t3ph"
async function slash(did, enterTime, exitTime, rate) {
// Calculate charge
const TIME_MULTIPLIER = 5
let timeElapsedInMinutes = ((Date.parse(exitTime) - Date.parse(enterTime)) * TIME_MULTIPLIER) / 1000
// Connect to contract
let antenna = new Antenna.default("http://api.testnet.iotex.one:80");
let vehicleRegContract = new Contract(VEHICLE_REGISTER_ABI, VEHICLE_REGISTER_ADDRESS, {provider: antenna.iotx});
// Get vehicle's document
let uri = await antenna.iotx.readContractByMethod({
from: "io1y3cncf05k0wh4jfhp9rl9enpw9c4d9sltedhld",
contractAddress: "io1zyksvtuqyxeadegsqsw6vsqrzr36cs7u2aa0ag",
abi: DID_REGISTER_ABI,
method: "getURI"
}, did);
let res = await axios.get(uri)
// Read owner from vehicle
let vehicleOwner = res.data.creator
// Slash owner (admin needs to use the private key of the owner of the VehicleRegistry contract)
let admin = await antenna.iotx.accounts.privateKeyToAccount(
"eec04109aab7af268a1158b88717bd6f62026895920aeb296d4150a7a309dec8"
);
try {
let actionHash = await vehicleRegContract.methods.slash(toRau((rate * timeElapsedInMinutes).toFixed(2).toString(), "Iotx"), vehicleOwner, did, {
account: admin,
gasLimit: "1000000",
gasPrice: toRau("1", "Qev")
});
console.log("Slash occurs now on:", vehicleOwner, "who owns", did, "at a rate of", rate, "totalling", (rate * timeElapsedInMinutes).toFixed(2) , ". Slashing action hash:")
return actionHash
} catch (err) {
console.log(err);
}
}
if (process.env.NODE_ENV === 'production') {
// Serve static files from the React frontend app
server.use(express.static(path.join(__dirname, '/build')))
}
server.use(bodyParser.urlencoded({
extended: false
}));
let PORT = process.env.PORT || 3001
const http = server.listen(PORT, () => {
console.log(`Express server and socket.io websocket are running on localhost:${PORT}`);
});
const io = require('socket.io')(http);
io.on('connection', async (client) => {
// Start enclave listener
const SecureWorker = require('./secureworker');
const worker = new SecureWorker('enclave.so', 'enclave-point-polygon-check.js');
let counter = 1;
// When we receive a request for new points, send the points and polygons into the enclave and run the check
client.on('fetchNewPositionsFromServer', function (points, dids) {
worker.postMessage({
type: 'pointInPolygonCheck',
points,
dids,
counter
})
counter += 1;
// We'll add the non-enclave tests and event emission here
});
// Listen for results from enclave
worker.onMessage(async (message) => {
if (message.type === 'enteringNotification') {
// If enclave detects a vehicle entering a zone, send that to the client
client.emit('fetchNewPositionsFromServerResponse', message.notification)
} else if (message.type === 'exitingNotification') {
// If enclave detects a vehicle exiting a zone, send that to the client and slash vehicle
console.log(message.notification.rate)
let hash = await slash(message.notification.vehicleDetails.id, message.notification.vehicleDetails.enterTime, message.notification.vehicleDetails.exitTime, message.notification.rate)
client.emit('fetchNewPositionsFromServerResponse', message.notification, hash)
} else if (message.type === 'updatePositions') {
// When enclave finishes, get the new positions updated vehicle info and send to client
client.emit('updatePositions', message.newPositions, message.points)
}
})
client.on('disconnect', function () {
console.log('user disconnected')
})
})
server.get('/api/getAllVehicles', async (req, res) => {
let antenna = new Antenna.default("http://api.testnet.iotex.one:80");
// Get total number of registered vehicles
try {
let numberOfRegisteredVehicles = await antenna.iotx.readContractByMethod({
from: "io1y3cncf05k0wh4jfhp9rl9enpw9c4d9sltedhld",
abi: VEHICLE_REGISTER_ABI,
contractAddress: VEHICLE_REGISTER_ADDRESS,
method: "numberOfRegisteredVehicles"
},
0);
numberOfRegisteredVehicles = parseInt(numberOfRegisteredVehicles.toString())
let registeredVehicles = []
// Iterate through the registered vehicles array and return each string
for (let i = 0; i < numberOfRegisteredVehicles; i++) {
const vehicleID = await antenna.iotx.readContractByMethod({
from: "io1y3cncf05k0wh4jfhp9rl9enpw9c4d9sltedhld",
abi: VEHICLE_REGISTER_ABI,
contractAddress: VEHICLE_REGISTER_ADDRESS,
method: "allVehicles"
},
i);
//console.log(vehicleID)
registeredVehicles.push(vehicleID)
}
let ret = []
// Get the DID documents associated with each
for (let i in registeredVehicles) {
let uri = await antenna.iotx.readContractByMethod({
from: "io1y3cncf05k0wh4jfhp9rl9enpw9c4d9sltedhld",
contractAddress: "io1zyksvtuqyxeadegsqsw6vsqrzr36cs7u2aa0ag",
abi: DID_REGISTER_ABI,
method: "getURI"
}, registeredVehicles[i]);
uri = uri.toString('hex');
if (uri) {
try {
let doc = await axios.get(uri)
ret.push(doc.data)
} catch (e) {
ret.push({})
}
}
}
res.send(ret)
} catch (err) {
console.log(err)
}
});
server.get('/api/getAllPolygons', async (req, res) => {
// Set up connection with ZoneRegistry contract on Ethereum
const ropstenProvider = new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/799a48033afc4389a1576386aee584dd");
const web3 = new Web3(ropstenProvider);
const SERVER_PRIVATE_KEY = "0xa6e027a167eed0a181893685e7bbfbc8d41d0017c9abf0eab8ec4f66ebe4848b";
const serverWallet = web3.eth.accounts.privateKeyToAccount(SERVER_PRIVATE_KEY);
let isListening = await web3.eth.net.isListening();
console.log('isListening', isListening);
console.log(serverWallet)
const zoneContract = new web3.eth.Contract(ZONE_REGISTER_ABI, ZONE_REGISTER_ADDRESS)
// Simulated Fetch DID URIs:
// var zoneAddresses = [ // @LEO -> can you set this up to fetch all registered DIDs?
// // "0x77DB10B97bbcE20656d386624ACb5469E57Dd21b", // <- UK
// // "0x375ef39Fe23128a42992d5cad5a166Ab04C20A88", // <- Netherlands
// // "0x3985dE49147725D64407d14c3430bd1dC9c11f04", // <- Germany
// // "0xe0eE166374DcD88e3dFE50E3f72005CEE37F64BD", // <- France
// "0xb7ec4260F21f6C1208Ef55ED4Afa550bCC37e5f5", // <- Wales
// "0x26A398bd8429Da356198D0c16D91BDEF3bdbCd76" // <- Birmingham
// ];
let zoneAddresses = await zoneContract.methods.getExistingDIDs().call({
from: serverWallet.address,
});
// Fetch Zone DID Docs from addresses, and geojson from DID docs:
// We should refactor this to maintain an up-to-date list of zones in the server
// Listening for an event from Ethereum. When an event happens, push the new zone
// to the list and push the new list to the browser. Auto-update in the browser
// when a new zone is registered.
zoneDIDDocs = await fetchDIDsAndGeometries(zoneAddresses, zoneContract);
//console.log('Zone DID Docs and geometries loaded', zoneDIDDocs);
zoneDIDDocs.map((did) => {
did.service.map((zone) => {
if (zone.geojson.type == 'FeatureCollection') {
turfPolygons.push(zone.geojson.features[0]);
} else if (zone.geojson.type == 'Feature') {
turfPolygons.push(zone.geojson)
}
});
});
res.send(zoneDIDDocs)
})
server.get('/api/getAllPoints', async (req, res) => {
let antenna = new Antenna.default("http://api.testnet.iotex.one:80");
let numberOfRegisteredVehicles;
// Get total number of registered vehicles
try {
numberOfRegisteredVehicles = await antenna.iotx.readContractByMethod({
from: "io1y3cncf05k0wh4jfhp9rl9enpw9c4d9sltedhld",
abi: VEHICLE_REGISTER_ABI,
contractAddress: VEHICLE_REGISTER_ADDRESS,
method: "numberOfRegisteredVehicles"
},
0);
numberOfRegisteredVehicles = numberOfRegisteredVehicles.toString();
} catch (err) {
console.log(err)
}
// Generates a route near LONDON right now ...
// NEXT up: pull random Terrestrial polygon from the zones and generate a route through that ...
let sampleRoutes = []
for (let i = 0; i < numberOfRegisteredVehicles; i++) {
let route = await generateRandomRoute(turfPolygons[Math.floor(Math.random() * turfPolygons.length)], mapboxtoken)
sampleRoutes.push(route);
}
let samplePts = sampleRoutes.map((line) => line.geometry.coordinates);
let points = samplePts[0].map((col, i) => samplePts.map(function (row) {
return {"coords": row[i]}
}));
res.send(points)
})
server.get('/api/getTotalStaked', async (req, res) => {
let meta;
try {
meta = await axios.post("https://testnet.iotexscan.io/api-gateway", {
operationName: null,
variables: {},
query: `
{
getAccount (address: "${VEHICLE_REGISTER_ADDRESS}"){
accountMeta {
balance
}
}
}
`
})
res.send({
totalStaked: meta.data.data.getAccount.accountMeta.balance / 1e18
})
} catch (e) {
console.log(e)
}
})
if (process.env.NODE_ENV === 'production') {
// Anything that doesn't match the above, send back index.html
server.get('*', (req, res) => {
res.sendFile(path.join(__dirname + '/build/index.html'))
})
}