forked from panda-suite/pandacash-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
139 lines (106 loc) · 3.32 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
#! /usr/bin/env node
const chalk = require('chalk');
const clear = require('clear');
const figlet = require('figlet');
const util = require('util');
const _exec = require('child_process').exec;
const exec = util.promisify(_exec);
const BITBOXSDK = require('bitbox-sdk/lib/bitbox-sdk');
const BITBOX = new BITBOXSDK.default();
const mnemonic = generateSeedMnemonic();
const keyPairs = generateSeedKeyPairs();
function generateSeedMnemonic() {
return BITBOX.Mnemonic.generate(128);
}
function generateSeedKeyPairs() {
return BITBOX.Mnemonic.toKeypairs(mnemonic, 10, true);
}
async function startDocker() {
// delete the pandacash
console.log('Restarting Bitcoin Cash Client');
try {
await exec('docker rm pandacash -f');
} catch (e) {
// ignored
}
exec('docker run --name pandacash -p 18332:18332 pandacash &');
await nodeAvailable();
console.log('Bitcoin Cash Client restarted and listens at port 18332');
}
async function nodeAvailable() {
try {
await exec('docker exec pandacash bitcoin-cli -conf=/opt/bitcoin/bitcoin.conf getblockchaininfo');
} catch (e) {
await sleep(500);
await nodeAvailable();
}
}
function sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
async function seedAccounts() {
console.log('Seeding accounts');
keyPairs.forEach(async (keyPair) => {
try {
await exec(`docker exec pandacash bitcoin-cli -conf=/opt/bitcoin/bitcoin.conf importaddress ${keyPair.address}`)
await exec(`docker exec pandacash bitcoin-cli -conf=/opt/bitcoin/bitcoin.conf generatetoaddress 10 ${keyPair.address}`);
} catch (e) {
console.log(e);
}
});
console.log('Advancing blockchain to enable spending');
await exec('docker exec pandacash bitcoin-cli -conf=/opt/bitcoin/bitcoin.conf generate 500');
}
async function enableLogging() {
console.log('Enabling loging of debug.db.');
_exec('docker exec -i pandacash tail -n10 -f /opt/bitcoin/regtest/debug.log')
.stdout.on('data', function(data) {
console.log(data.toString());
});
}
async function startBitboxApi() {
// delete the pandacash
console.log('Starting BITBOX API at port 3000');
await exec('BITCOINCOM_BASEURL=http://localhost:3000/api/ RPC_BASEURL=http://localhost:18332/ RPC_PASSWORD=regtest RPC_USERNAME=regtest ZEROMQ_PORT=0 ZEROMQ_URL=0 NETWORK=local node ./node_modules/rest.bitcoin.com/app.js');
}
function printPandaMessage() {
process.stdout.write(`
PandaCash CLI v0.0.1
Available Accounts
==================`);
keyPairs.forEach((keyPair, i) => {
process.stdout.write(`
(${i}) ${keyPair.address}`);
});
process.stdout.write(`
Private Keys
==================`);
keyPairs.forEach((keyPair, i) => {
process.stdout.write(`
(${i}) ${keyPair.privateKeyWIF}`);
});
console.log(`
HD Wallet
==================
Mnemonic: ${mnemonic}
Base HD Path: m/44'/145'/0'/0/{account_index}
Bitcoin Cash Listening on http://localhost:18332
BITBOX API running at http://localhost:3000/v1/
BITBOX API Docs running at http://localhost:3000/
`);
}
(async () => {
clear();
console.log(
chalk.yellow(
figlet.textSync('PandaCash', { horizontalLayout: 'full' })
)
);
await startDocker();
await seedAccounts();
startBitboxApi();
printPandaMessage();
enableLogging();
})();