Skip to content

Commit 8e9bde7

Browse files
authored
inputNumberPrompt and usage (#5443)
* 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
1 parent 0ce965d commit 8e9bde7

File tree

2 files changed

+58
-9
lines changed

2 files changed

+58
-9
lines changed

ironfish-cli/src/commands/wallet/multisig/dkg/create.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,13 @@ export class DkgCreateCommand extends IronfishCommand {
284284
}> {
285285
this.log('\nCollecting Participant Info and Performing Round 1...')
286286

287-
let input = await ui.inputPrompt('Enter the total number of participants', true)
288-
const totalParticipants = parseInt(input)
289-
if (isNaN(totalParticipants) || totalParticipants < 2) {
287+
const totalParticipants = await ui.inputNumberPrompt(
288+
this.logger,
289+
'Enter the total number of participants',
290+
{ required: true, integer: true },
291+
)
292+
293+
if (totalParticipants < 2) {
290294
throw new Error('Total number of participants must be at least 2')
291295
}
292296

@@ -304,13 +308,15 @@ export class DkgCreateCommand extends IronfishCommand {
304308
errorOnDuplicate: true,
305309
})
306310

307-
input = await ui.inputPrompt('Enter the number of minimum signers', true)
308-
const minSigners = parseInt(input)
309-
if (isNaN(minSigners) || minSigners < 2) {
310-
throw new Error('Minimum number of signers must be at least 2')
311-
} else if (minSigners > totalParticipants) {
311+
const minSigners = await ui.inputNumberPrompt(
312+
this.logger,
313+
'Enter the number of minimum signers',
314+
{ required: true, integer: true },
315+
)
316+
317+
if (minSigners < 2 || minSigners > totalParticipants) {
312318
throw new Error(
313-
'Minimum number of signers cannot be more than total number of participants',
319+
'Minimum number of signers must be between 2 and the total number of participants',
314320
)
315321
}
316322

@@ -322,6 +328,7 @@ export class DkgCreateCommand extends IronfishCommand {
322328
identities,
323329
minSigners,
324330
)
331+
325332
return {
326333
...result,
327334
totalParticipants,

ironfish-cli/src/ui/prompt.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* License, v. 2.0. If a copy of the MPL was not distributed with this
33
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
44

5+
import { Logger } from '@ironfish/sdk'
56
import { ux } from '@oclif/core'
67
import inquirer from 'inquirer'
78
import { longPrompt } from './longPrompt'
@@ -45,6 +46,47 @@ async function _inputPrompt(message: string, options?: { password: boolean }): P
4546
return result.prompt.trim()
4647
}
4748

49+
export async function inputNumberPrompt(
50+
logger: Logger,
51+
message: string,
52+
options: {
53+
required?: boolean
54+
integer?: boolean
55+
},
56+
): Promise<number> {
57+
const validateNumber = (input: string): number => {
58+
const num = Number(input)
59+
60+
if (isNaN(num)) {
61+
throw new Error('Input must be a number')
62+
}
63+
64+
if (options.integer && num % 1 !== 0) {
65+
throw new Error('Input must be an integer')
66+
}
67+
68+
return num
69+
}
70+
71+
if (options.required) {
72+
// eslint-disable-next-line no-constant-condition
73+
while (true) {
74+
try {
75+
const userInput = await _inputPrompt(message)
76+
return validateNumber(userInput)
77+
} catch (e) {
78+
if (e instanceof Error) {
79+
logger.error(e.message)
80+
} else {
81+
logger.error('An error occurred. Please try again.')
82+
}
83+
}
84+
}
85+
}
86+
87+
return validateNumber(await _inputPrompt(message))
88+
}
89+
4890
export async function inputPrompt(
4991
message: string,
5092
required: boolean = false,

0 commit comments

Comments
 (0)