hodl-wallet 1.1.6 → 1.2.2

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.
Files changed (2) hide show
  1. package/index.mjs +94 -83
  2. package/package.json +1 -1
package/index.mjs CHANGED
@@ -29,7 +29,7 @@ class Wallet {
29
29
  try {
30
30
  const networkPlugins = await this.loadNetworkPlugins();
31
31
  if (networkPlugins.length === 0) {
32
- console.log('No valid network plugins found.');
32
+ Wallet.displayError('No valid network plugins found.');
33
33
  process.exit(1);
34
34
  }
35
35
  await this.selectNetwork(networkPlugins);
@@ -37,11 +37,12 @@ class Wallet {
37
37
  await this.loadAccount();
38
38
 
39
39
  if (!this.account) {
40
- throw new Error('Failed to initialize account');
40
+ Wallet.displayError('Failed to initialize account.');
41
+ process.exit(1);
41
42
  }
42
43
 
43
44
  } catch (error) {
44
- console.error('Initialization failed:', error);
45
+ Wallet.displayError('Initialization failed', error);
45
46
  process.exit(1);
46
47
  }
47
48
  }
@@ -133,20 +134,31 @@ class Wallet {
133
134
  const { privateKey } = await inquirer.prompt({
134
135
  type: 'password',
135
136
  name: 'privateKey',
136
- message: 'Private key:',
137
+ message: 'Private-key:',
137
138
  mask: '*',
138
139
  });
139
140
 
140
- if (!privateKey) return;
141
+ if (!privateKey.trim()) {
142
+ Wallet.displayError('Private-key is empty.');
143
+ return;
144
+ }
141
145
 
142
- this.account = this.web3.eth.accounts.privateKeyToAccount(privateKey);
143
- await this.db.secureSet('account', this.account);
144
- this.displayAccountAddress();
146
+ try {
147
+ this.account = this.web3.eth.accounts.privateKeyToAccount(privateKey);
148
+ await this.db.secureSet('account', this.account);
149
+ this.displayAccountAddress();
150
+ } catch (error) {
151
+ Wallet.displayError('Invalid private-key.');
152
+ return;
153
+ }
145
154
  }
146
155
 
147
156
  if (accountAction === 'Import Mnemonic (12 words)') {
148
157
  account = await this.importFrom12Words();
149
- if (!account) return;
158
+ if (!account) {
159
+ Wallet.displayError('Invalid mnemonic.');
160
+ return;
161
+ }
150
162
  this.account = account;
151
163
  this.displayAccountAddress();
152
164
  }
@@ -186,7 +198,7 @@ class Wallet {
186
198
 
187
199
  async showBalance() {
188
200
  if (!this.account) {
189
- console.error('Account not initialized. Please try restarting the application.');
201
+ Wallet.displayError('Account not initialized.', Please try restarting the application.');
190
202
  return;
191
203
  }
192
204
 
@@ -299,19 +311,24 @@ class Wallet {
299
311
  }
300
312
 
301
313
  } catch (error) {
302
- this.displayError(error);
314
+ this.displayTransactionError(error);
303
315
  }
304
316
  }
305
317
 
306
- displayError(error) {
318
+ displayTransactionError(error) {
319
+ const data = error.reason ? error.reason.replace(/(\w+):/g, "\n$1:").trim() : null;
320
+ Wallet.displayError(error.message, data);
321
+ }
322
+
323
+ static displayError(message, data) {
307
324
  const table = new Table({
308
- head: [error.message],
325
+ head: [message],
309
326
  style: { head: ['red'] },
310
327
  wordWrap: true,
311
328
  });
312
329
 
313
- if (error.reason) {
314
- table.push([error.reason.replace(/(\w+):/g, "\n$1:").trim()]);
330
+ if (data) {
331
+ table.push([data]);
315
332
  }
316
333
 
317
334
  console.log(table.toString());
@@ -336,11 +353,11 @@ class Wallet {
336
353
  amount,
337
354
  hash: receipt.transactionHash
338
355
  };
339
- this.db.add('transactions', this.selectedNetwork.nativeToken, transaction);
356
+ this.db.add('transactions', this.account.address, this.selectedNetwork.nativeToken, transaction);
340
357
  }
341
358
 
342
359
  async showTransactions() {
343
- const history = this.db.values('transactions', this.selectedNetwork.nativeToken) || [];
360
+ const history = this.db.values('transactions', this.account.address, this.selectedNetwork.nativeToken) || [];
344
361
 
345
362
  const table = new Table({
346
363
  head: ['Date', 'Recipient', 'Token', 'Amount'],
@@ -425,7 +442,7 @@ class Wallet {
425
442
 
426
443
  return this.web3.eth.accounts.signTransaction(tx, this.account.privateKey);
427
444
  } catch (error) {
428
- this.displayError(error);
445
+ this.displayTransactionError(error);
429
446
  }
430
447
  }
431
448
 
@@ -445,13 +462,12 @@ class Wallet {
445
462
  if (balance < amountWei + gasCost) {
446
463
  const maxAmount = this.web3.utils.fromWei((balance - gasCost).toString(), 'ether');
447
464
  const table = new Table({
448
- head: [{ colSpan: 2, content: 'Insufficient balance for this transaction' }],
465
+ head: [{ colSpan: 2, content: 'Insufficient balance for this transaction.' }],
449
466
  style: { head: ['red'] },
450
467
  wordWrap: true
451
468
  });
452
469
  table.push(['Maximum Amount', `${maxAmount} ${this.selectedNetwork.nativeToken}`]);
453
470
  console.log(table.toString());
454
- return;
455
471
  }
456
472
 
457
473
  const tx = {
@@ -503,14 +519,14 @@ class Wallet {
503
519
 
504
520
  table.push(
505
521
  ['Address', this.account.address],
506
- ['Private Key', this.account.privateKey]
522
+ ['Private-key', this.account.privateKey]
507
523
  );
508
524
 
509
525
  if (this.account.mnemonic) {
510
526
  table.push(['Mnemonic Phrase', this.account.mnemonic]);
511
- table.push(['WARNING', "Please keep your private key and mnemonic phrase secure. Never share it."]);
527
+ table.push(['WARNING', "Please keep your private-key and mnemonic phrase secure. Never share it."]);
512
528
  } else {
513
- table.push(['WARNING', "Please keep your private key secure. Never share it."]);
529
+ table.push(['WARNING', "Please keep your private-key secure. Never share it."]);
514
530
  }
515
531
 
516
532
  console.log('\n' + table.toString());
@@ -588,77 +604,72 @@ class UIManager {
588
604
  }
589
605
  }
590
606
 
591
- async function main() {
592
- UIManager.displayWelcome();
593
- const encryptionKey = await UIManager.getEncryptionKey();
594
- const wallet = new Wallet(encryptionKey);
607
+ UIManager.displayWelcome();
608
+ const encryptionKey = await UIManager.getEncryptionKey();
609
+ const wallet = new Wallet(encryptionKey);
595
610
 
596
- // Check if an account exists
597
- const accountExists = wallet.db.secureGet('account');
598
- if (accountExists === null) {
599
- console.error('Wrong password.');
611
+ // Check if an account exists
612
+ const accountExists = wallet.db.secureGet('account');
613
+ if (accountExists === null) {
614
+ Wallet.displayError('Wrong password.');
615
+ process.exit(1);
616
+ }
617
+
618
+ if (!accountExists) {
619
+ const confirmedKey = await UIManager.confirmEncryptionKey(encryptionKey);
620
+ if (confirmedKey !== encryptionKey) {
621
+ Wallet.displayError('Passwords do not match. Please try again.');
600
622
  process.exit(1);
601
623
  }
624
+ }
602
625
 
603
- if (!accountExists) {
604
- const confirmedKey = await UIManager.confirmEncryptionKey(encryptionKey);
605
- if (confirmedKey !== encryptionKey) {
606
- console.error('Passwords do not match. Please try again.');
607
- process.exit(1);
608
- }
609
- }
626
+ try {
627
+ await wallet.initialize();
628
+ if (accountExists) wallet.displayAccountAddress();
629
+ } catch (error) {
630
+ Wallet.displayError('Failed to initialize wallet.', error);
631
+ process.exit(1);
632
+ }
610
633
 
611
- try {
612
- await wallet.initialize();
613
- if (accountExists) wallet.displayAccountAddress();
614
- } catch (error) {
615
- console.error('Failed to initialize wallet:', error);
616
- process.exit(1);
617
- }
634
+ // Register the displayExitPhrase method and account clearing to be called on process exit
635
+ process.on('exit', () => {
636
+ wallet.clearAccountData();
637
+ UIManager.displayExitPhrase();
638
+ });
618
639
 
619
- // Register the displayExitPhrase method and account clearing to be called on process exit
620
- process.on('exit', () => {
621
- wallet.clearAccountData();
622
- UIManager.displayExitPhrase();
640
+ while (true) {
641
+ const { action } = await inquirer.prompt({
642
+ type: 'list',
643
+ name: 'action',
644
+ message: 'What would you like to do?',
645
+ choices: [
646
+ { name: 'Transfer Funds', value: 'transfer' },
647
+ { name: 'Show Balance', value: 'balance' },
648
+ { name: 'Show Sents', value: 'history' },
649
+ { name: 'Account', value: 'account' },
650
+ { name: 'Exit', value: 'exit' }
651
+ ],
623
652
  });
624
653
 
625
- while (true) {
626
- const { action } = await inquirer.prompt({
627
- type: 'list',
628
- name: 'action',
629
- message: 'What would you like to do?',
630
- choices: [
631
- { name: 'Transfer Funds', value: 'transfer' },
632
- { name: 'Show Balance', value: 'balance' },
633
- { name: 'Show Sents', value: 'history' },
634
- { name: 'Account', value: 'account' },
635
- { name: 'Exit', value: 'exit' }
636
- ],
637
- });
638
-
639
- switch (action) {
640
- case 'balance':
641
- await wallet.showBalance();
642
- break;
643
- case 'transfer':
644
- await wallet.transferFunds();
645
- break;
646
- case 'history':
647
- await wallet.showTransactions();
648
- break;
649
- case 'account':
650
- await wallet.loadAccount(true);
651
- break;
652
- case 'exit':
653
- process.exit();
654
- }
654
+ switch (action) {
655
+ case 'balance':
656
+ await wallet.showBalance();
657
+ break;
658
+ case 'transfer':
659
+ await wallet.transferFunds();
660
+ break;
661
+ case 'history':
662
+ await wallet.showTransactions();
663
+ break;
664
+ case 'account':
665
+ await wallet.loadAccount(true);
666
+ break;
667
+ case 'exit':
668
+ process.exit();
655
669
  }
656
670
  }
657
671
 
658
- main().catch(error => {
659
- console.error('An unexpected error occurred:', error);
660
- process.exit(1);
661
- });
672
+
662
673
 
663
674
  process.on('SIGINT', () => {
664
675
  console.log('\n');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hodl-wallet",
3
- "version": "1.1.6",
3
+ "version": "1.2.2",
4
4
  "description": "🧊 HODL Wallet - Blazing-fast, transparent, and ad-free crypto wallet!",
5
5
  "main": "index.mjs",
6
6
  "bin": {