Skip to content

Commit

Permalink
Use inquirer for confirm prompt
Browse files Browse the repository at this point in the history
This also creates an abstraction for the confirm prompt so it is easier to use
confirm logic in commands
  • Loading branch information
mat-if committed Jun 21, 2024
1 parent 4e63986 commit e2d7472
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 151 deletions.
19 changes: 7 additions & 12 deletions ironfish-cli/src/commands/chain/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import fsAsync from 'fs/promises'
import { IronfishCommand } from '../../command'
import { LocalFlags } from '../../flags'
import { DownloadedSnapshot, getDefaultManifestUrl, SnapshotDownloader } from '../../snapshot'
import { ProgressBar, ProgressBarPresets } from '../../ui'
import { confirmOrQuit, ProgressBar, ProgressBarPresets } from '../../ui'

export default class Download extends IronfishCommand {
static hidden = false
Expand Down Expand Up @@ -75,17 +75,12 @@ export default class Download extends IronfishCommand {
const fileSize = FileUtils.formatFileSize(manifest.file_size)
const spaceRequired = FileUtils.formatFileSize(manifest.file_size * 2)

if (!flags.confirm) {
const confirm = await ux.confirm(
`Download ${fileSize} snapshot to update from block ${headSequence} to ${manifest.block_sequence}? ` +
`\nAt least ${spaceRequired} of free disk space is required to download and unzip the snapshot file.` +
`\nAre you sure? (Y)es / (N)o`,
)

if (!confirm) {
this.exit(0)
}
}
await confirmOrQuit(
`Download ${fileSize} snapshot to update from block ${headSequence} to ${manifest.block_sequence}? ` +
`\nAt least ${spaceRequired} of free disk space is required to download and unzip the snapshot file.` +
`\nAre you sure?`,
flags.confirm,
)

const snapshotUrl = await Downloader.snapshotURL()
const snapshotPath = await Downloader.snapshotPath()
Expand Down
6 changes: 2 additions & 4 deletions ironfish-cli/src/commands/chain/genesisadd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Flags, ux } from '@oclif/core'
import fs from 'fs/promises'
import { IronfishCommand } from '../../command'
import { LocalFlags } from '../../flags'
import { confirmOrQuit } from '../../ui'

export default class GenesisAddCommand extends IronfishCommand {
static hidden = true
Expand Down Expand Up @@ -116,10 +117,7 @@ export default class GenesisAddCommand extends IronfishCommand {
if (flags.dry) {
this.exit(0)
} else {
const result = await ux.confirm('\nCreate new genesis block? (y)es / (n)o')
if (!result) {
this.exit(0)
}
await confirmOrQuit('Create new genesis block?')
}

this.log('\nBuilding a genesis block...')
Expand Down
6 changes: 2 additions & 4 deletions ironfish-cli/src/commands/chain/genesisblock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { Flags, ux } from '@oclif/core'
import fs from 'fs/promises'
import { IronfishCommand } from '../../command'
import { LocalFlags } from '../../flags'
import { confirmOrQuit } from '../../ui'

export default class GenesisBlockCommand extends IronfishCommand {
static description = 'Create and serialize a genesis block'
Expand Down Expand Up @@ -171,10 +172,7 @@ export default class GenesisBlockCommand extends IronfishCommand {
if (flags.dry) {
this.exit(0)
} else {
const result = await ux.confirm('\nCreate the genesis block? (y)es / (n)o')
if (!result) {
this.exit(0)
}
await confirmOrQuit('Create the genesis block?')
}

this.log('\nBuilding a genesis block...')
Expand Down
21 changes: 8 additions & 13 deletions ironfish-cli/src/commands/chain/repair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Assert, BlockHeader, FullNode, IDatabaseTransaction, TimeUtils } from '
import { Flags, ux } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { LocalFlags } from '../../flags'
import { ProgressBar, ProgressBarPresets } from '../../ui'
import { confirmOrQuit, ProgressBar, ProgressBarPresets } from '../../ui'

const TREE_BATCH = 1000
const TREE_START = 1
Expand Down Expand Up @@ -53,18 +53,13 @@ export default class RepairChain extends IronfishCommand {
const total = Number(node.chain.head.sequence)
const estimate = TimeUtils.renderEstimate(0, total, SPEED_ESTIMATE)

const confirmed =
flags.confirm ||
(await ux.confirm(
`\n⚠️ If you start repairing your database, you MUST finish the\n` +
`process or your database will be in a corrupt state. Repairing\n` +
`may take ${estimate} or longer.\n\n` +
`Are you SURE? (y)es / (n)o`,
))

if (!confirmed) {
return
}
await confirmOrQuit(
`⚠️ If you start repairing your database, you MUST finish the` +
`\nprocess or your database will be in a corrupt state. Repairing` +
`\nmay take ${estimate} or longer.` +
`\n\nAre you sure?`,
flags.confirm,
)

await this.repairChain(node, progress)
await this.repairTrees(node, progress, flags.force)
Expand Down
12 changes: 4 additions & 8 deletions ironfish-cli/src/commands/reset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
VerboseFlag,
VerboseFlagKey,
} from '../flags'
import { confirmOperation } from '../utils'
import { confirmOrQuit } from '../ui'

export default class Reset extends IronfishCommand {
static description = 'Reset the node to its initial state'
Expand Down Expand Up @@ -54,18 +54,14 @@ export default class Reset extends IronfishCommand {
}

const message =
'\nYou are about to destroy your local copy of the blockchain. The following directories and files will be deleted:\n' +
'You are about to destroy your local copy of the blockchain. The following directories and files will be deleted:\n' +
`\nBlockchain: ${chainDatabasePath}` +
`\nHosts: ${hostFilePath}` +
'\nYour wallet, accounts, and keys will NOT be deleted.' +
networkIdMessage +
`\n\nAre you sure? (Y)es / (N)o`
`\n\nAre you sure?`

await confirmOperation({
confirm: flags.confirm,
confirmMessage: message,
cancelledMessage: 'Reset aborted.',
})
await confirmOrQuit(message, flags.confirm)

ux.action.start('Deleting databases...')

Expand Down
9 changes: 4 additions & 5 deletions ironfish-cli/src/commands/wallet/burn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { Flags, ux } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { IronFlag, RemoteFlags, ValueFlag } from '../../flags'
import { confirmOperation } from '../../utils'
import { confirmOrQuit } from '../../ui'
import { selectAsset } from '../../utils/asset'
import { promptCurrency } from '../../utils/currency'
import { promptExpiration } from '../../utils/expiration'
Expand Down Expand Up @@ -288,10 +288,9 @@ export class Burn extends IronfishCommand {
const renderedAmount = CurrencyUtils.render(amount, true, asset.id, asset.verification)
const renderedFee = CurrencyUtils.render(fee, true)

await confirmOperation({
await confirmOrQuit(
`You are about to burn ${renderedAmount} plus a transaction fee of ${renderedFee} with the account ${account}\nDo you confirm?`,
confirm,
confirmMessage: `You are about to burn: ${renderedAmount} plus a transaction fee of ${renderedFee} with the account ${account}\nDo you confirm(Y/N)?`,
cancelledMessage: 'Burn aborted.',
})
)
}
}
5 changes: 3 additions & 2 deletions ironfish-cli/src/commands/wallet/chainport/send.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import inquirer from 'inquirer'
import * as validator from 'web3-validator'
import { IronfishCommand } from '../../../command'
import { HexFlag, IronFlag, RemoteFlags, ValueFlag } from '../../../flags'
import { confirmOperation, selectAsset } from '../../../utils'
import { confirmOrQuit } from '../../../ui'
import { selectAsset } from '../../../utils'
import {
ChainportBridgeTransaction,
ChainportNetwork,
Expand Down Expand Up @@ -113,7 +114,7 @@ export class BridgeCommand extends IronfishCommand {
assetData,
)

await confirmOperation({})
await confirmOrQuit()

const postTransaction = await client.wallet.postTransaction({
transaction: RawTransactionSerde.serialize(rawTransaction).toString('hex'),
Expand Down
14 changes: 5 additions & 9 deletions ironfish-cli/src/commands/wallet/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { AccountFormat, ErrorUtils, LanguageUtils } from '@ironfish/sdk'
import { Args, Flags, ux } from '@oclif/core'
import { Args, Flags } from '@oclif/core'
import fs from 'fs'
import jsonColorizer from 'json-colorizer'
import path from 'path'
import { IronfishCommand } from '../../command'
import { ColorFlag, ColorFlagKey, EnumLanguageKeyFlag, RemoteFlags } from '../../flags'
import { confirmOrQuit } from '../../ui'

export class ExportCommand extends IronfishCommand {
static description = `Export an account`
Expand Down Expand Up @@ -88,15 +89,10 @@ export class ExportCommand extends IronfishCommand {
}

if (fs.existsSync(resolved)) {
this.log(`There is already an account backup at ${exportPath}`)

const confirmed = await ux.confirm(
`\nOverwrite the account backup with new file?\nAre you sure? (Y)es / (N)o`,
await confirmOrQuit(
`There is already an account backup at ${exportPath}` +
`\n\nOverwrite the account backup with new file?`,
)

if (!confirmed) {
this.exit(1)
}
}

await fs.promises.writeFile(resolved, output)
Expand Down
12 changes: 4 additions & 8 deletions ironfish-cli/src/commands/wallet/mint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
import { Flags, ux } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { IronFlag, RemoteFlags, ValueFlag } from '../../flags'
import { confirmOperation } from '../../utils'
import { confirmOrQuit, confirmPrompt } from '../../ui'
import { selectAsset } from '../../utils/asset'
import { promptCurrency } from '../../utils/currency'
import { promptExpiration } from '../../utils/expiration'
Expand Down Expand Up @@ -147,7 +147,7 @@ export class Mint extends IronfishCommand {
// name is provided
let isMintingNewAsset = Boolean(name || metadata)
if (!assetId && !metadata && !name) {
isMintingNewAsset = await ux.confirm('Do you want to create a new asset (Y/N)?')
isMintingNewAsset = await confirmPrompt('Do you want to create a new asset?')
}

if (isMintingNewAsset) {
Expand Down Expand Up @@ -388,12 +388,8 @@ export class Mint extends IronfishCommand {
)
}

confirmMessage.push('Do you confirm (Y/N)?')
confirmMessage.push('Do you confirm?')

await confirmOperation({
confirmMessage: confirmMessage.join('\n'),
cancelledMessage: 'Mint aborted.',
confirm,
})
await confirmOrQuit(confirmMessage.join('\n'), confirm)
}
}
10 changes: 3 additions & 7 deletions ironfish-cli/src/commands/wallet/multisig/commitment/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { UnsignedTransaction } from '@ironfish/sdk'
import { Flags, ux } from '@oclif/core'
import { Flags } from '@oclif/core'
import { IronfishCommand } from '../../../../command'
import { RemoteFlags } from '../../../../flags'
import { confirmOrQuit } from '../../../../ui'
import { longPrompt } from '../../../../utils/input'
import { MultisigTransactionJson } from '../../../../utils/multisig'
import { renderUnsignedTransactionDetails } from '../../../../utils/transaction'
Expand Down Expand Up @@ -80,12 +81,7 @@ export class CreateSigningCommitmentCommand extends IronfishCommand {
this.logger,
)

if (!flags.confirm) {
const confirmed = await ux.confirm('Confirm signing commitment creation (Y/N)')
if (!confirmed) {
this.error('Creating signing commitment aborted')
}
}
await confirmOrQuit('Confirm signing commitment creation', flags.confirm)

const response = await client.wallet.multisig.createSigningCommitment({
account: flags.account,
Expand Down
8 changes: 3 additions & 5 deletions ironfish-cli/src/commands/wallet/multisig/signature/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { multisig } from '@ironfish/rust-nodejs'
import { UnsignedTransaction } from '@ironfish/sdk'
import { Flags, ux } from '@oclif/core'
import { Flags } from '@oclif/core'
import { IronfishCommand } from '../../../../command'
import { RemoteFlags } from '../../../../flags'
import { confirmOrQuit } from '../../../../ui'
import { longPrompt } from '../../../../utils/input'
import { MultisigTransactionJson } from '../../../../utils/multisig'
import { renderUnsignedTransactionDetails } from '../../../../utils/transaction'
Expand Down Expand Up @@ -62,10 +63,7 @@ export class CreateSignatureShareCommand extends IronfishCommand {
)

if (!flags.confirm) {
const confirmed = await ux.confirm('Confirm new signature share creation (Y/N)')
if (!confirmed) {
this.error('Creating signature share aborted')
}
await confirmOrQuit('Confirm new signature share creation')
}

const signatureShareResponse = await client.wallet.multisig.createSignatureShare({
Expand Down
8 changes: 3 additions & 5 deletions ironfish-cli/src/commands/wallet/notes/combine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import { Flags, ux } from '@oclif/core'
import inquirer from 'inquirer'
import { IronfishCommand } from '../../../command'
import { HexFlag, IronFlag, RemoteFlags } from '../../../flags'
import { confirmOperation, getAssetsByIDs, selectAsset } from '../../../utils'
import { confirmOrQuit } from '../../../ui'
import { getAssetsByIDs, selectAsset } from '../../../utils'
import { getExplorer } from '../../../utils/explorer'
import { selectFee } from '../../../utils/fees'
import { fetchNotes } from '../../../utils/note'
Expand Down Expand Up @@ -358,10 +359,7 @@ export class CombineNotesCommand extends IronfishCommand {
})}`,
)

await confirmOperation({
confirm: flags.confirm,
cancelledMessage: 'Combine aborted.',
})
await confirmOrQuit('', flags.confirm)

transactionTimer.start()

Expand Down
41 changes: 15 additions & 26 deletions ironfish-cli/src/commands/wallet/post.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import { RawTransaction, RawTransactionSerde, RpcClient, Transaction } from '@ironfish/sdk'
import { RawTransactionSerde, RpcClient, Transaction } from '@ironfish/sdk'
import { Args, Flags, ux } from '@oclif/core'
import { IronfishCommand } from '../../command'
import { RemoteFlags } from '../../flags'
import { confirmOrQuit } from '../../ui'
import { longPrompt } from '../../utils/input'
import { renderRawTransactionDetails } from '../../utils/transaction'

Expand Down Expand Up @@ -58,14 +59,22 @@ export class PostCommand extends IronfishCommand {

const client = await this.sdk.connectRpc()

if (!flags.confirm) {
const confirm = await this.confirm(client, raw)
const senderAddress = raw.sender()
if (!senderAddress) {
this.error('Unable to determine sender for raw transaction')
}

if (!confirm) {
this.exit(0)
}
const account = await this.getAccountName(client, senderAddress)
if (!account) {
this.error(
`Wallet does not contain sender account with public address ${senderAddress}. Unable to post transaction.`,
)
}

await renderRawTransactionDetails(client, raw, account, this.logger)

await confirmOrQuit('Do you want to post this?', flags.confirm)

ux.action.start(`Posting the transaction`)

const response = await client.wallet.postTransaction({
Expand Down Expand Up @@ -95,26 +104,6 @@ export class PostCommand extends IronfishCommand {
}
}

async confirm(client: Pick<RpcClient, 'wallet'>, raw: RawTransaction): Promise<boolean> {
const senderAddress = raw.sender()

if (!senderAddress) {
this.error('Unable to determine sender for raw transaction')
}

const account = await this.getAccountName(client, senderAddress)

if (!account) {
this.error(
`Wallet does not contain sender account with public address ${senderAddress}. Unable to post transaction.`,
)
}

await renderRawTransactionDetails(client, raw, account, this.logger)

return ux.confirm('Do you want to post this (Y/N)?')
}

async getAccountName(
client: Pick<RpcClient, 'wallet'>,
publicAddress: string,
Expand Down
Loading

0 comments on commit e2d7472

Please sign in to comment.