import React, { FormEvent, ReactElement, useState } from 'react'; import { useDispatch } from 'react-redux'; import { setKey } from '../../store/notaryKey'; import keys from '../../utils/keys.json'; export default function NotaryKey(): ReactElement { const dispatch = useDispatch(); const defaultKey: string = keys.defaultKey const notaryPseKey: string = keys.notaryPseKey const [notaryKey, setNotaryKey] = useState(defaultKey); const [errors, setError] = useState(null); const isValidPEMKey = (key: string): boolean => { try { const trimmedKey = key.trim(); if (!trimmedKey.startsWith('-----BEGIN PUBLIC KEY-----') || !trimmedKey.endsWith('-----END PUBLIC KEY-----')) { setError('Invalid PEM format: header or footer missing'); return false; } const keyContent = trimmedKey .replace('-----BEGIN PUBLIC KEY-----', '') .replace('-----END PUBLIC KEY-----', '') .trim(); try { const decodedKeyContent = atob(keyContent); } catch (err) { console.log('here'); setError('Invalid Base64 encoding'); return false; } return true; } catch (err) { console.error('Error validating key:', err); return false; } }; const handleInput = (e: FormEvent | React.MouseEvent, key?: string | undefined) => { setError(null); const keyInput = key !== undefined ? key : (e.currentTarget instanceof HTMLTextAreaElement ? e.currentTarget.value : ''); if (isValidPEMKey(keyInput)) { setNotaryKey(keyInput); dispatch(setKey(keyInput)); } else { setNotaryKey(keyInput); } } return (
Change Notary Public Key: {errors &&

{errors}

}
) }