From ff0c032b916349bfdfbc76079dd9de9d036d6d4e Mon Sep 17 00:00:00 2001 From: im-adithya Date: Thu, 19 Oct 2023 14:08:17 +0530 Subject: [PATCH 1/2] fix: use latest webln if available in globalThis after init --- src/lightning-address.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/lightning-address.ts b/src/lightning-address.ts index a6503fe..c6a7e37 100644 --- a/src/lightning-address.ts +++ b/src/lightning-address.ts @@ -40,7 +40,7 @@ export default class LightningAddress { constructor(address: string, options?: LightningAddressOptions) { this.address = address; - this.options = { proxy: DEFAULT_PROXY, webln: globalThis.webln }; + this.options = { proxy: DEFAULT_PROXY }; this.options = Object.assign(this.options, options); this.parse(); this.webln = this.options.webln; @@ -54,6 +54,10 @@ export default class LightningAddress { } } + getWebLN() { + return this.webln || globalThis.webln + } + async fetch() { if (this.options.proxy) { return this.fetchWithProxy(); @@ -186,7 +190,7 @@ export default class LightningAddress { boost, }, { - webln: this.webln, + webln: this.getWebLN(), }, ); } @@ -233,12 +237,13 @@ export default class LightningAddress { options: ZapOptions = {}, ): Promise { const invoice = this.zapInvoice(args, options); - if (!this.webln) { + const webln = this.getWebLN() + if (!webln) { // mainly for TS throw new Error("WebLN not available"); } - await this.webln.enable(); - const response = this.webln.sendPayment((await invoice).paymentRequest); + await webln.enable(); + const response = webln.sendPayment((await invoice).paymentRequest); return response; } From 0e9ad9ea2fa21531c1feeb3b450c5b948b513a5c Mon Sep 17 00:00:00 2001 From: im-adithya Date: Thu, 19 Oct 2023 14:26:06 +0530 Subject: [PATCH 2/2] chore: add check for webln in boost --- src/lightning-address.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/lightning-address.ts b/src/lightning-address.ts index c6a7e37..478f7e5 100644 --- a/src/lightning-address.ts +++ b/src/lightning-address.ts @@ -181,6 +181,10 @@ export default class LightningAddress { throw new Error("No keysendData available. Please call fetch() first."); } const { destination, customKey, customValue } = this.keysendData; + const webln = this.getWebLN() + if (!webln) { + throw new Error("WebLN not available"); + } return booster( { destination, @@ -189,9 +193,7 @@ export default class LightningAddress { amount, boost, }, - { - webln: this.getWebLN(), - }, + { webln }, ); } @@ -239,7 +241,6 @@ export default class LightningAddress { const invoice = this.zapInvoice(args, options); const webln = this.getWebLN() if (!webln) { - // mainly for TS throw new Error("WebLN not available"); } await webln.enable();