Skip to content

Commit

Permalink
v1.10.0: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
yerofey committed Aug 7, 2023
1 parent 05f82c3 commit e3e2284
Show file tree
Hide file tree
Showing 10 changed files with 435 additions and 442 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.16
20.5
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ $ cw -l
- [ ] v17.x ⛔
- [x] v18.x ✅
- [x] v19.x ✅
- [x] v20.x ✅

*tested on Mac M1*

Expand Down
42 changes: 1 addition & 41 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,12 @@
#!/usr/bin/env node
'use strict';

import { program } from 'commander';
import chalk from 'chalk';
import { options } from './src/options.js';
import { log, supportedChains } from './src/utils.js';
import Method from './src/Method.js';

program.option('-b, --chain <ticker>', 'Wallet for specific blockchain', 'ERC');
program.option('-c, --chain <ticker>', 'Wallet for specific blockchain', 'ERC');
program.option(
'-D, --csv [filename]',
'Save result into CSV file'
);
program.option(
'-f, --format <format>',
'Wallet format type (for cryptos with multiple wallet formats)'
);
program.option(
'-F, --filename <filename>',
'Filename to output the data (works with -o argument)'
);
program.option('-g, --geek', 'Display some more info (geeky)');
program.option('-l, --list', 'List all supported cryptos');
program.option(
'-m, --mnemonic [mnemonic]',
'Generate wallet from mnemonic string OR just a mnemonic string'
);
program.option(
'-n, --number <number>',
'Number of wallets to generate (if supported)'
);
program.option('-o, --output <format>', 'Return results into some file');
program.option('-p, --prefix <prefix>', 'Desired wallet prefix');
program.option(
'-P, --prefix-sensitive <prefix>',
'Desired wallet prefix (case-sensitive)'
);
program.option('-s, --suffix <suffix>', 'Desired wallet suffix');
program.option(
'-S, --suffix-sensitive <suffix>',
'Desired wallet suffix (case-sensitive)'
);
program.option('-v, --version', 'Display cryptowallet version');
program.parse();

(async () => {
const options = program.opts();

if (options.list !== undefined) {
return new Method('list').init();
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@yerofey/cryptowallet-cli",
"version": "1.9.0",
"version": "1.10.0",
"description": "Crypto wallet generator CLI tool",
"type": "module",
"homepage": "https://github.com/yerofey/cryptowallet-cli",
Expand Down
23 changes: 9 additions & 14 deletions src/CW.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ import { Wallet } from './Wallet.js';

class CW {
constructor(chain, options = {}) {
this.chain = chain;
this.options = this._prepareOptions(chain, options);
}

_prepareOptions(chain, options) {
const csvFilename = 'cw-output';

if (options.csv !== undefined) {
options.output = 'csv';

if (typeof options.csv === 'string' && (options.csv).length > 0) {
if (typeof options.csv === 'string' && options.csv.length > 0) {
options.filename = options.csv;
}
}
Expand All @@ -26,26 +32,15 @@ class CW {
suffixIsCaseSensitive: options.suffixSensitive !== undefined,
};

for (const key of Object.keys(defaultValues)) {
// eslint-disable-next-line no-prototype-builtins
if (!options.hasOwnProperty(key)) {
options[key] = defaultValues[key];
}
}

this.chain = chain;
this.options = options;
return { ...defaultValues, ...options };
}

async init() {
const chainData = await new Chain(this.chain, this.options.format).init();
this.row = chainData.row || {};

const w = await new Wallet(this).init();

for (const key of Object.keys(w)) {
this[key] = w[key];
}
Object.assign(this, w);

return this;
}
Expand Down
55 changes: 30 additions & 25 deletions src/Chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,43 @@ import path from 'node:path';
import { loadJson } from './utils.js';

class Chain {
constructor(chain, format) {
constructor(chain, format = '') {
this.chain = chain;
this.format = format;
this.row = null;
}

async init() {
// eslint-disable-next-line no-undef
const content = await loadJson(`${path.dirname(import.meta.url)}/chains/${this.chain}.json`.replace('file://', ''));
const data = (() => {
if (content.formats !== undefined) {
if (this.format != '' && this.format != content.defaultFormat) {
// case-insensitive format
for (const key of Object.keys(content.formats)) {
if (this.format.toLowerCase() == key.toLowerCase()) {
return content.formats[key];
}
}
}

return content.formats[content.defaultFormat];
}

return content;
})();

this.row = {
...content,
...data,
};

const content = await this._loadChainJson();
this.row = this._parseContent(content);
return this;
}

async _loadChainJson() {
const filePath = `${path.dirname(import.meta.url)}/chains/${
this.chain
}.json`.replace('file://', '');
return await loadJson(filePath);
}

_parseContent(content) {
if (!content.formats) return content;

const formatKey = this._findFormatKey(content);
return { ...content, ...content.formats[formatKey] };
}

_findFormatKey(content) {
if (!this.format || this.format === content.defaultFormat)
return content.defaultFormat;

const normalizedFormat = this.format.toLowerCase();
const matchingKey = Object.keys(content.formats).find(
(key) => key.toLowerCase() === normalizedFormat
);

return matchingKey || content.defaultFormat;
}
}

export default Chain;
Loading

0 comments on commit e3e2284

Please sign in to comment.