import React, { useState, useContext, useCallback, useMemo } from 'react'; import { I18nManager, ScrollView, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; import { Icon } from 'react-native-elements'; import { BlueButton, BlueCard, BlueLoading, BlueSpacing20, BlueSpacing40, BlueText, SafeBlueArea } from '../../BlueComponents'; import navigationStyle from '../../components/navigationStyle'; import Lnurl from '../../class/lnurl'; import { Chain } from '../../models/bitcoinUnits'; import loc from '../../loc'; import { BlueStorageContext } from '../../blue_modules/storage-context'; import { useNavigation, useRoute, useTheme } from '@react-navigation/native'; import selectWallet from '../../helpers/select-wallet'; import url from 'url'; import { SuccessView } from '../send/success'; const AuthState = { USER_PROMPT: 0, IN_PROGRESS: 1, SUCCESS: 2, ERROR: 3, }; const LnurlAuth = () => { const { wallets } = useContext(BlueStorageContext); 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 node/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(wallet => setParams({ walletID: wallet.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`]} {renderWalletSelectionButton} )} {authState === AuthState.SUCCESS && ( <> {loc.formatString(loc.lnurl_auth[`${parsedLnurl.query.action || 'auth'}_answer`], { hostname: parsedLnurl.hostname })} )} {authState === AuthState.ERROR && ( {loc.formatString(loc.lnurl_auth.could_not_auth, { hostname: parsedLnurl.hostname })} {errMsg} )} ); }; export default LnurlAuth; const styles = StyleSheet.create({ alignSelfCenter: { alignSelf: 'center', }, domainName: { alignSelf: 'center', fontWeight: 'bold', fontSize: 25, paddingVertical: 10, }, root: { flex: 1, justifyContent: 'center', }, walletSelectRoot: { alignItems: 'center', justifyContent: 'flex-end', }, walletSelectTouch: { flexDirection: 'row', alignItems: 'center', }, walletSelectText: { color: '#9aa0aa', fontSize: 14, marginRight: 8, }, walletWrap: { flexDirection: 'row', alignItems: 'center', marginVertical: 4, }, walletWrapTouch: { flexDirection: 'row', alignItems: 'center', }, walletWrapLabel: { fontSize: 14, }, }); LnurlAuth.navigationOptions = navigationStyle({ title: '', closeButton: true, closeButtonFunc: ({ navigation }) => navigation.dangerouslyGetParent().popToTop(), });