-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
373 lines (321 loc) · 9.54 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
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
var express = require('express');
var request = require('request');
var sqlite3 = require('sqlite3').verbose();
var fs = require('fs');
var oip041 = require('oip-npm');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
var fs = require("fs");
var db;
// Include Florincoin RPC connection
var oip;
function initDb() {
var dbfile = (settings.datadir || __dirname) + '/tpb.db';
var exists = fs.existsSync(dbfile);
db = new sqlite3.Database(dbfile);
db.serialize(function() {
if(!exists) {
try {
// Create the logs table
db.run("CREATE TABLE log (id INTEGER PRIMARY KEY NOT NULL, timestamp INTEGER NOT NULL, type VARCHAR NOT NULL, message VARCHAR NOT NULL, extrainfo VARCHAR);");
} catch (error) {
console.log("Error creating database tables.");
}
}
});
}
var settings;
app.get('/', function (req, res) {
res.status(403);
res.send("");
});
app.post('/add', function (req, res) {
var response;
var status;
var sent = false;
var ip = req.connection.remoteAddress;
// Verify the IP is allowed
if (settings.allow_ip != '*' && ip != settings.allow_ip){
console.log(ip);
status = 403;
response = generateResponseMessage(false, "IP Not Allowed");
} // Test API Keys
else if (req.body.api_key && req.body['api_key'] == settings['api_key']){
// Verify artifact data is there, if not, throw an error.
if (!req.body.artifact){
status = 400;
response = generateResponseMessage(false, "No artifact data detected in POST");
} else {
var oipArtifact = req.body.artifact;
oip.publishArtifact(oipArtifact, function(oipRes){
if (!sent){
try{
if (oipRes.success)
res.status(200);
else
res.status(400);
console.log(oipRes);
log("info", oipRes);
res.send(oipRes);
} catch(e) {}
}
})
}
} else {
status = 403;
response = generateResponseMessage(false, "Incorrect API Key");
}
// This will check if there was an error/response before the callback and get it sent out.
if (response || status){
// Check status
if(status == 403){
// Incorrect API key, log this.
log("warning", response, "IP: " + ip)
} else {
log("info", response);
}
// Mark sent so we don't return twice.
sent = true;
res.status(status)
console.log(response);
res.send(response);
}
});
app.post('/edit', function (req, res) {
var response;
var status;
var sent = false;
var ip = req.connection.remoteAddress;
// Verify the IP is allowed
if (settings.allow_ip != '*' && ip != settings.allow_ip){
console.log(ip);
status = 403;
response = generateResponseMessage(false, "IP Not Allowed");
} // Test API Keys
else if (req.body.api_key && req.body['api_key'] == settings['api_key']){
// Verify artifact data is there, if not, throw an error.
if (!req.body.artifact){
status = 400;
response = generateResponseMessage(false, "No artifact data detected in POST");
} else {
var oipArtifact = req.body.artifact;
oip.editArtifact(oipArtifact, function(oipRes){
if (!sent){
try{
if (oipRes.success)
res.status(200);
else
res.status(400);
console.log(oipRes);
log("info", oipRes);
res.send(oipRes);
} catch(e) {}
}
})
}
} else {
status = 403;
response = generateResponseMessage(false, "Incorrect API Key");
}
// This will check if there was an error/response before the callback and get it sent out.
if (response || status){
// Check status
if(status == 403){
// Incorrect API key, log this.
log("warning", response, "IP: " + ip)
} else {
log("info", response);
}
// Mark sent so we don't return twice.
sent = true;
res.status(status)
console.log(response);
res.send(response);
}
});
app.post('/remove', function (req, res) {
var response;
var status;
var ip = req.connection.remoteAddress;
// Verify the IP is allowed
if (settings.allow_ip != '*' && ip != settings.allow_ip){
status = 403;
response = generateResponseMessage(false, "IP Not Allowed");
} // Test API Keys
else if (req.body.api_key && req.body['api_key'] == settings['api_key']){
// Verify artifact data is there, if not, throw an error.
if (!req.body.artifact){
status = 400;
response = generateResponseMessage(false, "No artifact data detected in POST");
} else {
var oipArtifact = req.body.artifact;
oipArtifact = JSON.parse(oipArtifact);
oip.deactivateArtifact(oipArtifact.txid, oipArtifact.artifactTitle, function(oipRes){
try{
if (oipRes.success)
res.status(200);
else
res.status(400);
console.log(oipRes);
log("info", oipRes);
res.send(oipRes);
} catch(e) {}
})
}
} else {
status = 403;
response = generateResponseMessage(false, "Incorrect API Key");
}
if (response || status){
// Check status
if(status == 403){
// Incorrect API key, log this.
log("warning", response, "IP: " + ip)
} else {
log("info", response);
}
// Mark sent so we don't return twice.
sent = true;
res.status(status)
console.log(response);
res.send(response);
}
});
app.post('/transfer', function (req, res) {
var response;
var status;
var sent = false;
var ip = req.connection.remoteAddress;
// Verify the IP is allowed
if (settings.allow_ip != '*' && ip != settings.allow_ip){
console.log(ip);
status = 403;
response = generateResponseMessage(false, "IP Not Allowed");
} // Test API Keys
else if (req.body.api_key && req.body['api_key'] == settings['api_key']){
// Verify artifact data is there, if not, throw an error.
if (!req.body.artifact){
status = 400;
response = generateResponseMessage(false, "No artifact data detected in POST");
} else {
var oipArtifact = req.body.artifact;
if (typeof oipArtifact == "string"){
try {
oipArtifact = JSON.parse(oipArtifact);
} catch(e) {
status = 400;
response = generateResponseMessage(false, "You must submit valid JSON!");
}
}
if (!response){
oip.transferArtifact(oipArtifact.txid, oipArtifact.originalOwnerAddress, oipArtifact.newOwnerAddress, function(oipRes){
if (!sent){
try{
if (oipRes.success)
res.status(200);
else
res.status(400);
console.log(oipRes);
log("info", oipRes);
res.send(JSON.stringify(oipRes));
} catch(e) {}
}
})
}
}
} else {
status = 403;
response = generateResponseMessage(false, "Incorrect API Key");
}
// This will check if there was an error/response before the callback and get it sent out.
if (response || status){
// Check status
if(status == 403){
// Incorrect API key, log this.
log("warning", response, "IP: " + ip)
} else {
log("info", response);
}
// Mark sent so we don't return twice.
sent = true;
res.status(status)
console.log(response);
res.send(response);
}
});
function log(type, message, extrainfo, table){
if (!extrainfo)
extrainfo = '';
if (!table)
table = 'log';
if (table == 'log')
var cols = '(timestamp, type, message, extrainfo)';
// Store log in database
db.serialize(function() {
db.run("INSERT INTO " + table + " " + cols + " VALUES (" + parseInt(Date.now() / 1000) + ",'" + type + "', '" + message + "', '" + extrainfo + "');");
});
}
function throwError(message, extraInfo){
if (!extraInfo)
extraInfo = '';
log('error', message, extraInfo, 'log');
console.log(message);
console.log(extraInfo);
}
function loadConfig(){
if (!fs.existsSync(__dirname + '/settings.cfg'))
copyFile(__dirname + '/settings.example.cfg', __dirname + '/settings.cfg');
var data = fs.readFileSync(__dirname + '/settings.cfg');
try {
if (data == ''){
copyFile(__dirname + '/settings.example.cfg', __dirname + '/settings.cfg');
var data = fs.readFileSync(__dirname + '/settings.cfg');
}
settings = JSON.parse(data);
} catch(e) {
throwError('Error loading Config', 'There was an error loading the config, please double check that it is correctly written and valid JSON!\n' + e);
}
}
function saveConfig(){
try {
fs.writeFileSync(__dirname + '/settings.cfg', JSON.stringify(settings, null, 4));
} catch (e) {
throwError('Error writing config', e);
}
}
function copyFile(source, target) {
try {
var data = fs.readFileSync(source);
fs.writeFileSync(target, data);
} catch (e) {
throwError('Error creating default settings file from settings.example.cfg', e);
}
}
function generateResponseMessage(success, message) {
return '{ "success": ' + success + (success ? ', "message": "' : ', "error": "') + message + '"}';
}
loadConfig();
initDb();
// Create oip-npm link
oip = new oip041({
host: settings.florincoin_rpc_ip,
port: settings.florincoin_rpc_port,
username: settings.florincoin_username,
password: settings.florincoin_password
});
/*
// Example message signing
client.signMessage('FD6qwMcfpnsKmoL2kJSfp1czBMVicmkK1Q', 'TokenlyPublisherBridgeTestPublisher-FD6qwMcfpnsKmoL2kJSfp1czBMVicmkK1Q-1480201268', function(err, signature) {
if (err) console.log(err);
// Example message publishing
client.sendToAddress('FD6qwMcfpnsKmoL2kJSfp1czBMVicmkK1Q', 0.0001, "", "", '{"alexandria-publisher":{"name":"TokenlyPublisherBridgeTestPublisher","address":"FD6qwMcfpnsKmoL2kJSfp1czBMVicmkK1Q","timestamp":1480201268,"emailmd5":"","bitmessage":""},"signature":"' + signature + '"}', function(err, msg) {
if (err) console.log(err);
console.log('Tx: ' + msg);
});
});*/
app.listen(3200, function () {
console.log('TokenlyPublisherBridge listening on port 3200!');
log('info', 'Started up TokenlyPublisherBridge on port 3200');
});