Skip to content

Commit

Permalink
feat: improve handling for fixed amount LNURLs (#133)
Browse files Browse the repository at this point in the history
* feat: improve handling for fixed amount LNURLs

* fix: invert readonly mode

* fix: invert readonly mode

* fix: add comment

* fix: only allow comments if supported

* fix: pass amount

* fix: handle fixed amount lnurls in send screen

* fix: remove obsolete amount
  • Loading branch information
reneaaron authored Sep 25, 2024
1 parent 0d03586 commit 7936aac
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 22 deletions.
3 changes: 3 additions & 0 deletions components/DualCurrencyInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ type DualCurrencyInputProps = {
amount: string;
setAmount(amount: string): void;
autoFocus?: boolean;
readOnly?: boolean;
};

export function DualCurrencyInput({
amount,
setAmount,
autoFocus = false,
readOnly = false,
}: DualCurrencyInputProps) {
const getFiatAmount = useGetFiatAmount();
const getSatsAmount = useGetSatsAmount();
Expand Down Expand Up @@ -58,6 +60,7 @@ export function DualCurrencyInput({
style={styles.amountInput}
autoFocus={autoFocus}
returnKeyType="done"
readOnly={readOnly}
// aria-errormessage="inputError"
/>
<Pressable onPress={toggleInputMode}>
Expand Down
41 changes: 27 additions & 14 deletions pages/send/LNURLPay.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Screen from "~/components/Screen";
import { router, useLocalSearchParams } from "expo-router";
import React from "react";
import React, { useEffect } from "react";
import { View } from "react-native";
import { Button } from "~/components/ui/button";
import { Text } from "~/components/ui/text";
Expand All @@ -21,6 +21,15 @@ export function LNURLPay() {
const [isLoading, setLoading] = React.useState(false);
const [amount, setAmount] = React.useState("");
const [comment, setComment] = React.useState("");
const [isAmountReadOnly, setAmountReadOnly] = React.useState(false);

useEffect(() => {
// Handle fixed amount LNURLs
if (lnurlDetails.minSendable === lnurlDetails.maxSendable) {
setAmount((lnurlDetails.minSendable / 1000).toString());
setAmountReadOnly(true);
}
}, [lnurlDetails.minSendable, lnurlDetails.maxSendable]);

async function requestInvoice() {
setLoading(true);
Expand Down Expand Up @@ -53,20 +62,24 @@ export function LNURLPay() {
<DualCurrencyInput
amount={amount}
setAmount={setAmount}
autoFocus
readOnly={isAmountReadOnly}
autoFocus={!isAmountReadOnly}
/>
<View className="w-full">
<Text className="text-muted-foreground text-center font-semibold2">
Comment
</Text>
<Input
className="w-full border-transparent bg-transparent text-center native:text-2xl font-semibold2"
placeholder="Enter an optional comment"
value={comment}
onChangeText={setComment}
returnKeyType="done"
/>
</View>
{lnurlDetails.commentAllowed &&
<View className="w-full">
<Text className="text-muted-foreground text-center font-semibold2">
Comment
</Text>
<Input
className="w-full border-transparent bg-transparent text-center native:text-2xl font-semibold2"
placeholder="Enter an optional comment"
value={comment}
onChangeText={setComment}
returnKeyType="done"
maxLength={lnurlDetails.commentAllowed}
/>
</View>
}
<View>
<Text className="text-muted-foreground text-center font-semibold2">
To
Expand Down
2 changes: 1 addition & 1 deletion pages/send/PaymentSuccess.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function PaymentSuccess() {
<Screen
title="Success"
/>
<View className="flex-1 justify-center items-center gap-8">
<View className="flex-1 justify-center items-center gap-8 p-6">
<Paid />
<View className="flex flex-col items-center gap-2 -mt-24">
<View className="flex flex-row items-end justify-center">
Expand Down
30 changes: 23 additions & 7 deletions pages/send/Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,29 @@ export function Send() {
throw new Error("LNURL tag " + lnurlDetails.tag + " not supported");
}

router.replace({
pathname: "/send/lnurl-pay",
params: {
lnurlDetailsJSON: JSON.stringify(lnurlDetails),
originalText,
},
});
// Handle fixed amount LNURLs
if (lnurlDetails.minSendable === lnurlDetails.maxSendable && !lnurlDetails.commentAllowed) {
try {
const callback = new URL(lnurlDetails.callback);
callback.searchParams.append("amount", (lnurlDetails.minSendable).toString());
const lnurlPayInfo = await lnurl.getPayRequest(callback.toString());
router.push({
pathname: "/send/confirm",
params: { invoice: lnurlPayInfo.pr, originalText },
});
} catch (error) {
console.error(error);
errorToast(error);
}
} else {
router.replace({
pathname: "/send/lnurl-pay",
params: {
lnurlDetailsJSON: JSON.stringify(lnurlDetails),
originalText,
},
});
}
} else {
// Check if this is a valid invoice
new Invoice({
Expand Down

0 comments on commit 7936aac

Please sign in to comment.