Skip to content

Commit

Permalink
Adjust Ledger lookups via hash/apps only (#5618)
Browse files Browse the repository at this point in the history
* Adjust Ledger lookups via hash/apps only

* Lookup on all genesis hashes

* Lookup against all hashes

* Adjust ledger lookups
  • Loading branch information
jacogr authored Jun 29, 2021
1 parent 8c33c60 commit 7c84bcc
Showing 1 changed file with 9 additions and 6 deletions.
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

0 comments on commit 7c84bcc

Please sign in to comment.