Skip to content

Commit

Permalink
Merge pull request #96 from getAlby/task-latest-webln
Browse files Browse the repository at this point in the history
fix: use latest webln if available in globalThis after init
  • Loading branch information
rolznz authored Oct 19, 2023
2 parents 9662648 + 0e9ad9e commit 2294939
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/lightning-address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -54,6 +54,10 @@ export default class LightningAddress {
}
}

getWebLN() {
return this.webln || globalThis.webln
}

async fetch() {
if (this.options.proxy) {
return this.fetchWithProxy();
Expand Down Expand Up @@ -177,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,
Expand All @@ -185,9 +193,7 @@ export default class LightningAddress {
amount,
boost,
},
{
webln: this.webln,
},
{ webln },
);
}

Expand Down Expand Up @@ -233,12 +239,12 @@ export default class LightningAddress {
options: ZapOptions = {},
): Promise<SendPaymentResponse> {
const invoice = this.zapInvoice(args, options);
if (!this.webln) {
// mainly for TS
const webln = this.getWebLN()
if (!webln) {
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;
}

Expand Down

0 comments on commit 2294939

Please sign in to comment.