-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
267 lines (225 loc) · 7.88 KB
/
app.js
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
"use strict"
const Web3 = require('web3')
const express = require('express')
const https = require('https');
const fs = require('fs');
const app = express()
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded b
app.use(require('helmet')()); // security for https
// Set up express server here
const options = {
cert: fs.readFileSync('../../../../etc/letsencrypt/live/api.aigang.network/fullchain.pem'),
key: fs.readFileSync('../../../../etc/letsencrypt/live/api.aigang.network/privkey.pem')
};
const web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
var obj = JSON.parse(fs.readFileSync('./build/contracts/BatteryInsurancePolicy.json', 'utf8'));
var abiArray = obj.abi;
// Insurance policy contract address Ropsten testnet
var contractAddress = '0x554ab5f5fe7006223c2dd321b420e9a05d48ec93';
var policyContract = web3.eth.contract(abiArray).at(contractAddress);
var adminAccount = '0x2033d81c062de642976300c6eabcba149e4372be';
var adminPass = '';
var apiKey = '';
app.get('/balance/:address', function (req, res) {
var balance = web3.eth.getBalance(req.params.address).toNumber()
var balanceInEth = balance / 1000000000000000000;
res.send('' + balanceInEth);
})
app.post('/sendTestnetEthers/:address', function (req, res) {
var account = req.params.address;
var receivedApiKey = req.body.apiKey;
if(receivedApiKey != apiKey) {
res.status(401);
res.send();
return;
}
web3.personal.unlockAccount(account, req.body.password, 4, function(err, accResult) {
if(accResult) {
// unlocking admin account for ethers sending
web3.personal.unlockAccount(adminAccount, adminPass, 4, function(err, adminAccResult) {
web3.eth.sendTransaction({value: 50000000000000000,
gas: 2000000, from: adminAccount, to: account}, function(err, result) {
if(err) {
console.log(err);
res.send(false);
} else {
var txId = result;
res.send('' + txId);
}
});
});
} else {
console.log(err);
res.send(false);
}
});
})
app.post('/register', function (req, res) {
var receivedApiKey = req.body.apiKey;
if(receivedApiKey != apiKey) {
res.status(401);
res.send();
return;
}
// password hash
if(req.body.password) {
// Password should be used the one provided by user and secured
web3.personal.newAccount(req.body.password, function(err, acc) {
if(!err) {
web3.personal.unlockAccount(acc, req.body.password, 2, function(err, result) {
if(result) {
res.send(acc);
} else {
res.status(400);
res.send('' + err);
}
});
}
else {
res.status(400);
res.send('' + err);
}
});
}
else {
res.status(400);
res.send('' + err);
}
});
app.post('/insurancePrice/:address', function (req, res) {
var account = req.params.address;
var deviceBrand = req.body.deviceBrand;
var deviceYear = req.body.deviceYear;
var wearLevel = req.body.wearLevel;
var region = req.body.region;
var result = policyContract.policyPrice(deviceBrand, deviceYear, wearLevel, region);
var priceInEth = result / 1000000000000000000;
res.send('' + priceInEth);
})
app.get('/maxPayout', function (req, res) {
var account = req.params.address;
var result = policyContract.maxPayout.call();
var payoutInEth = result / 1000000000000000000;
res.send('' + payoutInEth);
})
app.post('/insure/:address/', function (req, res) {
var receivedApiKey = req.body.apiKey;
if(receivedApiKey != apiKey) {
res.status(401);
res.send();
return;
}
var account = req.params.address;
var itemId = req.body.itemId;
var deviceBrand = req.body.deviceBrand;
var deviceYear = req.body.deviceYear;
var wearLevel = req.body.wearLevel;
var region = req.body.region;
var policyMonthlyPayment = Math.round(policyContract.policyPrice(deviceBrand, deviceYear, wearLevel, region) / 12);
console.log(itemId + '' + deviceBrand+ '' + deviceYear+ '' +region + '' + policyMonthlyPayment);
web3.personal.unlockAccount(account, req.body.password, 2, function(err, result) {
if(result) {
policyContract.insure(itemId, deviceBrand, deviceYear, wearLevel, region,
{value: policyMonthlyPayment, gas: 300000, from: account},
function(err, result) {
if(err) {
console.log(err);
res.status(400);
res.send('1' + err);
} else {
var txIdinsure = result;
res.send(txIdinsure);
let filter = web3.eth.filter('latest')
filter.watch(function(error, result) {
console.log(error);
if (!error) {
let confirmedBlock = web3.eth.getBlock(web3.eth.blockNumber - 1)
if (confirmedBlock.transactions.length > 0) {
let transaction = web3.eth.getTransaction(txIdinsure);
if (transaction && transaction.from == account) {
//---- confirmation transaction is needed from OWNER , TODO: refactor it and move to other file
web3.personal.unlockAccount(adminAccount, adminPass, 2, function(err, result) {
if(result) {
policyContract.confirmPolicy(account, {gas: 200000, from: adminAccount}, function(err, result) {
if(err) {
console.log('' + err);
// res.status(400);
// res.send('2' + err);
} else {
// res.send(txIdinsure);
console.log('success confirmation');
}
});
} else {
console.log('' + err);
}
});
//-------
} else{
res.status(400);
res.send('4' + error);
}
filter.stopWatching();
}
}
});
}
});
} else {
res.status(400);
res.send('5' + err);
}
});
})
app.get('/policyEndDate/:address', function (req, res) {
var account = req.params.address;
var result = policyContract.getPolicyEndDateTimestamp({from: account});
res.send('' + result);
})
app.get('/nextPayment/:address', function (req, res) {
var account = req.params.address;
var result = policyContract.getPolicyNextPayment({from: account});
res.send('' + result);
})
app.get('/claimed/:address', function (req, res) {
var account = req.params.address;
var result = policyContract.claimed({from: account});
res.send('' + result);
})
// Not secure, it should come trusted authority, probably as an Oracle directly to smart contract
app.post('/claim/:address', function (req, res) {
var receivedApiKey = req.body.apiKey;
if(receivedApiKey != apiKey) {
res.status(401);
res.send();
return;
}
var account = req.params.address;
var wearLevel = req.body.wearLevel;
web3.personal.unlockAccount(account, req.body.password, 2, function(err, result) {
if(result) {
policyContract.claim(wearLevel, {gas: 300000, from: account}, function(err, result) {
if(err) {
console.log(err);
res.status(400);
res.send('' + false);
} else {
var txId = result;
res.send(txId);
}
});
} else {
res.status(400);
res.send('' + false);
}
});
})
app.get('/', function (req, res) {
res.send('Welcome to API. Specs can be found: ');
})
var server = https.createServer(options, app);
server.listen(3000, function () {
console.log('Example app listening on port 3000 and https!')
})