-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProofOfExistence.app.js
More file actions
338 lines (295 loc) · 8.26 KB
/
ProofOfExistence.app.js
File metadata and controls
338 lines (295 loc) · 8.26 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
var express = require('express');
var connect = require('connect');
var path = require('path');
var logger = require('morgan');
var bodyParser = require('body-parser');
const crypto = require('crypto');
/* Express*/
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// app.use(express.static(path.join(__dirname, 'public')));
/* --- connect-rest */
var Rest = require('connect-rest');
// initial configuration of connect-rest. all-of-them are optional.
// default context is /api, all services are off by default
var options = {
context: '/api'
};
var rest = Rest.create(options);
app.use(rest.processRequest());
app.disable('x-powered-by');
/* --- Variables: */
var apiKey = process.env.APIKEY;
/* --- web3.js: */
Web3 = require('web3');
// see: https://github.com/ethereum/wiki/wiki/JavaScript-API#adding-web3
// To make sure you don't overwrite the already set provider , check first if the web3 is available:
var web3;
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}
// create contract instance:
var abi = [{
"constant": true,
"inputs": [],
"name": "created",
"outputs": [{
"name": "",
"type": "string"
}],
"type": "function"
}, {
"constant": true,
"inputs": [{
"name": "",
"type": "bytes32"
}],
"name": "sha3Docs",
"outputs": [{
"name": "docIndex",
"type": "uint256"
}, {
"name": "publisher",
"type": "string"
}, {
"name": "publishedOnUnixTime",
"type": "uint256"
}, {
"name": "publishedInBlockNumber",
"type": "uint256"
}, {
"name": "docText",
"type": "string"
}, {
"name": "sha256Hash",
"type": "bytes32"
}, {
"name": "sha3Hash",
"type": "bytes32"
}],
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "docIndex",
"outputs": [{
"name": "",
"type": "uint256"
}],
"type": "function"
}, {
"constant": true,
"inputs": [],
"name": "manager",
"outputs": [{
"name": "",
"type": "address"
}],
"type": "function"
}, {
"constant": true,
"inputs": [{
"name": "",
"type": "uint256"
}],
"name": "indexedDocs",
"outputs": [{
"name": "docIndex",
"type": "uint256"
}, {
"name": "publisher",
"type": "string"
}, {
"name": "publishedOnUnixTime",
"type": "uint256"
}, {
"name": "publishedInBlockNumber",
"type": "uint256"
}, {
"name": "docText",
"type": "string"
}, {
"name": "sha256Hash",
"type": "bytes32"
}, {
"name": "sha3Hash",
"type": "bytes32"
}],
"type": "function"
}, {
"constant": true,
"inputs": [{
"name": "",
"type": "bytes32"
}],
"name": "sha256Docs",
"outputs": [{
"name": "docIndex",
"type": "uint256"
}, {
"name": "publisher",
"type": "string"
}, {
"name": "publishedOnUnixTime",
"type": "uint256"
}, {
"name": "publishedInBlockNumber",
"type": "uint256"
}, {
"name": "docText",
"type": "string"
}, {
"name": "sha256Hash",
"type": "bytes32"
}, {
"name": "sha3Hash",
"type": "bytes32"
}],
"type": "function"
}, {
"constant": false,
"inputs": [{
"name": "_publisher",
"type": "string"
}, {
"name": "_docText",
"type": "string"
}],
"name": "addDoc",
"outputs": [{
"name": "",
"type": "bytes32"
}],
"type": "function"
}, {
"inputs": [],
"type": "constructor"
}, {
"anonymous": false,
"inputs": [{
"indexed": false,
"name": "docIndex",
"type": "uint256"
}, {
"indexed": false,
"name": "publisher",
"type": "string"
}, {
"indexed": false,
"name": "publishedOnUnixTime",
"type": "uint256"
}],
"name": "DocumentAdded",
"type": "event"
}];
var contractAddress = "0x80f84866d4872f1ea412ddf10e2ed7af0b8ca8fb";
var contractManagerAddress = "0x4412041c61EF9a1783Eb3F8bC526aE5d05588cDa";
var ProofOfExistence = web3.eth.contract(abi).at(contractAddress);
console.log("contract at address: " + contractAddress);
console.log(JSON.stringify(ProofOfExistence));
/* --- API routes: */
// 1) --- add document
rest.post("/proofofexistence-add", function(request, content, callback) {
console.log("request.body: " + JSON.stringify(request.body));
console.log('content: ' + JSON.stringify(content));
console.log("request.headers: " + JSON.stringify(request.headers));
console.log("request.headers.apikey: " + request.headers.apikey);
var resObj = {}; // obj to return
if (request.headers.apikey === apiKey || content.apikey === apiKey) {
var transactionObject = {
from: contractManagerAddress, // or web3.eth.accounts[0] //
// to: ProofOfExistence.address,
gas: 4000000
// nonce: web3.eth.getTransactionCount(contractManagerAddress)
};
resObj.txHash =
ProofOfExistence.addDoc.sendTransaction( // <<< -- sendTransaction
content.publisher,
content.text,
transactionObject
);
console.log("resObj.txHash: " + resObj.txHash);
resObj.tx = web3.eth.getTransaction(resObj.txHash);
console.log("resObj.tx: " + JSON.stringify(resObj.tx));
resObj.storedText = content.text;
resObj.storedTextSha256 = crypto.createHash('sha256').update(content.text).digest('hex');
callback(null, resObj); // <<< ----------- this is success response
} else {
resObj.error = "not authorized to call API";
callback(null, resObj);
}
});
// 2) --- get document by sha256 hash
rest.post("/proofofexistence-get", function(request, content, callback) {
console.log('Received JSON object:' + JSON.stringify(content));
var resObj = {}; // obj to return
if (request.headers.apikey === apiKey || content.apikey === apiKey) {
if (!content.sha256 || content.sha256.length < 64) {
resObj.error = "hash invalid or empty";
callback(null, resObj);
} else {
var txObjForCall = {
// from: contractManagerAddress,
// to: ProofOfExistence.address
// ,
// gas: 1000000,
// nonce: web3.eth.getTransactionCount(contractManagerAddress)
};
console.log("GET transactionObject: " + JSON.stringify(txObjForCall));
resObj.storedDoc = // array (struct Doc)
ProofOfExistence.sha256Docs.call(
// ProofOfExistence.sha256Docs(
content.sha256
// ,
// txObjForCall
);
resObj.receivedSha256 = content.sha256;
console.log("resObj: " + JSON.stringify(resObj));
callback(null, resObj);
}
} else {
resObj.error = "not authorized to call API";
callback(null, resObj);
}
});
/* --- Express routes */
// app.use('/', routes);
// app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
// development error handler
// will print stacktrace
// if (app.get('env') === 'development') {
// app.use(function (err, req, res, next) {
// res.status(err.status || 500);
// res.render('error', {
// message: err.message,
// error: err
// });
// });
// }
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;