wative 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/account.ts ADDED
@@ -0,0 +1,558 @@
1
+ const chalk = require('chalk');
2
+ import * as fs from "fs";
3
+ import * as path from "path";
4
+ import {
5
+ getAccountLabel,
6
+ selectSomething,
7
+ inputPassword,
8
+ inputSomething,
9
+ tagValidator,
10
+ passphraseValidator,
11
+ expandAmountValidator,
12
+ lastAccountNoValidator,
13
+ } from "./utils";
14
+ import { getAccountAddress, getAccountBalance, getNetworkInfoByName, getNetworkTypeByName, getPKAccountAddress, selectDefaultNetwork, switchNetworkByAccountLabel, switchPPNetwork } from "./network";
15
+ import { showAssets } from './assets';
16
+ import { selectToolsOptions } from './tools';
17
+
18
+ const cliProgress = require('cli-progress');
19
+ const { WativeCore } = require("wative-core");
20
+
21
+ const preAccountImport = async (keystore_path: string) => {
22
+ const networks = getNetworkInfo(keystore_path)
23
+
24
+ let account_label_list: string[] = [];
25
+ if (networks.accounts) {
26
+ account_label_list = networks.accounts;
27
+ }
28
+
29
+ let account_label = await getAccountLabel('Account Label', account_label_list);
30
+ if (account_label === null) {
31
+ return null;
32
+ }
33
+
34
+ let password = await inputPassword(`Please set a password for Account [${account_label}]`);
35
+ let selected_default_network = await selectDefaultNetwork(keystore_path);
36
+
37
+ let pre_account_info = {
38
+ account_label,
39
+ password,
40
+ selected_default_network
41
+ };
42
+
43
+ return pre_account_info;
44
+ }
45
+
46
+
47
+ // account label
48
+ // cureent network
49
+ // data version 1: only evm or only solana 2: evm and solana
50
+ const importPassphrase = async (keystore_path: string, wative_core: typeof WativeCore) => {
51
+ let pre_account_info = await preAccountImport(keystore_path);
52
+ if (!pre_account_info) {
53
+ return;
54
+ }
55
+
56
+ let passphrase = await inputSomething('Please enter a mnemonic [split by space]', passphraseValidator);
57
+ passphrase = passphrase.replace(/\s+/g, " ");
58
+
59
+ const passphrase_account = wative_core.account.generatePPAccount(pre_account_info.account_label, passphrase, pre_account_info.password);
60
+ if (!passphrase_account.status) {
61
+ console.log(
62
+ chalk.red(passphrase_account.output)
63
+ );
64
+ return;
65
+ }
66
+ let account_info_path = path.join(keystore_path, `accounts/${pre_account_info.account_label}.json`);
67
+
68
+ passphrase_account.output.default_network = pre_account_info.selected_default_network;
69
+ fs.writeFileSync(account_info_path, JSON.stringify(passphrase_account.output, null, 4));
70
+
71
+ const network_path = path.resolve(keystore_path, 'network.json');
72
+ const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
73
+ networks.accounts.push(pre_account_info.account_label);
74
+ fs.writeFileSync(network_path, JSON.stringify(networks, null, 4));
75
+ }
76
+
77
+ const importPrivateKey = async (keystore_path: string, wative_core: typeof WativeCore) => {
78
+ let pre_account_info = await preAccountImport(keystore_path);
79
+ if (!pre_account_info) {
80
+ return;
81
+ }
82
+
83
+ let private_key = await inputSomething('Please enter a private key');
84
+ let network_info = await getNetworkInfoByName(keystore_path, pre_account_info.selected_default_network);
85
+
86
+ let privatekey_account = await wative_core.account.generatePKAccount(pre_account_info.account_label, private_key, pre_account_info.password, network_info.chainId);
87
+ if (!privatekey_account.status) {
88
+ console.log(
89
+ chalk.red(privatekey_account.output)
90
+ );
91
+ return;
92
+ }
93
+ let account_info_path = path.join(keystore_path, `accounts/${pre_account_info.account_label}.json`);
94
+
95
+ privatekey_account.output.default_network = pre_account_info.selected_default_network;
96
+ fs.writeFileSync(account_info_path, JSON.stringify(privatekey_account.output, null, 4));
97
+
98
+ const network_path = path.resolve(keystore_path, 'network.json');
99
+ const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
100
+ networks.accounts.push(pre_account_info.account_label);
101
+ fs.writeFileSync(network_path, JSON.stringify(networks, null, 4));
102
+ }
103
+
104
+ const createAccount = async (keystore_path: string, wative_core: typeof WativeCore) => {
105
+ const create_account_options = [
106
+ '> Import Passphrase',
107
+ '> Import Private Key',
108
+ '> Back'
109
+ ];
110
+
111
+ let selected_create_account = await selectSomething(create_account_options);
112
+
113
+ switch (selected_create_account) {
114
+ case '> Import Passphrase': {
115
+ await importPassphrase(keystore_path, wative_core);
116
+ break;
117
+ }
118
+ case '> Import Private Key': {
119
+ await importPrivateKey(keystore_path, wative_core);
120
+ break;
121
+ }
122
+ case '> Back': {
123
+ return;
124
+ }
125
+ }
126
+ }
127
+
128
+ const getNetworkInfo = (keystore_path: string) => {
129
+ const network_path = path.resolve(keystore_path, 'network.json');
130
+ const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
131
+ return networks;
132
+ }
133
+
134
+ export const getAccountInfo = (keystore_path: string, account_label: string) => {
135
+ const account_info_path = path.join(keystore_path, `accounts/${account_label}.json`);
136
+ const account_info = JSON.parse(fs.readFileSync(account_info_path, 'utf8'));
137
+ return account_info;
138
+ }
139
+
140
+ export const saveAccountInfo = (keystore_path: string, account_label: string, account_info: any) => {
141
+ const account_info_path = path.join(keystore_path, `accounts/${account_label}.json`);
142
+ fs.writeFileSync(account_info_path, JSON.stringify(account_info, null, 4));
143
+ }
144
+
145
+ export const loginIn = async (account_label: string, wative_core: typeof WativeCore) => {
146
+ const is_login = wative_core.account.isLogin(account_label);
147
+ if (is_login) {
148
+ return true;
149
+ }
150
+ let password = await inputPassword(`Please input the password for Account [${account_label}]`);
151
+ const login_result = await wative_core.account.login(account_label, password);
152
+ if (login_result.status) {
153
+ return true;
154
+ } else {
155
+ console.log(
156
+ chalk.red(login_result.output)
157
+ );
158
+ return false;
159
+ }
160
+ }
161
+
162
+ const selectAccount = async (keystore_path: string, wative_core: typeof WativeCore) => {
163
+ const networks = getNetworkInfo(keystore_path);
164
+ const account_label_list = networks.accounts;
165
+
166
+ let select_account_options = [
167
+ "> Back"
168
+ ];
169
+
170
+ const account_label_len = account_label_list.length;
171
+ if (account_label_len === 0) {
172
+ console.log(
173
+ chalk.red("No address here yet")
174
+ );
175
+ return;
176
+ }
177
+
178
+ for (let i = 0; i < account_label_len; i++) {
179
+ const account_label = account_label_list[i];
180
+ const account_info = getAccountInfo(keystore_path, account_label);
181
+ if (account_info.account_type === "PP") {
182
+ select_account_options.push(`${i + 1}) ${account_label} [${account_info.account_type}:${account_info.data.length}]`);
183
+ } else {
184
+ select_account_options.push(`${i + 1}) ${account_label} [${account_info.account_type}:1]`);
185
+ }
186
+ }
187
+
188
+ const selected_account = await selectSomething(select_account_options, "Select an account");
189
+ if (selected_account === "> Back") {
190
+ return;
191
+ }
192
+
193
+ const selected_account_label = account_label_list[select_account_options.indexOf(selected_account) - 1];
194
+ let login_result = await loginIn(selected_account_label, wative_core);
195
+ if (login_result) {
196
+ await accountManager(keystore_path, selected_account_label, wative_core);
197
+ }
198
+
199
+ await selectAccount(keystore_path, wative_core);
200
+ }
201
+
202
+
203
+ const accountManager = async (keystore_path: string, account_label: string, wative_core: typeof WativeCore) => {
204
+ let account_info = await getAccountInfo(keystore_path, account_label);
205
+
206
+ let account_manager_options: string[];
207
+
208
+ if (account_info.account_type === "PP") {
209
+ account_manager_options = [
210
+ '1.Address List',
211
+ '2.Expand Address',
212
+ '3.Slice Address',
213
+ '4.Reset Account Password',
214
+ `5.Switch Networks [${account_info.default_network}]`,
215
+ '> Back'
216
+ ];
217
+ } else {
218
+ account_manager_options = [
219
+ '1.Address List',
220
+ '2.Reset Account Password',
221
+ `3.Switch Networks [${account_info.default_network}]`,
222
+ '> Back'
223
+ ];
224
+ }
225
+
226
+ const selected_account_manager = await selectSomething(account_manager_options);
227
+ switch (selected_account_manager) {
228
+ case '1.Address List': {
229
+ if (account_info.account_type === "PP") {
230
+ await showPPAccounts(keystore_path, account_label, wative_core);
231
+ } else {
232
+ await showPKAccounts(keystore_path, account_label, wative_core);
233
+ }
234
+ break;
235
+ }
236
+ case '2.Expand Address': {
237
+ const count = await inputSomething('Expand accounts count', expandAmountValidator);
238
+ if (Number(count) > 2000) {
239
+ console.log(
240
+ chalk.red("Too many accounts, please input less than 2000")
241
+ );
242
+ break;
243
+ }
244
+
245
+ await expandAddress(keystore_path, account_label, Number(count), wative_core);
246
+ break;
247
+ }
248
+ case '3.Slice Address': {
249
+ const last_account_no = await inputSomething('Please input last account number', lastAccountNoValidator);
250
+ await sliceAddress(keystore_path, account_label, Number(last_account_no), wative_core);
251
+ break;
252
+ }
253
+ case '4.Reset Account Password': {
254
+ const password1 = await inputPassword(`Please input new password for Account [${account_label}]`);
255
+ const password2 = await inputPassword(`Please confirm new password for Account [${account_label}]`);
256
+
257
+ if (password1 !== password2) {
258
+ console.log(
259
+ chalk.red("Passwords do not match")
260
+ );
261
+ break;
262
+ }
263
+
264
+ await wative_core.account.resetPassword(account_label, password1);
265
+ return;
266
+ }
267
+ case `3.Switch Networks [${account_info.default_network}]`: {
268
+ await switchNetworkByAccountLabel(keystore_path, account_label, true);
269
+ break;
270
+ }
271
+ case `5.Switch Networks [${account_info.default_network}]`: {
272
+ await switchNetworkByAccountLabel(keystore_path, account_label, true);
273
+ break;
274
+ }
275
+ case '2.Reset Account Password': {
276
+ const password1 = await inputPassword(`Please input new password for Account [${account_label}]`);
277
+ const password2 = await inputPassword(`Please confirm new password for Account [${account_label}]`);
278
+
279
+ if (password1 !== password2) {
280
+ console.log(
281
+ chalk.red("Passwords do not match")
282
+ );
283
+ break;
284
+ }
285
+
286
+ await wative_core.account.resetPassword(account_label, password1);
287
+ return;
288
+ }
289
+ case '> Back': {
290
+ return;
291
+ }
292
+ }
293
+
294
+ await accountManager(keystore_path, account_label, wative_core);
295
+ }
296
+
297
+ const showPPAccounts = async (keystore_path: string, account_label: string, wative_core: typeof WativeCore) => {
298
+ const account_info = await getAccountInfo(keystore_path, account_label);
299
+ let option_accounts = [
300
+ '> Back'
301
+ ];
302
+
303
+ for (let i = 0; i < account_info.data.length; i++) {
304
+ let default_network = account_info.default_network;
305
+ if ("default_network" in account_info.data[i]) {
306
+ default_network = account_info.data[i].default_network;
307
+ }
308
+
309
+ let network_type = getNetworkTypeByName(keystore_path, default_network);
310
+
311
+ let tag = "";
312
+ if ("tag" in account_info.data[i]) {
313
+ tag = `(${account_info.data[i].tag})`;
314
+ }
315
+
316
+ if (network_type === "evm") {
317
+ option_accounts.push(`#${i} ${account_info.data[i].ciphertexts.evm.address} ${tag} [${default_network}]`);
318
+ } else if (network_type === "solana") {
319
+ option_accounts.push(`#${i} ${account_info.data[i].ciphertexts.solana.address} ${tag} [${default_network}]`);
320
+ }
321
+ }
322
+
323
+ let selected_account = await selectSomething(option_accounts, "Select an account");
324
+ if (selected_account === "> Back") {
325
+ return;
326
+ }
327
+
328
+ let selected_account_id = option_accounts.indexOf(selected_account) - 1;
329
+
330
+ await showPPAccountDetail(keystore_path, account_label, selected_account_id, wative_core);
331
+ await showPPAccounts(keystore_path, account_label, wative_core);
332
+ }
333
+
334
+ const showPPAccountDetail = async (keystore_path: string, account_label: string, selected_account_id: number, wative_core: typeof WativeCore) => {
335
+ const account_info = await getAccountInfo(keystore_path, account_label);
336
+
337
+ let default_network = account_info.default_network;
338
+ if ("default_network" in account_info.data[selected_account_id]) {
339
+ default_network = account_info.data[selected_account_id].default_network;
340
+ }
341
+
342
+ const network = await getNetworkInfoByName(keystore_path, default_network);
343
+ let account_address = getAccountAddress(keystore_path, account_label, selected_account_id);
344
+ const balance_result = await getAccountBalance(
345
+ account_address,
346
+ keystore_path,
347
+ default_network
348
+ );
349
+
350
+ if (balance_result.status) {
351
+ console.log(
352
+ chalk.green(` Address No: #${selected_account_id}\n Network: ${default_network}\n ${network.nativeCurrency.symbol} Balance: ${balance_result.output}`)
353
+ );
354
+ } else {
355
+ console.log(
356
+ chalk.green(` Address No: #${selected_account_id}\n Network: ${default_network}\n ${network.nativeCurrency.symbol}`)
357
+ );
358
+ }
359
+
360
+ let tag = "> Tag()";
361
+ if ("tag" in account_info.data[selected_account_id] && account_info.data[selected_account_id].tag !== "") {
362
+ tag = `> Tag(${account_info.data[selected_account_id].tag})`;
363
+ }
364
+
365
+ let options_account_detail = [
366
+ tag,
367
+ '> Switch network',
368
+ '> Assets',
369
+ '> Tools',
370
+ '> Dump private key',
371
+ '> Back'
372
+ ];
373
+
374
+ let selected_account_detail = await selectSomething(options_account_detail);
375
+
376
+ switch (selected_account_detail) {
377
+ case tag: {
378
+ let cureent_tag = 'Current tag ()';
379
+ if ("tag" in account_info.data[selected_account_id] && account_info.data[selected_account_id].tag !== "") {
380
+ cureent_tag = `Current tag (${account_info.data[selected_account_id].tag})`;
381
+ }
382
+ let tag = await inputSomething(cureent_tag, tagValidator);
383
+ tag = tag.replace(/\s+/g, " ").trim();
384
+ account_info.data[selected_account_id].tag = tag.toLocaleLowerCase();
385
+ saveAccountInfo(keystore_path, account_label, account_info);
386
+ break;
387
+ }
388
+ case '> Switch network': {
389
+ await switchPPNetwork(keystore_path, account_label, selected_account_id);
390
+ break;
391
+ }
392
+ case '> Assets': {
393
+ await showAssets(keystore_path, account_address, default_network);
394
+ break;
395
+ }
396
+ case '> Tools': {
397
+ await selectToolsOptions(keystore_path, account_label, account_address, wative_core);
398
+ break;
399
+ }
400
+ case '> Dump private key': {
401
+ let account_address = getAccountAddress(keystore_path, account_label, selected_account_id);
402
+ let private_key = await wative_core.account.showPrivateKey(account_address);
403
+ console.log("account: ", account_address);
404
+ console.log("private key: ", private_key);
405
+ break;
406
+ }
407
+ case '> Back': {
408
+ return;
409
+ }
410
+ }
411
+ }
412
+
413
+ const showPKAccounts = async (keystore_path: string, account_label: string, wative_core: typeof WativeCore) => {
414
+ const account_info = await getAccountInfo(keystore_path, account_label);
415
+ let option_accounts = [
416
+ '> Back'
417
+ ];
418
+
419
+ let tag = "";
420
+ if ("tag" in account_info && account_info.tag !== "") {
421
+ tag = `(${account_info.tag})`;
422
+ }
423
+
424
+ option_accounts.push(`#0 ${account_info.ciphertexts.address} ${tag} [${account_info.default_network}]`);
425
+ let selected_account = await selectSomething(option_accounts, "Select an account");
426
+ if (selected_account === "> Back") {
427
+ return;
428
+ }
429
+
430
+ let default_network = account_info.default_network;
431
+ const network = await getNetworkInfoByName(keystore_path, default_network);
432
+ console.log(
433
+ chalk.green(` Address No: #0\n Network: ${default_network}\n ${network.nativeCurrency.symbol} Balance: 0`)
434
+ );
435
+
436
+ await showPKAccountDetail(keystore_path, account_label, wative_core);
437
+ await showPKAccounts(keystore_path, account_label, wative_core);
438
+ }
439
+
440
+
441
+ const showPKAccountDetail = async (keystore_path: string, account_label: string, wative_core: typeof WativeCore) => {
442
+ const account_info = await getAccountInfo(keystore_path, account_label);
443
+ let tag = "> Tag()";
444
+ if ("tag" in account_info && account_info.tag !== "") {
445
+ tag = `> Tag(${account_info.tag})`;
446
+ }
447
+
448
+ let options_account_detail = [
449
+ tag,
450
+ '> Switch network',
451
+ '> Assets',
452
+ '> Tools',
453
+ '> Dump private key',
454
+ '> Back'
455
+ ];
456
+
457
+ let selected_account_detail = await selectSomething(options_account_detail);
458
+
459
+ switch (selected_account_detail) {
460
+ case tag: {
461
+ let cureent_tag = 'Current tag ()';
462
+ if ("tag" in account_info && account_info.tag !== "") {
463
+ cureent_tag = `Current tag (${account_info.tag})`;
464
+ }
465
+ let tag = await inputSomething(cureent_tag, tagValidator);
466
+ account_info.tag = tag.replace(/\s+/g, " ").trim().toLocaleLowerCase();
467
+ saveAccountInfo(keystore_path, account_label, account_info);
468
+ break;
469
+ }
470
+ case '> Switch network': {
471
+ await switchNetworkByAccountLabel(keystore_path, account_label, false);
472
+ break;
473
+ }
474
+ case '> Assets': {
475
+ let account_address = getPKAccountAddress(keystore_path, account_label);
476
+ await showAssets(keystore_path, account_address, account_info.default_network);
477
+ break;
478
+ }
479
+ case '> Tools': {
480
+ let account_address = getPKAccountAddress(keystore_path, account_label);
481
+ await selectToolsOptions(keystore_path, account_label, account_address, wative_core);
482
+ break;
483
+ }
484
+ case '> Dump private key': {
485
+ let account_address = getPKAccountAddress(keystore_path, account_label);
486
+ let private_key = await wative_core.account.showPrivateKey(account_address);
487
+ console.log("account: ", account_address);
488
+ console.log("private key: ", private_key);
489
+ break;
490
+ }
491
+ case '> Back': {
492
+ return;
493
+ }
494
+ }
495
+
496
+ await showPKAccountDetail(keystore_path, account_label, wative_core);
497
+ }
498
+
499
+ const expandAddress = async (keystore_path: string, account_label: string, expand_amount: number, wative_core: typeof WativeCore) => {
500
+ const account_info = await getAccountInfo(keystore_path, account_label);
501
+ const account_len = account_info.data.length;
502
+ const bar1 = new cliProgress.SingleBar({}, cliProgress.Presets.legacy);
503
+ bar1.start(expand_amount, 0);
504
+ for (let i = account_len; i < account_len + expand_amount; i++) {
505
+ let sub_account = await wative_core.account.generateSubAccount(account_label, i);
506
+ if (!sub_account.status) {
507
+ console.log(
508
+ chalk.red(sub_account.output)
509
+ );
510
+ return;
511
+ }
512
+ account_info.data.push({
513
+ ciphertexts: sub_account.output
514
+ });
515
+ bar1.increment();
516
+ }
517
+ bar1.stop();
518
+
519
+ saveAccountInfo(keystore_path, account_label, account_info);
520
+ }
521
+
522
+ const sliceAddress = async (keystore_path: string, account_label: string, last_account_no: number, wative_core: typeof WativeCore) => {
523
+ const account_info = await getAccountInfo(keystore_path, account_label);
524
+ account_info.data = account_info.data.slice(0, last_account_no + 1);
525
+ saveAccountInfo(keystore_path, account_label, account_info);
526
+ wative_core.account.reloadAccount();
527
+ }
528
+
529
+ export const accountSetting = async (keystore_path: string, wative_core: typeof WativeCore) => {
530
+ const account_path = path.resolve(keystore_path, 'accounts');
531
+ if (!fs.existsSync(account_path)) {
532
+ fs.mkdirSync(account_path);
533
+ }
534
+
535
+ const account_setting_options = [
536
+ '> Select an account',
537
+ '> Create a new account',
538
+ '> Back'
539
+ ];
540
+
541
+ let selected_account_setting = await selectSomething(account_setting_options);
542
+
543
+ switch (selected_account_setting) {
544
+ case '> Select an account': {
545
+ await selectAccount(keystore_path, wative_core);
546
+ break;
547
+ }
548
+ case '> Create a new account': {
549
+ await createAccount(keystore_path, wative_core);
550
+ break;
551
+ }
552
+ case '> Back': {
553
+ return;
554
+ }
555
+ }
556
+
557
+ await accountSetting(keystore_path, wative_core);
558
+ }