Skip to content

Commit

Permalink
updates collectStrings to prompt for new item on duplicate (#5538)
Browse files Browse the repository at this point in the history
if a user inputs a duplicate string into a prompt from collectStrings, they will
be informed of the duplicate and the prompt will be repeated

adds option to allow duplicates

updates usage of collectStrings in 'wallet:multisig:dkg:create' and
'wallet:multisig:sign' not to error on duplicates
  • Loading branch information
hughy authored Oct 11, 2024
1 parent 8da2886 commit f07f881
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
6 changes: 3 additions & 3 deletions ironfish-cli/src/commands/wallet/multisig/dkg/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ export class DkgCreateCommand extends IronfishCommand {
)
identities = await ui.collectStrings('Participant Identity', totalParticipants - 1, {
additionalStrings: [currentIdentity],
errorOnDuplicate: true,
logger: this.logger,
})
} else {
identities = await sessionManager.getIdentities(currentIdentity, totalParticipants)
Expand Down Expand Up @@ -504,7 +504,7 @@ export class DkgCreateCommand extends IronfishCommand {
totalParticipants - 1,
{
additionalStrings: [round1Result.publicPackage],
errorOnDuplicate: true,
logger: this.logger,
},
)
} else {
Expand Down Expand Up @@ -669,7 +669,7 @@ export class DkgCreateCommand extends IronfishCommand {
totalParticipants - 1,
{
additionalStrings: [round2Result.publicPackage],
errorOnDuplicate: true,
logger: this.logger,
},
)
} else {
Expand Down
6 changes: 3 additions & 3 deletions ironfish-cli/src/commands/wallet/multisig/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ export class SignMultisigTransactionCommand extends IronfishCommand {

signatureShares = await ui.collectStrings('Signature Share', totalParticipants - 1, {
additionalStrings: [signatureShare],
errorOnDuplicate: true,
logger: this.logger,
})
} else {
signatureShares = await sessionManager.getSignatureShares(
Expand Down Expand Up @@ -395,7 +395,7 @@ export class SignMultisigTransactionCommand extends IronfishCommand {

commitments = await ui.collectStrings('Commitment', identities.length - 1, {
additionalStrings: [commitment],
errorOnDuplicate: true,
logger: this.logger,
})
} else {
commitments = await sessionManager.getSigningCommitments(commitment, totalParticipants)
Expand Down Expand Up @@ -430,7 +430,7 @@ export class SignMultisigTransactionCommand extends IronfishCommand {

identities = await ui.collectStrings('Participant Identity', totalParticipants - 1, {
additionalStrings: [participant.identity],
errorOnDuplicate: true,
logger: this.logger,
})
} else {
identities = await sessionManager.getIdentities(participant.identity, totalParticipants)
Expand Down
40 changes: 23 additions & 17 deletions ironfish-cli/src/ui/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,36 @@ export async function collectStrings(
itemName: string,
itemAmount: number,
options?: {
additionalStrings: string[]
errorOnDuplicate: boolean
additionalStrings?: string[]
allowDuplicate?: boolean
errorOnDuplicate?: boolean
logger?: Logger
},
): Promise<string[]> {
const array = []
const strings = new Set(options?.additionalStrings || [])
const duplicates = []

for (let i = 0; i < itemAmount; i++) {
const input = await longPrompt(`${itemName} #${i + 1}`, { required: true })
array.push(input)
}

const additionalStrings = options?.additionalStrings || []

const strings = [...array, ...additionalStrings]

if (options?.errorOnDuplicate) {
const withoutDuplicates = [...new Set(strings)]

if (withoutDuplicates.length !== strings.length) {
throw new Error(`Duplicate ${itemName} found in the list`)
let item
while (!item) {
item = await longPrompt(`${itemName} #${i + 1}`, { required: true })

if (strings.has(item)) {
if (options?.allowDuplicate) {
duplicates.push(item)
continue
} else if (options?.errorOnDuplicate) {
throw new Error(`Duplicate ${itemName} found in the list`)
} else {
options?.logger?.log(`Duplicate ${itemName}`)
item = undefined
}
}
}
strings.add(item)
}

return strings
return [...strings, ...duplicates]
}

async function _inputPrompt(message: string, options?: { password: boolean }): Promise<string> {
Expand Down

0 comments on commit f07f881

Please sign in to comment.