@yerofey/cryptowallet-cli 1.16.2 → 1.17.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.
package/cli.js CHANGED
@@ -11,8 +11,9 @@ import Method from './src/Method.js';
11
11
  return new Method('list').init();
12
12
  }
13
13
 
14
- if (options.mnemonic == true) {
15
- return new Method('mnemonic').init();
14
+ // generate mnemonic string if no argument is passed or only the mnemonic length is passed
15
+ if (options.mnemonic === true || options.mnemonic === '' || options.mnemonic.split(' ').length === 1) {
16
+ return new Method('mnemonic').init({ mnemonic: options.mnemonic });
16
17
  }
17
18
 
18
19
  if (options.version) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yerofey/cryptowallet-cli",
3
- "version": "1.16.2",
3
+ "version": "1.17.0",
4
4
  "description": "Crypto wallet generator CLI tool",
5
5
  "type": "module",
6
6
  "homepage": "https://github.com/yerofey/cryptowallet-cli",
@@ -19,7 +19,6 @@
19
19
  },
20
20
  "scripts": {
21
21
  "lint": "pnpm exec eslint src/*.js",
22
- "postinstall": "cw --donate",
23
22
  "test": "ava"
24
23
  },
25
24
  "files": [
package/src/Method.js CHANGED
@@ -19,6 +19,7 @@ class Method {
19
19
  this.name = name;
20
20
  this.params = params;
21
21
  this.callMethods = this._initializeMethods();
22
+ this.inputOptions = {};
22
23
  }
23
24
 
24
25
  _initializeMethods() {
@@ -61,16 +62,23 @@ class Method {
61
62
  }
62
63
 
63
64
  _mnemonic() {
65
+ const mnemonic = this.inputOptions.mnemonic || '12';
66
+ const mnemonicLength = ['12', '18', '24'].includes(mnemonic)
67
+ ? parseInt(mnemonic, 10)
68
+ : 12;
69
+
64
70
  log(
65
71
  `✨ ${green('Done!')} ${blueBright(
66
- 'Here is your randomly generated 12 words mnemonic string:'
72
+ `Here is your randomly generated ${
73
+ mnemonicLength || 12
74
+ } words mnemonic string:`
67
75
  )}\n`
68
76
  );
69
- log(`📄 ${generateMnemonicString()}`);
77
+ log(`📄 ${generateMnemonicString(mnemonicLength)}`);
70
78
  log();
71
79
  log(
72
80
  greenBright(
73
- 'â„šī¸ You can import this wallet into MetaMask, Trust Wallet and many other wallet apps'
81
+ 'â„šī¸ You can import it into your favorite wallet app or use it to generate a wallet with "-m" flag'
74
82
  )
75
83
  );
76
84
  }
@@ -349,7 +357,7 @@ class Method {
349
357
  if (cw.row.network == 'EVM' || false) {
350
358
  log(
351
359
  yellow(
352
- '🆒 You can use this wallet in Ethereum, Binance Smart Chain, Polygon and few more networks (EVM compatible)'
360
+ '🆒 You can use this wallet in Ethereum, Binance Smart Chain, Polygon and many others networks (EVM compatible)'
353
361
  )
354
362
  );
355
363
  }
@@ -381,7 +389,11 @@ class Method {
381
389
 
382
390
  // donation
383
391
  log();
384
- log(blueBright('🙏 Consider supporting the project - see donation options with: cw --donate'));
392
+ log(
393
+ blueBright(
394
+ '🙏 Consider supporting the project - see donation options with: cw --donate'
395
+ )
396
+ );
385
397
  }
386
398
  }
387
399
 
@@ -409,7 +421,8 @@ class Method {
409
421
  `);
410
422
  }
411
423
 
412
- async init() {
424
+ async init(inputOptions = {}) {
425
+ this.inputOptions = inputOptions;
413
426
  return (this.callMethods[this.name] || this.callMethods['_'])();
414
427
  }
415
428
  }
package/src/Wallet.js CHANGED
@@ -18,12 +18,20 @@ import pkutils from 'ethereum-mnemonic-privatekey-utils';
18
18
  import bCrypto from '@binance-chain/javascript-sdk/lib/crypto/index.js';
19
19
  import tronWeb from 'tronweb';
20
20
  import tezos from 'tezos-sign';
21
- import { Keypair as SolanaKeypair, PublicKey as SolanaPublickey } from '@solana/web3.js';
21
+ import {
22
+ Keypair as SolanaKeypair,
23
+ PublicKey as SolanaPublickey,
24
+ } from '@solana/web3.js';
22
25
  import bs58 from 'bs58';
23
- import { TonClient, WalletContractV4, internal as TonInternal } from "@ton/ton";
24
- import { mnemonicNew as newTonMnemonic, mnemonicToPrivateKey as TonMnemonicToPrivateKey } from "@ton/crypto";
26
+ import { TonClient, WalletContractV4, internal as TonInternal } from '@ton/ton';
27
+ import {
28
+ mnemonicNew as newTonMnemonic,
29
+ mnemonicToPrivateKey as TonMnemonicToPrivateKey,
30
+ } from '@ton/crypto';
25
31
  const { red } = chalk;
26
32
 
33
+ const supportedMnemonicLengths = [12, 18, 24];
34
+
27
35
  class Wallet {
28
36
  constructor(cw) {
29
37
  this.cw = cw;
@@ -246,7 +254,17 @@ class Wallet {
246
254
  const options = cw.options;
247
255
 
248
256
  let format = options.format || '';
249
- let mnemonicString = options.mnemonic || '';
257
+ const mnemonic = options.mnemonic || '';
258
+ let mnemonicLength = 12;
259
+ let mnemonicString = '';
260
+ const mnemonicWordsCount = (mnemonic.split(' ') || []).length || 0;
261
+ if (mnemonicWordsCount == 1) {
262
+ const mnemonicInput = parseInt(mnemonic.split(' ')[0], 10);
263
+ mnemonicLength = supportedMnemonicLengths.includes(mnemonicInput) ? mnemonicInput : 12;
264
+ } else {
265
+ mnemonicString = mnemonic;
266
+ mnemonicLength = mnemonicWordsCount;
267
+ }
250
268
  let number = options.number || 1;
251
269
  let result = {};
252
270
 
@@ -338,7 +356,7 @@ class Wallet {
338
356
  }
339
357
 
340
358
  let addresses = [];
341
- const mnemonic = mnemonicString || bip39.generateMnemonic();
359
+ const mnemonic = mnemonicString || generateMnemonicString(mnemonicLength);
342
360
 
343
361
  if (number == 1) {
344
362
  const privateKey = bCrypto.getPrivateKeyFromMnemonic(mnemonic, true, 0);
@@ -375,7 +393,7 @@ class Wallet {
375
393
  }
376
394
 
377
395
  let addresses = [];
378
- const mnemonic = mnemonicString || bip39.generateMnemonic();
396
+ const mnemonic = mnemonicString || generateMnemonicString(mnemonicLength);
379
397
  const privateKey = pkutils.getPrivateKeyFromMnemonic(mnemonic);
380
398
 
381
399
  if (number == 1) {
@@ -448,22 +466,25 @@ class Wallet {
448
466
  ],
449
467
  });
450
468
  } else if (chain == 'TON') {
451
- // Create a new TON client
452
- // const client = new TonClient({
453
- // endpoint: 'https://toncenter.com/api/v2/jsonRPC',
454
- // });
455
469
  // Generate new mnemonics and derive key pair
456
- const mnemonics = await newTonMnemonic();
470
+ let mnemonics;
471
+ if (mnemonicString != '' && !bip39.validateMnemonic(mnemonicString)) {
472
+ mnemonics = mnemonicString.split(' ');
473
+ } else {
474
+ mnemonics = await newTonMnemonic(); // array of 24 words
475
+ mnemonicString = mnemonics.join(' ');
476
+ }
457
477
  const keyPair = await TonMnemonicToPrivateKey(mnemonics);
458
478
  // Define the workchain (usually 0)
459
479
  const workchain = 0;
460
480
  // Create a new wallet contract instance
461
- const wallet = WalletContractV4.create({ workchain, publicKey: keyPair.publicKey });
462
- // const contract = client.open(wallet);
481
+ const wallet = WalletContractV4.create({
482
+ workchain,
483
+ publicKey: keyPair.publicKey,
484
+ });
463
485
  // Get the wallet address
464
486
  const address = wallet.address.toString();
465
487
 
466
- // TODO: add support for multiple addresses
467
488
  // TODO: add support for new UQ address format
468
489
 
469
490
  Object.assign(result, {
@@ -473,7 +494,7 @@ class Wallet {
473
494
  address,
474
495
  },
475
496
  ],
476
- mnemonic: mnemonics.join(' '),
497
+ mnemonic: mnemonicString,
477
498
  });
478
499
  } else if (chain == 'TRX') {
479
500
  try {
@@ -521,8 +542,27 @@ class Wallet {
521
542
  }
522
543
  }
523
544
 
524
- function generateMnemonicString() {
525
- return bip39.generateMnemonic();
545
+ function generateMnemonicString(length = 12) {
546
+ let entropy;
547
+ switch (length) {
548
+ case 12:
549
+ entropy = 128;
550
+ break;
551
+ case 18:
552
+ entropy = 192;
553
+ break;
554
+ case 24:
555
+ entropy = 256;
556
+ break;
557
+ default:
558
+ throw new Error(
559
+ 'Invalid mnemonic length. Supported lengths are 12, 18, or 24.'
560
+ );
561
+ }
562
+
563
+ // Generate the mnemonic based on the specified entropy
564
+ const mnemonic = bip39.generateMnemonic(entropy);
565
+ return mnemonic;
526
566
  }
527
567
 
528
568
  export { generateMnemonicString, Wallet };