// Copyright 2019-2026 @polkadot/extension-ui authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { HexString } from '@polkadot/util/types'; import { faSync } from '@fortawesome/free-solid-svg-icons'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import React, { useCallback, useContext, useEffect, useMemo, useRef, useState } from 'react'; import { settings } from '@polkadot/ui-settings'; import { ActionContext, Address, Button, ButtonArea, Dropdown, SettingsContext, Switch, VerticalSpace, Warning } from '../components/index.js'; import { useLedger, useTranslation } from '../hooks/index.js'; import { createAccountHardware } from '../messaging.js'; import { Header, Name } from '../partials/index.js'; import { styled } from '../styled.js'; import ledgerChains from '../util/legerChains.js'; interface AccOption { text: string; value: number; } interface NetworkOption { text: string; value: string; } const AVAIL: number[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]; const SELECT_NETWORK = ''; const ALLOW_ANY_NETWORK = '__allow_any_network__'; interface Props { className?: string; } function ImportLedger ({ className }: Props): React.ReactElement { const { t } = useTranslation(); const [accountIndex, setAccountIndex] = useState(0); const [addressOffset, setAddressOffset] = useState(0); const [error, setError] = useState(null); const [isBusy, setIsBusy] = useState(false); const [genesis, setGenesis] = useState(null); const [isEthereum, setIsEthereum] = useState(false); const onAction = useContext(ActionContext); const { ledgerApp } = useContext(SettingsContext); const [name, setName] = useState(null); const isGenericLedgerApp = ledgerApp === 'generic'; const isChainSpecific = ledgerApp === 'chainSpecific'; const { address, error: ledgerError, isLoading: ledgerLoading, isLocked: ledgerLocked, refresh, type, warning: ledgerWarning } = useLedger(genesis, accountIndex, addressOffset, isEthereum); useEffect(() => { if (address) { settings.set({ ledgerConn: 'webusb' }); } }, [address]); useEffect(() => { if (isChainSpecific) { setIsEthereum(false); } }, [isChainSpecific]); const accOps = useRef(AVAIL.map((value): AccOption => ({ text: t('Account type {{index}}', { replace: { index: value } }), value }))); const addOps = useRef(AVAIL.map((value): AccOption => ({ text: t('Address index {{index}}', { replace: { index: value } }), value }))); const onNetworkChange = useCallback((value: string) => { if (value === ALLOW_ANY_NETWORK) { setGenesis(null); return; } setGenesis(value ? value as HexString : null); }, []); const networkOps = useMemo( () => [ ...(isGenericLedgerApp ? [{ text: t('Allow use on any chain'), value: ALLOW_ANY_NETWORK }] : [{ text: t('Select network'), value: SELECT_NETWORK }]), ...ledgerChains.map(({ displayName, genesisHash }): NetworkOption => ({ text: displayName, value: genesisHash[0] })) ], [isGenericLedgerApp, t] ); const _onSave = useCallback( () => { if (address && name && type) { setIsBusy(true); createAccountHardware(address, 'ledger', accountIndex, addressOffset, name, genesis, type) .then(() => onAction('/')) .catch((error: Error) => { console.error(error); setIsBusy(false); setError(error.message); }); } }, [accountIndex, address, addressOffset, genesis, name, onAction, type] ); // select element is returning a string const _onSetAccountIndex = useCallback((value: number) => setAccountIndex(Number(value)), []); const _onSetAddressOffset = useCallback((value: number) => setAddressOffset(Number(value)), []); return ( <>
{!!address && !ledgerError && ( )} {!!name && ( <> )} {!!ledgerWarning && ( {ledgerWarning} )} {(!!error || !!ledgerError) && ( {error || ledgerError} )}
{ledgerLocked ? ( ) : ( ) } ); } export default styled(ImportLedger)` .refreshIcon { margin-right: 0.3rem; } .ethereum-toggle { margin: 1rem 0; } `;