Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: lnurl pay to use existing lightning address #660

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/screens/wallet/Send.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export default function Send() {
if (lnAddress.lnurlpData) {
navigate(`/wallet/send/lnurl-pay`, {
state: {
args: { lnurlDetails: lnAddress.lnurlpData },
args: { lnAddress },
},
});
return;
Expand Down
27 changes: 14 additions & 13 deletions frontend/src/screens/wallet/send/LnurlPay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Label } from "src/components/ui/label";
import { LoadingButton } from "src/components/ui/loading-button";
import { useToast } from "src/components/ui/use-toast";

import { LightningAddress, LnUrlPayResponse } from "@getalby/lightning-tools";
import { LightningAddress } from "@getalby/lightning-tools";
import { Link, useLocation, useNavigate } from "react-router-dom";
import Loading from "src/components/Loading";

Expand All @@ -14,19 +14,18 @@ export default function LnurlPay() {
const navigate = useNavigate();
const { toast } = useToast();

const lnurlDetails = state?.args?.lnurlDetails as LnUrlPayResponse;
const lnAddress = state?.args?.lnAddress as LightningAddress;
const [amount, setAmount] = React.useState("");
const [comment, setComment] = React.useState("");
const [isLoading, setLoading] = React.useState(false);

const onSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault();
try {
if (!lnAddress) {
throw new Error("no lightning address set");
}
setLoading(true);
const lnAddress = new LightningAddress(lnurlDetails.identifier);
// this is set instead of calling fetch because
// requestInvoice uses lnurlpData.callback
lnAddress.lnurlpData = lnurlDetails;
const invoice = await lnAddress.requestInvoice({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried doing this first but for some weird reason this function was not being passed to this screen via state, but somehow it works for me now 👍

satoshi: parseInt(amount),
comment,
Expand All @@ -49,23 +48,25 @@ export default function LnurlPay() {
};

React.useEffect(() => {
if (!lnurlDetails) {
if (!lnAddress) {
navigate("/wallet/send");
}
}, [navigate, lnurlDetails]);
}, [navigate, lnAddress]);

if (!lnurlDetails) {
if (!lnAddress) {
return <Loading />;
}

return (
<form onSubmit={onSubmit} className="grid gap-4">
<div>
<p className="font-medium text-lg mb-2">{lnurlDetails.identifier}</p>
{lnurlDetails?.description && (
<p className="font-medium text-lg mb-2">{lnAddress.address}</p>
{lnAddress.lnurlpData?.description && (
<div className="mb-2">
<Label>Description</Label>
<p className="text-muted-foreground">{lnurlDetails.description}</p>
<p className="text-muted-foreground">
{lnAddress.lnurlpData.description}
</p>
</div>
)}
<div className="mb-2">
Expand All @@ -83,7 +84,7 @@ export default function LnurlPay() {
autoFocus
/>
</div>
{!!lnurlDetails?.commentAllowed && (
{!!lnAddress.lnurlpData?.commentAllowed && (
<div className="mb-2">
<Label htmlFor="comment">Comment</Label>
<Input
Expand Down
Loading