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: use latest webln if available in globalThis after init #96

Merged
merged 2 commits into from
Oct 19, 2023
Merged
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
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) {
im-adithya marked this conversation as resolved.
Show resolved Hide resolved
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