@yerofey/cryptowallet-cli 1.42.0 → 1.43.0

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/package.json +3 -1
  2. package/src/Wallet.js +118 -44
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yerofey/cryptowallet-cli",
3
- "version": "1.42.0",
3
+ "version": "1.43.0",
4
4
  "description": "Crypto wallet generator CLI tool",
5
5
  "type": "module",
6
6
  "homepage": "https://github.com/yerofey/cryptowallet-cli",
@@ -126,6 +126,7 @@
126
126
  "@yerofey/dogecoin-bip84": "^0.0.5",
127
127
  "@yerofey/litecoin-bip84": "^0.0.5",
128
128
  "bigint-buffer": "^1.1.5",
129
+ "bip32": "^5.0.0",
129
130
  "bip39": "^3.1.0",
130
131
  "bip84": "^0.2.9",
131
132
  "bip86": "^0.0.4",
@@ -149,6 +150,7 @@
149
150
  "ripple-keypairs": "^2.0.0",
150
151
  "stellar-hd-wallet": "^1.0.2",
151
152
  "tezos-sign": "1.4.1",
153
+ "tiny-secp256k1": "^2.2.4",
152
154
  "tonweb": "^0.0.66",
153
155
  "tronweb": "^6.0.4",
154
156
  "xrpl": "^4.2.0"
package/src/Wallet.js CHANGED
@@ -6,6 +6,8 @@ const { red, yellow, gray } = chalk;
6
6
  import CoinKey from 'coinkey';
7
7
  import CoinInfo from 'coininfo';
8
8
  import bip39 from 'bip39';
9
+ import { BIP32Factory } from 'bip32';
10
+ import * as ecc from 'tiny-secp256k1';
9
11
  import bip84 from 'bip84';
10
12
  const { fromMnemonic, fromZPrv } = bip84;
11
13
  import bip86 from 'bip86';
@@ -46,6 +48,8 @@ import StellarHDWallet from 'stellar-hd-wallet';
46
48
  import { Seed as CardanoSeed } from 'cardano-wallet-js';
47
49
  import CardanoWasm from '@emurgo/cardano-serialization-lib-nodejs';
48
50
 
51
+ const bip32 = BIP32Factory(ecc);
52
+
49
53
  config({
50
54
  quiet: true,
51
55
  });
@@ -412,32 +416,64 @@ class Wallet {
412
416
 
413
417
  const mnemonic = mnemonicString || bip39.generateMnemonic();
414
418
 
415
- const root =
416
- row.format == 'taproot'
417
- ? new fromMnemonicBip86(mnemonic, '')
418
- : new fromMnemonic(mnemonic, '');
419
- const child =
420
- row.format == 'taproot' ? root.deriveAccount(0) : root.deriveAccount(0);
421
- const account =
422
- row.format == 'taproot' ? new fromXPrv(child) : new fromZPrv(child);
419
+ if (row.format == 'legacy') {
420
+ const seed = bip39.mnemonicToSeedSync(mnemonic);
421
+ const root = bip32.fromSeed(seed);
422
+ const coinPath = normalizeLegacyPath(row.path, "m/44'/0'");
423
+ const accountNode = root.derivePath(`${coinPath}/0'`);
424
+ const changeNode = accountNode.derive(0);
425
+ const coinInfo = CoinInfo(chain).versions;
426
+
427
+ let addresses = [];
428
+ if (number >= 1) {
429
+ for (let i = 0; i < number; i++) {
430
+ const child = changeNode.derive(i);
431
+ const key = new CoinKey(child.privateKey, coinInfo);
432
+ key.compressed = true;
433
+ addresses.push({
434
+ index: i,
435
+ address: key.publicAddress,
436
+ privateKey: key.privateWif,
437
+ });
438
+ }
439
+ }
423
440
 
424
- let addresses = [];
425
- if (number >= 1) {
426
- for (let i = 0; i < number; i++) {
427
- addresses.push({
428
- index: i,
429
- address: account.getAddress(i, false, row.purpose),
430
- privateKey: account.getPrivateKey(i),
431
- });
441
+ Object.assign(result, {
442
+ format: row.format,
443
+ addresses,
444
+ privateExtendedKey: accountNode.toBase58(),
445
+ mnemonic,
446
+ });
447
+ } else {
448
+ const root =
449
+ row.format == 'taproot'
450
+ ? new fromMnemonicBip86(mnemonic, '')
451
+ : new fromMnemonic(mnemonic, '');
452
+ const child =
453
+ row.format == 'taproot'
454
+ ? root.deriveAccount(0)
455
+ : root.deriveAccount(0);
456
+ const account =
457
+ row.format == 'taproot' ? new fromXPrv(child) : new fromZPrv(child);
458
+
459
+ let addresses = [];
460
+ if (number >= 1) {
461
+ for (let i = 0; i < number; i++) {
462
+ addresses.push({
463
+ index: i,
464
+ address: account.getAddress(i, false, row.purpose),
465
+ privateKey: account.getPrivateKey(i),
466
+ });
467
+ }
432
468
  }
433
- }
434
469
 
435
- Object.assign(result, {
436
- format: row.format,
437
- addresses,
438
- privateExtendedKey: account.getAccountPrivateKey(),
439
- mnemonic,
440
- });
470
+ Object.assign(result, {
471
+ format: row.format,
472
+ addresses,
473
+ privateExtendedKey: account.getAccountPrivateKey(),
474
+ mnemonic,
475
+ });
476
+ }
441
477
  } else if (chain == 'DOGE' || chain == 'LTC') {
442
478
  // Validate mnemonic
443
479
  if (mnemonicString != '' && !bip39.validateMnemonic(mnemonicString)) {
@@ -446,31 +482,62 @@ class Wallet {
446
482
  };
447
483
  }
448
484
 
449
- const _fromMnemonic =
450
- chain == 'DOGE' ? fromMnemonicDoge : fromMnemonicLite;
451
- const _fromZPrv = chain == 'DOGE' ? fromZPrvDoge : fromZPrvLite;
452
485
  const mnemonic = mnemonicString || bip39.generateMnemonic();
453
- const root = new _fromMnemonic(mnemonic, '');
454
- const child = root.deriveAccount(0);
455
- const account = new _fromZPrv(child);
486
+ if (row.format == 'legacy') {
487
+ const seed = bip39.mnemonicToSeedSync(mnemonic);
488
+ const root = bip32.fromSeed(seed);
489
+ const fallbackPath = chain == 'DOGE' ? "m/44'/3'" : "m/44'/2'";
490
+ const coinPath = normalizeLegacyPath(row.path, fallbackPath);
491
+ const accountNode = root.derivePath(`${coinPath}/0'`);
492
+ const changeNode = accountNode.derive(0);
493
+ const coinInfo = CoinInfo(chain).versions;
494
+
495
+ let addresses = [];
496
+ if (number >= 1) {
497
+ for (let i = 0; i < number; i++) {
498
+ const child = changeNode.derive(i);
499
+ const key = new CoinKey(child.privateKey, coinInfo);
500
+ key.compressed = true;
501
+ addresses.push({
502
+ index: i,
503
+ address: key.publicAddress,
504
+ privateKey: key.privateWif,
505
+ });
506
+ }
507
+ }
456
508
 
457
- let addresses = [];
458
- if (number >= 1) {
459
- for (let i = 0; i < number; i++) {
460
- addresses.push({
461
- index: i,
462
- address: account.getAddress(i, false, row.purpose),
463
- privateKey: account.getPrivateKey(i),
464
- });
509
+ Object.assign(result, {
510
+ format: row.format,
511
+ addresses,
512
+ privateExtendedKey: accountNode.toBase58(),
513
+ mnemonic,
514
+ });
515
+ } else {
516
+ const _fromMnemonic =
517
+ chain == 'DOGE' ? fromMnemonicDoge : fromMnemonicLite;
518
+ const _fromZPrv = chain == 'DOGE' ? fromZPrvDoge : fromZPrvLite;
519
+ const root = new _fromMnemonic(mnemonic, '');
520
+ const child = root.deriveAccount(0);
521
+ const account = new _fromZPrv(child);
522
+
523
+ let addresses = [];
524
+ if (number >= 1) {
525
+ for (let i = 0; i < number; i++) {
526
+ addresses.push({
527
+ index: i,
528
+ address: account.getAddress(i, false, row.purpose),
529
+ privateKey: account.getPrivateKey(i),
530
+ });
531
+ }
465
532
  }
466
- }
467
533
 
468
- Object.assign(result, {
469
- format: row.format,
470
- addresses,
471
- privateExtendedKey: account.getAccountPrivateKey(),
472
- mnemonic,
473
- });
534
+ Object.assign(result, {
535
+ format: row.format,
536
+ addresses,
537
+ privateExtendedKey: account.getAccountPrivateKey(),
538
+ mnemonic,
539
+ });
540
+ }
474
541
  } else if (row.format == 'BEP2') {
475
542
  // Validate mnemonic
476
543
  if (mnemonicString != '' && !bip39.validateMnemonic(mnemonicString)) {
@@ -924,4 +991,11 @@ function generateMnemonicString(length = 12) {
924
991
  return bip39.generateMnemonic(entropy);
925
992
  }
926
993
 
994
+ function normalizeLegacyPath(pathValue, fallbackPath) {
995
+ const basePath = (pathValue && pathValue.trim()) || fallbackPath || '';
996
+ if (basePath === '') return basePath;
997
+ if (basePath.endsWith("'")) return basePath;
998
+ return basePath.replace(/\/(\d+)$/, "/$1'");
999
+ }
1000
+
927
1001
  export { generateMnemonicString, Wallet };