-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitcoin_rpc.js
77 lines (66 loc) · 2.17 KB
/
bitcoin_rpc.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
const fs = require('fs');
class Bitcoin {
constructor() {
this.state = JSON.parse(fs.readFileSync('chainstate.json', 'utf8'))
}
rpc(method, params) {
if (!method) {
throw new Error("First argument 'method' is required.\nExecute `rpc('help')` for help")
}
if (this[method]) {
if (params) {
return this[method](params)
} else {
return this[method]()
}
} else {
throw new Error(`Method '${method}' not found\nExecute \`rpc('help')\` for help`)
}
}
help() {
return `
Bitcoin Core v253.1.2
RPC commands:
getinfo
Returns basic node and network information
help
Returns this message
getblock ( hash )
Returns JSON-formatted block with the given hash
getblocksbyheight ( height )
Returns an array of hashes of blocks at the specified height in the tree
`;
}
getblocksbyheight(height) {
if (!height) {
throw new Error("Method 'getblocksbyheight' requires one argument (height)");
}
if (!(height in this.state["blocks_by_height"])) {
throw new Error(`No blocks available at height ${height}`);
}
return this.state["blocks_by_height"][height];
}
getblock(bhash) {
if (!bhash) {
throw new Error("Method 'getblock' requires one argument (hash)");
}
if (!(bhash in this.state["blocks_by_hash"])) {
throw new Error(`Block not found with hash ${bhash}`);
}
const block = JSON.parse(JSON.stringify(this.state["blocks_by_hash"][bhash]));
delete block.valid;
return block;
}
getinfo() {
return {
version: "Bitcoin Core v253.1.2",
blocks: parseInt(Math.max(...Object.keys(this.state["blocks_by_height"]))),
headers: parseInt(Math.max(...Object.keys(this.state["blocks_by_height"]))),
prune_height: parseInt(Math.min(...Object.keys(this.state["blocks_by_height"]))),
verification_progress: "1.0000",
difficulty: "3007592928481984.23",
peers: 23
};
}
}
module.exports = Bitcoin;