From 73d093715d385ac79dbf0f1d378f45fe3ef0c0bf Mon Sep 17 00:00:00 2001 From: Kenny Joseph Date: Fri, 10 Oct 2025 17:03:08 -0400 Subject: [PATCH] lint bitcore-cli --- eslint.config.mjs | 3 +- packages/bitcore-cli/package.json | 5 +- packages/bitcore-cli/src/cli.ts | 414 +++++++++--------- .../bitcore-cli/src/commands/addresses.ts | 4 +- packages/bitcore-cli/src/commands/balance.ts | 2 +- .../src/commands/create/createMultiSig.ts | 6 +- .../src/commands/create/createSingleSig.ts | 6 +- .../src/commands/create/createThresholdSig.ts | 10 +- .../bitcore-cli/src/commands/create/index.ts | 8 +- packages/bitcore-cli/src/commands/derive.ts | 4 +- packages/bitcore-cli/src/commands/export.ts | 4 +- packages/bitcore-cli/src/commands/import.ts | 6 +- .../bitcore-cli/src/commands/join/index.ts | 4 +- .../src/commands/join/joinMultiSig.ts | 8 +- .../src/commands/join/joinThresholdSig.ts | 12 +- .../bitcore-cli/src/commands/preferences.ts | 2 +- packages/bitcore-cli/src/commands/sign.ts | 6 +- packages/bitcore-cli/src/commands/status.ts | 6 +- packages/bitcore-cli/src/commands/token.ts | 2 +- .../bitcore-cli/src/commands/transaction.ts | 8 +- .../bitcore-cli/src/commands/txhistory.ts | 12 +- .../bitcore-cli/src/commands/txproposals.ts | 18 +- packages/bitcore-cli/src/commands/utxos.ts | 10 +- packages/bitcore-cli/src/filestorage.ts | 9 +- packages/bitcore-cli/src/prompts.ts | 4 +- packages/bitcore-cli/src/tss.ts | 6 +- packages/bitcore-cli/src/utils.ts | 6 +- packages/bitcore-cli/src/wallet.ts | 42 +- packages/bitcore-cli/types/cli.d.ts | 4 +- packages/bitcore-cli/types/wallet.d.ts | 4 +- 30 files changed, 315 insertions(+), 320 deletions(-) diff --git a/eslint.config.mjs b/eslint.config.mjs index 8be21271bcf..b1f773b2224 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -15,7 +15,8 @@ export default defineConfig([ ecmaVersion: 2020, globals: { ...globals.node, - 'NodeJS': 'readonly' + 'NodeJS': 'readonly', + 'BufferEncoding': 'readonly', } } }, diff --git a/packages/bitcore-cli/package.json b/packages/bitcore-cli/package.json index b48c0a752cd..d52ca57302b 100644 --- a/packages/bitcore-cli/package.json +++ b/packages/bitcore-cli/package.json @@ -34,8 +34,9 @@ "postcompile": "node createBin -v", "clean": "rm -rf ./build", "lint": "eslint .", - "fix": "eslint --fix --quiet .", - "precommit": "echo 'TODO: npm run lint; npm run fix'" + "fix:errors": "eslint --fix --quiet .", + "fix:all": "eslint --fix .", + "precommit": "npm run lint; npm run fix:errors" }, "dependencies": { "@clack/prompts": "1.0.0-alpha.3", diff --git a/packages/bitcore-cli/src/cli.ts b/packages/bitcore-cli/src/cli.ts index 100ee04ca1b..b2cfa6234ac 100755 --- a/packages/bitcore-cli/src/cli.ts +++ b/packages/bitcore-cli/src/cli.ts @@ -1,18 +1,18 @@ #!/usr/bin/env node +import * as commands from './commands'; +import * as Errors from './errors'; import * as prompt from '@clack/prompts'; -import Mnemonic from 'bitcore-mnemonic'; import { Errors as BWCErrors, Status } from 'bitcore-wallet-client'; -import { program } from 'commander'; -import fs from 'fs'; -import os from 'os'; -import path from 'path'; import { CommonArgs, ICliOptions } from '../types/cli'; -import { getCommands } from './cli-commands'; -import * as commands from './commands'; import { bitcoreLogo } from './constants'; -import * as Errors from './errors'; +import fs from 'fs'; import { getAction } from './prompts'; +import { getCommands } from './cli-commands'; +import Mnemonic from 'bitcore-mnemonic'; +import os from 'os'; +import path from 'path'; +import { program } from 'commander'; import { Utils } from './utils'; import { Wallet } from './wallet'; @@ -34,9 +34,7 @@ program .option('--register', 'Register the wallet with the Bitcore Wallet Service if it does not exist') .option('--walletId ', 'Support Staff Only: Wallet ID to provide support for') .option('-h, --help', 'Display help message. Use with --command to get help for a specific command') - .version(version, '--version', 'Output the version number of this tool') - // .parse(process.argv); - // .parseOptions(process.argv) + .version(version, '--version', 'Output the version number of this tool'); const opts = program.opts() as ICliOptions; @@ -73,15 +71,15 @@ if (require.main === module) { if (opts.command === 'help') { const padLen = 18; program - .addHelpText('after', os.EOL + - // 'New Wallet Commands:' + os.EOL + - // COMMANDS.NEW.map(cmd => ` ${cmd.value.padEnd(padLen)}${cmd.hint}`).join(os.EOL) + os.EOL + os.EOL + - 'Wallet Commands:' + os.EOL + - COMMANDS.BASIC.filter(cmd => !cmd['noCmd']).map(cmd => ` ${cmd.value.padEnd(padLen)}${cmd.hint}`).join(os.EOL) + os.EOL + os.EOL + - 'Advanced Commands:' + os.EOL + - COMMANDS.ADVANCED.filter(cmd => !cmd['noCmd']).map(cmd => ` ${cmd.value.padEnd(padLen)}${cmd.hint}`).join(os.EOL) - ) - .help(); + .addHelpText('after', os.EOL + + // 'New Wallet Commands:' + os.EOL + + // COMMANDS.NEW.map(cmd => ` ${cmd.value.padEnd(padLen)}${cmd.hint}`).join(os.EOL) + os.EOL + os.EOL + + 'Wallet Commands:' + os.EOL + + COMMANDS.BASIC.filter(cmd => !cmd['noCmd']).map(cmd => ` ${cmd.value.padEnd(padLen)}${cmd.hint}`).join(os.EOL) + os.EOL + os.EOL + + 'Advanced Commands:' + os.EOL + + COMMANDS.ADVANCED.filter(cmd => !cmd['noCmd']).map(cmd => ` ${cmd.value.padEnd(padLen)}${cmd.hint}`).join(os.EOL) + ) + .help(); } else if (isCmdHelp) { if (!commands[opts.command]) { Utils.die(`Unknown command "${opts.command}"`); @@ -95,209 +93,209 @@ if (require.main === module) { mustExist: false, doNotComplete: isCmdHelp || opts.register }) - .catch((err) => { - if (err instanceof BWCErrors.NOT_AUTHORIZED) { - if (opts.register) { - return commands.register.registerWallet({ wallet, opts }); + .catch((err) => { + if (err instanceof BWCErrors.NOT_AUTHORIZED) { + if (opts.register) { + return commands.register.registerWallet({ wallet, opts }); + } else { + prompt.log.error('This wallet does not appear to be registered with the Bitcore Wallet Service. Use --register to do so.'); + Utils.die(err); + } } else { - prompt.log.error('This wallet does not appear to be registered with the Bitcore Wallet Service. Use --register to do so.'); Utils.die(err); } - } else { - Utils.die(err); - } - }) - .then(async () => { - if (walletName === 'list') { - for (const file of fs.readdirSync(opts.dir)) { - if (file.endsWith('.json')) { - console.log(`- ${file.replace('.json', '')}`); + }) + .then(async () => { + if (walletName === 'list') { + for (const file of fs.readdirSync(opts.dir)) { + if (file.endsWith('.json')) { + console.log(`- ${file.replace('.json', '')}`); + } } + return; } - return; - } - const cmdParams: CommonArgs = { - wallet, - program: opts.command ? program : undefined, - opts, - status: null as Status - }; + const cmdParams: CommonArgs = { + wallet, + program: opts.command ? program : undefined, + opts, + status: null as Status + }; - if (!wallet.client?.credentials) { - prompt.intro(`No wallet found named ${Utils.colorText(walletName, 'orange')}`); - const action: NewCommand | symbol = await prompt.select({ - message: 'What would you like to do?', - options: [].concat(COMMANDS.NEW, COMMANDS.EXIT) - }); - switch (action) { - case 'create': - await commands.create.createWallet(cmdParams); - break; - case 'join': - await commands.join.joinWallet(cmdParams); - break; - case 'import-seed': - const mnemonic = await prompt.password({ - message: 'Enter your 12-24 word mnemonic phrase:', - validate: (input) => !Mnemonic.isValid(input) ? 'Invalid mnemonic. Please check your spelling and try again' : undefined, - }); - if (prompt.isCancel(mnemonic)) { - throw new Errors.UserCancelled(); - } - cmdParams.opts.mnemonic = mnemonic; - await commands.create.createWallet(cmdParams); - break; - case 'import-file': - await commands.import.importWallet(cmdParams); - break; - case 'exit': - default: - opts.exit = true; - break; - } - prompt.outro(`${Utils.colorText('✔', 'green')} Wallet ${Utils.colorText(walletName, 'orange')} created successfully!`); - } else { + if (!wallet.client?.credentials) { + prompt.intro(`No wallet found named ${Utils.colorText(walletName, 'orange')}`); + const action: NewCommand | symbol = await prompt.select({ + message: 'What would you like to do?', + options: [].concat(COMMANDS.NEW, COMMANDS.EXIT) + }); + switch (action) { + case 'create': + await commands.create.createWallet(cmdParams); + break; + case 'join': + await commands.join.joinWallet(cmdParams); + break; + case 'import-seed': + const mnemonic = await prompt.password({ + message: 'Enter your 12-24 word mnemonic phrase:', + validate: (input) => !Mnemonic.isValid(input) ? 'Invalid mnemonic. Please check your spelling and try again' : undefined, + }); + if (prompt.isCancel(mnemonic)) { + throw new Errors.UserCancelled(); + } + cmdParams.opts.mnemonic = mnemonic; + await commands.create.createWallet(cmdParams); + break; + case 'import-file': + await commands.import.importWallet(cmdParams); + break; + case 'exit': + default: + opts.exit = true; + break; + } + prompt.outro(`${Utils.colorText('✔', 'green')} Wallet ${Utils.colorText(walletName, 'orange')} created successfully!`); + } else { - if (opts.status) { - prompt.intro(`Status for ${Utils.colorText(walletName, 'orange')}`); - const status = await commands.status.walletStatus({ wallet, opts }); - cmdParams.status = status; - prompt.outro('Welcome to the Bitcore CLI!'); - } + if (opts.status) { + prompt.intro(`Status for ${Utils.colorText(walletName, 'orange')}`); + const status = await commands.status.walletStatus({ wallet, opts }); + cmdParams.status = status; + prompt.outro('Welcome to the Bitcore CLI!'); + } - let advancedActions = false; - do { - // Don't display the intro if running a specific command - !opts.command && prompt.intro(`${Utils.colorText('~~ Main Menu ~~', 'blue')} (${Utils.colorText(walletName, 'orange')})`); - cmdParams.status.pendingTxps = opts.command ? [] : await wallet.client.getTxProposals({}); - - const dynamicCmdArgs = { - ppNum: cmdParams.status.pendingTxps.length ? Utils.colorText(` (${cmdParams.status.pendingTxps.length})`, 'yellow') : '', - sNum: Utils.colorText(` (${'TODO'})`, 'yellow'), - token: cmdParams.opts?.token - }; + let advancedActions = false; + do { + // Don't display the intro if running a specific command + !opts.command && prompt.intro(`${Utils.colorText('~~ Main Menu ~~', 'blue')} (${Utils.colorText(walletName, 'orange')})`); + cmdParams.status.pendingTxps = opts.command ? [] : await wallet.client.getTxProposals({}); + + const dynamicCmdArgs = { + ppNum: cmdParams.status.pendingTxps.length ? Utils.colorText(` (${cmdParams.status.pendingTxps.length})`, 'yellow') : '', + sNum: Utils.colorText(` (${'TODO'})`, 'yellow'), + token: cmdParams.opts?.token + }; - const BASIC = COMMANDS.BASIC - .filter(cmd => cmd['show']?.() ?? true) - .map(cmd => ({ ...cmd, label: typeof cmd.label === 'function' ? cmd.label(dynamicCmdArgs) : cmd.label })) - const ADVANCED = COMMANDS.ADVANCED - .filter(cmd => cmd['show']?.() ?? true) + const BASIC = COMMANDS.BASIC + .filter(cmd => cmd['show']?.() ?? true) + .map(cmd => ({ ...cmd, label: typeof cmd.label === 'function' ? cmd.label(dynamicCmdArgs) : cmd.label })); + const ADVANCED = COMMANDS.ADVANCED + .filter(cmd => cmd['show']?.() ?? true); - const menuAction: Command | symbol = opts.command as Command || (opts.register - ? 'register' - : await prompt.select({ - message: 'What would you like to do?', - options: (BASIC as Array) - .concat(advancedActions ? ADVANCED : [COMMANDS.SHOW_ADVANCED]) - .concat(COMMANDS.EXIT), - initialValue: advancedActions ? ADVANCED[0].value : BASIC[0].value, - })); + const menuAction: Command | symbol = opts.command as Command || (opts.register + ? 'register' + : await prompt.select({ + message: 'What would you like to do?', + options: (BASIC as Array) + .concat(advancedActions ? ADVANCED : [COMMANDS.SHOW_ADVANCED]) + .concat(COMMANDS.EXIT), + initialValue: advancedActions ? ADVANCED[0].value : BASIC[0].value, + })); - let action: string | symbol | undefined; - advancedActions = false; - try { - switch (menuAction as Command) { - case 'token': - const result = await commands.token.setToken(cmdParams); - const { tokenObj } = result; - action = result.action; - // If context has changed... - if (cmdParams.opts?.tokenAddress?.toLowerCase() !== tokenObj?.contractAddress.toLowerCase()) { - // ...update status - cmdParams.status = await wallet.client.getStatus({ tokenAddress: tokenObj?.contractAddress }) - } - cmdParams.opts.tokenAddress = tokenObj?.contractAddress; - cmdParams.opts.token = tokenObj?.displayCode; - break; - case 'address': - await commands.address.createAddress(cmdParams); - break; - case 'balance': - const balance = await commands.balance.getBalance(cmdParams); - if (cmdParams.status) { - cmdParams.status.balance = balance; - } - break; - case 'history': - ({ action } = await commands.history.getTxHistory(cmdParams)); - break; - case 'transaction': - await commands.transaction.createTransaction(cmdParams); - break; - case 'txproposals': - ({ action } = await commands.txproposals.getTxProposals(cmdParams)); - break; - case 'status': - cmdParams.status = await commands.status.walletStatus(cmdParams); - break; - case 'sign': - await commands.sign.signMessage(cmdParams); - break; - case 'advanced': - advancedActions = true; - action = 'advanced'; - break; - case 'addresses': - await commands.addresses.getAddresses(cmdParams); - break; - case 'utxos': - await commands.utxos.getUtxos(cmdParams); - break; - case 'preferences': - await commands.preferences.getPreferences(cmdParams); - break; - case 'derive': - ({ action } = await commands.derive.deriveKey(cmdParams)); - break; - case 'export': - await commands.export.exportWallet(cmdParams); - break; - case 'scan': - await commands.scan.scanWallet(cmdParams); - break; - case 'register': - await commands.register.registerWallet(cmdParams); - break; - case 'clearcache': - await commands.clearcache.clearCache(cmdParams); - break; - default: - if (opts.command) { - throw new Error(`Unknown command: ${menuAction as string}`); - } - case 'exit': - opts.exit = true; - break; - } + let action: string | symbol | undefined; + advancedActions = false; + try { + switch (menuAction as Command) { + case 'token': + const result = await commands.token.setToken(cmdParams); + const { tokenObj } = result; + action = result.action; + // If context has changed... + if (cmdParams.opts?.tokenAddress?.toLowerCase() !== tokenObj?.contractAddress.toLowerCase()) { + // ...update status + cmdParams.status = await wallet.client.getStatus({ tokenAddress: tokenObj?.contractAddress }); + } + cmdParams.opts.tokenAddress = tokenObj?.contractAddress; + cmdParams.opts.token = tokenObj?.displayCode; + break; + case 'address': + await commands.address.createAddress(cmdParams); + break; + case 'balance': + const balance = await commands.balance.getBalance(cmdParams); + if (cmdParams.status) { + cmdParams.status.balance = balance; + } + break; + case 'history': + ({ action } = await commands.history.getTxHistory(cmdParams)); + break; + case 'transaction': + await commands.transaction.createTransaction(cmdParams); + break; + case 'txproposals': + ({ action } = await commands.txproposals.getTxProposals(cmdParams)); + break; + case 'status': + cmdParams.status = await commands.status.walletStatus(cmdParams); + break; + case 'sign': + await commands.sign.signMessage(cmdParams); + break; + case 'advanced': + advancedActions = true; + action = 'advanced'; + break; + case 'addresses': + await commands.addresses.getAddresses(cmdParams); + break; + case 'utxos': + await commands.utxos.getUtxos(cmdParams); + break; + case 'preferences': + await commands.preferences.getPreferences(cmdParams); + break; + case 'derive': + ({ action } = await commands.derive.deriveKey(cmdParams)); + break; + case 'export': + await commands.export.exportWallet(cmdParams); + break; + case 'scan': + await commands.scan.scanWallet(cmdParams); + break; + case 'register': + await commands.register.registerWallet(cmdParams); + break; + case 'clearcache': + await commands.clearcache.clearCache(cmdParams); + break; + default: + case 'exit': + if (opts.command) { + throw new Error(`Unknown command: ${menuAction as string}`); + } + opts.exit = true; + break; + } - if (action === 'exit' || prompt.isCancel(action)) { - opts.exit = true; - } - } catch (err) { - if (err instanceof Errors.UserCancelled) { - prompt.log.warn('Action cancelled by user.'); - } else { - prompt.log.error((opts.verbose ? err.stack : err.message) || err.message || err); + if (action === 'exit' || prompt.isCancel(action)) { + opts.exit = true; + } + } catch (err) { + if (err instanceof Errors.UserCancelled) { + prompt.log.warn('Action cancelled by user.'); + } else { + prompt.log.error((opts.verbose ? err.stack : err.message) || err.message || err); + } } - } - - if (opts.command) { - // always exit when in command mode - opts.exit = true; - } else if (!opts.exit && !action) { - action = await getAction(); - if (action === 'exit' || prompt.isCancel(action)) { + + if (opts.command) { + // always exit when in command mode opts.exit = true; - } - } + } else if (!opts.exit && !action) { + action = await getAction(); + if (action === 'exit' || prompt.isCancel(action)) { + opts.exit = true; + } + } - !opts.command && prompt.outro(); + !opts.command && prompt.outro(); - } while (!opts.exit); - - !opts.command && Utils.goodbye(); - } - }) - .catch(Utils.die); + } while (!opts.exit); + + !opts.command && Utils.goodbye(); + } + }) + .catch(Utils.die); } \ No newline at end of file diff --git a/packages/bitcore-cli/src/commands/addresses.ts b/packages/bitcore-cli/src/commands/addresses.ts index db6763bfabb..e880cea247c 100755 --- a/packages/bitcore-cli/src/commands/addresses.ts +++ b/packages/bitcore-cli/src/commands/addresses.ts @@ -1,6 +1,6 @@ import * as prompt from '@clack/prompts'; -import os from 'os'; import type { CommonArgs } from '../../types/cli'; +import os from 'os'; import { Utils } from '../utils'; export function command(args: CommonArgs) { @@ -26,7 +26,7 @@ export async function getAddresses(args: CommonArgs<{ pageSize?: number; page?: } const { pageSize } = opts; - await Utils.paginate(async (page, viewAction) => { + await Utils.paginate(async (page, _viewAction) => { const addresses = await wallet.client.getMainAddresses({ // doNotVerify: true, limit: pageSize, diff --git a/packages/bitcore-cli/src/commands/balance.ts b/packages/bitcore-cli/src/commands/balance.ts index 67904e94cf0..9f149e76c8a 100755 --- a/packages/bitcore-cli/src/commands/balance.ts +++ b/packages/bitcore-cli/src/commands/balance.ts @@ -1,7 +1,7 @@ import * as prompt from '@clack/prompts'; -import os from 'os'; import type { CommonArgs } from '../../types/cli'; import type { ITokenObj } from '../../types/wallet'; +import os from 'os'; import { Utils } from '../utils'; export function command(args: CommonArgs) { diff --git a/packages/bitcore-cli/src/commands/create/createMultiSig.ts b/packages/bitcore-cli/src/commands/create/createMultiSig.ts index 3c25c5f1e87..3044cb6e5b4 100644 --- a/packages/bitcore-cli/src/commands/create/createMultiSig.ts +++ b/packages/bitcore-cli/src/commands/create/createMultiSig.ts @@ -1,8 +1,8 @@ import * as prompt from '@clack/prompts'; +import { getAddressType, getCopayerName, getPassword } from '../../prompts'; +import type { CommonArgs } from '../../../types/cli'; import { type Network } from 'bitcore-wallet-client'; import os from 'os'; -import type { CommonArgs } from '../../../types/cli'; -import { getAddressType, getCopayerName, getPassword } from '../../prompts'; import { Utils } from '../../utils'; export async function createMultiSigWallet( @@ -32,5 +32,5 @@ export async function createMultiSigWallet( return { mnemonic: key.get(password).mnemonic, - } + }; } diff --git a/packages/bitcore-cli/src/commands/create/createSingleSig.ts b/packages/bitcore-cli/src/commands/create/createSingleSig.ts index 8eda72d9633..b14350ed636 100755 --- a/packages/bitcore-cli/src/commands/create/createSingleSig.ts +++ b/packages/bitcore-cli/src/commands/create/createSingleSig.ts @@ -1,7 +1,7 @@ import * as prompt from '@clack/prompts'; -import { type Network } from 'bitcore-wallet-client'; -import type { CommonArgs } from '../../../types/cli'; import { getAddressType, getPassword } from '../../prompts'; +import type { CommonArgs } from '../../../types/cli'; +import { type Network } from 'bitcore-wallet-client'; import { Utils } from '../../utils'; export async function createSingleSigWallet( @@ -34,5 +34,5 @@ export async function createSingleSigWallet( return { mnemonic: key.get(password).mnemonic, - } + }; } diff --git a/packages/bitcore-cli/src/commands/create/createThresholdSig.ts b/packages/bitcore-cli/src/commands/create/createThresholdSig.ts index 2f7306be49c..c16b0b2f027 100644 --- a/packages/bitcore-cli/src/commands/create/createThresholdSig.ts +++ b/packages/bitcore-cli/src/commands/create/createThresholdSig.ts @@ -1,11 +1,11 @@ import * as prompt from '@clack/prompts'; -import { Key, TssKey, type Network } from 'bitcore-wallet-client' +import { getAddressType, getCopayerName, getPassword } from '../../prompts'; +import { Key, type Network, TssKey } from 'bitcore-wallet-client'; +import type { CommonArgs } from '../../../types/cli'; import crypto from 'crypto'; import os from 'os'; import url from 'url'; -import type { CommonArgs } from '../../../types/cli'; import { UserCancelled } from '../../errors'; -import { getAddressType, getCopayerName, getPassword } from '../../prompts'; import { Utils } from '../../utils'; @@ -45,7 +45,7 @@ export async function createThresholdSigWallet( for (let i = 1; i < n; i++) { const pubkey = await prompt.text({ message: `Enter party ${i}'s public key:`, - validate: (input) => !!input ? undefined : 'Public key cannot be empty.', + validate: (input) => input ? undefined : 'Public key cannot be empty.', }); if (prompt.isCancel(pubkey)) { throw new UserCancelled(); @@ -98,7 +98,7 @@ export async function createThresholdSigWallet( }); tss.on('roundsubmitted', (round) => spinner.message(`Round ${round} submitted`)); tss.on('error', prompt.log.error); - tss.on('wallet', async (wallet) => { + tss.on('wallet', async (_wallet) => { // TODO: what to do with the wallet? // console.log('Created wallet at BWS:', wallet); }); diff --git a/packages/bitcore-cli/src/commands/create/index.ts b/packages/bitcore-cli/src/commands/create/index.ts index e9f8a549df7..acf7340ee76 100644 --- a/packages/bitcore-cli/src/commands/create/index.ts +++ b/packages/bitcore-cli/src/commands/create/index.ts @@ -1,10 +1,10 @@ -import { Constants } from 'crypto-wallet-core'; -import type { CommonArgs } from '../../../types/cli'; import { getChain, getIsMultiParty, getMofN, getMultiPartyScheme, getNetwork } from '../../prompts'; -import { Utils } from '../../utils'; +import type { CommonArgs } from '../../../types/cli'; +import { Constants } from 'crypto-wallet-core'; import { createMultiSigWallet } from './createMultiSig'; import { createSingleSigWallet } from './createSingleSig'; import { createThresholdSigWallet } from './createThresholdSig'; +import { Utils } from '../../utils'; export async function createWallet(args: CommonArgs<{ mnemonic?: string }>) { const { wallet, opts } = args; @@ -31,7 +31,7 @@ export async function createWallet(args: CommonArgs<{ mnemonic?: string }>) { if (useTss) { ({ mnemonic } = await createThresholdSigWallet({ wallet, chain, network, opts, m, n })); } else { - ({ mnemonic} = await createMultiSigWallet({ wallet, chain, network, opts, m, n })); + ({ mnemonic } = await createMultiSigWallet({ wallet, chain, network, opts, m, n })); } } diff --git a/packages/bitcore-cli/src/commands/derive.ts b/packages/bitcore-cli/src/commands/derive.ts index 32d2d23a43b..86782ee6224 100755 --- a/packages/bitcore-cli/src/commands/derive.ts +++ b/packages/bitcore-cli/src/commands/derive.ts @@ -1,9 +1,9 @@ import * as prompt from '@clack/prompts'; +import type { CommonArgs } from '../../types/cli'; import { Deriver } from 'crypto-wallet-core'; +import { getAction } from '../prompts'; import os from 'os'; -import type { CommonArgs } from '../../types/cli'; import { UserCancelled } from '../errors'; -import { getAction } from '../prompts'; export function command(args: CommonArgs) { const { program } = args; diff --git a/packages/bitcore-cli/src/commands/export.ts b/packages/bitcore-cli/src/commands/export.ts index bf7182a33ba..b207b7c72cf 100755 --- a/packages/bitcore-cli/src/commands/export.ts +++ b/packages/bitcore-cli/src/commands/export.ts @@ -1,10 +1,10 @@ import * as prompt from '@clack/prompts'; +import type { CommonArgs } from '../../types/cli'; import fs from 'fs'; +import { getPassword } from '../prompts'; import os from 'os'; import path from 'path'; -import type { CommonArgs } from '../../types/cli'; import { UserCancelled } from '../errors'; -import { getPassword } from '../prompts'; export function command(args: CommonArgs) { const { wallet, program } = args; diff --git a/packages/bitcore-cli/src/commands/import.ts b/packages/bitcore-cli/src/commands/import.ts index 5d9ce14961d..596b9c49f73 100755 --- a/packages/bitcore-cli/src/commands/import.ts +++ b/packages/bitcore-cli/src/commands/import.ts @@ -1,12 +1,12 @@ import * as prompt from '@clack/prompts'; +import type { CommonArgs } from '../../types/cli'; import fs from 'fs'; +import { getPassword } from '../prompts'; import os from 'os'; -import type { CommonArgs } from '../../types/cli'; import { UserCancelled } from '../errors'; -import { getPassword } from '../prompts'; export async function importWallet(args: CommonArgs) { - const { wallet, opts } = args; + const { wallet } = args; const replaceTilde = str => str.startsWith('~') ? str.replace('~', os.homedir()) : str; const filename = await prompt.text({ diff --git a/packages/bitcore-cli/src/commands/join/index.ts b/packages/bitcore-cli/src/commands/join/index.ts index c512349734c..c4b29656a9f 100644 --- a/packages/bitcore-cli/src/commands/join/index.ts +++ b/packages/bitcore-cli/src/commands/join/index.ts @@ -1,10 +1,10 @@ import * as prompt from '@clack/prompts'; -import { Constants } from 'crypto-wallet-core'; import type { CommonArgs } from '../../../types/cli'; +import { Constants } from 'crypto-wallet-core'; import { getChain } from '../../prompts'; -import { Utils } from '../../utils'; import { joinMultiSigWallet } from './joinMultiSig'; import { joinThresholdSigWallet } from './joinThresholdSig'; +import { Utils } from '../../utils'; export async function joinWallet(args: CommonArgs<{ mnemonic?: string; }>) { diff --git a/packages/bitcore-cli/src/commands/join/joinMultiSig.ts b/packages/bitcore-cli/src/commands/join/joinMultiSig.ts index cb861896a87..3692876f40d 100644 --- a/packages/bitcore-cli/src/commands/join/joinMultiSig.ts +++ b/packages/bitcore-cli/src/commands/join/joinMultiSig.ts @@ -1,7 +1,7 @@ import * as prompt from '@clack/prompts'; +import { getCopayerName, getPassword } from '../../prompts'; import BWC from 'bitcore-wallet-client'; import type { CommonArgs } from '../../../types/cli'; -import { getCopayerName, getPassword } from '../../prompts'; import { Utils } from '../../utils'; export async function joinMultiSigWallet(args: CommonArgs<{ mnemonic?: string; }>) { @@ -10,7 +10,7 @@ export async function joinMultiSigWallet(args: CommonArgs<{ mnemonic?: string; } const secret = (await prompt.text({ message: 'Enter the secret to join the wallet:', - validate: (input) => !!input?.trim() ? null : 'Secret cannot be empty.', + validate: (input) => input?.trim() ? null : 'Secret cannot be empty.', })).toString().trim(); const parsed = BWC.parseSecret(secret); @@ -21,7 +21,7 @@ export async function joinMultiSigWallet(args: CommonArgs<{ mnemonic?: string; } const copayerName = await getCopayerName(); const password = await getPassword('Enter a password for the wallet:', { hidden: false }); - const { key, creds } = await wallet.create({ chain, network, account: 0, n: 2, password, mnemonic, copayerName }); // n gets overwritten + const { key } = await wallet.create({ chain, network, account: 0, n: 2, password, mnemonic, copayerName }); // n gets overwritten const joinedWallet = await wallet.client.joinWallet(secret, copayerName, { chain }); await wallet.load(); // Is this needed after joining? @@ -30,5 +30,5 @@ export async function joinMultiSigWallet(args: CommonArgs<{ mnemonic?: string; } return { mnemonic: key.get(password).mnemonic, - } + }; }; \ No newline at end of file diff --git a/packages/bitcore-cli/src/commands/join/joinThresholdSig.ts b/packages/bitcore-cli/src/commands/join/joinThresholdSig.ts index 36917b627f2..10aad2aabad 100644 --- a/packages/bitcore-cli/src/commands/join/joinThresholdSig.ts +++ b/packages/bitcore-cli/src/commands/join/joinThresholdSig.ts @@ -1,14 +1,14 @@ import * as prompt from '@clack/prompts'; -import { Key, TssKey } from 'bitcore-wallet-client' +import { getCopayerName, getNetwork, getPassword } from '../../prompts'; +import { Key, TssKey } from 'bitcore-wallet-client'; +import type { CommonArgs } from '../../../types/cli'; import os from 'os'; import url from 'url'; -import type { CommonArgs } from '../../../types/cli'; import { UserCancelled } from '../../errors'; -import { getCopayerName, getNetwork, getPassword } from '../../prompts'; import { Utils } from '../../utils'; export async function joinThresholdSigWallet( -args: CommonArgs<{ mnemonic?: string; }> & { chain: string; } + args: CommonArgs<{ mnemonic?: string; }> & { chain: string; } ) { const { wallet, chain, opts } = args; const { verbose, mnemonic } = opts; @@ -84,7 +84,7 @@ args: CommonArgs<{ mnemonic?: string; }> & { chain: string; } tss.subscribe({ copayerName }); tss.on('roundsubmitted', (round) => spinner.message(`Round ${round} submitted`)); tss.on('error', prompt.log.error); - tss.on('wallet', async (wallet) => { + tss.on('wallet', async (_wallet) => { // TOOD: what to do with this? // console.log('Joined wallet at BWS:', wallet); }); @@ -111,7 +111,7 @@ args: CommonArgs<{ mnemonic?: string; }> & { chain: string; } } catch (err) { reject(err); } - }) + }); }); return { diff --git a/packages/bitcore-cli/src/commands/preferences.ts b/packages/bitcore-cli/src/commands/preferences.ts index b40eddb569a..a281c1a1497 100644 --- a/packages/bitcore-cli/src/commands/preferences.ts +++ b/packages/bitcore-cli/src/commands/preferences.ts @@ -1,6 +1,6 @@ import * as prompt from '@clack/prompts'; -import os from 'os'; import type { CommonArgs } from '../../types/cli'; +import os from 'os'; export function command(args: CommonArgs) { const { program } = args; diff --git a/packages/bitcore-cli/src/commands/sign.ts b/packages/bitcore-cli/src/commands/sign.ts index 8f628411b37..aec6dedba66 100644 --- a/packages/bitcore-cli/src/commands/sign.ts +++ b/packages/bitcore-cli/src/commands/sign.ts @@ -1,7 +1,7 @@ -import prompt from '@clack/prompts'; -import { Deriver, type Types as CWCTypes, Validation } from 'crypto-wallet-core'; -import os from 'os'; +import { type Types as CWCTypes, Deriver, Validation } from 'crypto-wallet-core'; import { type CommonArgs } from '../../types/cli'; +import os from 'os'; +import prompt from '@clack/prompts'; import { UserCancelled } from '../errors'; import { Utils } from '../utils'; diff --git a/packages/bitcore-cli/src/commands/status.ts b/packages/bitcore-cli/src/commands/status.ts index 43f24d7e321..b960b40700a 100755 --- a/packages/bitcore-cli/src/commands/status.ts +++ b/packages/bitcore-cli/src/commands/status.ts @@ -1,10 +1,10 @@ import * as prompt from '@clack/prompts'; -import { Status } from 'bitcore-wallet-client'; -import os from 'os'; import type { CommonArgs } from '../../types/cli'; +import { displayBalance } from './balance'; import type { ITokenObj } from '../../types/wallet'; +import os from 'os'; +import { Status } from 'bitcore-wallet-client'; import { Utils } from '../utils'; -import { displayBalance } from './balance'; export async function command(args: CommonArgs) { const { program } = args; diff --git a/packages/bitcore-cli/src/commands/token.ts b/packages/bitcore-cli/src/commands/token.ts index a72ced4efcc..583b080c145 100644 --- a/packages/bitcore-cli/src/commands/token.ts +++ b/packages/bitcore-cli/src/commands/token.ts @@ -6,7 +6,7 @@ import { Wallet } from '../wallet'; export async function setToken(args: CommonArgs) { - const { wallet, opts } = args; + const { wallet } = args; const currencies = await Wallet.getCurrencies(wallet.network); function findTokenObj(value) { diff --git a/packages/bitcore-cli/src/commands/transaction.ts b/packages/bitcore-cli/src/commands/transaction.ts index e4de463f5ff..772ef1ddb0f 100755 --- a/packages/bitcore-cli/src/commands/transaction.ts +++ b/packages/bitcore-cli/src/commands/transaction.ts @@ -1,14 +1,14 @@ import * as prompt from '@clack/prompts'; -import { type Txp } from 'bitcore-wallet-client'; -import { Validation } from 'crypto-wallet-core'; -import os from 'os'; import type { CommonArgs } from '../../types/cli'; import type { ITokenObj } from '../../types/wallet'; +import os from 'os'; +import { type Txp } from 'bitcore-wallet-client'; import { UserCancelled } from '../errors'; import { Utils } from '../utils'; +import { Validation } from 'crypto-wallet-core'; export function command(args: CommonArgs) { - const { wallet, program } = args; + const { program } = args; program .description('Create and send a transaction') .usage(' --command transaction [options]') diff --git a/packages/bitcore-cli/src/commands/txhistory.ts b/packages/bitcore-cli/src/commands/txhistory.ts index fc253a44b96..7401e3342fd 100755 --- a/packages/bitcore-cli/src/commands/txhistory.ts +++ b/packages/bitcore-cli/src/commands/txhistory.ts @@ -1,9 +1,9 @@ import * as prompt from '@clack/prompts'; -import fs from 'fs'; -import os from 'os'; import type { CommonArgs } from '../../types/cli'; -import type { ITokenObj } from '../../types/wallet'; +import fs from 'fs'; import { getFileName } from '../prompts'; +import type { ITokenObj } from '../../types/wallet'; +import os from 'os'; import { Utils } from '../utils'; export function command(args: CommonArgs) { @@ -90,9 +90,9 @@ export async function getTxHistory( const outputFile = opts.command ? Utils.replaceTilde(typeof opts.export === 'string' ? opts.export : defaultValue) : await getFileName({ - message: 'Enter output file path to save proposal:', - defaultValue, - }); + message: 'Enter output file path to save proposal:', + defaultValue, + }); await fs.promises.writeFile(outputFile, JSON.stringify(history, null, 2)); prompt.log.info(`Page ${page} exported to: ${outputFile}`); diff --git a/packages/bitcore-cli/src/commands/txproposals.ts b/packages/bitcore-cli/src/commands/txproposals.ts index bc95ac8e682..ae400cffdb4 100755 --- a/packages/bitcore-cli/src/commands/txproposals.ts +++ b/packages/bitcore-cli/src/commands/txproposals.ts @@ -1,9 +1,9 @@ import * as prompt from '@clack/prompts'; +import { getAction, getFileName } from '../prompts'; +import type { CommonArgs } from '../../types/cli'; import fs from 'fs'; import os from 'os'; -import type { CommonArgs } from '../../types/cli'; import { UserCancelled } from '../errors'; -import { getAction, getFileName } from '../prompts'; import { Utils } from '../utils'; export function command(args: CommonArgs) { @@ -141,9 +141,9 @@ export async function getTxProposals( action = opts.command ? opts.action || (opts.export ? 'export' : 'exit') : await getAction({ - options, - initialValue - }); + options, + initialValue + }); if (prompt.isCancel(action)) { throw new UserCancelled(); } @@ -208,9 +208,9 @@ export async function getTxProposals( const outputFile = opts.command ? Utils.replaceTilde(typeof opts.export === 'string' ? opts.export : defaultValue) : await getFileName({ - message: 'Enter output file path to save proposal:', - defaultValue, - }); + message: 'Enter output file path to save proposal:', + defaultValue, + }); fs.writeFileSync(outputFile, JSON.stringify(txp, null, 2)); prompt.log.success(`Exported to ${outputFile}`); break; @@ -225,7 +225,7 @@ export async function getTxProposals( action = 'exit'; // Exit after processing the action in command mode } // TODO: handle actions - } while (!['menu', 'exit'].includes(action)) + } while (!['menu', 'exit'].includes(action)); return { action }; }; \ No newline at end of file diff --git a/packages/bitcore-cli/src/commands/utxos.ts b/packages/bitcore-cli/src/commands/utxos.ts index d463a610a6d..51ec2ae5dcd 100755 --- a/packages/bitcore-cli/src/commands/utxos.ts +++ b/packages/bitcore-cli/src/commands/utxos.ts @@ -1,8 +1,8 @@ import * as prompt from '@clack/prompts'; -import fs from 'fs'; -import os from 'os'; import type { CommonArgs } from '../../types/cli'; +import fs from 'fs'; import { getFileName } from '../prompts'; +import os from 'os'; import { Utils } from '../utils'; export function command(args: CommonArgs) { @@ -93,9 +93,9 @@ export async function getUtxos( const filename = opts.command ? Utils.replaceTilde(typeof opts.export === 'string' ? opts.export : defaultValue) : await getFileName({ - message: 'Enter output file name:', - defaultValue: `~/${wallet.name}_utxos_${new Date().toISOString()}.json` - }); + message: 'Enter output file name:', + defaultValue: `~/${wallet.name}_utxos_${new Date().toISOString()}.json` + }); await fs.promises.writeFile(filename, JSON.stringify(utxos)); prompt.log.info(`UTXOs exported to: ${filename}`); } else if (printRaw) { diff --git a/packages/bitcore-cli/src/filestorage.ts b/packages/bitcore-cli/src/filestorage.ts index 657995eb83f..38a01994b1c 100644 --- a/packages/bitcore-cli/src/filestorage.ts +++ b/packages/bitcore-cli/src/filestorage.ts @@ -27,7 +27,7 @@ export class FileStorage { let data = await fs.promises.readFile(this.filename, 'utf8'); data = Utils.jsonParseWithBuffer(data); return data as any; // TODO provide a proper type - } catch (e) { + } catch { Utils.die('Invalid input file'); } } @@ -35,9 +35,4 @@ export class FileStorage { exists() { return fs.existsSync(this.filename); } -}; - -export interface IWalletData { - key: {}; - creds: {}; -} \ No newline at end of file +}; \ No newline at end of file diff --git a/packages/bitcore-cli/src/prompts.ts b/packages/bitcore-cli/src/prompts.ts index 8ab69438eb5..ee3f6400736 100644 --- a/packages/bitcore-cli/src/prompts.ts +++ b/packages/bitcore-cli/src/prompts.ts @@ -1,7 +1,7 @@ import * as prompt from '@clack/prompts'; -import { Network } from 'bitcore-wallet-client'; import { BitcoreLib, BitcoreLibLtc } from 'crypto-wallet-core'; import { Constants } from './constants'; +import { Network } from 'bitcore-wallet-client'; import { UserCancelled } from './errors'; import { Utils } from './utils'; @@ -135,7 +135,7 @@ export async function getMultiPartyScheme() { }; export async function getCopayerName() { - const defaultVal = process.env['BITCORE_CLI_COPAYER_NAME'] || process.env.USER + const defaultVal = process.env['BITCORE_CLI_COPAYER_NAME'] || process.env.USER; const copayerName = await prompt.text({ message: 'Your name (helps to identify you):', placeholder: `Default: ${defaultVal}`, diff --git a/packages/bitcore-cli/src/tss.ts b/packages/bitcore-cli/src/tss.ts index 6ab8d1414ec..c2fe9423f81 100644 --- a/packages/bitcore-cli/src/tss.ts +++ b/packages/bitcore-cli/src/tss.ts @@ -1,11 +1,11 @@ import * as prompt from '@clack/prompts'; -import { TssSign, Utils as BWCUtils } from 'bitcore-wallet-client'; -import { ethers, type Types as CWCTypes } from 'crypto-wallet-core'; -import url from 'url'; +import { Utils as BWCUtils, TssSign } from 'bitcore-wallet-client'; +import { type Types as CWCTypes, ethers } from 'crypto-wallet-core'; import { type TssKeyType, type WalletData } from '../types/wallet'; +import url from 'url'; import { UserCancelled } from './errors'; /** diff --git a/packages/bitcore-cli/src/utils.ts b/packages/bitcore-cli/src/utils.ts index 347da0c3e69..d18e29b5a3b 100644 --- a/packages/bitcore-cli/src/utils.ts +++ b/packages/bitcore-cli/src/utils.ts @@ -1,11 +1,11 @@ import * as prompt from '@clack/prompts'; import { Utils as BWCUtils } from 'bitcore-wallet-client'; +import type { Color } from '../types/constants'; +import { Constants } from './constants'; import { edit } from 'external-editor'; +import type { ITokenObj } from '../types/wallet'; import os from 'os'; import path from 'path'; -import type { Color } from '../types/constants'; -import type { ITokenObj } from '../types/wallet'; -import { Constants } from './constants'; import { UserCancelled } from './errors'; let _verbose = false; diff --git a/packages/bitcore-cli/src/wallet.ts b/packages/bitcore-cli/src/wallet.ts index 72e6fb6de19..7149d71c25a 100644 --- a/packages/bitcore-cli/src/wallet.ts +++ b/packages/bitcore-cli/src/wallet.ts @@ -1,39 +1,38 @@ import * as prompt from '@clack/prompts'; import { API, + Utils as BWCUtils, Credentials, Encryption, EncryptionTypes, Key, - TssKey, - Txp, type Network, - Utils as BWCUtils + TssKey, + Txp } from 'bitcore-wallet-client'; -import { - ethers, - Message, - type Types as CWCTypes, - Utils as CWCUtils, - Web3 -} from 'crypto-wallet-core'; -import fs from 'fs'; -import path from 'path'; -import url from 'url'; import type { ClientType, ITokenObj, IWallet, KeyType, TssKeyType, - TssSigType, WalletData } from '../types/wallet'; +import { + type Types as CWCTypes, + Utils as CWCUtils, + ethers, + Message, + Web3 +} from 'crypto-wallet-core'; import { Constants } from './constants'; import { ERC20Abi } from './erc20Abi'; import { FileStorage } from './filestorage'; +import fs from 'fs'; import { getPassword } from './prompts'; +import path from 'path'; import { sign as tssSign } from './tss'; +import url from 'url'; import { Utils } from './utils'; const Client = API; @@ -158,7 +157,7 @@ export class Wallet implements IWallet { addressType?: string; copayerName: string; }) { - const { key, chain, network, addressType, password, copayerName } = args; + const { key, chain, network, addressType, password } = args; if (!this.client) { await this.getClient({ mustExist: true }); } @@ -181,7 +180,7 @@ export class Wallet implements IWallet { await this.getClient({ mustExist: true }); } const { chain, network, m, n, addressType } = this.client.credentials; - const { wallet, secret } = await this.client.createWallet(this.name, args.copayerName, m, n, { chain, network: network as Network, ...Utils.getSegwitInfo(addressType) }); + const { secret } = await this.client.createWallet(this.name, args.copayerName, m, n, { chain, network: network as Network, ...Utils.getSegwitInfo(addressType) }); return secret as string | undefined; } @@ -197,7 +196,7 @@ export class Wallet implements IWallet { try { walletData = JSON.parse(Encryption.decryptWithPassword(walletData as EncryptionTypes.IEncrypted, password).toString()); this.isFullyEncrypted = true; - } catch (e) { + } catch { Utils.die('Could not open wallet. Wrong password.'); } } @@ -215,11 +214,11 @@ export class Wallet implements IWallet { let key: KeyType; try { - let imported = Client.upgradeCredentialsV1(walletData); + const imported = Client.upgradeCredentialsV1(walletData); this.client.fromString(JSON.stringify(imported.credentials)); key = instantiateKey(); - } catch (e) { + } catch { try { this.client.fromObj(walletData.creds); key = instantiateKey(); @@ -236,12 +235,12 @@ export class Wallet implements IWallet { if (doNotComplete) return key; - this.client.on('walletCompleted', (wallet) => { + this.client.on('walletCompleted', (_wallet) => { this.save().then(() => { _verbose && prompt.log.info('Your wallet has just been completed.'); }); }); - const isComplete = await this.client.openWallet(); + await this.client.openWallet(); return key; }; @@ -455,6 +454,7 @@ export class Wallet implements IWallet { const isUtxo = BWCUtils.isUtxoChain(txp.chain); const isEvm = BWCUtils.isEvmChain(txp.chain); + // eslint-disable-next-line @typescript-eslint/no-unused-vars -- TODO add support for SVM chains const isSvm = BWCUtils.isSvmChain(txp.chain); if (!isEvm) { diff --git a/packages/bitcore-cli/types/cli.d.ts b/packages/bitcore-cli/types/cli.d.ts index f6409989554..88be9d7a65c 100644 --- a/packages/bitcore-cli/types/cli.d.ts +++ b/packages/bitcore-cli/types/cli.d.ts @@ -1,6 +1,6 @@ +import type { IWallet } from './wallet'; import { program } from 'commander'; import { type Status } from 'bitcore-wallet-client'; -import type { IWallet } from './wallet'; export interface ICliOptions { dir: string; @@ -20,7 +20,7 @@ export interface ICliOptions { export type Program = typeof program; -export interface CommonArgs { +export interface CommonArgs { wallet: IWallet; program?: Program; opts?: ICliOptions & MoreOpts; diff --git a/packages/bitcore-cli/types/wallet.d.ts b/packages/bitcore-cli/types/wallet.d.ts index 36dc605f85c..09a85a7432b 100644 --- a/packages/bitcore-cli/types/wallet.d.ts +++ b/packages/bitcore-cli/types/wallet.d.ts @@ -2,10 +2,10 @@ import { API, Credentials, Key, + type Network, TssKey, TssSign, - Txp, - type Network + Txp } from 'bitcore-wallet-client'; import { type Types as CWCTypes } from 'crypto-wallet-core';