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 06b67d6 commit 6ae31e9
Showing 1 changed file with 52 additions and 11 deletions.
63 changes: 52 additions & 11 deletions ironfish-cli/src/commands/wallet/multisig/dkg/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import {
ACCOUNT_SCHEMA_VERSION,
AccountFormat,
Assert,
encodeAccountImport,
PromiseUtils,
RpcClient,
Expand Down Expand Up @@ -117,16 +116,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,
)

this.log(`Identity for ${participantName}: \n${identity} \n`)

Expand Down Expand Up @@ -207,6 +201,53 @@ export class DkgCreateCommand extends IronfishCommand {
multisigClient?.stop()
}

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
}

const created = await client.wallet.multisig.createParticipant({ name })
return { name, identity: created.content.identity }
}

private async getParticipant(client: RpcClient, name: string) {
const identities = await client.wallet.multisig.getIdentities()
const foundIdentity = identities.content.identities.find((i) => i.name === name)
Expand Down

0 comments on commit 6ae31e9

Please sign in to comment.