-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
294 lines (269 loc) · 8.24 KB
/
index.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
import { stat, writeFile, mkdir } from "fs";
import { homedir, platform } from "os";
import { spawn, exec } from "child_process";
import { createWallet, getWalletInfo, loadWallet, start } from "./rpc/commands.js";
/**
*
* @SPEC create multipule instances of a regtest node.
*
*
*/
// todo: Promot in a cli for user options. auth
// ttl on node dirs. how to set?
// delete old nodes, how to keep clean/manage nodes?
const nodes = 3; // How many node to be created.
const ip = "127.0.0.1:";
const linux = "/.bitcoin/";
//const windows = "UsersYourUserNameAppdataRoamingBitcoin"; // (Vista and 7)
const mac = "/Library/Application Support/Bitcoin/";
const env = platform();
const home = homedir();
const paths = [];
const ports = [];
const startPort = [8333]; // startPort 8333 is where the port generation starts from.
// Add os check to determine where data directories should be created.
const init = (env, home) => {
const pkgName = "ledgerd"; // Directory name to build network in.
if (env === "linux") {
console.log(env);
const url = home + linux + pkgName;
return url;
}
if (env === "darwin") {
console.log(env);
const url = home + mac + pkgName;
return url;
}
};
// Determine database location
const db = init(env, home);
for (let i = nodes; i--; ) {
// Generate paths
const url = db + "/node" + i + "/";
paths.push(url);
// Sequential port generation.
const port = startPort[0]++; // 8333++
ports.push({ port: port + nodes, rpc: port });
}
for (let i = paths.length; i--; ) {
// Generate directories
stat(paths[i], (err, stats) => {
if (err) console.log(`Directory doesn't exist, making now...`);
if (stats) {
console.log("Directories ready:", true);
} else {
mkdir(paths[i], { recursive: true }, (err) => {
if (err) {
console.log("errrrr", err);
}
});
console.log("created dir:", paths[i]);
}
});
}
// if (ports.length && paths.length > 0) {
// console.log("auth handeling can go here");
// }
// **Important in production code:**
// todo: need auth handeling: ./auth.js
// todo: Update bitcoind auth settings to latest standard: rpcauth=user:passwordhash
// to use "rpcauth=" in regtest with multipule nodes requires unique hashes for each node.
// const rpcauth = {
// user: "jamesdon",
// password: ":01714fd71a8c04711b73191ebcd2c7bb$20860750b73dbc98d5a793cc92a91c89541dd3beceac51bcb91a3c4d9268e23a",
// };
// rpcauth=${auth.user + auth.password}
if (paths.length === 0) {
console.log("ERROR: no paths found..");
}
for (let i = ports.length; i--; ) {
const port = ports[i].port;
const rpc = ports[i].rpc;
// using unsecure auth settings "rpcuser" and "rpcpassword"
const conf = `
regtest=1
debug=rpc
server=1
rpcuser=jamesdon
rpcpassword=thisisnotapassword
datadir=${db}/node${i}/
[regtest]
bind=127.0.0.1:${port}
rpcport=${rpc}
port=${port}
`;
writeFile(`${db}/node${i}/bitcoin.conf`, conf, (err) => {
if (err) console.log(err);
console.log("bitcoin.conf:", true);
});
}
// Node env is set at this point.
const nodesReady = [];
for (let i = paths.length; i--; ) {
// Build a "ready to run" node Object.
const nodeName = paths[i].slice(58, 63);
const node = {
name: nodeName,
key: nodeName.slice(4, 5),
ports: ports[i],
paths: paths[i],
running: false,
wallets: [],
addresses: [],
account: [],
};
nodesReady.push(node);
}
// Time to run the start bitcoind.
if (nodesReady.length > 0) {
for (let i = nodesReady.length; i--; ) {
start(db, nodesReady[i], nodesReady[i].key);
}
}
// using setTimeout to "hang" the process allowing for time for start up.
setTimeout(() => {
nodeController(nodesReady);
}, 2500);
/**
*
* @spec
*
*/
const nodesActive = [];
const walletActivate = async () => {
for (let i = nodesReady.length; i--; ) {
const loaded = await loadWallet(db, nodesReady[i]);
if (loaded === "new wallet!") {
const wallet = await createWallet(db, nodesReady[i]);
// nodesActive.push(wallet);
}
if (nodesActive.length <= nodes) {
// console.log('nodesActive length:', nodesActive)
// nodesActive.push(loaded[0]);
}
if (loaded[1] === 'wallet active') {
const info = await getWalletInfo(db, nodesReady[i]);
nodesActive.push(info)
// console.log('info:::', info)
}
}
console.log('nodessss', nodesActive)
};
const nodeController = (nodesReady) => {
process.stdout.write(
"1) Activate wallet \n2) Add new addresses. \n3) Add new addresses. : \n4) Exit program. : 5 \n"
);
process.stdin.resume();
process.stdin.setEncoding("utf-8");
let buffer = "";
process.stdin.on("data", (data) => {
buffer = data.trim();
switch (buffer) {
case "1":
walletActivate();
break;
case "2":
for (let i = nodesReady.length; i--; ) {
// could be added to walletActivate func?
};
break;
case "3":
for (let i = nodesReady.length; i--; ) {
// const start = spawn(
// "bitcoin-cli",
// [
// "-regtest",
// `-rpcport=${nodesReady[i].ports.rpc}`,
// "-datadir=" + db + "/node" + nodesReady[i].key,
// "getnewaddress", // add address type.
// ],
// { encoding: "utf-8", stdio: "pipe" }
// );
// start.stderr.on("data", (data) => {
// console.log("Gen new address error:", data.toString());
// });
// start.stdout.on("data", (data) => {
// if (data) {
// const address = {
// key: nodesReady[i].key,
// node: nodesReady[i].name,
// address: data,
// };
// console.log("Address ready:", address);
// nodesReady[i].address.push(address);
// }
// });
// start.on("close", (data) => {
// console.log("Node started, closing processes. ", data);
// console.log("node started:", nodesReady[i].running);
// });
};
break;
case "4":
for (let i = nodesReady.length; i--; ) {
const start = spawn(
"bitcoin-cli",
[
"-regtest",
`-rpcport=${nodesReady[i].ports.rpc}`,
"-datadir=" + db + "/node" + nodesReady[i].key,
"getaccount",
],
{ encoding: "utf-8", stdio: "pipe" }
);
start.stderr.on("data", (data) => {
console.log("Get account details error:", data.toString());
});
start.stdout.on("data", (data) => {
if (data) {
const address = {
key: nodesReady[i].key,
node: nodesReady[i].name,
address: data,
};
console.log("Address ready:", address);
nodesReady[i].address.push(address);
}
});
start.on("close", (data) => {
console.log("Node started, closing processes. ", data);
console.log("node started:", nodesReady[i].running);
});
}
break;
case "5":
console.log("Closing cli, bitcoind daemon still alive...");
process.exit(0);
case "6":
console.log("Nodes ready:", nodesReady);
break;
default:
console.log("invalid option.");
}
});
};
// "SIGINT" signal interupt which triggers node stop command.
// if the need to abort arisies use "control + c". (mac)
process.on("SIGINT", () => {
console.log("\nInterupting process.");
// Stop bitcoind
for (let i = nodesReady.length; i--; ) {
const bitcoinStop = `bitcoin-cli -rpcport=${nodesReady[i].ports.rpc} -datadir="${db}/node${nodesReady[i].key}" stop`;
// console.log("stop commands here:", bitcoinStop);
exec(bitcoinStop, (error, stdout, stderr) => {
if (error) {
console.log("error", error);
process.exit(0);
}
if (stderr) {
console.log("stderr", stderr);
process.exit(0);
}
if (stdout) {
console.log(stdout, nodesReady[i].key);
nodesReady[i].running = false;
process.exit(0);
}
});
}
});