|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ |
| 4 | +import { DEFAULT_UNLOCK_TIMEOUT_MS, RpcRequestError } from '@ironfish/sdk' |
| 5 | +import { Flags } from '@oclif/core' |
| 6 | +import { IronfishCommand } from '../../command' |
| 7 | +import { RemoteFlags } from '../../flags' |
| 8 | +import { inputPrompt } from '../../ui' |
| 9 | + |
| 10 | +export class UnlockCommand extends IronfishCommand { |
| 11 | + static hidden = true |
| 12 | + |
| 13 | + static description = 'unlock accounts in the wallet' |
| 14 | + |
| 15 | + static flags = { |
| 16 | + ...RemoteFlags, |
| 17 | + passphrase: Flags.string({ |
| 18 | + description: 'Passphrase to unlock the wallet with', |
| 19 | + }), |
| 20 | + timeout: Flags.integer({ |
| 21 | + description: |
| 22 | + 'How long to unlock the wallet for in ms. Use -1 to keep the wallet unlocked until the process stops', |
| 23 | + }), |
| 24 | + } |
| 25 | + |
| 26 | + async start(): Promise<void> { |
| 27 | + const { flags } = await this.parse(UnlockCommand) |
| 28 | + |
| 29 | + const client = await this.connectRpc() |
| 30 | + |
| 31 | + const response = await client.wallet.getAccountsStatus() |
| 32 | + if (!response.content.encrypted) { |
| 33 | + this.log('Wallet is already decrypted') |
| 34 | + this.exit(1) |
| 35 | + } |
| 36 | + |
| 37 | + let passphrase = flags.passphrase |
| 38 | + if (!passphrase) { |
| 39 | + passphrase = await inputPrompt('Enter a passphrase to unlock the wallet', true) |
| 40 | + } |
| 41 | + |
| 42 | + try { |
| 43 | + await client.wallet.unlock({ |
| 44 | + passphrase, |
| 45 | + timeout: flags.timeout, |
| 46 | + }) |
| 47 | + } catch (e) { |
| 48 | + if (e instanceof RpcRequestError) { |
| 49 | + this.log('Wallet unlock failed') |
| 50 | + this.exit(1) |
| 51 | + } |
| 52 | + |
| 53 | + throw e |
| 54 | + } |
| 55 | + |
| 56 | + const timeout = flags.timeout || DEFAULT_UNLOCK_TIMEOUT_MS |
| 57 | + if (timeout === -1) { |
| 58 | + this.log( |
| 59 | + 'Unlocked the wallet. Call wallet:lock or stop the node to lock the wallet again.', |
| 60 | + ) |
| 61 | + } else { |
| 62 | + this.log(`Unlocked the wallet for ${timeout}ms`) |
| 63 | + } |
| 64 | + |
| 65 | + this.exit(0) |
| 66 | + } |
| 67 | +} |
0 commit comments