-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtracker-rest-api.js
75 lines (60 loc) · 2.21 KB
/
tracker-rest-api.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
/*!
* tracker/tracker-rest-api.js
* Copyright (c) 2016-2019, Samourai Wallet (CC BY-NC-ND 4.0 License).
*/
import validator from 'validator'
import errors from '../lib/errors.js'
import authMgr from '../lib/auth/authorizations-manager.js'
import HttpServer from '../lib/http-server/http-server.js'
import network from '../lib/bitcoin/network.js'
import keysFile from '../keys/index.js'
const keys = keysFile[network.key]
/**
* Tracker API endpoints
*/
class TrackerRestApi {
/**
* Constructor
* @param {HttpServer} httpServer - HTTP server
* @param {Tracker} tracker - tracker
*/
constructor(httpServer, tracker) {
this.httpServer = httpServer
this.tracker = tracker
// Establish routes. Proxy server strips /pushtx
this.httpServer.app.get(
`/${keys.prefixes.support}/rescan`,
authMgr.checkHasAdminProfile.bind(authMgr),
this.getBlocksRescan.bind(this),
)
}
/**
* Rescan a range of blocks
*/
async getBlocksRescan(req, res) {
// Check req.arguments
if (!req.query)
return HttpServer.sendError(res, errors.body.INVDATA)
if (!req.query.fromHeight || !validator.isInt(req.query.fromHeight))
return HttpServer.sendError(res, errors.body.INVDATA)
if (req.query.toHeight && !validator.isInt(req.query.toHeight))
return HttpServer.sendError(res, errors.body.INVDATA)
// Retrieve the req.arguments
const fromHeight = Number.parseInt(req.query.fromHeight, 10)
const toHeight = req.query.toHeight ? Number.parseInt(req.query.toHeight, 10) : fromHeight
if (req.query.toHeight && (toHeight < fromHeight))
return HttpServer.sendError(res, errors.body.INVDATA)
try {
await this.tracker.blockchainProcessor.rescanBlocks(fromHeight, toHeight)
const returnValue = {
status: 'Rescan complete',
fromHeight: fromHeight,
toHeight: toHeight
}
HttpServer.sendRawData(res, JSON.stringify(returnValue, null, 2))
} catch(error) {
return HttpServer.sendError(res, error)
}
}
}
export default TrackerRestApi