// Copyright 2017-2023 @polkadot/app-utilities authors & contributors // SPDX-License-Identifier: Apache-2.0 import React, { useCallback, useMemo, useState } from 'react'; import { createOption } from '@polkadot/app-settings/util'; import { createSs58 } from '@polkadot/apps-config'; import { allNetworks } from '@polkadot/networks'; import { Dropdown, InputAddressSimple, Static } from '@polkadot/react-components'; import { useApi } from '@polkadot/react-hooks'; import { formatNumber } from '@polkadot/util'; import { base58Decode, checkAddressChecksum, encodeAddress, isAddress } from '@polkadot/util-crypto'; import { useTranslation } from './translate'; interface Props { className?: string; } interface State { address: string | null; inputSS58: number; } function getState (input: string | null): State { let address: string | null = null; let inputSS58 = 42; if (input && isAddress(input)) { const decoded = base58Decode(input); const [,,, ss58Decoded] = checkAddressChecksum(decoded); address = input; inputSS58 = ss58Decoded; } return { address, inputSS58 }; } function Addresses ({ className }: Props): React.ReactElement { const { t } = useTranslation(); const { chainSS58 } = useApi(); const [{ address, inputSS58 }, setState] = useState({ address: null, inputSS58: 42 }); const [prefix, setPrefix] = useState(-1); const setAddress = useCallback( (address: string | null) => setState(getState(address)), [] ); const prefixOptions = useMemo( () => { const network = allNetworks.find(({ prefix }) => prefix === chainSS58); return createSs58(t).map((o) => createOption(o, ['default'], 'empty', ( o.value === -1 ? network ? ` (${network.displayName}, ${chainSS58 || 0})` : ` (${chainSS58 || 0})` : ` (${o.value})` )) ); }, [chainSS58, t] ); const converted = useMemo( () => address && encodeAddress(address, prefix === -1 ? chainSS58 : prefix), [address, chainSS58, prefix] ); return (
('address to convert')} noConvert onChange={setAddress} />
('address prefix')} onChange={setPrefix} options={prefixOptions} />
{address && ( <>
('input ss58 prefix')} value={formatNumber(inputSS58)} />
{converted && (
('converted address')} />
)} )}
); } export default React.memo(Addresses);