wative 1.0.33 → 1.0.35
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 +28 -17
- package/src/index.ts +3 -2
- package/src/tools.ts +6 -17
- package/src/utils.ts +57 -4
package/src/account.ts
CHANGED
|
@@ -16,9 +16,11 @@ import {
|
|
|
16
16
|
numberValidator,
|
|
17
17
|
formatAddr,
|
|
18
18
|
listWithLazyBalanceLoading,
|
|
19
|
+
inputPasswordWithoutValidator,
|
|
20
|
+
format_account_label,
|
|
19
21
|
} from "./utils";
|
|
20
22
|
import { getAccountAddress, getAccountBalance, getNetworkInfoByName, getNetworkTypeByName, getStatus, selectDefaultNetwork, showAllAccounts, switchNetworkByAccountLabel } from "./network";
|
|
21
|
-
import { getAssetList,
|
|
23
|
+
import { getAssetList, showAssetsDetail } from './assets';
|
|
22
24
|
import { selectToolsOptions } from './tools';
|
|
23
25
|
import inquirer from "inquirer";
|
|
24
26
|
import { getChainType } from "./chain";
|
|
@@ -70,14 +72,15 @@ const importPassphrase = async (keystore_path: string, wative_core: typeof Wativ
|
|
|
70
72
|
let passphrase = await inputSomething('Please enter a mnemonic [split by space]', passphraseValidator);
|
|
71
73
|
passphrase = passphrase.replace(/\s+/g, " ");
|
|
72
74
|
|
|
73
|
-
|
|
75
|
+
let formatted_account_label = format_account_label(pre_account_info.account_label);
|
|
76
|
+
const passphrase_account = wative_core.account.generatePPAccount(formatted_account_label, passphrase, pre_account_info.password);
|
|
74
77
|
if (!passphrase_account.status) {
|
|
75
78
|
console.log(
|
|
76
79
|
chalk.red(passphrase_account.output)
|
|
77
80
|
);
|
|
78
81
|
return;
|
|
79
82
|
}
|
|
80
|
-
let account_info_path = path.join(keystore_path, `accounts/${
|
|
83
|
+
let account_info_path = path.join(keystore_path, `accounts/${formatted_account_label}.json`);
|
|
81
84
|
|
|
82
85
|
passphrase_account.output.default_network = pre_account_info.selected_default_network;
|
|
83
86
|
fs.writeFileSync(account_info_path, JSON.stringify(passphrase_account.output, null, 4));
|
|
@@ -96,10 +99,11 @@ const createPrivateKeyAccount = async (keystore_path: string, wative_core: typeo
|
|
|
96
99
|
}
|
|
97
100
|
|
|
98
101
|
let private_key = await inputSomething('Please enter a private key');
|
|
102
|
+
let formatted_account_label = format_account_label(pre_account_info.account_label);
|
|
99
103
|
await _importAccount(
|
|
100
104
|
keystore_path,
|
|
101
105
|
pre_account_info.password,
|
|
102
|
-
|
|
106
|
+
formatted_account_label,
|
|
103
107
|
private_key,
|
|
104
108
|
pre_account_info.selected_default_network,
|
|
105
109
|
wative_core,
|
|
@@ -109,6 +113,7 @@ const createPrivateKeyAccount = async (keystore_path: string, wative_core: typeo
|
|
|
109
113
|
const network_path = path.resolve(keystore_path, 'network.json');
|
|
110
114
|
const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
|
|
111
115
|
networks.accounts.push(pre_account_info.account_label);
|
|
116
|
+
|
|
112
117
|
fs.writeFileSync(network_path, JSON.stringify(networks, null, 4));
|
|
113
118
|
}
|
|
114
119
|
|
|
@@ -217,7 +222,7 @@ const createAccount = async (keystore_path: string, wative_core: typeof WativeCo
|
|
|
217
222
|
wative_core.account.reloadAccount();
|
|
218
223
|
}
|
|
219
224
|
|
|
220
|
-
const getNetworkInfo = (keystore_path: string) => {
|
|
225
|
+
export const getNetworkInfo = (keystore_path: string) => {
|
|
221
226
|
const network_path = path.resolve(keystore_path, 'network.json');
|
|
222
227
|
const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
|
|
223
228
|
return networks;
|
|
@@ -241,15 +246,11 @@ export const loginIn = async (account_label: string, wative_core: typeof WativeC
|
|
|
241
246
|
}
|
|
242
247
|
|
|
243
248
|
for (let i = 0; i < 3; i++) {
|
|
244
|
-
let isConfirm = await confirmSomething(`Confirm select account [${account_label}]`);
|
|
245
|
-
if (!isConfirm) {
|
|
246
|
-
return { isLoginIn: false };
|
|
247
|
-
}
|
|
248
249
|
let password: any;
|
|
249
250
|
if (process.env.WativePassword && process.env.WativePassword !== '') {
|
|
250
251
|
password = process.env.WativePassword;
|
|
251
252
|
} else {
|
|
252
|
-
password = await
|
|
253
|
+
password = await inputPasswordWithoutValidator(`Please input the password for Account [${account_label}]`);
|
|
253
254
|
}
|
|
254
255
|
if (!password) {
|
|
255
256
|
console.log(
|
|
@@ -261,10 +262,17 @@ export const loginIn = async (account_label: string, wative_core: typeof WativeC
|
|
|
261
262
|
const login_result = await wative_core.account.login(account_label, password);
|
|
262
263
|
if (login_result.status) {
|
|
263
264
|
return { isLoginIn: true, password };
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
const options = [
|
|
268
|
+
'> Try Again',
|
|
269
|
+
'> Back'
|
|
270
|
+
];
|
|
271
|
+
const selected_option = await selectSomething(options, login_result.output);
|
|
272
|
+
if (selected_option === '> Back') {
|
|
273
|
+
return { isLoginIn: false };
|
|
274
|
+
} else if (selected_option === '> Try Again') {
|
|
275
|
+
continue;
|
|
268
276
|
}
|
|
269
277
|
}
|
|
270
278
|
return { isLoginIn: false };
|
|
@@ -289,7 +297,8 @@ const selectAccount = async (keystore_path: string, wative_core: typeof WativeCo
|
|
|
289
297
|
|
|
290
298
|
for (let i = 0; i < account_label_len; i++) {
|
|
291
299
|
const account_label = account_label_list[i];
|
|
292
|
-
const
|
|
300
|
+
const formatted_account_label = format_account_label(account_label);
|
|
301
|
+
const account_info = getAccountInfo(keystore_path, formatted_account_label);
|
|
293
302
|
if ("data" in account_info) {
|
|
294
303
|
select_account_options.push(`${i + 1}) ${account_label} [${account_info.account_type}:${account_info.data.length}]`);
|
|
295
304
|
} else {
|
|
@@ -303,9 +312,11 @@ const selectAccount = async (keystore_path: string, wative_core: typeof WativeCo
|
|
|
303
312
|
}
|
|
304
313
|
|
|
305
314
|
const selected_account_label = account_label_list[select_account_options.indexOf(selected_account) - 2];
|
|
306
|
-
|
|
315
|
+
const formatted_account_label = format_account_label(selected_account_label);
|
|
316
|
+
|
|
317
|
+
let login_result = await loginIn(formatted_account_label, wative_core);
|
|
307
318
|
if (login_result.isLoginIn) {
|
|
308
|
-
await accountManager(keystore_path,
|
|
319
|
+
await accountManager(keystore_path, formatted_account_label, wative_core, login_result.password);
|
|
309
320
|
}
|
|
310
321
|
|
|
311
322
|
await selectAccount(keystore_path, wative_core);
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as fs from "fs";
|
|
2
2
|
import * as path from "path";
|
|
3
|
-
import { showIntroduction, getKeystorePath, updateStorage } from "./utils";
|
|
3
|
+
import { showIntroduction, getKeystorePath, updateStorage, batch_format_account_label } from "./utils";
|
|
4
4
|
import { createNetwork } from "./network";
|
|
5
5
|
import { showHomePage } from "./home_page";
|
|
6
6
|
|
|
@@ -20,7 +20,8 @@ const main = async () => {
|
|
|
20
20
|
|
|
21
21
|
const network_path = path.resolve(keystore_path, 'network.json');
|
|
22
22
|
const networks = JSON.parse(fs.readFileSync(network_path, 'utf8'));
|
|
23
|
-
|
|
23
|
+
let format_account_label_list = batch_format_account_label(networks.accounts);
|
|
24
|
+
const wative_core = new WativeCore(keystore_path, format_account_label_list, null, true);
|
|
24
25
|
await showHomePage(keystore_path, wative_core);
|
|
25
26
|
}
|
|
26
27
|
|
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 } from "./utils";
|
|
4
|
+
import { addrValidator, chainAddressValidator, confirmSomething, editorSomething, gasValidator, getAccountChainType, hexValidator, inputSomething, numberValidator, selectSomething, un_format_account_label } 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';
|
|
@@ -537,20 +537,6 @@ const sendSolanaTokenTransaction = async (keystore_path: string, chain_rpc_url:
|
|
|
537
537
|
return;
|
|
538
538
|
}
|
|
539
539
|
|
|
540
|
-
// let amount: any;
|
|
541
|
-
// for (let i = 0; i < 5; i++) {
|
|
542
|
-
// let input_amount = await inputSomething("Amount", numberValidator);
|
|
543
|
-
// if ((new BigNumber(input_amount).multipliedBy(Math.pow(10, Number(token_decimals)))).isLessThanOrEqualTo(new BigNumber(token_balance_result.output))) {
|
|
544
|
-
// amount = input_amount;
|
|
545
|
-
// break;
|
|
546
|
-
// }
|
|
547
|
-
// console.log(chalk.red("Not enough balance"));
|
|
548
|
-
// }
|
|
549
|
-
|
|
550
|
-
// if (!amount) {
|
|
551
|
-
// return;
|
|
552
|
-
// }
|
|
553
|
-
|
|
554
540
|
let transfer_amount = await getTransferTokenAmount(
|
|
555
541
|
token_balance_result.output,
|
|
556
542
|
token_name,
|
|
@@ -713,14 +699,14 @@ const getTransferTokenAmount = async (max_value: string, token_symbols: string,
|
|
|
713
699
|
let total_balance = (new BigNumber(max_value)).dividedBy(10 ** decimals).toFixed(4, BigNumber.ROUND_FLOOR);
|
|
714
700
|
|
|
715
701
|
let option_select_value_list: any = [
|
|
716
|
-
'> Input
|
|
702
|
+
'> Input amount',
|
|
717
703
|
`> Total Balance (${total_balance} ${token_symbols})`
|
|
718
704
|
]
|
|
719
705
|
let selected_value = await selectSomething(option_select_value_list);
|
|
720
706
|
let value_id = option_select_value_list.indexOf(selected_value);
|
|
721
707
|
let value = "0";
|
|
722
708
|
if (value_id === 0) {
|
|
723
|
-
let input_value = await inputSomething("
|
|
709
|
+
let input_value = await inputSomething("amount", numberValidator);
|
|
724
710
|
value = new BigNumber(input_value).multipliedBy(10 ** decimals).toFixed(0, BigNumber.ROUND_FLOOR);
|
|
725
711
|
} else if (value_id === 1) {
|
|
726
712
|
value = max_value;
|
|
@@ -895,6 +881,9 @@ export const showTools = async (keystore_path: string, wative_core: typeof Wativ
|
|
|
895
881
|
}
|
|
896
882
|
|
|
897
883
|
let account_label = account_label_info.account_label;
|
|
884
|
+
let show_account_label = un_format_account_label(keystore_path, account_label);
|
|
885
|
+
console.log(chalk.green(`Found account label: [${show_account_label}]`));
|
|
886
|
+
|
|
898
887
|
account_address = account_label_info.account_address;
|
|
899
888
|
if (account_label === null) {
|
|
900
889
|
console.log(chalk.red("Account not found"));
|
package/src/utils.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { getChainIdByEvm } from "./web3";
|
|
|
9
9
|
import { MNEMONIC_WORDS } from './config';
|
|
10
10
|
import { getChainType } from './chain';
|
|
11
11
|
import { getBatchAccountBalance } from './assets';
|
|
12
|
+
import { getNetworkInfo } from './account';
|
|
12
13
|
|
|
13
14
|
const USER_HOME: any = process.env.HOME || process.env.USERPROFILE
|
|
14
15
|
const BASE_PATH = `${USER_HOME}/.wative`
|
|
@@ -331,14 +332,50 @@ export const getColor = async (text: string) => {
|
|
|
331
332
|
return null;
|
|
332
333
|
}
|
|
333
334
|
|
|
335
|
+
export const un_format_account_label = (keystore_path: string, account_label: string) => {
|
|
336
|
+
const networks = getNetworkInfo(keystore_path);
|
|
337
|
+
|
|
338
|
+
const account_label_list = networks.accounts;
|
|
339
|
+
const formatted_account_label_list = batch_format_account_label(account_label_list);
|
|
340
|
+
|
|
341
|
+
const index = formatted_account_label_list.indexOf(account_label);
|
|
342
|
+
if (index !== -1) {
|
|
343
|
+
return account_label_list[index];
|
|
344
|
+
}
|
|
345
|
+
return account_label;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export const format_account_label = (account_label: string) => {
|
|
349
|
+
account_label = account_label.trim();
|
|
350
|
+
account_label = account_label.toLocaleLowerCase();
|
|
351
|
+
account_label = account_label.replace(/\s+/g, "-").trim();
|
|
352
|
+
account_label = account_label.replace(/\-+/g, "-").trim();
|
|
353
|
+
return account_label;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
export const batch_format_account_label = (account_label_list: string[]) => {
|
|
358
|
+
let formatted_account_label_list: string[] = [];
|
|
359
|
+
for (let i = 0; i < account_label_list.length; i++) {
|
|
360
|
+
formatted_account_label_list.push(
|
|
361
|
+
format_account_label(account_label_list[i])
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
return formatted_account_label_list;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
|
|
334
368
|
export const getAccountLabel = async (text: string, account_label_list: string[]) => {
|
|
369
|
+
let formatted_account_label_list = batch_format_account_label(account_label_list);
|
|
335
370
|
let reg = /^(?![-_])[a-zA-Z0-9_-]{4,20}(?<![-_])$/g;
|
|
336
371
|
for (let i = 0; i < 5; i++) {
|
|
337
372
|
let account_label = await inputSomething(text);
|
|
338
373
|
account_label = account_label.trim();
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
374
|
+
let formatted_account_label = format_account_label(account_label);
|
|
375
|
+
|
|
376
|
+
if (reg.test(formatted_account_label)) {
|
|
377
|
+
if (formatted_account_label_list.includes(formatted_account_label)) {
|
|
378
|
+
console.log(chalk.red("Keystore name already exists"));
|
|
342
379
|
continue;
|
|
343
380
|
} else {
|
|
344
381
|
return account_label;
|
|
@@ -394,6 +431,20 @@ const passwordValidator = function (value: string) {
|
|
|
394
431
|
}
|
|
395
432
|
};
|
|
396
433
|
|
|
434
|
+
export const inputPasswordWithoutValidator = async (text: string) => {
|
|
435
|
+
const questions = [
|
|
436
|
+
{
|
|
437
|
+
name: 'password',
|
|
438
|
+
type: 'password',
|
|
439
|
+
mask: '#',
|
|
440
|
+
message: `${text}:`
|
|
441
|
+
}
|
|
442
|
+
]
|
|
443
|
+
|
|
444
|
+
let { password } = await inquirer.prompt(questions);
|
|
445
|
+
return password;
|
|
446
|
+
}
|
|
447
|
+
|
|
397
448
|
export const inputPassword = async (text: string) => {
|
|
398
449
|
let count = 0;
|
|
399
450
|
|
|
@@ -701,7 +752,8 @@ export const updateStorage = (keystore_path: string) => {
|
|
|
701
752
|
let account_list_len = networks.accounts.length;
|
|
702
753
|
for (let i = 0; i < account_list_len; i++) {
|
|
703
754
|
let account_label = networks.accounts[account_list_len - 1 - i];
|
|
704
|
-
let
|
|
755
|
+
let formatted_account_label = format_account_label(account_label);
|
|
756
|
+
let account_label_path = path.resolve(keystore_path, `accounts/${formatted_account_label}.json`);
|
|
705
757
|
|
|
706
758
|
if (!fs.existsSync(account_label_path)) {
|
|
707
759
|
networks.accounts.splice(account_list_len - 1 - i, 1);
|
|
@@ -710,6 +762,7 @@ export const updateStorage = (keystore_path: string) => {
|
|
|
710
762
|
|
|
711
763
|
fs.writeFileSync(network_path, JSON.stringify(networks, null, 4));
|
|
712
764
|
let account_label_list = networks.accounts;
|
|
765
|
+
account_label_list = batch_format_account_label(account_label_list);
|
|
713
766
|
|
|
714
767
|
for (let i = 0; i < account_label_list.length; i++) {
|
|
715
768
|
let account_label_path = path.resolve(keystore_path, `accounts/${account_label_list[i]}.json`);
|