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

feat: add lookup invoice to nwc webln provider #73

Merged
merged 5 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions examples/nwc/lookup-invoice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as crypto from "node:crypto"; // required in node.js
global.crypto = crypto; // required in node.js
import "websocket-polyfill"; // required in node.js

import * as readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";

import { webln as providers } from "../../dist/index.module.js";

const rl = readline.createInterface({ input, output });

const nwcUrl = await rl.question(
"Nostr Wallet Connect URL (nostrwalletconnect://...): ",
);
rl.close();

const webln = new providers.NostrWebLNProvider({
nostrWalletConnectUrl: nwcUrl,
});
await webln.enable();
const response = await webln.lookupInvoice({
// provide one of the below
//invoice: 'lnbc10n1pjwxschpp5hg0pw234n9ww9q4uy25pnvu8y4jzpznysasyf7m9fka36t7fahysdqufet5xgzhv43ycn3qv4uxzmtsd3jscqzzsxqyz5vqsp5uw023qhxuxqfj69rvj9yns5gufczad5gqw4uer5cgqhw90slkavs9qyyssqvv2tw6c30ssgtpejc3zk7ns0svuj8547d8wxj0e36hltljx5a8x4qj59mk2y7qlt6qazf2j38fzc8uag3887nslxz6fe3vnyvg0f2xqqnlvcu2',
payment_hash: 'ba1e172a35995ce282bc22a819b3872564208a64876044fb654dbb1d2fc9edc9'
});

console.log(response);

webln.close();
26 changes: 25 additions & 1 deletion src/webln/NostrWeblnProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ interface GetBalanceResponse {
budget_renewal?: string;
}

interface LookupInvoiceArgs {
invoice?: string;
payment_hash?: string;
}

interface LookupInvoiceResponse {
invoice: string;
paid: boolean;
}

interface NostrWebLNOptions {
authorizationUrl?: string; // the URL to the NWC interface for the user to confirm the session
relayUrl: string;
Expand Down Expand Up @@ -214,7 +224,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
// TODO: use NIP-47 get_info call
async getInfo(): Promise<GetInfoResponse> {
return {
methods: ["getInfo", "sendPayment", "addinvoice", "getBalance"],
methods: ["getInfo", "sendPayment", "addinvoice", "getBalance", "lookupinvoice"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have sendPayment, so this should probably be lookupInvoice

also addinvoice should probably be makeInvoice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bumi thanks for confirming, I got a bit confused here.

The ones I put are LND methods, but then we also have the official WebLN methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bumi I have updated webln-types and fixed those inconsistencies. Thanks!

node: {} as WebLNNode,
supports: ["lightning"],
version: "NWC",
Expand Down Expand Up @@ -258,6 +268,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
): Promise<{ status: "OK" } | { status: "ERROR"; reason: string }> {
throw new Error("Method not implemented.");
}

makeInvoice(args: string | number | RequestInvoiceArgs) {
this.checkConnected();

Expand All @@ -283,6 +294,19 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
(result) => ({ paymentRequest: result.invoice }),
);
}

lookupInvoice(args: LookupInvoiceArgs) {
this.checkConnected();

return this.executeNip47Request<LookupInvoiceResponse>(
"lookup_invoice",
"lookupinvoice",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we actually need to pass the two here? or can this be some mapping?
this is only needed for the events, isn't it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bumi the webln request method is needed for the notification function. WebLN and NWC event names are inconsistent, I think we need to provide both.

Unless we can guarantee the only change is in casing, then we could have a mapping function from camelCase to lower_case_with_underscores

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but can't we do a mapping from

a = { 
  "lookup_invoice" : "lookupinvoice" 
}
a["lookup_invoice"]

that then can be checked?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bumi added, thanks

args,
(result) => result.invoice !== undefined && result.paid !== undefined,
(result) => ({ paymentRequest: result.invoice, paid: result.paid }),
);
}

request(method: RequestMethod, args?: unknown): Promise<unknown> {
throw new Error("Method not implemented.");
}
Expand Down