Skip to content

Commit

Permalink
fix: remove send-multi-payment dependence on lightning-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Jan 23, 2024
1 parent 50e59f8 commit 9bd01da
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 19 deletions.
9 changes: 6 additions & 3 deletions examples/nwc/send-multi-payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,11 @@ const webln = new providers.NostrWebLNProvider({
nostrWalletConnectUrl: nwcUrl,
});
await webln.enable();
const response = await webln.sendMultiPayment(invoices);

console.info(response);
try {
const response = await webln.sendMultiPayment(invoices);
console.info(response);
} catch (error) {
console.error("sendMultiPayment failed", error);
}

webln.close();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@
"prepare": "husky install"
},
"dependencies": {
"@getalby/lightning-tools": "^5.0.1",
"events": "^3.3.0",
"nostr-tools": "^1.17.0"
},
"devDependencies": {
"@commitlint/cli": "^17.7.1",
"@commitlint/config-conventional": "^17.7.0",
"@getalby/lightning-tools": "^5.0.1",
"@types/jest": "^29.5.5",
"@types/node": "^20.8.6",
"@typescript-eslint/eslint-plugin": "^6.3.0",
Expand Down
20 changes: 5 additions & 15 deletions src/webln/NostrWeblnProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
} from "@webbtc/webln-types";
import { GetInfoResponse } from "@webbtc/webln-types";
import { NWCAuthorizationUrlOptions } from "../types";
import { Invoice } from "@getalby/lightning-tools";

const NWCs: Record<string, NostrWebLNOptions> = {
alby: {
Expand Down Expand Up @@ -352,28 +351,21 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
): Promise<SendMultiPaymentResponse> {
await this.checkConnected();

// get payment hashes of the payment requests
const paymentHashToPaymentRequestMap: Record<string, string> = {};
for (const paymentRequest of paymentRequests) {
paymentHashToPaymentRequestMap[
new Invoice({ pr: paymentRequest }).paymentHash
] = paymentRequest;
}

const results = await this.executeMultiNip47Request<
{ preimage: string; paymentRequest: string },
Nip47PayResponse
>(
"multi_pay_invoice",
{
invoices: paymentRequests.map((paymentRequest) => ({
invoices: paymentRequests.map((paymentRequest, index) => ({
invoice: paymentRequest,
id: index.toString(),
})),
},
paymentRequests.length,
(result) => !!result.preimage,
(result) => {
const paymentRequest = paymentHashToPaymentRequestMap[result.dTag];
const paymentRequest = paymentRequests[parseInt(result.dTag)];
if (!paymentRequest) {
throw new Error(
"Could not find paymentRequest matching response d tag",
Expand Down Expand Up @@ -709,7 +701,6 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
resultMapper: (result: R & { dTag: string }) => T,
) {
const weblnMethod = nip47ToWeblnMultiRequestMap[nip47Method];
let numPaymentsReceived = 0;
const results: (R & { dTag: string })[] = [];
return new Promise<T[]>((resolve, reject) => {
(async () => {
Expand Down Expand Up @@ -752,7 +743,6 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
const replyTimeoutCheck = setTimeout(replyTimeout, 60000);

sub.on("event", async (event) => {
++numPaymentsReceived;
// console.log(`Received reply event: `, event);

const decryptedContent = await this.decrypt(
Expand Down Expand Up @@ -780,15 +770,15 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
);
}
const dTag = event.tags.find((tag) => tag[0] === "d")?.[1];
if (!dTag) {
if (dTag === undefined) {
throw new Error("No d tag found in response event");
}
// console.info("dTag", dTag);
results.push({
...response.result,
dTag,
});
if (numPaymentsReceived === numPayments) {
if (results.length === numPayments) {
clearTimeout(replyTimeoutCheck);
sub.unsub();
//console.log("Received results", results);
Expand Down

0 comments on commit 9bd01da

Please sign in to comment.