import { useState, ReactElement, useEffect } from 'react' import './PrivateKeyModal.scss' import { Button } from '../Button/Button' import { setSignerPk, getSigner } from '../../utils/common' import { PrivateKey } from '@ethersphere/bee-js' import ClipboardIcon from 'remixicon-react/FileCopyLineIcon' import CheckDoubleLineIcon from 'remixicon-react/CheckDoubleLineIcon' import { Tooltip } from '../Tooltip/Tooltip' import { TOOLTIPS } from '../../constants/tooltips' type Props = { onSaved: () => void } export function PrivateKeyModal({ onSaved }: Props): ReactElement { const [value, setValue] = useState('') const [confirmValue, setConfirmValue] = useState('') const [showError, setShowError] = useState(false) const [copied, setCopied] = useState(false) useEffect(() => { handleGenerateNew() }, []) const handleCopyPrivateKey = async () => { try { await navigator.clipboard.writeText(value) setCopied(true) } catch { // eslint-disable-next-line no-console console.debug('Failed to copy private key to clipboard') } } const handleGenerateNew = () => { const id = crypto.randomUUID() const signer = getSigner(id) const privKey = signer.toHex() setValue(privKey) setConfirmValue('') setCopied(false) setShowError(false) } const handleBlur = () => { if (!value.trim()) { return } try { new PrivateKey(value) setShowError(false) } catch { setShowError(true) setCopied(false) } } const handleSave = () => { try { new PrivateKey(value) setSignerPk(value) onSaved() } catch { setShowError(true) } } return (
Create Private Key
Using a private key ensures that only you can access this File Manager instance. Save it securely before continuing.
IMPORTANT: Lost keys cannot be recovered Swarm never stores private keys. If you lose this key, access to this File Manager instance will be permanently lost.
{ setValue(e.target.value) setCopied(false) setShowError(false) }} onBlur={handleBlur} spellCheck={false} /> { }
{showError ? 'Invalid private key.' : ''}
setConfirmValue(e.target.value)} spellCheck={false} />
{confirmValue && value === confirmValue ? '✓ Private keys match!' : ''}
Safety Reminder:
A copy of your private key is stored in this browser for convenience, but it’s not a backup - clearing browser data or switching devices will remove it.{' '} Make sure you’ve saved your private key before continuing.
) } export default PrivateKeyModal