Skip to content

Commit

Permalink
Backup ledger keys in a separate step
Browse files Browse the repository at this point in the history
This allows round3 and backup to fail independently and we can retry them separately.
  • Loading branch information
patnir committed Oct 8, 2024
1 parent 185ce24 commit 67744d8
Showing 1 changed file with 50 additions and 34 deletions.
84 changes: 50 additions & 34 deletions ironfish-cli/src/commands/wallet/multisig/dkg/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import {
ACCOUNT_SCHEMA_VERSION,
AccountFormat,
Assert,
encodeAccountImport,
PromiseUtils,
RpcClient,
encodeAccountImport,
} from '@ironfish/sdk'
import { Flags, ux } from '@oclif/core'
import fs from 'fs'
Expand Down Expand Up @@ -197,10 +197,58 @@ export class DkgCreateCommand extends IronfishCommand {
true,
)

if (ledger) {
await ui.retryStep(
async () => {
Assert.isNotUndefined(ledger)
return this.createBackup(ledger, accountName)
},
this.logger,
true,
)
}

this.log('Multisig account created successfully using DKG!')
multisigClient?.stop()
}

private async createBackup(
ledger: LedgerMultiSigner,
accountName: string,
) {
this.log()
this.log('Creating an encrypted backup of multisig keys from your Ledger device...')
this.log()

const encryptedKeys = await ui.ledger({
ledger,
message: 'Backup DKG Keys',
approval: true,
action: () => ledger.dkgBackupKeys(),
})

this.log()
this.log('Encrypted Ledger Multisig Backup:')
this.log(encryptedKeys.toString('hex'))
this.log()
this.log('Please save the encrypted keys shown above.')
this.log(
'Use `ironfish wallet:multisig:ledger:restore` if you need to restore the keys to your Ledger.',
)

const dataDir = this.sdk.fileSystem.resolve(this.sdk.dataDir)
const backupKeysPath = path.join(dataDir, `ironfish-ledger-${accountName}.txt`)

if (fs.existsSync(backupKeysPath)) {
await ui.confirmOrQuit(
`Error when backing up your keys: \nThe file ${backupKeysPath} already exists. \nOverwrite?`,
)
}

await fs.promises.writeFile(backupKeysPath, encryptedKeys.toString('hex'))
this.log(`A copy of your encrypted keys have been saved at ${backupKeysPath}`)
}

private async getOrCreateIdentity(
client: RpcClient,
ledger: LedgerMultiSigner | undefined,
Expand Down Expand Up @@ -389,8 +437,7 @@ export class DkgCreateCommand extends IronfishCommand {
this.log(`Identity for ${participantName}: \n${currentIdentity} \n`)

this.log(
`\nEnter ${
totalParticipants - 1
`\nEnter ${totalParticipants - 1
} identities of all other participants (excluding yours) `,
)
identities = await ui.collectStrings('Participant Identity', totalParticipants - 1, {
Expand Down Expand Up @@ -636,37 +683,6 @@ export class DkgCreateCommand extends IronfishCommand {
this.log(
`Account ${response.content.name} imported with public address: ${dkgKeys.publicAddress}`,
)
this.log()
this.log('Creating an encrypted backup of multisig keys from your Ledger device...')
this.log()

const encryptedKeys = await ui.ledger({
ledger,
message: 'Backup DKG Keys',
approval: true,
action: () => ledger.dkgBackupKeys(),
})

this.log()
this.log('Encrypted Ledger Multisig Backup:')
this.log(encryptedKeys.toString('hex'))
this.log()
this.log('Please save the encrypted keys shown above.')
this.log(
'Use `ironfish wallet:multisig:ledger:restore` if you need to restore the keys to your Ledger.',
)

const dataDir = this.sdk.fileSystem.resolve(this.sdk.dataDir)
const backupKeysPath = path.join(dataDir, `ironfish-ledger-${accountName}.txt`)

if (fs.existsSync(backupKeysPath)) {
await ui.confirmOrQuit(
`Error when backing up your keys: \nThe file ${backupKeysPath} already exists. \nOverwrite?`,
)
}

await fs.promises.writeFile(backupKeysPath, encryptedKeys.toString('hex'))
this.log(`A copy of your encrypted keys have been saved at ${backupKeysPath}`)
}

async performRound3(
Expand Down

0 comments on commit 67744d8

Please sign in to comment.