wative 1.0.9 → 1.0.11

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 CHANGED
@@ -13,15 +13,24 @@ import {
13
13
  getAccountIndex,
14
14
  getAccountRange,
15
15
  confirmSomething,
16
+ numberValidator,
17
+ formatAddr,
18
+ listWithLazyBalanceLoading,
16
19
  } from "./utils";
17
- import { getAccountAddress, getAccountBalance, getNetworkInfoByName, getNetworkTypeByName, getPKAccountAddress, selectDefaultNetwork, switchNetworkByAccountLabel, switchPPNetwork } from "./network";
18
- import { showAssets } from './assets';
20
+ import { getAccountAddress, getAccountBalance, getNetworkInfoByName, getNetworkTypeByName, getStatus, selectDefaultNetwork, showAllAccounts, switchNetworkByAccountLabel } from "./network";
21
+ import { getAssetList, showAssets, showAssetsDetail } from './assets';
19
22
  import { selectToolsOptions } from './tools';
20
23
  import inquirer from "inquirer";
24
+ import { getChainType } from "./chain";
21
25
 
22
26
  const cliProgress = require('cli-progress');
23
27
  const { WativeCore } = require("wative-core");
24
28
 
29
+ const DEFAULT_ACCOUNT: any = {
30
+ account_label: '',
31
+ default_account_index: 0
32
+ };
33
+
25
34
  const preAccountImport = async (keystore_path: string) => {
26
35
  const networks = getNetworkInfo(keystore_path)
27
36
 
@@ -78,40 +87,113 @@ const importPassphrase = async (keystore_path: string, wative_core: typeof Wativ
78
87
  fs.writeFileSync(network_path, JSON.stringify(networks, null, 4));
79
88
  }
80
89
 
81
- const importPrivateKey = async (keystore_path: string, wative_core: typeof WativeCore) => {
90
+
91
+ const createPrivateKeyAccount = async (keystore_path: string, wative_core: typeof WativeCore) => {
82
92
  let pre_account_info = await preAccountImport(keystore_path);
83
93
  if (!pre_account_info) {
84
94
  return;
85
95
  }
86
96
 
87
97
  let private_key = await inputSomething('Please enter a private key');
88
- let network_info = await getNetworkInfoByName(keystore_path, pre_account_info.selected_default_network);
98
+ await _importAccount(
99
+ keystore_path,
100
+ pre_account_info.password,
101
+ pre_account_info.account_label,
102
+ private_key,
103
+ pre_account_info.selected_default_network,
104
+ wative_core,
105
+ true
106
+ );
107
+
108
+ const network_path = path.resolve(keystore_path, 'network.json');
109
+ const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
110
+ networks.accounts.push(pre_account_info.account_label);
111
+ fs.writeFileSync(network_path, JSON.stringify(networks, null, 4));
112
+ }
113
+
114
+ const importPrivateKeyAccount = async (
115
+ keystore_path: string,
116
+ password: string,
117
+ account_label: string,
118
+ wative_core: typeof WativeCore
119
+ ) => {
120
+ let private_key = await inputSomething('Please enter a private key');
121
+ let account_info_path = path.join(keystore_path, `accounts/${account_label}.json`);
122
+ let exist_account_info = JSON.parse(fs.readFileSync(account_info_path, 'utf8'));
89
123
 
90
- let privatekey_account = await wative_core.account.generatePKAccount(pre_account_info.account_label, private_key, pre_account_info.password, network_info.chainId);
124
+ await _importAccount(
125
+ keystore_path,
126
+ password,
127
+ account_label,
128
+ private_key,
129
+ exist_account_info.default_network,
130
+ wative_core,
131
+ false
132
+ );
133
+ }
134
+
135
+ const _importAccount = async (
136
+ keystore_path: string,
137
+ password: string,
138
+ account_label: string,
139
+ private_key: string,
140
+ default_network: string,
141
+ wative_core: typeof WativeCore,
142
+ generate_account_label: boolean
143
+ ) => {
144
+ let network_info = await getNetworkInfoByName(keystore_path, default_network);
145
+ let network_type = getChainType(network_info.chainId);
146
+ if (network_type === 'evm' && !private_key.startsWith('0x')) {
147
+ private_key = "0x" + private_key;
148
+ }
149
+
150
+ let privatekey_account = await wative_core.account.generatePKAccount(account_label, private_key, password, network_info.chainId, generate_account_label);
91
151
  if (!privatekey_account.status) {
92
152
  console.log(
93
153
  chalk.red(privatekey_account.output)
94
154
  );
95
155
  return;
96
156
  }
97
- let account_info_path = path.join(keystore_path, `accounts/${pre_account_info.account_label}.json`);
157
+ let account_info_path = path.join(keystore_path, `accounts/${account_label}.json`);
158
+
159
+ let result: any;
160
+ if (fs.existsSync(account_info_path)) {
161
+ let exist_account_info = JSON.parse(fs.readFileSync(account_info_path, 'utf8'));
162
+ if ("data" in exist_account_info) {
163
+ exist_account_info.data.push({
164
+ ciphertexts: privatekey_account.output.ciphertexts
165
+ });
166
+ result = exist_account_info;
167
+ } else {
168
+ exist_account_info.data = [
169
+ { ciphertexts: exist_account_info.ciphertexts }
170
+ ]
171
+ delete exist_account_info.ciphertexts;
172
+ exist_account_info.data.push({
173
+ ciphertexts: privatekey_account.output.ciphertexts
174
+ });
175
+ result = exist_account_info;
176
+ }
177
+ } else {
178
+ privatekey_account.output.data = [
179
+ { ciphertexts: privatekey_account.output.ciphertexts }
180
+ ];
181
+ delete privatekey_account.output.ciphertexts;
182
+ privatekey_account.output.default_network = default_network;
183
+ result = privatekey_account.output;
184
+ }
98
185
 
99
- privatekey_account.output.default_network = pre_account_info.selected_default_network;
100
- fs.writeFileSync(account_info_path, JSON.stringify(privatekey_account.output, null, 4));
186
+ fs.writeFileSync(account_info_path, JSON.stringify(result, null, 4));
101
187
 
102
- const network_path = path.resolve(keystore_path, 'network.json');
103
- const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
104
- networks.accounts.push(pre_account_info.account_label);
105
- fs.writeFileSync(network_path, JSON.stringify(networks, null, 4));
188
+ wative_core.account.reloadAccount();
106
189
  }
107
190
 
108
191
  const createAccount = async (keystore_path: string, wative_core: typeof WativeCore) => {
109
192
  const create_account_options = [
110
- '> Back',
111
- new inquirer.Separator('----------------------------------'),
112
193
  '> Import Passphrase',
113
194
  '> Import Private Key',
114
-
195
+ new inquirer.Separator('----------------------------------'),
196
+ '> Back'
115
197
  ];
116
198
 
117
199
  let selected_create_account = await selectSomething(create_account_options);
@@ -122,13 +204,15 @@ const createAccount = async (keystore_path: string, wative_core: typeof WativeCo
122
204
  break;
123
205
  }
124
206
  case '> Import Private Key': {
125
- await importPrivateKey(keystore_path, wative_core);
207
+ await createPrivateKeyAccount(keystore_path, wative_core);
126
208
  break;
127
209
  }
128
210
  case '> Back': {
129
211
  return;
130
212
  }
131
213
  }
214
+
215
+ wative_core.account.reloadAccount();
132
216
  }
133
217
 
134
218
  const getNetworkInfo = (keystore_path: string) => {
@@ -151,21 +235,28 @@ export const saveAccountInfo = (keystore_path: string, account_label: string, ac
151
235
  export const loginIn = async (account_label: string, wative_core: typeof WativeCore) => {
152
236
  const is_login = wative_core.account.isLogin(account_label);
153
237
  if (is_login) {
154
- return true;
238
+ return { isLoginIn: true };
155
239
  }
156
240
 
157
241
  for (let i = 0; i < 3; i++) {
158
242
  const password = await inputPassword(`Please input the password for Account [${account_label}]`);
243
+ if (!password) {
244
+ console.log(
245
+ chalk.red("Password can't be empty")
246
+ );
247
+ return { isLoginIn: false };
248
+ }
249
+
159
250
  const login_result = await wative_core.account.login(account_label, password);
160
251
  if (login_result.status) {
161
- return true;
252
+ return { isLoginIn: true, password };
162
253
  } else {
163
254
  console.log(
164
255
  chalk.red(login_result.output)
165
256
  );
166
257
  }
167
258
  }
168
- return false;
259
+ return { isLoginIn: false };
169
260
  }
170
261
 
171
262
  const selectAccount = async (keystore_path: string, wative_core: typeof WativeCore) => {
@@ -174,7 +265,7 @@ const selectAccount = async (keystore_path: string, wative_core: typeof WativeCo
174
265
 
175
266
  let select_account_options = [
176
267
  '> Back',
177
- new inquirer.Separator('----------------------------------')
268
+ new inquirer.Separator('----------------------------------')
178
269
  ];
179
270
 
180
271
  const account_label_len = account_label_list.length;
@@ -188,7 +279,7 @@ const selectAccount = async (keystore_path: string, wative_core: typeof WativeCo
188
279
  for (let i = 0; i < account_label_len; i++) {
189
280
  const account_label = account_label_list[i];
190
281
  const account_info = getAccountInfo(keystore_path, account_label);
191
- if (account_info.account_type === "PP") {
282
+ if ("data" in account_info) {
192
283
  select_account_options.push(`${i + 1}) ${account_label} [${account_info.account_type}:${account_info.data.length}]`);
193
284
  } else {
194
285
  select_account_options.push(`${i + 1}) ${account_label} [${account_info.account_type}:1]`);
@@ -202,8 +293,8 @@ const selectAccount = async (keystore_path: string, wative_core: typeof WativeCo
202
293
 
203
294
  const selected_account_label = account_label_list[select_account_options.indexOf(selected_account) - 2];
204
295
  let login_result = await loginIn(selected_account_label, wative_core);
205
- if (login_result) {
206
- await accountManager(keystore_path, selected_account_label, wative_core);
296
+ if (login_result.isLoginIn) {
297
+ await accountManager(keystore_path, selected_account_label, wative_core, login_result.password);
207
298
  }
208
299
 
209
300
  await selectAccount(keystore_path, wative_core);
@@ -213,11 +304,11 @@ const setDisableAddress = async (keystore_path: string, account_label: string, w
213
304
  const account_info = await getAccountInfo(keystore_path, account_label);
214
305
 
215
306
  let disable_address_options = [
216
- '> Back',
307
+ "> Disabled Slots",
308
+ "> Add Single",
309
+ "> Add Range",
217
310
  new inquirer.Separator('----------------------------------'),
218
- "1.Disabled Slots",
219
- "2.Add Single",
220
- "3.Add Range"
311
+ '> Back'
221
312
  ];
222
313
 
223
314
  const selected_disable_address = await selectSomething(disable_address_options);
@@ -225,7 +316,7 @@ const setDisableAddress = async (keystore_path: string, account_label: string, w
225
316
  case '> Back': {
226
317
  return;
227
318
  }
228
- case "1.Disabled Slots": {
319
+ case "> Disabled Slots": {
229
320
  let disabled_slots = account_info.disabled_slots;
230
321
  if (!disabled_slots || disabled_slots.length === 0) {
231
322
  console.log(
@@ -307,7 +398,7 @@ const setDisableAddress = async (keystore_path: string, account_label: string, w
307
398
 
308
399
  break;
309
400
  }
310
- case "2.Add Single": {
401
+ case "> Add Single": {
311
402
  let account_index_or_address = await getAccountIndex(account_info, "Input address or index");
312
403
  if (account_index_or_address === null) {
313
404
  console.log(chalk.red("Invalid account index"));
@@ -329,7 +420,7 @@ const setDisableAddress = async (keystore_path: string, account_label: string, w
329
420
  saveAccountInfo(keystore_path, account_label, account_info);
330
421
  break;
331
422
  }
332
- case "3.Add Range": {
423
+ case "> Add Range": {
333
424
  let account_range = await getAccountRange(account_info);
334
425
  if (!account_range) {
335
426
  console.log(chalk.red("Invalid account range"));
@@ -351,44 +442,43 @@ const setDisableAddress = async (keystore_path: string, account_label: string, w
351
442
  }
352
443
 
353
444
 
354
- const accountManager = async (keystore_path: string, account_label: string, wative_core: typeof WativeCore) => {
445
+ const accountManager = async (keystore_path: string, account_label: string, wative_core: typeof WativeCore, password: string) => {
355
446
  let account_info = await getAccountInfo(keystore_path, account_label);
356
447
 
357
448
  let account_manager_options: any[];
358
449
 
359
450
  if (account_info.account_type === "PP") {
360
451
  account_manager_options = [
361
- '> Back',
452
+ '> Address List',
453
+ '> Expand Address',
454
+ '> Slice Address',
455
+ '> Disable Address',
456
+ '> Reset Account Password',
457
+ `> Switch Networks [${account_info.default_network}]`,
458
+ '> Remove',
362
459
  new inquirer.Separator('----------------------------------'),
363
- '1.Address List',
364
- '2.Expand Address',
365
- '3.Slice Address',
366
- '4.Disable Address',
367
- '5.Reset Account Password',
368
- `6.Switch Networks [${account_info.default_network}]`,
369
-
460
+ '> Back'
370
461
  ];
371
462
  } else {
372
463
  account_manager_options = [
373
- '> Back',
464
+ '> Address List',
465
+ '> Import Private Key',
466
+ '> Remove Address',
467
+ '> Reset Account Password',
468
+ `> Switch Networks [${account_info.default_network}]`,
469
+ '> Remove',
374
470
  new inquirer.Separator('----------------------------------'),
375
- '1.Address List',
376
- '2.Reset Account Password',
377
- `3.Switch Networks [${account_info.default_network}]`,
471
+ '> Back'
378
472
  ];
379
473
  }
380
474
 
381
475
  const selected_account_manager = await selectSomething(account_manager_options);
382
476
  switch (selected_account_manager) {
383
- case '1.Address List': {
384
- if (account_info.account_type === "PP") {
385
- await showPPAccounts(keystore_path, account_label, wative_core);
386
- } else {
387
- await showPKAccounts(keystore_path, account_label, wative_core);
388
- }
477
+ case '> Address List': {
478
+ await showAccounts(keystore_path, account_label, wative_core);
389
479
  break;
390
480
  }
391
- case '2.Expand Address': {
481
+ case '> Expand Address': {
392
482
  const count = await inputSomething('Expand accounts count', expandAmountValidator);
393
483
  if (Number(count) > 2000) {
394
484
  console.log(
@@ -400,20 +490,28 @@ const accountManager = async (keystore_path: string, account_label: string, wati
400
490
  await expandAddress(keystore_path, account_label, Number(count), wative_core);
401
491
  break;
402
492
  }
403
- case '3.Slice Address': {
493
+ case '> Import Private Key': {
494
+ await importPrivateKeyAccount(keystore_path, password, account_label, wative_core);
495
+ break;
496
+ }
497
+ case '> Slice Address': {
404
498
  const last_account_no = await inputSomething('Please input last account number', lastAccountNoValidator);
405
499
  await sliceAddress(keystore_path, account_label, Number(last_account_no), wative_core);
406
500
  break;
407
501
  }
408
- case '4.Disable Address': {
502
+ case '> Remove Address': {
503
+ await removeAddress(keystore_path, account_label, wative_core);
504
+ break;
505
+ }
506
+ case '> Disable Address': {
409
507
  await setDisableAddress(keystore_path, account_label, wative_core);
410
508
  break;
411
509
  }
412
- case '5.Reset Account Password': {
510
+ case '> Reset Account Password': {
413
511
  const password1 = await inputPassword(`Please input new password for Account [${account_label}]`);
414
512
  const password2 = await inputPassword(`Please confirm new password for Account [${account_label}]`);
415
513
 
416
- if (password1 !== password2) {
514
+ if (!password1 || !password2 || password1 !== password2) {
417
515
  console.log(
418
516
  chalk.red("Passwords do not match")
419
517
  );
@@ -423,26 +521,12 @@ const accountManager = async (keystore_path: string, account_label: string, wati
423
521
  await wative_core.account.resetPassword(account_label, password1);
424
522
  return;
425
523
  }
426
- case `3.Switch Networks [${account_info.default_network}]`: {
524
+ case `> Switch Networks [${account_info.default_network}]`: {
427
525
  await switchNetworkByAccountLabel(keystore_path, account_label, true);
428
526
  break;
429
527
  }
430
- case `6.Switch Networks [${account_info.default_network}]`: {
431
- await switchNetworkByAccountLabel(keystore_path, account_label, true);
432
- break;
433
- }
434
- case '2.Reset Account Password': {
435
- const password1 = await inputPassword(`Please input new password for Account [${account_label}]`);
436
- const password2 = await inputPassword(`Please confirm new password for Account [${account_label}]`);
437
-
438
- if (password1 !== password2) {
439
- console.log(
440
- chalk.red("Passwords do not match")
441
- );
442
- break;
443
- }
444
-
445
- await wative_core.account.resetPassword(account_label, password1);
528
+ case '> Remove': {
529
+ await removeAccountLabel(keystore_path, account_label);
446
530
  return;
447
531
  }
448
532
  case '> Back': {
@@ -450,79 +534,85 @@ const accountManager = async (keystore_path: string, account_label: string, wati
450
534
  }
451
535
  }
452
536
 
453
- await accountManager(keystore_path, account_label, wative_core);
537
+ await accountManager(keystore_path, account_label, wative_core, password);
454
538
  }
455
539
 
456
- const showPPAccounts = async (keystore_path: string, account_label: string, wative_core: typeof WativeCore) => {
540
+ const showAccounts = async (keystore_path: string, account_label: string, wative_core: typeof WativeCore) => {
457
541
  const account_info = await getAccountInfo(keystore_path, account_label);
542
+
458
543
  let option_accounts: any = [
459
544
  '> Back',
460
545
  new inquirer.Separator('----------------------------------'),
461
546
  ];
462
-
463
547
  let disabled_slots = account_info.disabled_slots;
464
- for (let i = 0; i < account_info.data.length; i++) {
548
+ let status = getStatus(keystore_path);
465
549
 
466
- let default_network = account_info.default_network;
467
- if ("default_network" in account_info.data[i]) {
468
- default_network = account_info.data[i].default_network;
469
- }
470
-
471
- let network_type = getNetworkTypeByName(keystore_path, default_network);
472
-
473
- let tag = "";
474
- if ("tag" in account_info.data[i]) {
475
- tag = `(${account_info.data[i].tag})`;
476
- }
550
+ let default_network = account_info.default_network;
551
+ let network_info = getNetworkInfoByName(keystore_path, default_network);
552
+ let network_type = getChainType(network_info.chainId.toString());
477
553
 
554
+ for (let i = 0; i < account_info.data.length; i++) {
555
+ let tag = account_info.data[i].tag ? `(${account_info.data[i].tag})` : "";
478
556
  let is_disable_address = wative_core.account.checkIsDisableAddress(disabled_slots, i);
479
- if (network_type === "evm") {
480
- if (is_disable_address) {
481
- option_accounts.push(new inquirer.Separator(
482
- chalk.yellow(`#${i} ${account_info.data[i].ciphertexts.evm.address} ${tag} [${default_network}]`)
483
- ))
484
- } else {
485
- option_accounts.push(`#${i} ${account_info.data[i].ciphertexts.evm.address} ${tag} [${default_network}]`);
486
- }
487
557
 
488
- } else if (network_type === "solana") {
489
- if (is_disable_address) {
490
- option_accounts.push(new inquirer.Separator(
491
- chalk.yellow(`#${i} ${account_info.data[i].ciphertexts.solana.address} ${tag} [${default_network}]`)
492
- ))
558
+ let account_address: string;
559
+ if (account_info.account_type === "PP") {
560
+ if (network_type === "evm") {
561
+ account_address = account_info.data[i].ciphertexts.evm.address;
493
562
  } else {
494
- option_accounts.push(`#${i} ${account_info.data[i].ciphertexts.solana.address} ${tag} [${default_network}]`);
563
+ account_address = account_info.data[i].ciphertexts.solana.address;
495
564
  }
565
+ } else {
566
+ account_address = account_info.data[i].ciphertexts.address;
567
+ }
568
+
569
+ if (is_disable_address) {
570
+ option_accounts.push(
571
+ new inquirer.Separator(chalk.yellow(`#${i} ${formatAddr(account_address, status.fullAddr)} ${tag}`))
572
+ )
573
+ } else {
574
+ option_accounts.push(`#${i} ${formatAddr(account_address, status.fullAddr)} ${tag}`);
496
575
  }
497
576
  }
498
577
 
499
- let selected_account = await selectSomething(option_accounts, "Select an account");
578
+ let selected_default_account = DEFAULT_ACCOUNT.account_label === account_label ? option_accounts[DEFAULT_ACCOUNT.account_index] : option_accounts[0];
579
+
580
+ let asset_info_list = await getAssetList(keystore_path, network_info.chainId);
581
+ let selected_account = await listWithLazyBalanceLoading(
582
+ option_accounts,
583
+ network_type === "evm",
584
+ keystore_path,
585
+ asset_info_list,
586
+ network_info,
587
+ status.gasToken,
588
+ status.extendedToken,
589
+ "Select an account",
590
+ selected_default_account
591
+ );
592
+
500
593
  if (selected_account === '> Back') {
501
594
  return;
502
595
  }
503
596
 
504
- let selected_account_id = option_accounts.indexOf(selected_account) - 2;
597
+ DEFAULT_ACCOUNT.account_label = account_label;
598
+ DEFAULT_ACCOUNT.account_index = option_accounts.indexOf(selected_account);
505
599
 
506
- await showPPAccountDetail(keystore_path, account_label, selected_account_id, wative_core);
507
- await showPPAccounts(keystore_path, account_label, wative_core);
600
+ await showAccountDetail(keystore_path, account_label, option_accounts.indexOf(selected_account) - 2, wative_core);
601
+ await showAccounts(keystore_path, account_label, wative_core);
508
602
  }
509
603
 
510
- const showPPAccountDetail = async (keystore_path: string, account_label: string, selected_account_id: number, wative_core: typeof WativeCore) => {
604
+ const showAccountDetail = async (keystore_path: string, account_label: string, selected_account_id: number, wative_core: typeof WativeCore) => {
511
605
  const account_info = await getAccountInfo(keystore_path, account_label);
512
606
 
513
607
  let default_network = account_info.default_network;
514
- if ("default_network" in account_info.data[selected_account_id]) {
515
- default_network = account_info.data[selected_account_id].default_network;
516
- }
608
+ let account_address = getAccountAddress(keystore_path, account_label, selected_account_id);
517
609
 
518
610
  const network = await getNetworkInfoByName(keystore_path, default_network);
519
- let account_address = getAccountAddress(keystore_path, account_label, selected_account_id);
520
611
  const balance_result = await getAccountBalance(
521
612
  account_address,
522
613
  keystore_path,
523
614
  default_network
524
615
  );
525
-
526
616
  if (balance_result.status) {
527
617
  console.log(
528
618
  chalk.green(` Address No: #${selected_account_id}\n Network: ${default_network}\n ${network.nativeCurrency.symbol} Balance: ${balance_result.output}`)
@@ -533,26 +623,29 @@ const showPPAccountDetail = async (keystore_path: string, account_label: string,
533
623
  );
534
624
  }
535
625
 
536
- let tag = '> Tag()';
537
- if ("tag" in account_info.data[selected_account_id] && account_info.data[selected_account_id].tag !== "") {
626
+ let tag = '> Tag';
627
+ if (
628
+ "tag" in account_info.data[selected_account_id] &&
629
+ account_info.data[selected_account_id].tag !== ""
630
+ ) {
538
631
  tag = `> Tag(${account_info.data[selected_account_id].tag})`;
539
632
  }
540
633
 
541
634
  let options_account_detail = [
542
- '> Back',
543
- new inquirer.Separator('----------------------------------'),
544
635
  tag,
545
- '> Switch network',
636
+ '> Show address',
546
637
  '> Assets',
547
638
  '> Tools',
548
- '> Dump private key'
639
+ '> Dump private key',
640
+ new inquirer.Separator('----------------------------------'),
641
+ '> Back'
549
642
  ];
550
643
 
551
644
  let selected_account_detail = await selectSomething(options_account_detail);
552
645
 
553
646
  switch (selected_account_detail) {
554
647
  case tag: {
555
- let cureent_tag = 'Current tag ()';
648
+ let cureent_tag = 'Current tag';
556
649
  if ("tag" in account_info.data[selected_account_id] && account_info.data[selected_account_id].tag !== "") {
557
650
  cureent_tag = `Current tag (${account_info.data[selected_account_id].tag})`;
558
651
  }
@@ -562,12 +655,13 @@ const showPPAccountDetail = async (keystore_path: string, account_label: string,
562
655
  saveAccountInfo(keystore_path, account_label, account_info);
563
656
  break;
564
657
  }
565
- case '> Switch network': {
566
- await switchPPNetwork(keystore_path, account_label, selected_account_id);
658
+ case '> Show address': {
659
+ await showAllAccounts(keystore_path, account_label, selected_account_id);
567
660
  break;
568
661
  }
569
662
  case '> Assets': {
570
- await showAssets(keystore_path, account_address, default_network);
663
+ await showAssetsDetail(keystore_path, default_network, account_address);
664
+ // await showAssets(keystore_path, account_address, default_network);
571
665
  break;
572
666
  }
573
667
  case '> Tools': {
@@ -587,94 +681,6 @@ const showPPAccountDetail = async (keystore_path: string, account_label: string,
587
681
  }
588
682
  }
589
683
 
590
- const showPKAccounts = async (keystore_path: string, account_label: string, wative_core: typeof WativeCore) => {
591
- const account_info = await getAccountInfo(keystore_path, account_label);
592
- let option_accounts = [
593
- '> Back',
594
- new inquirer.Separator('----------------------------------')
595
- ];
596
-
597
- let tag = "";
598
- if ("tag" in account_info && account_info.tag !== "") {
599
- tag = `(${account_info.tag})`;
600
- }
601
-
602
- option_accounts.push(`#0 ${account_info.ciphertexts.address} ${tag} [${account_info.default_network}]`);
603
- let selected_account = await selectSomething(option_accounts, "Select an account");
604
- if (selected_account === '> Back') {
605
- return;
606
- }
607
-
608
- let default_network = account_info.default_network;
609
- const network = await getNetworkInfoByName(keystore_path, default_network);
610
- console.log(
611
- chalk.green(` Address No: #0\n Network: ${default_network}\n ${network.nativeCurrency.symbol} Balance: 0`)
612
- );
613
-
614
- await showPKAccountDetail(keystore_path, account_label, wative_core);
615
- await showPKAccounts(keystore_path, account_label, wative_core);
616
- }
617
-
618
-
619
- const showPKAccountDetail = async (keystore_path: string, account_label: string, wative_core: typeof WativeCore) => {
620
- const account_info = await getAccountInfo(keystore_path, account_label);
621
- let tag = '> Tag()';
622
- if ("tag" in account_info && account_info.tag !== "") {
623
- tag = `> Tag(${account_info.tag})`;
624
- }
625
-
626
- let options_account_detail = [
627
- '> Back',
628
- new inquirer.Separator('----------------------------------'),
629
- tag,
630
- '> Switch network',
631
- '> Assets',
632
- '> Tools',
633
- '> Dump private key',
634
- ];
635
-
636
- let selected_account_detail = await selectSomething(options_account_detail);
637
-
638
- switch (selected_account_detail) {
639
- case tag: {
640
- let cureent_tag = 'Current tag ()';
641
- if ("tag" in account_info && account_info.tag !== "") {
642
- cureent_tag = `Current tag (${account_info.tag})`;
643
- }
644
- let tag = await inputSomething(cureent_tag, tagValidator);
645
- account_info.tag = tag.replace(/\s+/g, " ").trim().toLocaleLowerCase();
646
- saveAccountInfo(keystore_path, account_label, account_info);
647
- break;
648
- }
649
- case '> Switch network': {
650
- await switchNetworkByAccountLabel(keystore_path, account_label, false);
651
- break;
652
- }
653
- case '> Assets': {
654
- let account_address = getPKAccountAddress(keystore_path, account_label);
655
- await showAssets(keystore_path, account_address, account_info.default_network);
656
- break;
657
- }
658
- case '> Tools': {
659
- let account_address = getPKAccountAddress(keystore_path, account_label);
660
- await selectToolsOptions(keystore_path, account_label, account_address, wative_core);
661
- break;
662
- }
663
- case '> Dump private key': {
664
- let account_address = getPKAccountAddress(keystore_path, account_label);
665
- let private_key = await wative_core.account.showPrivateKey(account_address);
666
- console.log("account: ", account_address);
667
- console.log("private key: ", private_key);
668
- break;
669
- }
670
- case '> Back': {
671
- return;
672
- }
673
- }
674
-
675
- await showPKAccountDetail(keystore_path, account_label, wative_core);
676
- }
677
-
678
684
  const expandAddress = async (keystore_path: string, account_label: string, expand_amount: number, wative_core: typeof WativeCore) => {
679
685
  const account_info = await getAccountInfo(keystore_path, account_label);
680
686
  const account_len = account_info.data.length;
@@ -696,6 +702,7 @@ const expandAddress = async (keystore_path: string, account_label: string, expan
696
702
  bar1.stop();
697
703
 
698
704
  saveAccountInfo(keystore_path, account_label, account_info);
705
+ wative_core.account.reloadAccount();
699
706
  }
700
707
 
701
708
  const sliceAddress = async (keystore_path: string, account_label: string, last_account_no: number, wative_core: typeof WativeCore) => {
@@ -705,6 +712,41 @@ const sliceAddress = async (keystore_path: string, account_label: string, last_a
705
712
  wative_core.account.reloadAccount();
706
713
  }
707
714
 
715
+ const removeAddress = async (keystore_path: string, account_label: string, wative_core: typeof WativeCore) => {
716
+ let account_index = await inputSomething('Please input the accountIndex to remove this account', numberValidator);
717
+ if (account_index === "0") {
718
+ console.log(chalk.red("Can't remove the first account"));
719
+ return;
720
+ }
721
+
722
+ let account_info = await getAccountInfo(keystore_path, account_label);
723
+ let isConfirm = await confirmSomething(`Are you sure you want to remove this account(${account_info.data[account_index].ciphertexts.address})`);
724
+ if (!isConfirm) {
725
+ return;
726
+ }
727
+
728
+ account_info.data.splice(Number(account_index), 1);
729
+ saveAccountInfo(keystore_path, account_label, account_info);
730
+ wative_core.account.reloadAccount();
731
+ }
732
+
733
+ const removeAccountLabel = async (keystore_path: string, account_label: string) => {
734
+ let _accountLabel = await inputSomething('Please input the accountLabel to remove this account');
735
+ if (_accountLabel !== account_label) {
736
+ console.log(chalk.red("Can't remove this account"));
737
+ return;
738
+ }
739
+
740
+ const isConfirm = await confirmSomething(`Are you sure you want to remove this account(${account_label})`);
741
+ if (!isConfirm) {
742
+ return;
743
+ }
744
+
745
+ let account_label_path = path.join(keystore_path, `accounts/${account_label}.json`);
746
+ fs.rmSync(account_label_path);
747
+ }
748
+
749
+
708
750
  export const accountSetting = async (keystore_path: string, wative_core: typeof WativeCore) => {
709
751
  const account_path = path.resolve(keystore_path, 'accounts');
710
752
  if (!fs.existsSync(account_path)) {
@@ -717,16 +759,16 @@ export const accountSetting = async (keystore_path: string, wative_core: typeof
717
759
  let account_setting_options: any[];
718
760
  if (account_label_list.length === 0) {
719
761
  account_setting_options = [
720
- '> Back',
762
+ '> Create a new account',
721
763
  new inquirer.Separator('----------------------------------'),
722
- '> Create a new account'
764
+ '> Back'
723
765
  ];
724
766
  } else {
725
767
  account_setting_options = [
726
- '> Back',
727
- new inquirer.Separator('----------------------------------'),
728
768
  '> Select an account',
729
- '> Create a new account'
769
+ '> Create a new account',
770
+ new inquirer.Separator('----------------------------------'),
771
+ '> Back'
730
772
  ];
731
773
  }
732
774