Skip to content

Commit

Permalink
Cleanup DKG identity creation
Browse files Browse the repository at this point in the history
  • Loading branch information
NullSoldier committed Oct 3, 2024
1 parent 5e02531 commit 44be2bb
Showing 1 changed file with 41 additions and 67 deletions.
108 changes: 41 additions & 67 deletions ironfish-cli/src/commands/wallet/multisig/dkg/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,11 @@ export class DkgCreateCommand extends IronfishCommand {
}
}

const { name: participantName, identity } = ledger
? await ui.retryStep(
() => {
Assert.isNotUndefined(ledger)
return this.getIdentityFromLedger(ledger, client, accountName)
},
this.logger,
true,
)
: await this.getParticipant(client, accountName)
const { name: participantName, identity } = await this.getOrCreateIdentity(
client,
ledger,
accountName,
)

const { totalParticipants, minSigners } = await ui.retryStep(
async () => {
Expand Down Expand Up @@ -202,11 +197,46 @@ export class DkgCreateCommand extends IronfishCommand {
multisigClient?.stop()
}

private async getParticipant(client: RpcClient, name: string) {
private async getOrCreateIdentity(
client: RpcClient,
ledger: LedgerMultiSigner | undefined,
name: string,
): Promise<{
identity: string
name: string
}> {
const identities = await client.wallet.multisig.getIdentities()

if (ledger) {
const ledgerIdentity = await ledger.dkgGetIdentity(0)

const foundIdentity = identities.content.identities.find(
(i) => i.identity === ledgerIdentity.toString('hex'),
)

if (foundIdentity) {
this.debug('Identity from ledger already exists')
return foundIdentity
}

// We must use the ledger's identity
while (identities.content.identities.find((i) => i.name === name)) {
this.log('An identity with the same name already exists')
name = await ui.inputPrompt('Enter a new name for the identity', true)
}

const created = await client.wallet.multisig.importParticipant({
name,
identity: ledgerIdentity.toString('hex'),
})

return { name, identity: created.content.identity }
}

const foundIdentity = identities.content.identities.find((i) => i.name === name)

if (foundIdentity) {
this.debug(`Identity already exists with name: ${foundIdentity.name}`)
return foundIdentity
}

Expand Down Expand Up @@ -235,62 +265,6 @@ export class DkgCreateCommand extends IronfishCommand {
return name
}

async getIdentityFromLedger(
ledger: LedgerMultiSigner,
client: RpcClient,
name: string,
): Promise<{
name: string
identity: string
}> {
// TODO(hughy): support multiple identities using index
const identity = await ledger.dkgGetIdentity(0)

const allIdentities = (await client.wallet.multisig.getIdentities()).content.identities

const foundIdentity = allIdentities.find((i) => i.identity === identity.toString('hex'))

if (foundIdentity) {
this.log(`Identity already exists with name: ${foundIdentity.name}`)

return {
name: foundIdentity.name,
identity: identity.toString('hex'),
}
}

name = await ui.inputPrompt('Enter a name for the identity', true)

while (allIdentities.find((i) => i.name === name)) {
this.log('An identity with the same name already exists')
name = await ui.inputPrompt('Enter a new name for the identity', true)
}

await client.wallet.multisig.importParticipant({
name,
identity: identity.toString('hex'),
})

return {
name,
identity: identity.toString('hex'),
}
}

async createParticipant(
client: RpcClient,
name: string,
): Promise<{
name: string
identity: string
}> {
const identity = (await client.wallet.multisig.createParticipant({ name })).content.identity
return {
name,
identity,
}
}

async getDkgConfig(
multisigClient: MultisigClient | null,
ledger: boolean,
Expand Down

0 comments on commit 44be2bb

Please sign in to comment.