-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiServer.js
674 lines (617 loc) · 25.7 KB
/
ApiServer.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
var express = require('express');
var bodyParser = require('body-parser');
var UINT64 = require('cuint').UINT64;
var mutexify = require('mutexify');
var cors = require('cors');
function ApiServer(opts, libs) {
var db = libs.db;
var mempool = libs.mempool;
var marker = libs.marker;
var rpc = libs.rpc;
var bitcoin = libs.bitcoin;
var network = libs.network;
var lock = mutexify();
var lock_count = 0;
var lock_maxcount = 5;
var self = this;
var app;
var error_code = {
SUCCESS: 0,
ERROR: 1,
SYNCING: 2,
ROLLBACKING: 3,
ROLLBACKED: 4,
UNKNOWN_APIKEY: 5,
BUSY: 6,
TOO_MANY: 7,
TOO_HIGH: 8
};
this.status_code = {
SYNCED: 0,
SYNCING: 2,
ROLLBACKING: 3
}
var errval = self.status_code.SYNCING;
var status_sync = self.status_code.SYNCING;
this.set_status = function(status_code) {
errval = status_code;
status_sync = status_code;
}
var height = null;
this.set_height = function(true_height) {
height = true_height;
}
var tx_sequence = 0;
this.set_tx_sequence = function(true_tx_sequence) {
tx_sequence = true_tx_sequence;
}
this.start = function() {
app = express();
app.use(bodyParser.json({limit: "50mb"}));
app.use(bodyParser.urlencoded({limit: "50mb", extended: true, parameterLimit: 50000}));
var router = express.Router();
function conv_uint64(uint64_val) {
strval = uint64_val.toString();
val = parseInt(strval);
if(val > Number.MAX_SAFE_INTEGER) {
return strval;
}
return val;
}
function utxo_query_filter(query) {
var options = {};
if(query.gte != null) {
options.gte = parseInt(query.gte);
} else if(query.gt != null) {
options.gt = parseInt(query.gt);
}
if(query.lte != null) {
options.lte = parseInt(query.lte);
if(options.lte > tx_sequence) {
options.lte = tx_sequence;
}
} else if(query.lt != null) {
options.lt = parseInt(query.lt);
if(options.lt > tx_sequence + 1) {
options.lt = tx_sequence + 1;
}
} else {
options.lte = tx_sequence;
}
if(query.limit != null) {
options.limit = parseInt(query.limit);
}
if(query.seqbreak != null) {
options.seqbreak = parseInt(query.seqbreak);
}
if(query.reverse != null) {
options.reverse = parseInt(query.reverse);
}
if(query.unconf != null) {
options.unconf = parseInt(query.unconf);
}
return Object.keys(options).length > 0 ? options : null;
}
function addrlog_query_filter(query) {
var options = {};
if(query.gte != null) {
options.gte = parseInt(query.gte);
} else if(query.gt != null) {
options.gt = parseInt(query.gt);
}
if(query.lte != null) {
options.lte = parseInt(query.lte);
if(options.lte > tx_sequence) {
options.lte = tx_sequence;
}
} else if(query.lt != null) {
options.lt = parseInt(query.lt);
if(options.lt > tx_sequence + 1) {
options.lt = tx_sequence + 1;
}
} else {
options.lte = tx_sequence;
}
if(query.limit != null) {
options.limit = parseInt(query.limit);
}
if(query.seqbreak != null) {
options.seqbreak = parseInt(query.seqbreak);
}
if(query.reverse != null) {
options.reverse = parseInt(query.reverse);
}
return Object.keys(options).length > 0 ? options : null;
}
function bech32_address_filter(address) {
for(var i in network.bech32_extra) {
if(address.startsWith(network.bech32_extra[i])) {
var decode = null;
try {
decode = bitcoin.address.fromBech32(address);
} catch(e) {}
if(decode) {
if(decode.version === 0) {
if(decode.data.length === 20) {
return bitcoin.payments.p2wpkh({hash: decode.data, network: network}).address;
}
if(decode.data.length === 32) {
return bitcoin.payments.p2wsh({hash: decode.data, network: network}).address;
}
}
}
return address;
}
}
return address;
}
function bech32_address_prefix(address, network) {
var decode = null;
try {
decode = bitcoin.address.fromBech32(address);
} catch(e) {}
if(decode) {
if(decode.version === 0) {
if(decode.data.length === 20) {
return bitcoin.payments.p2wpkh({hash: decode.data, network: network}).address;
}
if(decode.data.length === 32) {
return bitcoin.payments.p2wsh({hash: decode.data, network: network}).address;
}
}
}
return address;
}
async function get_addr(address) {
address = bech32_address_filter(address);
var unconfs = mempool.unconfs(address);
if(unconfs.txouts || unconfs.spents) {
var utxos = await db.getUnspents(address);
var balance = UINT64(0);
var unconf_out = UINT64(0);
var unconf_in = UINT64(0);
var utxo_count = 0;
var txids = {};
if(utxos.length > 0) {
for(var i in utxos) {
var utxo = utxos[i];
balance.add(utxo.value);
txids[utxo.txid] = 1;
utxo_count++;
}
}
if(unconfs.txouts) {
var mempool_txouts = unconfs.txouts;
mempool_txouts = mempool_txouts.filter(function(txout) {
return !txids[txout.txid];
});
for(var i in mempool_txouts) {
var txout = mempool_txouts[i];
unconf_in.add(txout.value);
txids[txout.txid] = 1;
utxo_count++;
}
}
if(unconfs.spents) {
var mempool_spents = unconfs.spents;
mempool_spents = mempool_spents.filter(function(spent) {
return txids[spent.txid];
});
for(var i in mempool_spents) {
var spent = mempool_spents[i];
unconf_out.add(spent.value);
utxo_count--;
}
}
if(unconfs.warning) {
return {
balance: conv_uint64(balance),
utxo_count: utxo_count,
unconf: {out: conv_uint64(unconf_out), in: conv_uint64(unconf_in), warning: nconfs.warning}
};
}
return {
balance: conv_uint64(balance),
utxo_count: utxo_count,
unconf: {out: conv_uint64(unconf_out), in: conv_uint64(unconf_in)}
};
} else {
var addrval = await db.getAddrval(address);
if(addrval == null) {
return {};
} else {
return {
balance: conv_uint64(addrval.value),
utxo_count: addrval.utxo_count
};
}
}
}
async function get_utxos(address, options) {
address = bech32_address_filter(address);
var utxos = await db.getUnspents(address, options);
var unconfs = mempool.unconfs(address);
if(unconfs.txouts && options.unconf) {
var txids = {};
for(var i in utxos) {
var utxo = utxos[i];
txids[utxo.txid] = 1;
}
var mempool_txouts = unconfs.txouts;
mempool_txouts = mempool_txouts.filter(function(txout) {
return !txids[txout.txid];
});
for(var i in mempool_txouts) {
var txout = mempool_txouts[i];
txids[txout.txid] = 1;
}
utxos = utxos.concat(mempool_txouts);
}
if(unconfs.spents) {
var mempool_spent_txids = {};
for(var i in unconfs.spents) {
var spent = unconfs.spents[i];
mempool_spent_txids[spent.txid] = 1;
}
utxos = utxos.filter(function(utxo) {
return !mempool_spent_txids[utxo.txid];
});
}
for(var i in utxos) {
utxos[i].value = conv_uint64(utxos[i].value);
}
return utxos;
}
async function get_addrlogs(address, options) {
address = bech32_address_filter(address);
var addrlogs = await db.getAddrlogs(address, options);
for(var i in addrlogs) {
var addrlog = addrlogs[i];
addrlog.value = conv_uint64(addrlog.value);
var tx = await db.getTx(addrlog.txid);
addrlog.height = tx.height;
addrlog.time = tx.time;
}
return addrlogs;
}
async function get_unconf(address) {
address = bech32_address_filter(address);
var unconfs = mempool.unconfs(address);
var ret_unconfs = {};
if(unconfs.txouts) {
ret_unconfs.txouts = [];
}
if(unconfs.spents) {
ret_unconfs.spents = [];
}
for(var i in unconfs.txouts) {
var txout = unconfs.txouts[i];
ret_unconfs.txouts.push({txid: txout.txid, n: parseInt(txout.n), value: conv_uint64(txout.value)});
}
for(var i in unconfs.spents) {
var spent = unconfs.spents[i];
ret_unconfs.spents.push({txid: spent.txid, n: parseInt(spent.n), value: conv_uint64(spent.value), txid_out: spent.txid_out});
}
return ret_unconfs
}
// GET - /addr/{addr}
router.get('/addr/:addr', async function(req, res) {
res.json({err: errval, res: await get_addr(req.params.addr)});
});
// POST - {addrs: [addr1, addr2, ..., addrN]}
router.post('/addrs', async function(req, res) {
var addrs = req.body.addrs;
var balances = [];
for(var i in addrs) {
balances.push(await get_addr(addrs[i]));
}
res.json({err: errval, res: balances});
});
// GET - /utxo/{addr}
router.get('/utxo/:addr', async function(req, res) {
res.json({err: errval, res: await get_utxos(req.params.addr, utxo_query_filter(req.query))});
});
// POST - {addrs: [addr1, addr2, ..., addrN]}
router.post('/utxos', async function(req, res) {
var addrs = req.body.addrs;
var utxos = [];
for(var i in addrs) {
utxos.push(await get_utxos(addrs[i], utxo_query_filter(req.query)));
}
res.json({err: errval, res: utxos});
});
// GET - /addrlog/{addr}
router.get('/addrlog/:addr', async function(req, res) {
res.json({err: errval, res: await get_addrlogs(req.params.addr, addrlog_query_filter(req.query))});
});
// POST - {addrs: [addr1, addr2, ..., addrN]}
router.post('/addrlogs', async function(req, res) {
var addrs = req.body.addrs;
var multilogs = [];
for(var i in addrs) {
multilogs.push(await get_addrlogs(addrs[i], addrlog_query_filter(req.query)));
}
res.json({err: errval, res: multilogs});
});
// GET - /mempool
router.get('/mempool', async function(req, res) {
res.json({err: errval, res: mempool.stream_unconfs()});
});
// GET - /unconf/{addr}
router.get('/unconf/:addr', async function(req, res) {
res.json({err: errval, res: await get_unconf(req.params.addr)});
});
// POST - {unconfs: [addr1, addr2, ..., addrN]}
router.post('/unconfs', async function(req, res) {
var addrs = req.body.addrs;
var unconfs = [];
for(var i in addrs) {
unconfs.push(await get_unconf(addrs[i]));
}
res.json({err: errval, res: unconfs});
});
// GET - /marker/{apikey}
router.get('/marker/:apikey', async function(req, res) {
var apikey = req.params.apikey;
if(opts.apikeys[apikey]) {
var marker = await db.getMarker(apikey) || {sequence: 0};
if(marker.rollbacking) {
res.json({err: error_code.ROLLBACKING, res: {sequence: marker.sequence, last: tx_sequence}});
} else if(marker.rollback) {
res.json({err: error_code.ROLLBACKED, res: {sequence: marker.sequence, last: tx_sequence}});
} else {
res.json({err: error_code.SUCCESS, res: {sequence: marker.sequence, last: tx_sequence}});
}
} else {
res.json({err: error_code.UNKNOWN_APIKEY});
}
});
// POST - {apikey: apikey, sequence: sequence}
router.post('/marker', async function(req, res) {
var apikey = req.body.apikey;
var sequence = req.body.sequence;
if(opts.apikeys[apikey]) {
if(!marker.rollbacking) {
var check_marker = await db.getMarker(apikey);
if(check_marker != null && !check_marker.rollback) {
if(sequence > tx_sequence) {
await db.setMarker(apikey, sequence, 0);
res.json({err: error_code.TOO_HIGH, res: tx_sequence});
} else {
await db.setMarker(apikey, sequence, 0);
res.json({err: error_code.SUCCESS});
}
} else if(check_marker == null || check_marker.sequence == sequence) {
await db.setMarker(apikey, sequence, 0);
res.json({err: error_code.SUCCESS});
} else {
res.json({err: error_code.ROLLBACKED, res: check_marker.sequence});
}
} else {
res.json({err: error_code.ROLLBACKING});
}
} else {
res.json({err: error_code.UNKNOWN_APIKEY});
}
});
// GET - /height
router.get('/height', async function(req, res) {
res.json({err: height == null ? error_code.ERROR : error_code.SUCCESS, res: height});
});
// POST - {rawtx: rawtx}
router.post('/send', async function(req, res) {
if(lock_count >= lock_maxcount) {
res.json({err: error_code.BUSY});
console.log('\rWARNING: send tx busy');
return;
}
lock_count++;
lock(async function(release) {
var rawtx = req.body.rawtx;
var ret_rawtx = await rpc.sendRawTransaction(rawtx);
release(function() {
lock_count--;
if(ret_rawtx.code) {
res.json({err: error_code.ERROR, res: ret_rawtx});
console.log('\rERROR: sendRawTransaction code=' + ret_rawtx.code + ' message=' + ret_rawtx.message);
} else {
res.json({err: error_code.SUCCESS, res: ret_rawtx});
console.log('\rINFO: sendRawTransaction txid=' + ret_rawtx);
}
});
});
});
function get_script_addresses(script, network) {
try {
var address = bitcoin.address.fromOutputScript(script, network);
return [address];
} catch(ex) {}
var addresses = [];
var chunks = bitcoin.script.decompile(script);
for(var k in chunks) {
var chunk = chunks[k];
if(Buffer.isBuffer(chunk) && chunk.length !== 1) {
try {
var address = bitcoin.payments.p2pkh({ pubkey: chunk, network: network }).address;
addresses.push(address);
} catch(ex) {}
}
}
if(addresses.length > 0) {
return addresses;
}
return null;
}
// GET - /tx/{txid}
router.get('/tx/:txid', async function(req, res) {
if(lock_count >= lock_maxcount) {
res.json({err: error_code.BUSY});
console.log('\rWARNING: get tx busy');
return;
}
lock_count++;
lock(async function(release) {
var txid = req.params.txid;
var ret_rawtx = await rpc.getRawTransaction(txid);
var rawblock = null;
if(ret_rawtx.code && !opts.db.rawblocks) {
var dbtx = await db.getTx(txid);
if(dbtx) {
var height = dbtx.height;
var db_hash = await db.getBlockHash(height);
rawblock = await rpc.getBlock(db_hash.hash, false);
}
}
release(async function() {
lock_count--;
var dbtx = null;
if(ret_rawtx.code && rawblock == null) {
if(opts.db.rawblocks) {
dbtx = await db.getTx(txid);
if(dbtx) {
var height = dbtx.height;
var db_hash = await db.getBlockHash(height);
rawblock = await db.getRawBlock(height, db_hash.hash);
}
if(!rawblock) {
res.json({err: error_code.ERROR, res: ret_rawtx});
console.log('\rERROR: getRawTransactrion code=' + ret_rawtx.code + ' message=' + ret_rawtx.message + ' txid=' + txid + ' fallback failed');
return;
}
} else {
res.json({err: error_code.ERROR, res: ret_rawtx});
console.log('\rERROR: getRawTransactrion code=' + ret_rawtx.code + ' message=' + ret_rawtx.message + ' txid=' + txid);
return;
}
}
var tx = null;
if(rawblock) {
var block = bitcoin.Block.fromHex(rawblock);
for(var i in block.transactions) {
var block_tx = block.transactions[i];
if(block_tx.getId() == txid) {
tx = block_tx;
break;
}
}
if(!tx) {
res.json({err: error_code.ERROR, res: ret_rawtx});
console.log('\rERROR: getRawTransactrion code=' + ret_rawtx.code + ' message=' + ret_rawtx.message + ' txid=' + txid + ' fallback failed');
return;
}
}
tx = tx || bitcoin.Transaction.fromHex(ret_rawtx);
var ret_tx = {ins: [], outs: []};
var fee = UINT64(0);
var reward = false;
var target_network;
var bech32_flag = false;
if(req.query.bech32 && req.query.bech32.length <= 90 && /^[\x21-\x7e]+$/.test(req.query.bech32)) {
target_network = JSON.parse(JSON.stringify(network));
target_network.bech32 = req.query.bech32;
bech32_flag = true;
} else {
target_network = network;
}
for(var i in tx.ins) {
var in_txid = Buffer.from(tx.ins[i].hash).reverse().toString('hex');
var n = tx.ins[i].index;
if(n != 0xffffffff) {
var txout = await db.getTxout(in_txid, n);
if(!txout) {
console.log('\rERROR: Txout not found ' + in_txid + ' ' + n);
res.json({err: error_code.ERROR, res: 'Txout not found ' + in_txid + ' ' + n});
}
if(bech32_flag) {
for(var i in txout.addresses) {
txout.addresses[i] = bech32_address_prefix(txout.addresses[i], target_network);
}
}
ret_tx.ins.push({value: conv_uint64(txout.value), addrs: txout.addresses});
fee.add(txout.value);
} else {
reward = true;
}
}
if(reward) {
for(var i in tx.outs) {
ret_tx.outs.push({value: conv_uint64(tx.outs[i].value), addrs: get_script_addresses(tx.outs[i].script, target_network) || []});
}
} else {
for(var i in tx.outs) {
ret_tx.outs.push({value: conv_uint64(tx.outs[i].value), addrs: get_script_addresses(tx.outs[i].script, target_network) || []});
fee.subtract(tx.outs[i].value);
}
}
ret_tx.fee = conv_uint64(fee);
if(!dbtx) {
dbtx = await db.getTx(txid);
}
if(dbtx) {
ret_tx.height = dbtx.height;
ret_tx.time = dbtx.time;
ret_tx.sequence = dbtx.sequence;
}
res.json({err: errval, res: ret_tx});
});
});
});
// GET - /txout/{txid}
router.get('/txout/:txid', async function(req, res) {
var txid = req.params.txid;
var txout = await db.getTxouts(txid);
for(var i in txout) {
var item = txout[i];
item.value = conv_uint64(item.value);
}
res.json({err: errval, res: txout});
});
// POST - {txids: [txid1, txid2, ..., txidN]}
router.post('/txouts', async function(req, res) {
var txids = req.body.txids;
var txouts = [];
for(var i in txids) {
var txout = await db.getTxouts(txids[i]);
for(var i in txout) {
var item = txout[i];
item.value = conv_uint64(item.value);
}
txouts.push(txout);
}
res.json({err: errval, res: txouts});
});
// GET - /search/{keyword}
router.get('/search/:keyword', async function(req, res) {
var keyword = req.params.keyword;
var s_addrs = await db.searchAddresses(keyword);
if(s_addrs === null) {
res.json({err: error_code.TOO_MANY});
return;
}
var s_txids;
if((keyword.match(/([0-9]|[a-f])/gim) || []).length === keyword.length) {
s_txids = await db.searchTxids(keyword);
if(s_txids === null) {
res.json({err: error_code.TOO_MANY});
return;
}
}
res.json({err: error_code.SUCCESS, res: {addrs: s_addrs || [], txids: s_txids || []}});
});
// GET - /status
router.get('/status', async function(req, res) {
res.json({err: error_code.SUCCESS, res: {sync: status_sync}});
});
app.use(cors());
app.use(opts.server.http_path || '/api', router);
app.use(function(err, req, res, next) {
res.send({err: error_code.ERROR, res: err.message});
});
if(opts.server.http_port == opts.server.ws_port) {
return app;
}
app.listen(opts.server.http_port);
return null;
}
}
module.exports = ApiServer;