Expand error messaging for non-Ledger chains (#886)

This commit is contained in:
Jaco 2021-11-08 14:23:00 +01:00 committed by GitHub
parent 63d29819bc
commit 87e8206481
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 23 deletions

View File

@ -1,5 +1,12 @@
# CHANGELOG
## master
Changes:
- Expand error messaging for non-compatible Ledger chains
## 0.41.1 Nov 8, 2021
Contributed:

View File

@ -71,7 +71,7 @@ function LedgerSign ({ accountIndex, addressOffset, className, error, genesisHas
{error}
</Warning>
)}
{ledgerLocked
{(ledgerLocked || error)
? (
<Button
isBusy={isBusy || ledgerLoading}
@ -91,7 +91,6 @@ function LedgerSign ({ accountIndex, addressOffset, className, error, genesisHas
)
}
</div>
);
}

View File

@ -19,8 +19,6 @@ interface StateBase {
interface State extends StateBase {
address: string | null;
error: string | null;
isLedgerCapable: boolean;
isLedgerEnabled: boolean;
isLoading: boolean;
isLocked: boolean;
ledger: Ledger | null;
@ -28,24 +26,8 @@ interface State extends StateBase {
warning: string | null;
}
function getNetwork (genesis: string): Network | undefined {
return ledgerChains.find(({ genesisHash }) => genesisHash[0] === genesis);
}
function retrieveLedger (genesis: string): Ledger {
let ledger: Ledger | null = null;
const { isLedgerCapable } = getState();
assert(isLedgerCapable, 'Incompatible browser, only Chrome is supported');
const def = getNetwork(genesis);
assert(def, `Unable to find supported chain for ${genesis}`);
ledger = new Ledger('webusb', def.network);
return ledger;
function getNetwork (genesisHash: string): Network | undefined {
return ledgerChains.find(({ genesisHash: [hash] }) => hash === genesisHash);
}
function getState (): StateBase {
@ -57,6 +39,22 @@ function getState (): StateBase {
};
}
function retrieveLedger (genesis: string): Ledger {
let ledger: Ledger | null = null;
const { isLedgerCapable } = getState();
assert(isLedgerCapable, 'Incompatible browser, only Chrome is supported');
const def = getNetwork(genesis);
assert(def, 'There is no known Ledger app available for this chain');
ledger = new Ledger('webusb', def.network);
return ledger;
}
export function useLedger (genesis?: string | null, accountIndex = 0, addressOffset = 0): State {
const [isLoading, setIsLoading] = useState(false);
const [isLocked, setIsLocked] = useState(false);
@ -66,6 +64,7 @@ export function useLedger (genesis?: string | null, accountIndex = 0, addressOff
const [address, setAddress] = useState<string | null>(null);
const { t } = useTranslation();
const ledger = useMemo(() => {
setError(null);
setIsLocked(false);
setRefreshLock(false);
@ -77,7 +76,11 @@ export function useLedger (genesis?: string | null, accountIndex = 0, addressOff
return null;
}
return retrieveLedger(genesis);
try {
return retrieveLedger(genesis);
} catch (error) {
setError((error as Error).message);
}
}
return null;