Files
pezkuwi-apps/packages/react-hooks/src/useLedger.ts
T
Satoshi Qazi Muhammed 45d479d2ec Move to @pezkuwi/dev 0.86.4 (eslint 10 toolchain)
- dev ^0.86.4 at the root and in packages/apps; directives renamed by
  pezkuwi-dev-migrate-eslint10.
- eslint --fix with the 0.86.4 config; one fix broke syntax
  (typescript-eslint prefer-optional-chain turned
  `(isStarted && onStart) && onStart()` into `(isStarted && onStart?.();`
  in TxButton) and is corrected by hand.
- The remaining findings fixed rather than disabled: optional chains
  (each suggestion reviewed), `cause` on rethrown errors, `if` instead of
  `a && promise` in effects, parallel instead of sequential awaits in
  Promise.all, Error rejections, no `delete` on arrays (the storage query
  cache is a Map; an upload's file list no longer keeps a hole), no
  `[object Object]` stringification (TFunction's second argument is text
  or options), dead code removed (referenda's never-shown expand button,
  react-query's unused CoreDescriptor).
2026-10-10 02:06:12 +04:00

105 lines
3.2 KiB
TypeScript

// Copyright 2017-2026 @pezkuwi/react-hooks authors & contributors
// SPDX-License-Identifier: Apache-2.0
// This is for the use of `Ledger`
//
/* eslint-disable @typescript-eslint/no-deprecated */
import type { ApiPromise } from '@pezkuwi/api';
import type { TransportType } from '@pezkuwi/hw-ledger-transports/types';
import { useCallback, useMemo } from 'react';
import { Ledger, LedgerGeneric } from '@pezkuwi/hw-ledger';
import { knownGenesis, knownLedger } from '@pezkuwi/networks/defaults';
import { settings } from '@pezkuwi/ui-settings';
import { assert } from '@pezkuwi/util';
import { createNamedHook } from './createNamedHook.js';
import { useApi } from './useApi.js';
interface StateBase {
hasLedgerChain: boolean;
hasWebUsb: boolean;
isLedgerCapable: boolean;
isLedgerEnabled: boolean;
}
interface State extends StateBase {
getLedger: () => LedgerGeneric | Ledger;
}
const EMPTY_STATE: StateBase = {
hasLedgerChain: false,
hasWebUsb: false,
isLedgerCapable: false,
isLedgerEnabled: false
};
const hasWebUsb = !!(window as unknown as { USB?: unknown }).USB;
const ledgerChains = Object
.keys(knownGenesis)
.filter((n) => knownLedger[n]);
const ledgerHashes = ledgerChains.reduce<string[]>((all, n) => [...all, ...knownGenesis[n]], []);
let ledger: LedgerGeneric | Ledger | null = null;
let ledgerType: TransportType | null = null;
let ledgerApp: string | null;
function retrieveLedger (api: ApiPromise): LedgerGeneric | Ledger {
const currType = settings.get().ledgerConn as TransportType;
const currApp = settings.get().ledgerApp;
if (!ledger || ledgerType !== currType || currApp !== ledgerApp) {
const genesisHex = api.genesisHash.toHex();
const network = ledgerChains.find((network) => knownGenesis[network].includes(genesisHex));
assert(network, `Unable to find a known Ledger config for genesisHash ${genesisHex}`);
if (currApp === 'generic') {
// All chains use the `slip44` from pezkuwi in their derivation path in ledger.
// This interface is specific to the underlying PezkuwiGenericApp.
ledger = new LedgerGeneric(currType, network, knownLedger.pezkuwi);
} else if (currApp === 'migration') {
ledger = new LedgerGeneric(currType, network, knownLedger[network]);
} else if (currApp === 'chainSpecific') {
ledger = new Ledger(currType, network);
} else {
// This will never get touched since it will always hit the above two. This satisfies the compiler.
ledger = new LedgerGeneric(currType, network, knownLedger.pezkuwi);
}
ledgerType = currType;
ledgerApp = currApp;
}
return ledger;
}
function getState (api: ApiPromise): StateBase {
const hasLedgerChain = ledgerHashes.includes(api.genesisHash.toHex());
const isLedgerCapable = hasWebUsb && hasLedgerChain;
return {
hasLedgerChain,
hasWebUsb,
isLedgerCapable,
isLedgerEnabled: isLedgerCapable && settings.ledgerConn !== 'none'
};
}
function useLedgerImpl (): State {
const { api, isApiReady } = useApi();
const getLedger = useCallback(
() => retrieveLedger(api),
[api]
);
return useMemo(
() => ({ ...(isApiReady ? getState(api) : EMPTY_STATE), getLedger }),
[api, getLedger, isApiReady]
);
}
export const useLedger = createNamedHook('useLedger', useLedgerImpl);