Skip to content

Commit

Permalink
inputNumberPrompt and usage (#5443)
Browse files Browse the repository at this point in the history
* Adds prompt specific to numbers

The problem with parse int is that even if you pass it something like "1234123asdf" it will return a success and return 1234123.
This is not what we want. We want to make sure that the user has entered a valid number.

We also want to retry the prompt if the user enters an invalid number or a decimal when an integer is expected.

* adding min signers check back:

lint fix
  • Loading branch information
patnir authored Sep 27, 2024
1 parent 0ce965d commit 8e9bde7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
25 changes: 16 additions & 9 deletions ironfish-cli/src/commands/wallet/multisig/dkg/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,13 @@ export class DkgCreateCommand extends IronfishCommand {
}> {
this.log('\nCollecting Participant Info and Performing Round 1...')

let input = await ui.inputPrompt('Enter the total number of participants', true)
const totalParticipants = parseInt(input)
if (isNaN(totalParticipants) || totalParticipants < 2) {
const totalParticipants = await ui.inputNumberPrompt(
this.logger,
'Enter the total number of participants',
{ required: true, integer: true },
)

if (totalParticipants < 2) {
throw new Error('Total number of participants must be at least 2')
}

Expand All @@ -304,13 +308,15 @@ export class DkgCreateCommand extends IronfishCommand {
errorOnDuplicate: true,
})

input = await ui.inputPrompt('Enter the number of minimum signers', true)
const minSigners = parseInt(input)
if (isNaN(minSigners) || minSigners < 2) {
throw new Error('Minimum number of signers must be at least 2')
} else if (minSigners > totalParticipants) {
const minSigners = await ui.inputNumberPrompt(
this.logger,
'Enter the number of minimum signers',
{ required: true, integer: true },
)

if (minSigners < 2 || minSigners > totalParticipants) {
throw new Error(
'Minimum number of signers cannot be more than total number of participants',
'Minimum number of signers must be between 2 and the total number of participants',
)
}

Expand All @@ -322,6 +328,7 @@ export class DkgCreateCommand extends IronfishCommand {
identities,
minSigners,
)

return {
...result,
totalParticipants,
Expand Down
42 changes: 42 additions & 0 deletions ironfish-cli/src/ui/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* 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 { Logger } from '@ironfish/sdk'
import { ux } from '@oclif/core'
import inquirer from 'inquirer'
import { longPrompt } from './longPrompt'
Expand Down Expand Up @@ -45,6 +46,47 @@ async function _inputPrompt(message: string, options?: { password: boolean }): P
return result.prompt.trim()
}

export async function inputNumberPrompt(
logger: Logger,
message: string,
options: {
required?: boolean
integer?: boolean
},
): Promise<number> {
const validateNumber = (input: string): number => {
const num = Number(input)

if (isNaN(num)) {
throw new Error('Input must be a number')
}

if (options.integer && num % 1 !== 0) {
throw new Error('Input must be an integer')
}

return num
}

if (options.required) {
// eslint-disable-next-line no-constant-condition
while (true) {
try {
const userInput = await _inputPrompt(message)
return validateNumber(userInput)
} catch (e) {
if (e instanceof Error) {
logger.error(e.message)
} else {
logger.error('An error occurred. Please try again.')
}
}
}
}

return validateNumber(await _inputPrompt(message))
}

export async function inputPrompt(
message: string,
required: boolean = false,
Expand Down

0 comments on commit 8e9bde7

Please sign in to comment.