diff --git a/ironfish-cli/src/commands/wallet/multisig/ledger/restore.ts b/ironfish-cli/src/commands/wallet/multisig/ledger/restore.ts new file mode 100644 index 0000000000..726edc675a --- /dev/null +++ b/ironfish-cli/src/commands/wallet/multisig/ledger/restore.ts @@ -0,0 +1,45 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +import { Flags } from '@oclif/core' +import { IronfishCommand } from '../../../../command' +import * as ui from '../../../../ui' +import { Ledger } from '../../../../utils/ledger' + +export class MultisigLedgerRestore extends IronfishCommand { + static description = `Restore encrypted multisig keys to a Ledger device` + + static flags = { + backup: Flags.string({ + description: 'Encrypted multisig key backup from your Ledger device', + char: 'b', + }), + } + + async start(): Promise { + const { flags } = await this.parse(MultisigLedgerRestore) + + let encryptedKeys = flags.backup + if (!encryptedKeys) { + encryptedKeys = await ui.longPrompt( + 'Enter the encrypted multisig key backup to restore to your Ledger device', + ) + } + + const ledger = new Ledger(this.logger) + try { + await ledger.connect(true) + } catch (e) { + if (e instanceof Error) { + this.error(e.message) + } else { + throw e + } + } + + await ledger.dkgRestoreKeys(encryptedKeys) + + this.log() + this.log('Encrypted multisig key backup restored to Ledger.') + } +} diff --git a/ironfish-cli/src/utils/ledger.ts b/ironfish-cli/src/utils/ledger.ts index 0ed2d40135..e54620560f 100644 --- a/ironfish-cli/src/utils/ledger.ts +++ b/ironfish-cli/src/utils/ledger.ts @@ -317,6 +317,16 @@ export class Ledger { return encryptedKeys } + + dkgRestoreKeys = async (encryptedKeys: string): Promise => { + if (!this.app) { + throw new Error('Connect to Ledger first') + } + + this.logger.log('Please approve the request on your ledger device.') + + await this.tryInstruction(this.app.dkgRestoreKeys(encryptedKeys)) + } } function isResponseAddress(response: KeyResponse): response is ResponseAddress {