-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add UI action to make ledger reliable
This UI action will handle many of the failure cases that occur when running ledger commands. They'll use CLI-UX to inform the user of the current state, and what the state should be.
- Loading branch information
1 parent
6ae31e9
commit 752fe37
Showing
4 changed files
with
152 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* 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 { PromiseUtils } from '@ironfish/sdk' | ||
import { ux } from '@oclif/core' | ||
import { | ||
Ledger, | ||
LedgerAppNotOpen, | ||
LedgerClaNotSupportedError, | ||
LedgerConnectError, | ||
LedgerDeviceLockedError, | ||
LedgerGPAuthFailed, | ||
LedgerPortIsBusyError, | ||
} from '../ledger' | ||
|
||
export async function ledgerAction<TResult>( | ||
ledger: Ledger, | ||
action: () => TResult | Promise<TResult>, | ||
): Promise<TResult> { | ||
const statusRunning = ux.action.running | ||
let statusAdded = false | ||
|
||
try { | ||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
try { | ||
const result = await ledger.tryInstruction(async () => await action()) | ||
ux.action.stop() | ||
return result | ||
} catch (e) { | ||
if (e instanceof LedgerConnectError) { | ||
ux.action.start('Ledger not found, connect ledger') | ||
} else if (e instanceof LedgerDeviceLockedError) { | ||
ux.action.start('Ledger is locked, enter pin code') | ||
} else if (e instanceof LedgerPortIsBusyError) { | ||
ux.action.start('Ledger port is already in use') | ||
} else if (e instanceof LedgerGPAuthFailed) { | ||
ux.action.start('Ledger handshake failed... trying again') | ||
} else if (e instanceof LedgerClaNotSupportedError) { | ||
const appName = ledger.isMultisig ? 'Ironfish DKG' : 'Ironfish' | ||
ux.action.start(`Wrong Ledger app opened. Please open ${appName} APP.`) | ||
} else if (e instanceof LedgerAppNotOpen) { | ||
const appName = ledger.isMultisig ? 'Ironfish DKG' : 'Ironfish' | ||
ux.action.start(`Unlock your Ledger and open the ${appName} APP.`) | ||
} else { | ||
throw e | ||
} | ||
|
||
statusAdded = true | ||
await PromiseUtils.sleep(1000) | ||
continue | ||
break | ||
} | ||
} | ||
} finally { | ||
// Don't interrupt an existing status outside of ledgerAction() | ||
if (!statusRunning && statusAdded) { | ||
ux.action.stop() | ||
} | ||
} | ||
} |