import { useNavigation, useRoute } from '@react-navigation/native'; import React, { useCallback, useMemo, useState } from 'react'; import { I18nManager, ScrollView, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; import { Icon } from '@rneui/themed'; import URL from 'url'; import { BlueCard, BlueLoading, BlueSpacing20, BlueSpacing40, BlueText } from '../../BlueComponents'; import Lnurl from '../../class/lnurl'; import Button from '../../components/Button'; import SafeArea from '../../components/SafeArea'; import { useTheme } from '../../components/themes'; import selectWallet from '../../helpers/select-wallet'; import loc from '../../loc'; import { Chain } from '../../models/bitcoinUnits'; import { SuccessView } from '../send/success'; import { useStorage } from '../../hooks/context/useStorage'; const AuthState = { USER_PROMPT: 0, IN_PROGRESS: 1, SUCCESS: 2, ERROR: 3, }; const LnurlAuth = () => { const { wallets } = useStorage(); const { name } = useRoute(); const { walletID, lnurl } = useRoute().params; const wallet = useMemo(() => wallets.find(w => w.getID() === walletID), [wallets, walletID]); const LN = useMemo(() => new Lnurl(lnurl), [lnurl]); const parsedLnurl = useMemo( () => (lnurl ? URL.parse(Lnurl.getUrlFromLnurl(lnurl), true) : {}), // eslint-disable-line n/no-deprecated-api [lnurl], ); const [authState, setAuthState] = useState(AuthState.USER_PROMPT); const [errMsg, setErrMsg] = useState(''); const { setParams, navigate } = useNavigation(); const { colors } = useTheme(); const stylesHook = StyleSheet.create({ root: { backgroundColor: colors.background, }, walletWrapLabel: { color: colors.buttonAlternativeTextColor, }, }); const showSelectWalletScreen = useCallback(() => { selectWallet(navigate, name, Chain.OFFCHAIN).then(w => setParams({ walletID: w.getID() })); }, [navigate, name, setParams]); const authenticate = useCallback(() => { wallet .authenticate(LN) .then(() => { setAuthState(AuthState.SUCCESS); setErrMsg(''); }) .catch(err => { setAuthState(AuthState.ERROR); setErrMsg(err); }); }, [wallet, LN]); if (!parsedLnurl || !wallet || authState === AuthState.IN_PROGRESS) return ( ); const renderWalletSelectionButton = authState === AuthState.USER_PROMPT && ( {authState !== AuthState.IN_PROGRESS && ( {loc.wallets.select_wallet.toLowerCase()} )} {wallet.getLabel()} ); return ( {authState === AuthState.USER_PROMPT && ( <> {loc.lnurl_auth[`${parsedLnurl.query.action || 'auth'}_question_part_1`]} {parsedLnurl.hostname} {loc.lnurl_auth[`${parsedLnurl.query.action || 'auth'}_question_part_2`]}