// Copyright 2017-2025 @pezkuwi/app-staking authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { KeypairType } from '@pezkuwi/util-crypto/types'; import React, { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import { Button, Dropdown, Input, MarkWarning, Modal } from '@pezkuwi/react-components'; import { useQueue } from '@pezkuwi/react-hooks'; import { keyring } from '@pezkuwi/ui-keyring'; import { assert, u8aToHex } from '@pezkuwi/util'; import { keyExtractSuri, mnemonicValidate } from '@pezkuwi/util-crypto'; import { useTranslation } from '../../translate.js'; interface Props { onClose: () => void; } const CRYPTO_MAP: Record = { aura: ['ed25519', 'sr25519'], babe: ['sr25519'], gran: ['ed25519'], imon: ['ed25519', 'sr25519'], para: ['sr25519'] }; const EMPTY_KEY = '0x'; function InjectKeys ({ onClose }: Props): React.ReactElement | null { const { t } = useTranslation(); const { queueRpc } = useQueue(); // this needs to align with what is set as the first value in `type` const [crypto, setCrypto] = useState('sr25519'); const [publicKey, setPublicKey] = useState(EMPTY_KEY); const [suri, setSuri] = useState(''); const [keyType, setKeyType] = useState('babe'); const keyTypeOptRef = useRef([ { text: t('Aura'), value: 'aura' }, { text: t('Babe'), value: 'babe' }, { text: t('Grandpa'), value: 'gran' }, { text: t('I\'m Online'), value: 'imon' }, { text: t('Teyrchains'), value: 'para' } ]); useEffect((): void => { setCrypto(CRYPTO_MAP[keyType][0]); }, [keyType]); useEffect((): void => { try { const { phrase } = keyExtractSuri(suri); assert(mnemonicValidate(phrase), 'Invalid mnemonic phrase'); setPublicKey(u8aToHex(keyring.createFromUri(suri, {}, crypto).publicKey)); } catch { setPublicKey(EMPTY_KEY); } }, [crypto, suri]); const _onSubmit = useCallback( (): void => queueRpc({ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment rpc: { method: 'insertKey', section: 'author' } as any, values: [keyType, suri, publicKey] }), [keyType, publicKey, queueRpc, suri] ); const _cryptoOptions = useMemo( () => CRYPTO_MAP[keyType].map((value): { text: string; value: KeypairType } => ({ text: value === 'ed25519' ? t('ed25519, Edwards') : t('sr15519, Schnorrkel'), value })), [keyType, t] ); return (