import { useState } from "react"; import { FormattedMessage, useIntl } from "react-intl"; import { useNavigate } from "react-router-dom"; import { v4 as uuid } from "uuid"; import AsyncButton from "Element/AsyncButton"; import { LNWallet, WalletInfo, WalletKind, Wallets } from "Wallet"; import { unwrap } from "SnortUtils"; const ConnectLNC = () => { const { formatMessage } = useIntl(); const navigate = useNavigate(); const [pairingPhrase, setPairingPhrase] = useState(); const [error, setError] = useState(); const [connectedLNC, setConnectedLNC] = useState(); const [walletInfo, setWalletInfo] = useState(); const [walletPassword, setWalletPassword] = useState(); async function tryConnect(cfg: string) { try { const { LNCWallet } = await import("Wallet/LNCWallet"); const lnc = await LNCWallet.Initialize(cfg); const info = await lnc.getInfo(); // prompt password setConnectedLNC(lnc); setWalletInfo(info as WalletInfo); } catch (e) { if (e instanceof Error) { setError(e.message); } else { setError( formatMessage({ defaultMessage: "Unknown error", }), ); } } } function setLNCPassword(pw: string) { connectedLNC?.setPassword(pw); Wallets.add({ id: uuid(), kind: WalletKind.LNC, active: true, info: unwrap(walletInfo), }); navigate("/settings/wallet"); } function flowConnect() { if (connectedLNC) return null; return ( <>

setPairingPhrase(e.target.value)} />
tryConnect(unwrap(pairingPhrase))} disabled={!pairingPhrase}>
{error && {error}} ); } function flowSetPassword() { if (!connectedLNC) return null; return (

setWalletPassword(e.target.value)} />
setLNCPassword(unwrap(walletPassword))} disabled={(walletPassword?.length ?? 0) < 8}>
); } return ( <> {flowConnect()} {flowSetPassword()} ); }; export default ConnectLNC;