Skip to content

Commit f07f881

Browse files
authored
updates collectStrings to prompt for new item on duplicate (#5538)
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
1 parent 8da2886 commit f07f881

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ export class DkgCreateCommand extends IronfishCommand {
420420
)
421421
identities = await ui.collectStrings('Participant Identity', totalParticipants - 1, {
422422
additionalStrings: [currentIdentity],
423-
errorOnDuplicate: true,
423+
logger: this.logger,
424424
})
425425
} else {
426426
identities = await sessionManager.getIdentities(currentIdentity, totalParticipants)
@@ -504,7 +504,7 @@ export class DkgCreateCommand extends IronfishCommand {
504504
totalParticipants - 1,
505505
{
506506
additionalStrings: [round1Result.publicPackage],
507-
errorOnDuplicate: true,
507+
logger: this.logger,
508508
},
509509
)
510510
} else {
@@ -669,7 +669,7 @@ export class DkgCreateCommand extends IronfishCommand {
669669
totalParticipants - 1,
670670
{
671671
additionalStrings: [round2Result.publicPackage],
672-
errorOnDuplicate: true,
672+
logger: this.logger,
673673
},
674674
)
675675
} else {

ironfish-cli/src/commands/wallet/multisig/sign.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export class SignMultisigTransactionCommand extends IronfishCommand {
272272

273273
signatureShares = await ui.collectStrings('Signature Share', totalParticipants - 1, {
274274
additionalStrings: [signatureShare],
275-
errorOnDuplicate: true,
275+
logger: this.logger,
276276
})
277277
} else {
278278
signatureShares = await sessionManager.getSignatureShares(
@@ -395,7 +395,7 @@ export class SignMultisigTransactionCommand extends IronfishCommand {
395395

396396
commitments = await ui.collectStrings('Commitment', identities.length - 1, {
397397
additionalStrings: [commitment],
398-
errorOnDuplicate: true,
398+
logger: this.logger,
399399
})
400400
} else {
401401
commitments = await sessionManager.getSigningCommitments(commitment, totalParticipants)
@@ -430,7 +430,7 @@ export class SignMultisigTransactionCommand extends IronfishCommand {
430430

431431
identities = await ui.collectStrings('Participant Identity', totalParticipants - 1, {
432432
additionalStrings: [participant.identity],
433-
errorOnDuplicate: true,
433+
logger: this.logger,
434434
})
435435
} else {
436436
identities = await sessionManager.getIdentities(participant.identity, totalParticipants)

ironfish-cli/src/ui/prompt.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,36 @@ export async function collectStrings(
1111
itemName: string,
1212
itemAmount: number,
1313
options?: {
14-
additionalStrings: string[]
15-
errorOnDuplicate: boolean
14+
additionalStrings?: string[]
15+
allowDuplicate?: boolean
16+
errorOnDuplicate?: boolean
17+
logger?: Logger
1618
},
1719
): Promise<string[]> {
18-
const array = []
20+
const strings = new Set(options?.additionalStrings || [])
21+
const duplicates = []
1922

2023
for (let i = 0; i < itemAmount; i++) {
21-
const input = await longPrompt(`${itemName} #${i + 1}`, { required: true })
22-
array.push(input)
23-
}
24-
25-
const additionalStrings = options?.additionalStrings || []
26-
27-
const strings = [...array, ...additionalStrings]
28-
29-
if (options?.errorOnDuplicate) {
30-
const withoutDuplicates = [...new Set(strings)]
31-
32-
if (withoutDuplicates.length !== strings.length) {
33-
throw new Error(`Duplicate ${itemName} found in the list`)
24+
let item
25+
while (!item) {
26+
item = await longPrompt(`${itemName} #${i + 1}`, { required: true })
27+
28+
if (strings.has(item)) {
29+
if (options?.allowDuplicate) {
30+
duplicates.push(item)
31+
continue
32+
} else if (options?.errorOnDuplicate) {
33+
throw new Error(`Duplicate ${itemName} found in the list`)
34+
} else {
35+
options?.logger?.log(`Duplicate ${itemName}`)
36+
item = undefined
37+
}
38+
}
3439
}
40+
strings.add(item)
3541
}
3642

37-
return strings
43+
return [...strings, ...duplicates]
3844
}
3945

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

0 commit comments

Comments
 (0)