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: WebLN QR fallback for anon users #1858

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

Soxasora
Copy link
Member

@Soxasora Soxasora commented Feb 3, 2025

Description

Fixes #1758
When a WebLN wallet request fails (eg. cancelling a transaction) it can't be re-requested until next reload, throwing an error and prompting an invoice retry with QR that fails if the user is not logged in.

This fix checks for 'me' on useWalletPayment, throwing a new AnonWalletError that can be caught during QR fallback to show both the WebLN client and the QR payment

Screenshots

Screen.Recording.2025-02-10.at.09.45.51.mov

Additional Context

Fix specifically applied for WebLN

Checklist

Are your changes backwards compatible? Please answer below:
Yes, only affects WebLN wallet

On a scale of 1-10 how well and how have you QA'd this change and any features it might affect? Please answer below:
7, more testing

For frontend changes: Tested on mobile, light and dark mode? Please answer below:
n/a

Did you introduce any new environment variables? If so, call them out explicitly here:
No

@Soxasora Soxasora added the bug label Feb 3, 2025
@Soxasora Soxasora force-pushed the anon_webln_qr_fallback branch from 89da4b4 to 53f63c6 Compare February 3, 2025 11:06
@Soxasora Soxasora marked this pull request as ready for review February 4, 2025 19:53
Comment on lines 66 to 72
try {
walletInvoice = await invoiceHelper.retry(walletInvoice, { update: updateOnFallback })
} catch (err) {
if (walletError.wallet === 'webln') { // show QR code for WebLN errors
throw err
}
}
Copy link
Member Author

@Soxasora Soxasora Feb 5, 2025

Choose a reason for hiding this comment

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

While thinking about it, the major errors that this would ignore are already handled: Invoice cancel, Invoice expiration. If I'm not mistaken, retryPaidAction checks for Invoice failed and Invoice already paid, in the context of anon payment this shouldn't really happen as every paidAction generates a new invoice

Edit: Tested with alby extension on timeout, on close and on error

@Soxasora Soxasora marked this pull request as draft February 6, 2025 14:38
@Soxasora Soxasora marked this pull request as ready for review February 8, 2025 12:24
@huumn huumn requested a review from ekzyis February 8, 2025 19:19
Copy link
Member

@ekzyis ekzyis left a comment

Choose a reason for hiding this comment

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

Mhh, it's a bit hard to understand the implications of these changes. For example, I am worried if the priority of the WebLN wallet makes a difference for these changes here.

I think a better way to fix #1758 is to make useWalletPayment aware of if we're logged in or not.

If we're not logged in, try to pay with WebLN if available (typeof window.webln !== 'undefined') and ignore the rest of the code. That it currently might not ignore it for anons because WebLN might have been enabled (wallets.length > 0) is the bug we should fix imo. 99% of our code for payments assumes you're logged in and thus that anons will never hit that code.

What anons can do in comparison to stackers is very limited (no sender and receiver fallbacks, no retries etc.) so I suspect if we (continue to) handle anons during payments immediately, it will be much easier to implement, maintain, read etc.

try {
await window.webln.enable()
} catch (err) {
throw new WalletError('cannot re-enable wallet')
Copy link
Member

Choose a reason for hiding this comment

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

It can also fail for other reasons, like if the user closed the prompt etc. so the error message can be misleading.

I think it should just reuse the WebLN error message:

Suggested change
throw new WalletError('cannot re-enable wallet')
throw new WalletError(err.message)

@Soxasora Soxasora force-pushed the anon_webln_qr_fallback branch 2 times, most recently from ed59dd3 to 0eaf9d4 Compare February 10, 2025 08:42
@Soxasora Soxasora force-pushed the anon_webln_qr_fallback branch from 0eaf9d4 to c819a05 Compare February 10, 2025 08:42
@Soxasora
Copy link
Member Author

Thanks a lot for the correct redirection, now we call WebLN's sendPayment when QR shows up! But only if we reached QR stage because of AnonWalletError that gets thrown by useWalletPayment if the user attempting to pay is Anon.

I think it's the correct behavior and gives a choice, if anything we could 'await' for sendPayment to throw an error to show the QR if we ever change idea.
Thanks again ^^

@ekzyis ekzyis self-requested a review February 12, 2025 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

QR code fallbacks don't work for anon if WebLN enabled
2 participants