@yerofey/cryptowallet-cli 1.6.1 → 1.7.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/src/Wallet.js CHANGED
@@ -1,373 +1,493 @@
1
- const { log } = require('./utils');
2
- const chalk = require('chalk');
1
+ import { log } from './utils.js';
2
+ import chalk from 'chalk';
3
+ const { red } = chalk;
4
+ import CoinKey from 'coinkey';
5
+ import CoinInfo from 'coininfo';
6
+ import bip39 from 'bip39';
7
+ import bip84 from 'bip84';
8
+ const {
9
+ fromMnemonic,
10
+ fromZPrv,
11
+ } = bip84;
12
+ import ethereumBip from 'ethereum-bip84';
13
+ const {
14
+ fromMnemonic: fromMnemonicEthereum,
15
+ fromZPrv: fromZPrvEthereum,
16
+ } = ethereumBip;
17
+ import dogecoinBip from '@yerofey/dogecoin-bip84';
18
+ const {
19
+ fromMnemonic: fromMnemonicDoge,
20
+ fromZPrv: fromZPrvDoge
21
+ } = dogecoinBip;
22
+ import litecoinBip from '@yerofey/litecoin-bip84';
23
+ const {
24
+ fromMnemonic: fromMnemonicLite,
25
+ fromZPrv: fromZPrvLite,
26
+ } = litecoinBip;
27
+ import { Account } from 'eth-lib/lib/index.js';
28
+ import { Wallet as HarmonyWallet } from '@harmony-js/account';
29
+ import pkutils from 'ethereum-mnemonic-privatekey-utils';
30
+ import bCrypto from '@binance-chain/javascript-sdk/lib/crypto/index.js';
31
+ import tronWeb from 'tronweb';
32
+ import tezos from 'tezos-sign';
3
33
 
4
34
  class Wallet {
5
- constructor(cw) {
6
- this.cw = cw;
7
- }
35
+ constructor(cw) {
36
+ this.cw = cw;
37
+ }
38
+
39
+ async init() {
40
+ const cw = this.cw;
41
+ const row = cw.row;
42
+ const options = cw.options;
43
+
44
+ const desiredSymbolsArray =
45
+ options.prefix.length > 0 || options.suffix.length > 0
46
+ ? options.prefix.split('').concat(options.suffix.split(''))
47
+ : [];
48
+ const desiredSymbolsUniqueArray = desiredSymbolsArray.filter(
49
+ (item, pos) => desiredSymbolsArray.indexOf(item) === pos
50
+ );
51
+ const badSymbolsArray =
52
+ desiredSymbolsUniqueArray.filter(
53
+ (char) => !RegExp(row.prefixTest, 'g').test(char)
54
+ ) || [];
55
+
56
+ let wallet = {};
57
+ let prefixFound = false;
58
+ let prefixFoundInWallets = [];
59
+ let suffixFound = false;
60
+ let suffixFoundInWallets = [];
61
+ let onlyPrefix = false;
62
+ let onlySuffix = false;
63
+ let onlyBoth = false;
64
+
65
+ const prefixFoundInAddress = (address, isCaseSensitive, prefix, symbol) =>
66
+ (isCaseSensitive && address.startsWith(symbol + '' + prefix)) ||
67
+ (!isCaseSensitive &&
68
+ address.toUpperCase().startsWith((symbol + '' + prefix).toUpperCase()));
69
+ const suffixFoundInAddress = (address, isCaseSensitive, suffix) =>
70
+ (isCaseSensitive && address.endsWith(suffix)) ||
71
+ (!isCaseSensitive && address.toUpperCase().endsWith(suffix));
72
+
73
+ if (
74
+ (options.prefix && row.flags.includes('p')) ||
75
+ (options.suffix && row.flags.includes('s'))
76
+ ) {
77
+ if (badSymbolsArray.length === 0) {
78
+ if (options.prefix && options.suffix) {
79
+ // prefix & suffix
80
+ log(
81
+ `⏳ Generating wallet with "${options.prefix}" prefix and "${options.suffix}" suffix, this for sure will take a while...`
82
+ );
83
+ onlyBoth = true;
84
+ } else {
85
+ // prefix
86
+ if (
87
+ options.prefix.length > 0 ||
88
+ ('rareSymbols' in row &&
89
+ RegExp(row.rareSymbols, 'g').test(options.prefix))
90
+ ) {
91
+ log(
92
+ `⏳ Generating wallet with "${options.prefix}" prefix, this might take a while...`
93
+ );
94
+ onlyPrefix = true;
95
+ }
96
+ // suffix
97
+ if (
98
+ options.suffix.length > 0 ||
99
+ ('rareSymbols' in row &&
100
+ RegExp(row.rareSymbols, 'g').test(options.suffix))
101
+ ) {
102
+ log(
103
+ `⏳ Generating wallet with "${options.suffix}" suffix, this might take a while...`
104
+ );
105
+ onlySuffix = true;
106
+ }
107
+ }
108
+
109
+ const startsWithSymbols = row.startsWith.split('|');
110
+ // eslint-disable-next-line no-constant-condition
111
+ loop: while (true) {
112
+ wallet = await this.createWallet();
113
+ for (let firstSymbol of startsWithSymbols) {
114
+ if (wallet.address !== undefined) {
115
+ // one address
116
+ if (
117
+ onlyPrefix &&
118
+ prefixFoundInAddress(
119
+ wallet.address,
120
+ options.prefixIsCaseSensitive,
121
+ options.prefix,
122
+ firstSymbol
123
+ )
124
+ ) {
125
+ prefixFound = true;
126
+ break loop;
127
+ }
128
+
129
+ if (
130
+ onlySuffix &&
131
+ suffixFoundInAddress(
132
+ wallet.address,
133
+ options.suffixIsCaseSensitive,
134
+ options.suffix
135
+ )
136
+ ) {
137
+ suffixFound = true;
138
+ break loop;
139
+ }
8
140
 
9
- async init() {
10
- const cw = this.cw;
11
- const row = cw.row;
12
- const options = cw.options;
13
-
14
- const desiredSymbolsArray = (options.prefix.length > 0 || options.suffix.length > 0) ? options.prefix.split('').concat(options.suffix.split('')) : [];
15
- const desiredSymbolsUniqueArray = desiredSymbolsArray.filter((item, pos) => desiredSymbolsArray.indexOf(item) === pos);
16
- const badSymbolsArray = desiredSymbolsUniqueArray.filter(char => !RegExp(row.prefixTest, 'g').test(char)) || [];
17
-
18
- let wallet = {};
19
- let prefixFound = false;
20
- let prefixFoundInWallets = [];
21
- let suffixFound = false;
22
- let suffixFoundInWallets = [];
23
- let onlyPrefix = false;
24
- let onlySuffix = false;
25
- let onlyBoth = false;
26
-
27
- const prefixFoundInAddress = (address, isCaseSensitive, prefix, symbol) => (isCaseSensitive && address.startsWith(symbol + '' + prefix) || !isCaseSensitive && (address).toUpperCase().startsWith((symbol + '' + prefix).toUpperCase()));
28
- const suffixFoundInAddress = (address, isCaseSensitive, suffix) => (isCaseSensitive && address.endsWith(suffix) || !isCaseSensitive && (address).toUpperCase().endsWith(suffix));
29
-
30
- if (options.prefix && row.flags.includes('p') || options.suffix && row.flags.includes('s')) {
31
- if (badSymbolsArray.length === 0) {
32
- if (options.prefix && options.suffix) {
33
- // prefix & suffix
34
- log(`⏳ Generating wallet with "${options.prefix}" prefix and "${options.suffix}" suffix, this for sure will take a while...`);
35
- onlyBoth = true;
36
- } else {
37
- // prefix
38
- if (options.prefix.length > 0 || 'rareSymbols' in row && RegExp(row.rareSymbols, 'g').test(options.prefix)) {
39
- log(`⏳ Generating wallet with "${options.prefix}" prefix, this might take a while...`);
40
- onlyPrefix = true;
41
- }
42
- // suffix
43
- if (options.suffix.length > 0 || 'rareSymbols' in row && RegExp(row.rareSymbols, 'g').test(options.suffix)) {
44
- log(`⏳ Generating wallet with "${options.suffix}" suffix, this might take a while...`);
45
- onlySuffix = true;
46
- }
141
+ if (
142
+ onlyBoth &&
143
+ prefixFoundInAddress(
144
+ wallet.address,
145
+ options.prefixIsCaseSensitive,
146
+ options.prefix,
147
+ firstSymbol
148
+ ) &&
149
+ suffixFoundInAddress(
150
+ wallet.address,
151
+ options.suffixIsCaseSensitive,
152
+ options.suffix
153
+ )
154
+ ) {
155
+ prefixFound = true;
156
+ suffixFound = true;
157
+ break loop;
158
+ }
159
+ } else if (wallet.addresses !== undefined) {
160
+ // multiple addresses
161
+ for (let item of wallet.addresses) {
162
+ if (
163
+ onlyPrefix &&
164
+ prefixFoundInAddress(
165
+ item.address,
166
+ options.prefixIsCaseSensitive,
167
+ options.prefix,
168
+ firstSymbol
169
+ )
170
+ ) {
171
+ prefixFound = true;
172
+ prefixFoundInWallets.push(item.address);
47
173
  }
48
-
49
- const startsWithSymbols = row.startsWith.split('|');
50
- loop:
51
- while (true) {
52
- wallet = await this.createWallet();
53
- for (let firstSymbol of startsWithSymbols) {
54
- if (wallet.address !== undefined) { // one address
55
- if (onlyPrefix && prefixFoundInAddress(wallet.address, options.prefixIsCaseSensitive, options.prefix, firstSymbol)) {
56
- prefixFound = true;
57
- break loop;
58
- }
59
-
60
- if (onlySuffix && suffixFoundInAddress(wallet.address, options.suffixIsCaseSensitive, options.suffix)) {
61
- suffixFound = true;
62
- break loop;
63
- }
64
-
65
- if (onlyBoth && prefixFoundInAddress(wallet.address, options.prefixIsCaseSensitive, options.prefix, firstSymbol) && suffixFoundInAddress(wallet.address, options.suffixIsCaseSensitive, options.suffix)) {
66
- prefixFound = true;
67
- suffixFound = true;
68
- break loop;
69
- }
70
- } else if (wallet.addresses !== undefined) { // multiple addresses
71
- for (let item of wallet.addresses) {
72
- if (onlyPrefix && prefixFoundInAddress(item.address, options.prefixIsCaseSensitive, options.prefix, firstSymbol)) {
73
- prefixFound = true;
74
- prefixFoundInWallets.push(item.address);
75
- }
76
-
77
- if (onlySuffix && suffixFoundInAddress(item.address, options.suffixIsCaseSensitive, options.suffix)) {
78
- suffixFound = true;
79
- suffixFoundInWallets.push(item.address);
80
- }
81
-
82
- if (onlyBoth && prefixFoundInAddress(item.address, options.prefixIsCaseSensitive, options.prefix, firstSymbol) && suffixFoundInAddress(item.address, options.suffixIsCaseSensitive, options.suffix)) {
83
- prefixFound = true;
84
- prefixFoundInWallets.push(item.address);
85
- suffixFound = true;
86
- suffixFoundInWallets.push(item.address);
87
- }
88
- }
89
- if (onlyPrefix && prefixFound || onlySuffix && suffixFound || onlyBoth && prefixFound && suffixFound) {
90
- break loop;
91
- }
92
- } else {
93
- break loop;
94
- }
95
-
96
- }
174
+
175
+ if (
176
+ onlySuffix &&
177
+ suffixFoundInAddress(
178
+ item.address,
179
+ options.suffixIsCaseSensitive,
180
+ options.suffix
181
+ )
182
+ ) {
183
+ suffixFound = true;
184
+ suffixFoundInWallets.push(item.address);
97
185
  }
98
- } else {
99
- let badSymbolsString = '';
100
- for (const symbol of badSymbolsArray) {
101
- badSymbolsString += '"' + symbol + '", ';
186
+
187
+ if (
188
+ onlyBoth &&
189
+ prefixFoundInAddress(
190
+ item.address,
191
+ options.prefixIsCaseSensitive,
192
+ options.prefix,
193
+ firstSymbol
194
+ ) &&
195
+ suffixFoundInAddress(
196
+ item.address,
197
+ options.suffixIsCaseSensitive,
198
+ options.suffix
199
+ )
200
+ ) {
201
+ prefixFound = true;
202
+ prefixFoundInWallets.push(item.address);
203
+ suffixFound = true;
204
+ suffixFoundInWallets.push(item.address);
102
205
  }
103
-
104
- // TODO: add prefix/suffix own message log
105
- log(chalk.red('⛔️ Error: prefix or suffix contains non-supported characters (' + badSymbolsString.substr(0, badSymbolsString.length - 2) + ')!'));
106
- process.exit(1);
206
+ }
207
+ if (
208
+ (onlyPrefix && prefixFound) ||
209
+ (onlySuffix && suffixFound) ||
210
+ (onlyBoth && prefixFound && suffixFound)
211
+ ) {
212
+ break loop;
213
+ }
214
+ } else {
215
+ break loop;
107
216
  }
108
- } else {
109
- wallet = await this.createWallet();
217
+ }
110
218
  }
219
+ } else {
220
+ let badSymbolsString = '';
221
+ for (const symbol of badSymbolsArray) {
222
+ badSymbolsString += '"' + symbol + '", ';
223
+ }
224
+
225
+ // TODO: add prefix/suffix own message log
226
+ log(
227
+ red(
228
+ '⛔️ Error: prefix or suffix contains non-supported characters (' +
229
+ badSymbolsString.substr(0, badSymbolsString.length - 2) +
230
+ ')!'
231
+ )
232
+ );
233
+ // eslint-disable-next-line no-undef
234
+ process.exit(1);
235
+ }
236
+ } else {
237
+ wallet = await this.createWallet();
238
+ }
239
+
240
+ return {
241
+ wallet,
242
+ prefixFound,
243
+ prefixFoundInWallets,
244
+ suffixFound,
245
+ suffixFoundInWallets,
246
+ };
247
+ }
248
+
249
+ async createWallet() {
250
+ const cw = this.cw;
251
+ const chain = cw.chain;
252
+ const row = cw.row;
253
+ const options = cw.options;
111
254
 
255
+ let format = options.format || '';
256
+ let mnemonicString = options.mnemonic || '';
257
+ let number = options.number || 1;
258
+ let result = {};
259
+
260
+ if (row.length == 0) {
261
+ return {
262
+ error: 'this blockchain is not found',
263
+ };
264
+ }
265
+
266
+ if (row.script == 'coinkey') {
267
+ const wallet = CoinKey.createRandom(CoinInfo(chain).versions);
268
+
269
+ result = Object.assign(result, {
270
+ format,
271
+ addresses: [
272
+ {
273
+ index: 0,
274
+ address: wallet.publicAddress,
275
+ privateKey: wallet.privateWif,
276
+ },
277
+ ],
278
+ });
279
+ } else if (chain == 'BTC') {
280
+ if (mnemonicString != '' && !bip39.validateMnemonic(mnemonicString)) {
112
281
  return {
113
- wallet,
114
- prefixFound,
115
- prefixFoundInWallets,
116
- suffixFound,
117
- suffixFoundInWallets,
282
+ error: 'mnemonic is not valid',
118
283
  };
119
- }
284
+ }
120
285
 
121
- async createWallet() {
122
- const cw = this.cw;
123
- const chain = cw.chain;
124
- const row = cw.row;
125
- const options = cw.options;
126
-
127
- let format = options.format || '';
128
- let mnemonicString = options.mnemonic || '';
129
- let number = options.number || 1;
130
- let result = {};
131
-
132
- if (row.length == 0) {
133
- return {
134
- error: 'this blockchain is not found',
135
- }
286
+ const mnemonic = mnemonicString || bip39.generateMnemonic();
287
+ const root = new fromMnemonic(mnemonic, '');
288
+ const child = root.deriveAccount(0);
289
+ const account = new fromZPrv(child);
290
+
291
+ let addresses = [];
292
+ if (number >= 1) {
293
+ for (let i = 0; i < number; i++) {
294
+ addresses.push({
295
+ index: i,
296
+ address: account.getAddress(i, false, row.purpose),
297
+ privateKey: account.getPrivateKey(i),
298
+ });
136
299
  }
137
-
138
- if (row.script == 'coinkey') {
139
- const CoinKey = require('coinkey');
140
- const CoinInfo = require('coininfo');
141
-
142
- const wallet = CoinKey.createRandom(CoinInfo(chain).versions);
143
-
144
- result = Object.assign(result, {
145
- format,
146
- addresses: [{
147
- index: 0,
148
- address: wallet.publicAddress,
149
- privateKey: wallet.privateWif,
150
- }]
151
- });
152
- } else if (chain == 'BTC') {
153
- const bip39 = require('bip39');
154
- const { fromMnemonic, fromZPrv } = require('bip84');
155
-
156
- if (mnemonicString != '' && !bip39.validateMnemonic(mnemonicString)) {
157
- return {
158
- error: 'mnemonic is not valid'
159
- }
160
- }
161
-
162
- const mnemonic = mnemonicString || bip39.generateMnemonic();
163
- const root = new fromMnemonic(mnemonic, '');
164
- const child = root.deriveAccount(0);
165
- const account = new fromZPrv(child);
166
-
167
- let addresses = [];
168
- if (number >= 1) {
169
- for (let i = 0; i < number; i++) {
170
- addresses.push({
171
- index: i,
172
- address: account.getAddress(i, false, row.purpose),
173
- privateKey: account.getPrivateKey(i)
174
- });
175
- }
176
- }
177
-
178
- Object.assign(result, {
179
- format: row.format,
180
- addresses,
181
- privateExtendedKey: account.getAccountPrivateKey(),
182
- mnemonic
183
- });
184
- } else if (chain == 'DOGE' || chain == 'LTC') {
185
- const bip39 = require('bip39');
186
- const { fromMnemonic, fromZPrv } = require('@yerofey/' + row.title.toLowerCase() + '-bip84');
187
-
188
- if (mnemonicString != '' && !bip39.validateMnemonic(mnemonicString)) {
189
- return {
190
- error: 'mnemonic is not valid'
191
- }
192
- }
193
-
194
- const mnemonic = mnemonicString || bip39.generateMnemonic();
195
- const root = new fromMnemonic(mnemonic, '');
196
- const child = root.deriveAccount(0);
197
- const account = new fromZPrv(child);
198
-
199
- let addresses = [];
200
- if (number >= 1) {
201
- for (let i = 0; i < number; i++) {
202
- addresses.push({
203
- index: i,
204
- address: account.getAddress(i, false, row.purpose),
205
- privateKey: account.getPrivateKey(i)
206
- });
207
- }
208
- }
209
-
210
- Object.assign(result, {
211
- format: row.format,
212
- addresses,
213
- privateExtendedKey: account.getAccountPrivateKey(),
214
- mnemonic
215
- });
216
- } else if (row.format == 'BEP2') {
217
- const bip39 = require('bip39');
218
- const bCrypto = require('@binance-chain/javascript-sdk/lib/crypto');
219
-
220
- if (mnemonicString != '' && !bip39.validateMnemonic(mnemonicString)) {
221
- return {
222
- error: 'mnemonic is not valid'
223
- }
224
- }
225
-
226
- let addresses = [];
227
- const mnemonic = mnemonicString || bip39.generateMnemonic();
228
-
229
- if (number == 1) {
230
- const privateKey = bCrypto.getPrivateKeyFromMnemonic(mnemonic, true, 0);
231
- addresses.push({
232
- index: 0,
233
- address: bCrypto.getAddressFromPrivateKey(privateKey, 'bnb'),
234
- privateKey
235
- });
236
- } else {
237
- for (let i = 0; i <= number; i++) {
238
- const privateKey = bCrypto.getPrivateKeyFromMnemonic(mnemonic, true, i);
239
- addresses.push({
240
- index: i,
241
- address: bCrypto.getAddressFromPrivateKey(privateKey, 'bnb'),
242
- privateKey
243
- });
244
- }
245
- }
300
+ }
246
301
 
247
- Object.assign(result, {
248
- format: 'BEP2',
249
- addresses,
250
- mnemonic
251
- });
252
- } else if (row.network == 'EVM') {
253
- const bip39 = require('bip39');
254
- const pkutils = require('ethereum-mnemonic-privatekey-utils');
255
- const { Account } = require('eth-lib/lib');
256
- const { fromMnemonic, fromZPrv } = require('ethereum-bip84');
257
-
258
- if (mnemonicString != '' && !bip39.validateMnemonic(mnemonicString)) {
259
- return {
260
- error: 'mnemonic is not valid',
261
- }
262
- }
263
-
264
- let addresses = [];
265
- const mnemonic = mnemonicString || bip39.generateMnemonic();
266
- const privateKey = pkutils.getPrivateKeyFromMnemonic(mnemonic);
267
-
268
- if (number == 1) {
269
- const account = Account.fromPrivate('0x' + privateKey);
270
-
271
- addresses.push({
272
- index: 0,
273
- address: account.address,
274
- privateKey,
275
- });
276
- } else {
277
- // TODO: add variable for accountId
278
- const root = new fromMnemonic(mnemonic, '');
279
- const child = root.deriveAccount(0);
280
- const account = new fromZPrv(child);
281
- for (let walletId = 0; walletId <= number; walletId++) {
282
- const walletAddress = account.getAddress(walletId);
283
- const privateKey = account.getPrivateKey(walletId);
284
-
285
- addresses.push({
286
- index: walletId,
287
- address: walletAddress,
288
- privateKey,
289
- });
290
- }
291
- }
292
-
293
- Object.assign(result, {
294
- format: row.format || '',
295
- addresses,
296
- mnemonic,
297
- });
298
- } else if (chain == 'ONE') {
299
- const bip39 = require('bip39');
300
- const { Wallet } = require('@harmony-js/account');
301
-
302
- if (mnemonicString != '' && !bip39.validateMnemonic(mnemonicString)) {
303
- return {
304
- error: 'mnemonic is not valid'
305
- }
306
- }
307
-
308
- const wallet = new Wallet();
309
- const mnemonic = mnemonicString || bip39.generateMnemonic();
310
- wallet.addByMnemonic(mnemonic);
311
- const publicKey = wallet.accounts[0];
312
- const account = wallet.getAccount(publicKey);
313
-
314
- Object.assign(result, {
315
- addresses: [{
316
- index: 0,
317
- address: account.bech32Address,
318
- privateKey: account.privateKey,
319
- }],
320
- mnemonic,
321
- });
322
- } else if (chain == 'TRX') {
323
- const tronWeb = require('tronweb');
324
-
325
- try {
326
- const wallet = await tronWeb.createAccount();
327
-
328
- Object.assign(result, {
329
- addresses: [{
330
- index: 0,
331
- address: wallet.address.base58,
332
- privateKey: wallet.privateKey
333
- }],
334
- });
335
- } catch (error) {
336
- return {
337
- error
338
- }
339
- }
340
- } else if (chain == 'XTZ') {
341
- const tezos = require('tezos-sign');
342
- const wallet = tezos.generateKeysNoSeed();
343
-
344
- Object.assign(result, {
345
- addresses: [{
346
- index: 0,
347
- address: wallet.pkh,
348
- privateKey: wallet.sk,
349
- }],
350
- });
351
- } else {
352
- return {
353
- error: 'your desired blockchain is not supported yet'
354
- }
302
+ Object.assign(result, {
303
+ format: row.format,
304
+ addresses,
305
+ privateExtendedKey: account.getAccountPrivateKey(),
306
+ mnemonic,
307
+ });
308
+ } else if (chain == 'DOGE' || chain == 'LTC') {
309
+ if (mnemonicString != '' && !bip39.validateMnemonic(mnemonicString)) {
310
+ return {
311
+ error: 'mnemonic is not valid',
312
+ };
313
+ }
314
+
315
+ const _fromMnemonic = (chain == 'DOGE') ? fromMnemonicDoge : fromMnemonicLite;
316
+ const _fromZPrv = (chain == 'DOGE') ? fromZPrvDoge : fromZPrvLite;
317
+ const mnemonic = mnemonicString || bip39.generateMnemonic();
318
+ const root = new _fromMnemonic(mnemonic, '');
319
+ const child = root.deriveAccount(0);
320
+ const account = new _fromZPrv(child);
321
+
322
+ let addresses = [];
323
+ if (number >= 1) {
324
+ for (let i = 0; i < number; i++) {
325
+ addresses.push({
326
+ index: i,
327
+ address: account.getAddress(i, false, row.purpose),
328
+ privateKey: account.getPrivateKey(i),
329
+ });
330
+ }
331
+ }
332
+
333
+ Object.assign(result, {
334
+ format: row.format,
335
+ addresses,
336
+ privateExtendedKey: account.getAccountPrivateKey(),
337
+ mnemonic,
338
+ });
339
+ } else if (row.format == 'BEP2') {
340
+ if (mnemonicString != '' && !bip39.validateMnemonic(mnemonicString)) {
341
+ return {
342
+ error: 'mnemonic is not valid',
343
+ };
344
+ }
345
+
346
+ let addresses = [];
347
+ const mnemonic = mnemonicString || bip39.generateMnemonic();
348
+
349
+ if (number == 1) {
350
+ const privateKey = bCrypto.getPrivateKeyFromMnemonic(mnemonic, true, 0);
351
+ addresses.push({
352
+ index: 0,
353
+ address: bCrypto.getAddressFromPrivateKey(privateKey, 'bnb'),
354
+ privateKey,
355
+ });
356
+ } else {
357
+ for (let i = 0; i <= number; i++) {
358
+ const privateKey = bCrypto.getPrivateKeyFromMnemonic(
359
+ mnemonic,
360
+ true,
361
+ i
362
+ );
363
+ addresses.push({
364
+ index: i,
365
+ address: bCrypto.getAddressFromPrivateKey(privateKey, 'bnb'),
366
+ privateKey,
367
+ });
355
368
  }
356
-
357
- if (row.tested !== undefined && row.tested == false) {
358
- Object.assign(result, {
359
- tested: false,
360
- });
369
+ }
370
+
371
+ Object.assign(result, {
372
+ format: 'BEP2',
373
+ addresses,
374
+ mnemonic,
375
+ });
376
+ } else if (row.network == 'EVM') {
377
+ if (mnemonicString != '' && !bip39.validateMnemonic(mnemonicString)) {
378
+ return {
379
+ error: 'mnemonic is not valid',
380
+ };
381
+ }
382
+
383
+ let addresses = [];
384
+ const mnemonic = mnemonicString || bip39.generateMnemonic();
385
+ const privateKey = pkutils.getPrivateKeyFromMnemonic(mnemonic);
386
+
387
+ if (number == 1) {
388
+ const account = Account.fromPrivate('0x' + privateKey);
389
+
390
+ addresses.push({
391
+ index: 0,
392
+ address: account.address,
393
+ privateKey,
394
+ });
395
+ } else {
396
+ // TODO: add variable for accountId
397
+ const root = new fromMnemonicEthereum(mnemonic, '');
398
+ const child = root.deriveAccount(0);
399
+ const account = new fromZPrvEthereum(child);
400
+ for (let walletId = 0; walletId <= number; walletId++) {
401
+ const walletAddress = account.getAddress(walletId);
402
+ const privateKey = account.getPrivateKey(walletId);
403
+
404
+ addresses.push({
405
+ index: walletId,
406
+ address: walletAddress,
407
+ privateKey,
408
+ });
361
409
  }
362
-
363
- return result;
410
+ }
411
+
412
+ Object.assign(result, {
413
+ format: row.format || '',
414
+ addresses,
415
+ mnemonic,
416
+ });
417
+ } else if (chain == 'ONE') {
418
+ if (mnemonicString != '' && !bip39.validateMnemonic(mnemonicString)) {
419
+ return {
420
+ error: 'mnemonic is not valid',
421
+ };
422
+ }
423
+
424
+ const wallet = new HarmonyWallet();
425
+ const mnemonic = mnemonicString || bip39.generateMnemonic();
426
+ wallet.addByMnemonic(mnemonic);
427
+ const publicKey = wallet.accounts[0];
428
+ const account = wallet.getAccount(publicKey);
429
+
430
+ Object.assign(result, {
431
+ addresses: [
432
+ {
433
+ index: 0,
434
+ address: account.bech32Address,
435
+ privateKey: account.privateKey,
436
+ },
437
+ ],
438
+ mnemonic,
439
+ });
440
+ } else if (chain == 'TRX') {
441
+ try {
442
+ const wallet = await tronWeb.createAccount();
443
+
444
+ Object.assign(result, {
445
+ addresses: [
446
+ {
447
+ index: 0,
448
+ address: wallet.address.base58,
449
+ privateKey: wallet.privateKey,
450
+ },
451
+ ],
452
+ });
453
+ } catch (error) {
454
+ return {
455
+ error,
456
+ };
457
+ }
458
+ } else if (chain == 'XTZ') {
459
+ const wallet = tezos.generateKeysNoSeed();
460
+
461
+ Object.assign(result, {
462
+ addresses: [
463
+ {
464
+ index: 0,
465
+ address: wallet.pkh,
466
+ privateKey: wallet.sk,
467
+ },
468
+ ],
469
+ });
470
+ } else {
471
+ return {
472
+ error: 'your desired blockchain is not supported yet',
473
+ };
364
474
  }
475
+
476
+ if (row.tested !== undefined && row.tested == false) {
477
+ Object.assign(result, {
478
+ tested: false,
479
+ });
480
+ }
481
+
482
+ return result;
483
+ }
365
484
  }
366
485
 
367
486
  function generateMnemonicString() {
368
- const bip39 = require('bip39');
369
- return bip39.generateMnemonic();
487
+ return bip39.generateMnemonic();
370
488
  }
371
489
 
372
- module.exports.generateMnemonicString = generateMnemonicString;
373
- module.exports.Wallet = Wallet;
490
+ const _generateMnemonicString = generateMnemonicString;
491
+ export { _generateMnemonicString as generateMnemonicString };
492
+ const _Wallet = Wallet;
493
+ export { _Wallet as Wallet };