wative 1.0.32 → 1.0.34

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
@@ -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, showAssets, showAssetsDetail } from './assets';
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
- const passphrase_account = wative_core.account.generatePPAccount(pre_account_info.account_label, passphrase, pre_account_info.password);
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/${pre_account_info.account_label}.json`);
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
- pre_account_info.account_label,
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;
@@ -245,7 +250,7 @@ export const loginIn = async (account_label: string, wative_core: typeof WativeC
245
250
  if (process.env.WativePassword && process.env.WativePassword !== '') {
246
251
  password = process.env.WativePassword;
247
252
  } else {
248
- password = await inputPassword(`Please input the password for Account [${account_label}]`);
253
+ password = await inputPasswordWithoutValidator(`Please input the password for Account [${account_label}]`);
249
254
  }
250
255
  if (!password) {
251
256
  console.log(
@@ -257,10 +262,17 @@ export const loginIn = async (account_label: string, wative_core: typeof WativeC
257
262
  const login_result = await wative_core.account.login(account_label, password);
258
263
  if (login_result.status) {
259
264
  return { isLoginIn: true, password };
260
- } else {
261
- console.log(
262
- chalk.red(login_result.output)
263
- );
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;
264
276
  }
265
277
  }
266
278
  return { isLoginIn: false };
@@ -285,7 +297,8 @@ const selectAccount = async (keystore_path: string, wative_core: typeof WativeCo
285
297
 
286
298
  for (let i = 0; i < account_label_len; i++) {
287
299
  const account_label = account_label_list[i];
288
- const account_info = getAccountInfo(keystore_path, account_label);
300
+ const formatted_account_label = format_account_label(account_label);
301
+ const account_info = getAccountInfo(keystore_path, formatted_account_label);
289
302
  if ("data" in account_info) {
290
303
  select_account_options.push(`${i + 1}) ${account_label} [${account_info.account_type}:${account_info.data.length}]`);
291
304
  } else {
@@ -299,9 +312,11 @@ const selectAccount = async (keystore_path: string, wative_core: typeof WativeCo
299
312
  }
300
313
 
301
314
  const selected_account_label = account_label_list[select_account_options.indexOf(selected_account) - 2];
302
- let login_result = await loginIn(selected_account_label, wative_core);
315
+ const formatted_account_label = format_account_label(selected_account_label);
316
+
317
+ let login_result = await loginIn(formatted_account_label, wative_core);
303
318
  if (login_result.isLoginIn) {
304
- await accountManager(keystore_path, selected_account_label, wative_core, login_result.password);
319
+ await accountManager(keystore_path, formatted_account_label, wative_core, login_result.password);
305
320
  }
306
321
 
307
322
  await selectAccount(keystore_path, wative_core);
@@ -658,8 +673,7 @@ const showAccountDetail = async (keystore_path: string, account_label: string, s
658
673
  current_tag = `Current tag (${account_info.data[selected_account_id].tag})`;
659
674
  }
660
675
  let tag = await inputSomething(current_tag, tagValidator);
661
- tag = tag.replace(/\s+/g, " ").trim();
662
- account_info.data[selected_account_id].tag = tag.toLocaleLowerCase();
676
+ account_info.data[selected_account_id].tag = tag.replace(/\s+/g, " ").trim();
663
677
  saveAccountInfo(keystore_path, account_label, account_info);
664
678
  break;
665
679
  }
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
- const wative_core = new WativeCore(keystore_path, networks.accounts, null, true);
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';
@@ -334,9 +334,10 @@ const sendEvmTokenTransaction = async (keystore_path: string, chain_rpc_url: str
334
334
 
335
335
  let token_address: any;
336
336
  let token_decimals: any;
337
+ let token_name: any;
337
338
  switch (selected_token_address) {
338
339
  case '> Input token ticker':
339
- let token_name = await inputSomething("token ticker");
340
+ token_name = await inputSomething("token ticker");
340
341
 
341
342
  let token_address_list = await getAssetListByTokenName(keystore_path, chain_id, token_name);
342
343
  if (token_address_list.length === 0) {
@@ -381,6 +382,7 @@ const sendEvmTokenTransaction = async (keystore_path: string, chain_rpc_url: str
381
382
  return;
382
383
  }
383
384
  token_decimals = Number(token_info.output.decimals);
385
+ token_name = token_info.output.name;
384
386
  break;
385
387
  }
386
388
 
@@ -390,19 +392,6 @@ const sendEvmTokenTransaction = async (keystore_path: string, chain_rpc_url: str
390
392
  console.log(chalk.red(token_balance_result.output));
391
393
  return;
392
394
  }
393
- let amount: any;
394
- for (let i = 0; i < 5; i++) {
395
- let input_amount = await inputSomething("Amount", numberValidator);
396
- if ((new BigNumber(input_amount).multipliedBy(Math.pow(10, Number(token_decimals)))).isLessThanOrEqualTo(new BigNumber(token_balance_result.output))) {
397
- amount = input_amount;
398
- break;
399
- }
400
- console.log(chalk.red("Not enough balance"));
401
- }
402
-
403
- if (!amount) {
404
- return;
405
- }
406
395
 
407
396
  const gasUtil = new GasUtil(chain_rpc_url);
408
397
  const estimateGasPrice = await gasUtil.getGasPrice();
@@ -411,8 +400,12 @@ const sendEvmTokenTransaction = async (keystore_path: string, chain_rpc_url: str
411
400
  const inputGasPrice = await inputSomething(`Gas price (${_estimateGasPrice}), agree or enter`, gasValidator);
412
401
  let gasPrice = inputGasPrice ? inputGasPrice : _estimateGasPrice;
413
402
  gasPrice = (new BigNumber(gasPrice)).multipliedBy(1e9).toFixed(0);
403
+ let transfer_amount = await getTransferTokenAmount(
404
+ token_balance_result.output,
405
+ token_name,
406
+ token_decimals
407
+ );
414
408
 
415
- let transfer_amount = new BigNumber(amount).multipliedBy(Math.pow(10, token_decimals)).toFixed(0);
416
409
  const data = await getTransferTokenData(receipt, transfer_amount, chain_rpc_url);
417
410
 
418
411
  const estimateNonce = await gasUtil.getTransactionCount(account_address);
@@ -485,9 +478,10 @@ const sendSolanaTokenTransaction = async (keystore_path: string, chain_rpc_url:
485
478
 
486
479
  let token_address: any;
487
480
  let token_decimals: any;
481
+ let token_name: any;
488
482
  switch (selected_token_address) {
489
483
  case '> Input token ticker':
490
- let token_name = await inputSomething("token ticker");
484
+ token_name = await inputSomething("token ticker");
491
485
 
492
486
  let token_address_list = await getAssetListByTokenName(keystore_path, chain_id, token_name);
493
487
  if (token_address_list.length === 0) {
@@ -532,30 +526,22 @@ const sendSolanaTokenTransaction = async (keystore_path: string, chain_rpc_url:
532
526
  return;
533
527
  }
534
528
  token_decimals = Number(token_info.output.decimals);
529
+ token_name = token_info.output.name;
535
530
  break;
536
531
  }
537
532
 
538
533
  const receipt = await inputSomething("Receipt", chainAddressValidator);
539
- const token_balance_result = await getTokenBalanceInEvm(account_address, token_address, chain_rpc_url);
534
+ const token_balance_result = await getTokenBalanceInSolana(account_address, token_address, chain_rpc_url);
540
535
  if (!token_balance_result.status) {
541
536
  console.log(chalk.red(token_balance_result.output));
542
537
  return;
543
538
  }
544
- let amount: any;
545
- for (let i = 0; i < 5; i++) {
546
- let input_amount = await inputSomething("Amount", numberValidator);
547
- if ((new BigNumber(input_amount).multipliedBy(Math.pow(10, Number(token_decimals)))).isLessThanOrEqualTo(new BigNumber(token_balance_result.output))) {
548
- amount = input_amount;
549
- break;
550
- }
551
- console.log(chalk.red("Not enough balance"));
552
- }
553
539
 
554
- if (!amount) {
555
- return;
556
- }
557
-
558
- let transfer_amount = new BigNumber(amount).multipliedBy(Math.pow(10, token_decimals)).toFixed(0);
540
+ let transfer_amount = await getTransferTokenAmount(
541
+ token_balance_result.output,
542
+ token_name,
543
+ token_decimals
544
+ );
559
545
  let from_token_account = getAssociatedTokenAddressSync(
560
546
  new PublicKey(token_address),
561
547
  new PublicKey(account_address),
@@ -709,6 +695,31 @@ const sendSolanaTokenTransaction = async (keystore_path: string, chain_rpc_url:
709
695
  }
710
696
 
711
697
 
698
+ const getTransferTokenAmount = async (max_value: string, token_symbols: string, decimals: number): Promise<string> => {
699
+ let total_balance = (new BigNumber(max_value)).dividedBy(10 ** decimals).toFixed(4, BigNumber.ROUND_FLOOR);
700
+
701
+ let option_select_value_list: any = [
702
+ '> Input amount',
703
+ `> Total Balance (${total_balance} ${token_symbols})`
704
+ ]
705
+ let selected_value = await selectSomething(option_select_value_list);
706
+ let value_id = option_select_value_list.indexOf(selected_value);
707
+ let value = "0";
708
+ if (value_id === 0) {
709
+ let input_value = await inputSomething("amount", numberValidator);
710
+ value = new BigNumber(input_value).multipliedBy(10 ** decimals).toFixed(0, BigNumber.ROUND_FLOOR);
711
+ } else if (value_id === 1) {
712
+ value = max_value;
713
+ }
714
+
715
+ if ((new BigNumber(value)).isLessThanOrEqualTo(new BigNumber(max_value))) {
716
+ return value;
717
+ }
718
+
719
+ console.log(chalk.red("Not enough balance"));
720
+ return getTransferTokenAmount(max_value, token_symbols, decimals);
721
+ }
722
+
712
723
  const excuteTools = async (keystore_path: string, account_label: string, account_address: string, wative_core: typeof WativeCore) => {
713
724
  let default_network = getDefaultNetworkByAddress(keystore_path, account_label, account_address);
714
725
  let network_type = getNetworkTypeByName(keystore_path, default_network);
@@ -870,6 +881,9 @@ export const showTools = async (keystore_path: string, wative_core: typeof Wativ
870
881
  }
871
882
 
872
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
+
873
887
  account_address = account_label_info.account_address;
874
888
  if (account_label === null) {
875
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`
@@ -317,7 +318,7 @@ export const getSymbol = async (text: string) => {
317
318
  }
318
319
 
319
320
  export const getColor = async (text: string) => {
320
- let reg = /^#[0-9A_F]{6}$/g;
321
+ let reg = /^#[0-9A-Fa-f]{6}$/g;
321
322
  for (let i = 0; i < 5; i++) {
322
323
  let color = await inputSomething(text);
323
324
  if (color === "") {
@@ -331,14 +332,49 @@ 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
+ return account_label;
353
+ }
354
+
355
+
356
+ export const batch_format_account_label = (account_label_list: string[]) => {
357
+ let formatted_account_label_list: string[] = [];
358
+ for (let i = 0; i < account_label_list.length; i++) {
359
+ formatted_account_label_list.push(
360
+ format_account_label(account_label_list[i])
361
+ );
362
+ }
363
+ return formatted_account_label_list;
364
+ }
365
+
366
+
334
367
  export const getAccountLabel = async (text: string, account_label_list: string[]) => {
335
- let reg = /^(?![-_]).*?[a-zA-Z0-9_-]{4,20}(?<![-_])$/g;
368
+ let formatted_account_label_list = batch_format_account_label(account_label_list);
369
+ let reg = /^(?![-_])[a-zA-Z0-9_-]{4,20}(?<![-_])$/g;
336
370
  for (let i = 0; i < 5; i++) {
337
371
  let account_label = await inputSomething(text);
338
372
  account_label = account_label.trim();
339
- if (reg.test(account_label)) {
340
- if (account_label_list.includes(account_label)) {
341
- console.log(chalk.red("Account label already exists"));
373
+ let formatted_account_label = format_account_label(account_label);
374
+
375
+ if (reg.test(formatted_account_label)) {
376
+ if (formatted_account_label_list.includes(formatted_account_label)) {
377
+ console.log(chalk.red("Keystore name already exists"));
342
378
  continue;
343
379
  } else {
344
380
  return account_label;
@@ -394,6 +430,20 @@ const passwordValidator = function (value: string) {
394
430
  }
395
431
  };
396
432
 
433
+ export const inputPasswordWithoutValidator = async (text: string) => {
434
+ const questions = [
435
+ {
436
+ name: 'password',
437
+ type: 'password',
438
+ mask: '#',
439
+ message: `${text}:`
440
+ }
441
+ ]
442
+
443
+ let { password } = await inquirer.prompt(questions);
444
+ return password;
445
+ }
446
+
397
447
  export const inputPassword = async (text: string) => {
398
448
  let count = 0;
399
449
 
@@ -701,7 +751,8 @@ export const updateStorage = (keystore_path: string) => {
701
751
  let account_list_len = networks.accounts.length;
702
752
  for (let i = 0; i < account_list_len; i++) {
703
753
  let account_label = networks.accounts[account_list_len - 1 - i];
704
- let account_label_path = path.resolve(keystore_path, `accounts/${account_label}.json`);
754
+ let formatted_account_label = format_account_label(account_label);
755
+ let account_label_path = path.resolve(keystore_path, `accounts/${formatted_account_label}.json`);
705
756
 
706
757
  if (!fs.existsSync(account_label_path)) {
707
758
  networks.accounts.splice(account_list_len - 1 - i, 1);
@@ -710,6 +761,7 @@ export const updateStorage = (keystore_path: string) => {
710
761
 
711
762
  fs.writeFileSync(network_path, JSON.stringify(networks, null, 4));
712
763
  let account_label_list = networks.accounts;
764
+ account_label_list = batch_format_account_label(account_label_list);
713
765
 
714
766
  for (let i = 0; i < account_label_list.length; i++) {
715
767
  let account_label_path = path.resolve(keystore_path, `accounts/${account_label_list[i]}.json`);