Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust Ledger lookups via hash/apps only #5618

Merged
merged 5 commits into from
Jun 29, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions packages/react-hooks/src/useLedger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { LedgerTypes } from '@polkadot/hw-ledger/types';
import { useCallback, useMemo } from 'react';

import { Ledger } from '@polkadot/hw-ledger';
import networks from '@polkadot/networks';
import { knownGenesis, knownLedger } from '@polkadot/networks/defaults';
import uiSettings from '@polkadot/ui-settings';
import { assert } from '@polkadot/util';

Expand All @@ -28,7 +28,10 @@ const EMPTY_STATE: StateBase = {
};

const hasWebUsb = !!(window as unknown as { USB?: unknown }).USB;
const ledgerChains = networks.filter((n) => !!n.hasLedgerSupport);
const ledgerChains = Object
.keys(knownGenesis)
.filter((n) => knownLedger[n]);
const ledgerHashes = ledgerChains.reduce<string[]>((all, n) => [...all, ...knownGenesis[n]], []);
let ledger: Ledger | null = null;
let ledgerType: LedgerTypes | null = null;

Expand All @@ -37,19 +40,19 @@ function retrieveLedger (api: ApiPromise): Ledger {

if (!ledger || ledgerType !== currType) {
const genesisHex = api.genesisHash.toHex();
const def = ledgerChains.find(({ genesisHash }) => genesisHash[0] === genesisHex);
const network = ledgerChains.find((network) => knownGenesis[network].includes(genesisHex));

assert(def, `Unable to find supported chain for ${genesisHex}`);
assert(network, `Unable to find a known Ledger config for genesisHash ${genesisHex}`);

ledger = new Ledger(currType, def.network);
ledger = new Ledger(currType, network);
ledgerType = currType;
}

return ledger;
}

function getState (api: ApiPromise): StateBase {
const isLedgerCapable = hasWebUsb && ledgerChains.map(({ genesisHash }) => genesisHash[0]).includes(api.genesisHash.toHex());
const isLedgerCapable = hasWebUsb && ledgerHashes.includes(api.genesisHash.toHex());

return {
isLedgerCapable,
Expand Down