-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtools.js
64 lines (55 loc) · 1.93 KB
/
tools.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
const eventNames = require('./eventNames'),
config = require('./config'),
request = require('request');
function createJsonData(method) {
let args = [];
for (let i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
return {"jsonrpc": "2.0", "method": method, "params": args, "id": 1}
}
function createUri() {
// there is some problem with requests library and options, everything will be in inside url.
return config.phored_host + ":" + config.phored_rpc_port + config.phored_rpc_path;
}
function createBasicAuthHeader() {
if (config.rpc_pass == null || config.rpc_user == null) {
return {};
}
return {
Authorization: "Basic " + Buffer.from(config.rpc_user + ":" + config.rpc_pass).toString("base64")
}
}
function sendRpcCall(rpcCommand, callback, ...arg) {
return request.post(createUri(), {
headers: createBasicAuthHeader(),
json: createJsonData(rpcCommand, ...arg),
}, callback);
}
module.exports = {
// Convert a hex string to a byte array
hexToBytes: function (hex) {
let bytes = [];
for (let c = 0; c < hex.length; c += 2)
bytes.push(parseInt(hex.substr(c, 2), 16));
return bytes;
},
// Convert a byte array to a hex string
bytesToHex: function (bytes) {
let hex = [];
for (let i = 0; i < bytes.length; i++) {
hex.push((bytes[i] >>> 4).toString(16));
hex.push((bytes[i] & 0xF).toString(16));
}
return hex.join("");
},
downloadBlock: function (blockHash, callback) {
return sendRpcCall(eventNames.rpc.getblock, callback, blockHash);
},
downloadRawTransactionVerbose: function (txHash, callback) {
// set verbose to true (last parameter = 1)
return sendRpcCall(eventNames.rpc.getrawtransaction, callback, txHash, 1);
},
createUri: createUri,
createBasicAuthHeader: createBasicAuthHeader,
};