joule-labs / webln

Spec and client library for WebLN apps and providers
244 stars 28 forks source link

Detect WebLN provider without enabling it? #52

Open ericpp opened 1 year ago

ericpp commented 1 year ago

Is there a recommended way to detect if the user has a WebLN provider installed? I'm wanting to use this detection to decide whether to show LN payment options to the user or to show them a message to download a WebLN provider.

The requestProvider() function seems like the best way to do this within the webln library, but it also enables the WebLN provider and causes some providers (Alby) to immediately prompt the user for authorization: https://github.com/joule-labs/webln/blob/ab22196dfa4675ad682632dc87400f03516cd211/src/client.ts#L26

Alternatively, I could use the same window.webln check that requestProvider() uses without the enable call, but it feels wrong to go outside of the library to do this: https://github.com/joule-labs/webln/blob/ab22196dfa4675ad682632dc87400f03516cd211/src/client.ts#L21

ericpp commented 1 year ago

A detectProvider function could be added to the library for this purpose, see: https://github.com/ericpp/webln/commit/5eaf0a64632904c18b312379f5717fbaca002819

wbobeirne commented 1 year ago

I think you're absolutely right that requestProvider should not necessarily automatically enable the provider, that was probably a bad move on the initial implementation.

I am however hesitant to implement a synchronous detectProvider function. One thing I wanted to keep in mind was alternative providers that aren't just browser extensions. For instance, I could see there being some kind of standard port that webln will try to reach out to and see if there's a local application listening. Or maybe an iframe-based postMessage implementation. Both of these would require you to wait for some kind of response.

If someone wants to hack around the lack of a detect method today by checking for window.webln, they can definitely do that, but it might not be future proof for some other future implementation.