/* global __APP_VERSION__, __GIT_COMMIT__, __GIT_COMMIT_URL__, __RELEASE_URL__ */ import React, { useEffect, useMemo, useState } from 'react' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { faClock, faCheck } from '@fortawesome/free-solid-svg-icons' import { Hooks } from 'applesauce-react' import { DebugBus, type DebugLogEntry } from '../utils/debugBus' const defaultPayload = 'The quick brown fox jumps over the lazy dog.' const Debug: React.FC = () => { const activeAccount = Hooks.useActiveAccount() const [payload, setPayload] = useState(defaultPayload) const [cipher44, setCipher44] = useState('') const [cipher04, setCipher04] = useState('') const [plain44, setPlain44] = useState('') const [plain04, setPlain04] = useState('') const [tEncrypt44, setTEncrypt44] = useState(null) const [tEncrypt04, setTEncrypt04] = useState(null) const [tDecrypt44, setTDecrypt44] = useState(null) const [tDecrypt04, setTDecrypt04] = useState(null) const [logs, setLogs] = useState(DebugBus.snapshot()) const [debugEnabled, setDebugEnabled] = useState(() => localStorage.getItem('debug') === '*') useEffect(() => { return DebugBus.subscribe((e) => setLogs(prev => [...prev, e].slice(-300))) }, []) const signer = useMemo(() => (activeAccount as unknown as { signer?: unknown })?.signer, [activeAccount]) const pubkey = (activeAccount as unknown as { pubkey?: string })?.pubkey const hasNip04 = typeof (signer as { nip04?: { encrypt?: unknown; decrypt?: unknown } } | undefined)?.nip04?.encrypt === 'function' const hasNip44 = typeof (signer as { nip44?: { encrypt?: unknown; decrypt?: unknown } } | undefined)?.nip44?.encrypt === 'function' const doEncrypt = async (mode: 'nip44' | 'nip04') => { if (!signer || !pubkey) return try { const api = (signer as any)[mode] DebugBus.info('debug', `encrypt start ${mode}`, { pubkey, len: payload.length }) const start = performance.now() const cipher = await api.encrypt(pubkey, payload) const ms = Math.round(performance.now() - start) DebugBus.info('debug', `encrypt done ${mode}`, { len: typeof cipher === 'string' ? cipher.length : -1, ms }) if (mode === 'nip44') setCipher44(cipher) else setCipher04(cipher) if (mode === 'nip44') setTEncrypt44(ms) else setTEncrypt04(ms) } catch (e) { DebugBus.error('debug', `encrypt error ${mode}`, e instanceof Error ? e.message : String(e)) } } const doDecrypt = async (mode: 'nip44' | 'nip04') => { if (!signer || !pubkey) return try { const api = (signer as any)[mode] const cipher = mode === 'nip44' ? cipher44 : cipher04 if (!cipher) { DebugBus.warn('debug', `no cipher to decrypt for ${mode}`) return } DebugBus.info('debug', `decrypt start ${mode}`, { len: cipher.length }) const start = performance.now() const plain = await api.decrypt(pubkey, cipher) const ms = Math.round(performance.now() - start) DebugBus.info('debug', `decrypt done ${mode}`, { len: typeof plain === 'string' ? plain.length : -1, ms }) if (mode === 'nip44') setPlain44(String(plain)) else setPlain04(String(plain)) if (mode === 'nip44') setTDecrypt44(ms) else setTDecrypt04(ms) } catch (e) { DebugBus.error('debug', `decrypt error ${mode}`, e instanceof Error ? e.message : String(e)) } } const toggleDebug = () => { const next = !debugEnabled setDebugEnabled(next) if (next) localStorage.setItem('debug', '*') else localStorage.removeItem('debug') } const CodeBox = ({ value }: { value: string }) => (
{value || 'โ€”'}
) const Stat = ({ label, value }: { label: string; value?: string | number | null }) => ( {label}: {value ?? 'โ€”'} ) return (

Bunker Debug

Active pubkey: {pubkey || 'none'}

Payload