-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbtc.js
86 lines (74 loc) · 2.25 KB
/
btc.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
const request = require('request')
var express = require('express')
var app = express()
app.use(express.json())
app.use(express.urlencoded())
const targetUrl = process.env.TARGET_URL
const targetPort = process.env.TARGET_PORT
const username = process.env.TARGET_USERNAME
const password = process.env.TARGET_PASSWORD
const port = process.env.PROXY_PORT
const url = "http://" + targetUrl + ":" + targetPort
let requestId = 0
app.post('/', function(req, res){
const json = req.body
let requestPromise
if(json.method == 'getblockbyheight') {
const height = json.params[0]
const verbosity = json.params[1]
requestPromise = getBlockByHeight(height, verbosity)
}else {
requestPromise = rpcRequest(json.method, json.params)
}
requestPromise.then(response => {
const returnValue = JSON.stringify(response)
res.setHeader('content-type', 'application/json')
res.send(JSON.stringify(returnValue))
}).catch(error => {
console.log(error)
})
});
console.log(`Target: ${targetUrl}:${targetPort} - Listening at: ${port} ---`)
app.listen(port)
async function rpcRequest(method, params) {
if(requestId == Number.MAX_SAFE_INTEGER) {
requestId = 0
}else {
requestId++
}
const body = {
"jsonrpc": "1.0",
"id": String(requestId),
method: method,
params: params || []
}
return new Promise((resolve, reject) => {
request.post(url, {
json: true,
auth: {
user: username,
password: password,
sendImmediately: false
},
body: body
}, function (error, response, body) {
if(!error && body){
resolve(body)
}else {
reject(error)
}
})
})
}
async function getBlockByHeight(height, verbosity) {
const hashResult = await rpcRequest('getblockhash', [height])
if(hashResult.error) {
console.log('notFoundHashForHeight: ', height)
return hashResult
}
const result = await rpcRequest('getblock', [hashResult.result, verbosity])
if(result.error) {
console.log('notFoundBlockForHeight: ', height)
}
return result
}