wative 1.0.34 → 1.0.36
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/lib/index.esm.js +1 -1
- package/lib/index.umd.js +1 -1
- package/package.json +1 -1
- package/src/account.ts +103 -98
- package/src/index.ts +3 -3
- package/src/network.ts +22 -22
- package/src/tools.ts +15 -15
- package/src/utils.ts +37 -39
package/src/network.ts
CHANGED
|
@@ -541,8 +541,8 @@ export const getNetworkInfoByName = (keystore_path: string, network_name: string
|
|
|
541
541
|
return null;
|
|
542
542
|
}
|
|
543
543
|
|
|
544
|
-
export const getAccountAddress = (keystore_path: string,
|
|
545
|
-
const account_info = getAccountInfo(keystore_path,
|
|
544
|
+
export const getAccountAddress = (keystore_path: string, keystore_slug: string, account_id: number) => {
|
|
545
|
+
const account_info = getAccountInfo(keystore_path, keystore_slug);
|
|
546
546
|
|
|
547
547
|
if (account_info.account_type === "PK") {
|
|
548
548
|
return account_info.data[account_id].ciphertexts.address;
|
|
@@ -560,18 +560,18 @@ export const getAccountAddress = (keystore_path: string, account_label: string,
|
|
|
560
560
|
return null;
|
|
561
561
|
}
|
|
562
562
|
|
|
563
|
-
export const getPKAccountAddress = (keystore_path: string,
|
|
564
|
-
const account_info = getAccountInfo(keystore_path,
|
|
563
|
+
export const getPKAccountAddress = (keystore_path: string, keystore_slug: string) => {
|
|
564
|
+
const account_info = getAccountInfo(keystore_path, keystore_slug);
|
|
565
565
|
return account_info.ciphertexts.address;
|
|
566
566
|
}
|
|
567
567
|
|
|
568
|
-
export const switchPPNetwork = async (keystore_path: string,
|
|
568
|
+
export const switchPPNetwork = async (keystore_path: string, keystore_slug: string, account_id: number) => {
|
|
569
569
|
const network_path = path.resolve(keystore_path, 'network.json');
|
|
570
570
|
const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
|
|
571
571
|
const network_list = networks.networks;
|
|
572
572
|
const network_len = network_list.length;
|
|
573
573
|
|
|
574
|
-
let account_info = getAccountInfo(keystore_path,
|
|
574
|
+
let account_info = getAccountInfo(keystore_path, keystore_slug);
|
|
575
575
|
let options_switch_network = [
|
|
576
576
|
'> Back',
|
|
577
577
|
new inquirer.Separator('----------------------------------')
|
|
@@ -613,7 +613,7 @@ export const switchPPNetwork = async (keystore_path: string, account_label: stri
|
|
|
613
613
|
account_info.data[account_id].default_network = network_list[selected_network_id - 3].name;
|
|
614
614
|
}
|
|
615
615
|
|
|
616
|
-
saveAccountInfo(keystore_path,
|
|
616
|
+
saveAccountInfo(keystore_path, keystore_slug, account_info);
|
|
617
617
|
|
|
618
618
|
let new_default_network = account_info.default_network;
|
|
619
619
|
if ("default_network" in account_info.data[account_id] && account_info.data[account_id].default_network !== "") {
|
|
@@ -626,13 +626,13 @@ export const switchPPNetwork = async (keystore_path: string, account_label: stri
|
|
|
626
626
|
);
|
|
627
627
|
}
|
|
628
628
|
|
|
629
|
-
export const showAllAccounts = async (keystore_path: string,
|
|
629
|
+
export const showAllAccounts = async (keystore_path: string, keystore_slug: string, account_index: number) => {
|
|
630
630
|
const network_path = path.resolve(keystore_path, 'network.json');
|
|
631
631
|
const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
|
|
632
632
|
const network_list = networks.networks;
|
|
633
633
|
const network_len = network_list.length;
|
|
634
634
|
|
|
635
|
-
let account_info = getAccountInfo(keystore_path,
|
|
635
|
+
let account_info = getAccountInfo(keystore_path, keystore_slug);
|
|
636
636
|
let default_network = account_info.default_network;
|
|
637
637
|
|
|
638
638
|
const network_type = getNetworkTypeByName(keystore_path, default_network);
|
|
@@ -664,13 +664,13 @@ export const showAllAccounts = async (keystore_path: string, account_label: stri
|
|
|
664
664
|
}
|
|
665
665
|
}
|
|
666
666
|
|
|
667
|
-
export const
|
|
667
|
+
export const switchNetworkByAccountName = async (keystore_path: string, keystore_slug: string, is_keystore_slug: boolean) => {
|
|
668
668
|
const network_path = path.resolve(keystore_path, 'network.json');
|
|
669
669
|
const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
|
|
670
670
|
const network_list = networks.networks;
|
|
671
671
|
const network_len = network_list.length;
|
|
672
672
|
|
|
673
|
-
let account_info = getAccountInfo(keystore_path,
|
|
673
|
+
let account_info = getAccountInfo(keystore_path, keystore_slug);
|
|
674
674
|
let options_switch_network = [
|
|
675
675
|
'> Back',
|
|
676
676
|
new inquirer.Separator('----------------------------------'),
|
|
@@ -679,7 +679,7 @@ export const switchNetworkByAccountLabel = async (keystore_path: string, account
|
|
|
679
679
|
let default_network = account_info.default_network;
|
|
680
680
|
|
|
681
681
|
let account_address = "";
|
|
682
|
-
if (!
|
|
682
|
+
if (!is_keystore_slug) {
|
|
683
683
|
account_address = account_info.ciphertexts.address;
|
|
684
684
|
}
|
|
685
685
|
let option_network_name_list: string[] = [];
|
|
@@ -693,13 +693,13 @@ export const switchNetworkByAccountLabel = async (keystore_path: string, account
|
|
|
693
693
|
continue;
|
|
694
694
|
}
|
|
695
695
|
if (network.name === default_network) {
|
|
696
|
-
if (
|
|
696
|
+
if (is_keystore_slug) {
|
|
697
697
|
options_switch_network.push(`● ${network.name}`);
|
|
698
698
|
} else {
|
|
699
699
|
options_switch_network.push(`● ${account_address} [${network.name}]`);
|
|
700
700
|
}
|
|
701
701
|
} else {
|
|
702
|
-
if (
|
|
702
|
+
if (is_keystore_slug) {
|
|
703
703
|
options_switch_network.push(`○ ${network.name}`);
|
|
704
704
|
} else {
|
|
705
705
|
options_switch_network.push(`○ ${account_address} [${network.name}]`);
|
|
@@ -715,9 +715,9 @@ export const switchNetworkByAccountLabel = async (keystore_path: string, account
|
|
|
715
715
|
|
|
716
716
|
let selected_network_id = options_switch_network.indexOf(selected_network);
|
|
717
717
|
account_info.default_network = option_network_name_list[selected_network_id - 2];
|
|
718
|
-
saveAccountInfo(keystore_path,
|
|
718
|
+
saveAccountInfo(keystore_path, keystore_slug, account_info);
|
|
719
719
|
|
|
720
|
-
if (!
|
|
720
|
+
if (!is_keystore_slug) {
|
|
721
721
|
let new_default_network = account_info.default_network;
|
|
722
722
|
let new_network = getNetworkInfoByName(keystore_path, new_default_network);
|
|
723
723
|
console.log(
|
|
@@ -726,14 +726,14 @@ export const switchNetworkByAccountLabel = async (keystore_path: string, account
|
|
|
726
726
|
}
|
|
727
727
|
}
|
|
728
728
|
|
|
729
|
-
export const accountBalanceLog = async (
|
|
730
|
-
const account_info = getAccountInfo(keystore_path,
|
|
729
|
+
export const accountBalanceLog = async (keystore_slug: string, account_id: number, keystore_path: string, default_network: string) => {
|
|
730
|
+
const account_info = getAccountInfo(keystore_path, keystore_slug);
|
|
731
731
|
|
|
732
732
|
let account_address: string;
|
|
733
733
|
if (account_info.account_type === "PK") {
|
|
734
|
-
account_address = getAccountAddress(keystore_path,
|
|
734
|
+
account_address = getAccountAddress(keystore_path, keystore_slug, account_id);
|
|
735
735
|
} else {
|
|
736
|
-
account_address = getPKAccountAddress(keystore_path,
|
|
736
|
+
account_address = getPKAccountAddress(keystore_path, keystore_slug);
|
|
737
737
|
}
|
|
738
738
|
|
|
739
739
|
const balance_result = await getAccountBalance(account_address, keystore_path, default_network);
|
|
@@ -778,8 +778,8 @@ export const getAccountBalance = async (account: string, keystore_path: string,
|
|
|
778
778
|
return result;
|
|
779
779
|
}
|
|
780
780
|
|
|
781
|
-
export const getDefaultNetworkByAddress = (keystore_path: string,
|
|
782
|
-
const account_info = getAccountInfo(keystore_path,
|
|
781
|
+
export const getDefaultNetworkByAddress = (keystore_path: string, keystore_slug: string, account_address: string) => {
|
|
782
|
+
const account_info = getAccountInfo(keystore_path, keystore_slug);
|
|
783
783
|
if (account_info.account_type === "PK") {
|
|
784
784
|
return account_info.default_network;
|
|
785
785
|
}
|
package/src/tools.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const chalk = require('chalk');
|
|
2
2
|
import { BigNumber } from 'bignumber.js';
|
|
3
3
|
import inquirer from 'inquirer';
|
|
4
|
-
import { addrValidator, chainAddressValidator, confirmSomething, editorSomething, gasValidator, getAccountChainType, hexValidator, inputSomething, numberValidator, selectSomething,
|
|
4
|
+
import { addrValidator, chainAddressValidator, confirmSomething, editorSomething, gasValidator, getAccountChainType, hexValidator, inputSomething, numberValidator, selectSomething, getAccountNameByKeystoreSlug } from "./utils";
|
|
5
5
|
import { loginIn } from './account';
|
|
6
6
|
import { getAccountBalance, getDefaultNetworkByAddress, getEvmNetworks, getNetworkInfoByName, getNetworkTypeByName } from './network';
|
|
7
7
|
import { GasUtil } from './tx_gas_utils';
|
|
@@ -720,8 +720,8 @@ const getTransferTokenAmount = async (max_value: string, token_symbols: string,
|
|
|
720
720
|
return getTransferTokenAmount(max_value, token_symbols, decimals);
|
|
721
721
|
}
|
|
722
722
|
|
|
723
|
-
const excuteTools = async (keystore_path: string,
|
|
724
|
-
let default_network = getDefaultNetworkByAddress(keystore_path,
|
|
723
|
+
const excuteTools = async (keystore_path: string, keystore_slug: string, account_address: string, wative_core: typeof WativeCore) => {
|
|
724
|
+
let default_network = getDefaultNetworkByAddress(keystore_path, keystore_slug, account_address);
|
|
725
725
|
let network_type = getNetworkTypeByName(keystore_path, default_network);
|
|
726
726
|
let account_chain_type = getAccountChainType(account_address);
|
|
727
727
|
|
|
@@ -817,7 +817,7 @@ const excuteTools = async (keystore_path: string, account_label: string, account
|
|
|
817
817
|
}
|
|
818
818
|
}
|
|
819
819
|
|
|
820
|
-
export const selectToolsOptions = async (keystore_path: string,
|
|
820
|
+
export const selectToolsOptions = async (keystore_path: string, keystore_slug: string, account_address: string, wative_core: typeof WativeCore) => {
|
|
821
821
|
const tools_options = [
|
|
822
822
|
'> SendTx',
|
|
823
823
|
'> SignMessage',
|
|
@@ -828,7 +828,7 @@ export const selectToolsOptions = async (keystore_path: string, account_label: s
|
|
|
828
828
|
const selected_tools_option = await selectSomething(tools_options);
|
|
829
829
|
switch (selected_tools_option) {
|
|
830
830
|
case '> SendTx': {
|
|
831
|
-
await excuteTools(keystore_path,
|
|
831
|
+
await excuteTools(keystore_path, keystore_slug, account_address, wative_core);
|
|
832
832
|
break;
|
|
833
833
|
}
|
|
834
834
|
case '> SignMessage': {
|
|
@@ -847,7 +847,7 @@ export const selectToolsOptions = async (keystore_path: string, account_label: s
|
|
|
847
847
|
return;
|
|
848
848
|
}
|
|
849
849
|
}
|
|
850
|
-
await selectToolsOptions(keystore_path,
|
|
850
|
+
await selectToolsOptions(keystore_path, keystore_slug, account_address, wative_core);
|
|
851
851
|
}
|
|
852
852
|
|
|
853
853
|
export const showTools = async (keystore_path: string, wative_core: typeof WativeCore) => {
|
|
@@ -873,28 +873,28 @@ export const showTools = async (keystore_path: string, wative_core: typeof Wativ
|
|
|
873
873
|
account_address = await inputSomething("Input part address to send tx", addrValidator);
|
|
874
874
|
}
|
|
875
875
|
|
|
876
|
-
let
|
|
877
|
-
if (
|
|
876
|
+
let keystore_slug_info = wative_core.account.getAccountLabel(account_address);
|
|
877
|
+
if (keystore_slug_info === null) {
|
|
878
878
|
console.log(chalk.red("Account not found"));
|
|
879
879
|
await showTools(keystore_path, wative_core);
|
|
880
880
|
return;
|
|
881
881
|
}
|
|
882
882
|
|
|
883
|
-
let
|
|
884
|
-
let
|
|
885
|
-
console.log(chalk.green(`Found account
|
|
883
|
+
let keystore_slug = keystore_slug_info.account_label;
|
|
884
|
+
let account_name = getAccountNameByKeystoreSlug(keystore_path, keystore_slug);
|
|
885
|
+
console.log(chalk.green(`Found account name: [${account_name}]`));
|
|
886
886
|
|
|
887
|
-
account_address =
|
|
888
|
-
if (
|
|
887
|
+
account_address = keystore_slug_info.account_address;
|
|
888
|
+
if (keystore_slug === null) {
|
|
889
889
|
console.log(chalk.red("Account not found"));
|
|
890
890
|
await showTools(keystore_path, wative_core);
|
|
891
891
|
return;
|
|
892
892
|
}
|
|
893
|
-
let login_result = await loginIn(
|
|
893
|
+
let login_result = await loginIn(keystore_path, keystore_slug, wative_core);
|
|
894
894
|
if (!login_result.isLoginIn) {
|
|
895
895
|
await showTools(keystore_path, wative_core);
|
|
896
896
|
return;
|
|
897
897
|
}
|
|
898
|
-
await selectToolsOptions(keystore_path,
|
|
898
|
+
await selectToolsOptions(keystore_path, keystore_slug, account_address, wative_core);
|
|
899
899
|
await showTools(keystore_path, wative_core);
|
|
900
900
|
}
|
package/src/utils.ts
CHANGED
|
@@ -332,52 +332,50 @@ export const getColor = async (text: string) => {
|
|
|
332
332
|
return null;
|
|
333
333
|
}
|
|
334
334
|
|
|
335
|
-
export const
|
|
335
|
+
export const getAccountNameByKeystoreSlug = (keystore_path: string, keystore_slug: string) => {
|
|
336
336
|
const networks = getNetworkInfo(keystore_path);
|
|
337
|
-
|
|
338
|
-
const
|
|
339
|
-
const
|
|
340
|
-
|
|
341
|
-
const index = formatted_account_label_list.indexOf(account_label);
|
|
337
|
+
const account_name_list = networks.accounts;
|
|
338
|
+
const keystore_slug_list = batchGetKeystoreSlugByAccountName(account_name_list);
|
|
339
|
+
const index = keystore_slug_list.indexOf(keystore_slug);
|
|
342
340
|
if (index !== -1) {
|
|
343
|
-
return
|
|
341
|
+
return account_name_list[index];
|
|
344
342
|
}
|
|
345
|
-
return
|
|
343
|
+
return keystore_slug;
|
|
346
344
|
}
|
|
347
345
|
|
|
348
|
-
export const
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
346
|
+
export const getKeystoreSlugByAccountName = (account_name: string) => {
|
|
347
|
+
account_name = account_name.trim();
|
|
348
|
+
account_name = account_name.toLocaleLowerCase();
|
|
349
|
+
account_name = account_name.replace(/\s+/g, "-").trim();
|
|
350
|
+
account_name = account_name.replace(/\-+/g, "-").trim();
|
|
351
|
+
return account_name;
|
|
353
352
|
}
|
|
354
353
|
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
let
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
format_account_label(account_label_list[i])
|
|
354
|
+
export const batchGetKeystoreSlugByAccountName = (account_name_list: string[]) => {
|
|
355
|
+
let keystore_slug_list: string[] = [];
|
|
356
|
+
for (let i = 0; i < account_name_list.length; i++) {
|
|
357
|
+
keystore_slug_list.push(
|
|
358
|
+
getKeystoreSlugByAccountName(account_name_list[i])
|
|
361
359
|
);
|
|
362
360
|
}
|
|
363
|
-
return
|
|
361
|
+
return keystore_slug_list;
|
|
364
362
|
}
|
|
365
363
|
|
|
366
364
|
|
|
367
|
-
export const
|
|
368
|
-
let
|
|
365
|
+
export const getAccountName = async (text: string, account_name_list: string[]) => {
|
|
366
|
+
let keystore_slug_list = batchGetKeystoreSlugByAccountName(account_name_list);
|
|
369
367
|
let reg = /^(?![-_])[a-zA-Z0-9_-]{4,20}(?<![-_])$/g;
|
|
370
368
|
for (let i = 0; i < 5; i++) {
|
|
371
|
-
let
|
|
372
|
-
|
|
373
|
-
let
|
|
369
|
+
let account_name = await inputSomething(text);
|
|
370
|
+
account_name = account_name.trim();
|
|
371
|
+
let keystore_slug = getKeystoreSlugByAccountName(account_name);
|
|
374
372
|
|
|
375
|
-
if (reg.test(
|
|
376
|
-
if (
|
|
377
|
-
console.log(chalk.red("
|
|
373
|
+
if (reg.test(keystore_slug)) {
|
|
374
|
+
if (keystore_slug_list.includes(keystore_slug)) {
|
|
375
|
+
console.log(chalk.red("Account name already exists"));
|
|
378
376
|
continue;
|
|
379
377
|
} else {
|
|
380
|
-
return
|
|
378
|
+
return account_name;
|
|
381
379
|
}
|
|
382
380
|
}
|
|
383
381
|
}
|
|
@@ -750,22 +748,22 @@ export const updateStorage = (keystore_path: string) => {
|
|
|
750
748
|
|
|
751
749
|
let account_list_len = networks.accounts.length;
|
|
752
750
|
for (let i = 0; i < account_list_len; i++) {
|
|
753
|
-
let
|
|
754
|
-
let
|
|
755
|
-
let
|
|
751
|
+
let account_name = networks.accounts[account_list_len - 1 - i];
|
|
752
|
+
let keystore_slug = getKeystoreSlugByAccountName(account_name);
|
|
753
|
+
let keystore_slug_path = path.resolve(keystore_path, `accounts/${keystore_slug}.json`);
|
|
756
754
|
|
|
757
|
-
if (!fs.existsSync(
|
|
755
|
+
if (!fs.existsSync(keystore_slug_path)) {
|
|
758
756
|
networks.accounts.splice(account_list_len - 1 - i, 1);
|
|
759
757
|
}
|
|
760
758
|
}
|
|
761
759
|
|
|
762
760
|
fs.writeFileSync(network_path, JSON.stringify(networks, null, 4));
|
|
763
|
-
let
|
|
764
|
-
|
|
761
|
+
let account_name_list = networks.accounts;
|
|
762
|
+
let keystore_slug_list = batchGetKeystoreSlugByAccountName(account_name_list);
|
|
765
763
|
|
|
766
|
-
for (let i = 0; i <
|
|
767
|
-
let
|
|
768
|
-
let account_info = JSON.parse(fs.readFileSync(
|
|
764
|
+
for (let i = 0; i < keystore_slug_list.length; i++) {
|
|
765
|
+
let keystore_slug_path = path.resolve(keystore_path, `accounts/${keystore_slug_list[i]}.json`);
|
|
766
|
+
let account_info = JSON.parse(fs.readFileSync(keystore_slug_path, 'utf8'));
|
|
769
767
|
if (account_info.account_type === "PP") {
|
|
770
768
|
continue;
|
|
771
769
|
}
|
|
@@ -778,6 +776,6 @@ export const updateStorage = (keystore_path: string) => {
|
|
|
778
776
|
}
|
|
779
777
|
];
|
|
780
778
|
delete account_info.ciphertexts;
|
|
781
|
-
fs.writeFileSync(
|
|
779
|
+
fs.writeFileSync(keystore_slug_path, JSON.stringify(account_info, null, 4));
|
|
782
780
|
}
|
|
783
781
|
}
|