-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
283 lines (224 loc) Β· 8.08 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
const chalk = require('chalk');
const readline = require('readline');
const {Wallet, NodeWallet} = require('./wallet')
const fs = require('fs');
const { HuginNode } = require('./nodes');
const { sleep } = require('./utils');
const { NODE_VERSION } = require('./constants');
async function start_check() {
console.log(chalk.green("You are running Hugin Node version", NODE_VERSION))
if (!fs.existsSync('./db')) {
fs.mkdirSync('./db');
return init()
} else {
return login()
}
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
async function start(pub) {
const node = new HuginNode()
await node.init(pub)
//More events here?
commands(node)
}
async function login(retry = false) {
if (!retry) {
console.log(huginArt)
console.log(chalk.white('Welcome back to Hugin Node!'));
}
const password = await input_pass(chalk.white('Enter your password:'))
if (!await Wallet.init(password)) {
console.log(chalk.red("Error starting your wallet."));
login(true)
return
}
if(!await NodeWallet.init()) {
console.log(chalk.red("Error importing node wallet."))
}
const pub = await public_or_private()
rl.close()
start(pub)
}
async function init() {
console.log(huginArt)
console.log(chalk.white('Welcome to the Hugin Node setup wizard!'));
console.log(chalk.green('Lets get started...'));
const password = await input_pass(chalk.white('Step 1. Choose a password: '));
console.log(chalk.white("......................................."))
console.log(chalk.yellow("**NOTE**"))
console.log(chalk.yellow("Make sure to remember your password!"))
console.log(chalk.white("......................................."))
await sleep(500)
console.log(chalk.green('Password set successfully!'));
if (!await Wallet.create(password)) {
console.log(chalk.red("Error creating your wallet."))
}
if (!await NodeWallet.import()) {
console.log(chalk.red("Error importing node wallet."))
}
await sleep(200)
const pub = await public_or_private()
const paymentAddr = await payments()
const auto = await autopay()
console.log(chalk.blue("Loading...."))
Wallet.payments(paymentAddr, auto)
rl.close()
console.log(chalk.magenta('π Setup complete! You are ready to go.'));
start(pub)
}
async function commands(node) {
const com = readline.createInterface({
input: process.stdin,
output: process.stdout
});
async function handle(command) {
switch (command) {
case 'stats':
SHOW_STATS(node)
break;
case 'info':
SHOW_INFO(node)
break;
case 'backup':
BACKUP_WALLET()
break;
case 'payout':
console.log(chalk.blue.bold('Creating manual payout transaction...'))
Wallet.payout()
break;
default:
console.log(`Unknown command: ${command}`)
}
}
com.on('line', (input) => {
handle(input);
});
}
const question = 'Is this a public node?'
const pub = 'yes/no'
function public_or_private() {
return new Promise((resolve, reject) => {
rl.question(chalk.green(`Step 2. ${question}\n. ${pub}\n`), (answer) => {
let val
if (answer === 'Y' || answer === 'y' || answer === 'Yes' || answer === 'yes') {
val = true
} else if (answer === 'N' || answer === 'n' || answer === 'No' || answer === 'no') {
val = false
} else {
console.log('Invalid input. Please enter Y or N.')
return resolve(public_or_private())
}
resolve(val)
});
});
}
function payments() {
return new Promise((resolve, reject) => {
rl.question(chalk.green(`Step 3. Choose a payout address: \n`), (answer) => {
if (answer.startsWith('SEKR') && answer.length === 99) {
resolve(answer)
} else {
console.log(chalk.red("Wrong address format."))
return payments()
}
});
});
}
function autopay() {
return new Promise((resolve, reject) => {
rl.question(chalk.green(`Do you want automatic payments to this address? Press Enter.`), (answer) => {
if (answer.length === 0) resolve(true)
else resolve(false)
})
})
}
function input_pass(query) {
return new Promise((resolve) => {
let password = '';
let done = false
rl.input.on('data', (char) => {
if (done) return
char = char.toString();
switch (char) {
case '\n':
case '\r':
console.log('');
resolve(password);
done = true
break;
case '\x03':
process.exit();
break;
case '\x03': // Ctrl+C
console.log('^C');
process.exit();
break;
case '\b': // Backspace (Windows)
break;
case '\x7f': // Backspace (Linux/macOS)
if (password.length > 0) {
password = password.slice(0, -1);
process.stdout.write('\b \b');
}
break;
default:
process.stdout.write('\b \b');
process.stdout.write('*');
password += char;
break;
}
});
rl.question(query, () => {});
});
}
const huginArt = `
${chalk.white('βββ ββββββ βββ βββββββ βββββββ βββ')}
${chalk.white('βββ ββββββ βββββββββββ ββββββββ βββ')}
${chalk.white('βββββββββββ ββββββ βββββββββββββ βββ')}
${chalk.white('βββββββββββ ββββββ ββββββββββββββββ')}
${chalk.white('βββ βββββββββββββββββββββββββββ ββββββ')}
${chalk.white('βββ βββ βββββββ βββββββ ββββββ βββββ')}`
const SHOW_INFO = (node) => {
console.log(chalk.blue.bold('::::::::::::::::::::::'))
console.log(chalk.blue.bold(':::::::::INFO:::::::::'))
console.log('Running hugin node version', NODE_VERSION)
console.log(chalk.blue.bold('............'))
console.log(chalk.green('Node address:'))
console.log(Wallet.address + node.network.keys.publicKey.toString('hex'))
console.log(chalk.blue.bold('............'))
console.log(chalk.green('Payout address:'))
console.log(Wallet.paymentAddress)
console.log(chalk.blue.bold('............'))
console.log(chalk.green('Auto payments:'))
console.log(Wallet.autoPay)
console.log(chalk.blue.bold('::::::::::::::::::::::'))
console.log(chalk.blue.bold('::::::::::::::::::::::'))
}
const BACKUP_WALLET = async () => {
//Do something for info
console.log(chalk.blue.bold('::::::::::::::::::::::::'))
console.log(chalk.blue.bold(':::::::::BACKUP:::::::::'))
console.log(chalk.blue.bold('............'))
console.log('Wallet seed:')
console.log(chalk.blue.bold('............'))
const [seed, error] = await Wallet.wallet.getMnemonicSeed()
console.log(seed)
console.log(chalk.blue.bold('::::::::::::::::::::::::'))
console.log(chalk.blue.bold('::::::::::::::::::::::::'))
}
const SHOW_STATS = async (node) => {
// More stats?
console.log(chalk.blue.bold(':::::::::::::::::::::::'))
console.log(chalk.blue.bold(':::::::::STATS:::::::::'))
console.log(`${chalk.green('Active clients:')} ${chalk.yellow(node.network.clients.length)}`)
console.log(`${chalk.green('Nodes:')} ${chalk.yellow(node.network.nodes.length)}`)
console.log(`${chalk.green('Messages in Pool:')} ${chalk.yellow(node.pool.length)}`)
console.log(`${chalk.green('Node balance:')} ${chalk.yellow(parseInt(await Wallet.wallet.getBalance()) / 100000)} XKR`)
console.log(chalk.blue.bold(':::::::::::::::::::::::'))
console.log(chalk.blue.bold(':::::::::::::::::::::::'))
//Number of relayed messages?
}
start_check()