Skip to content

Commit 1b9c51f

Browse files
authored
Merge pull request #160 from getAlby/feat/nwc-get-info
feat: add NWC getInfo method
2 parents 890edc8 + 265da21 commit 1b9c51f

File tree

2 files changed

+71
-12
lines changed

2 files changed

+71
-12
lines changed

examples/nwc/get-info.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import * as crypto from "node:crypto"; // required in node.js
2+
global.crypto = crypto; // required in node.js
3+
import "websocket-polyfill"; // required in node.js
4+
5+
import * as readline from "node:readline/promises";
6+
import { stdin as input, stdout as output } from "node:process";
7+
8+
import { webln as providers } from "../../dist/index.module.js";
9+
10+
const rl = readline.createInterface({ input, output });
11+
12+
const nwcUrl =
13+
process.env.NWC_URL ||
14+
(await rl.question("Nostr Wallet Connect URL (nostr+walletconnect://...): "));
15+
rl.close();
16+
17+
const webln = new providers.NostrWebLNProvider({
18+
nostrWalletConnectUrl: nwcUrl,
19+
});
20+
await webln.enable();
21+
const response = await webln.getInfo();
22+
23+
console.info(response);
24+
25+
webln.close();

src/webln/NostrWeblnProvider.ts

+46-12
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,18 @@ type Nip07Provider = {
4848
signEvent(event: UnsignedEvent): Promise<Event>;
4949
};
5050

51+
type Nip47GetInfoResponse = {
52+
alias: string;
53+
color: string;
54+
pubkey: string;
55+
network: string;
56+
block_height: number;
57+
block_hash: string;
58+
methods: string[];
59+
};
60+
5161
const nip47ToWeblnRequestMap = {
62+
get_info: "getInfo",
5263
get_balance: "getBalance",
5364
make_invoice: "makeInvoice",
5465
pay_invoice: "sendPayment",
@@ -215,18 +226,41 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
215226
// WebLN compatible response
216227
// TODO: use NIP-47 get_info call
217228
async getInfo(): Promise<GetInfoResponse> {
218-
return {
219-
methods: [
220-
"getInfo",
221-
"sendPayment",
222-
"makeInvoice",
223-
"getBalance",
224-
"lookupInvoice",
225-
],
226-
node: {} as WebLNNode,
227-
supports: ["lightning"],
228-
version: "NWC",
229-
};
229+
this.checkConnected();
230+
231+
const supports = ["lightning", "nostr"];
232+
const version = "Alby JS SDK";
233+
234+
try {
235+
return this.executeNip47Request<GetInfoResponse, Nip47GetInfoResponse>(
236+
"get_info",
237+
undefined,
238+
(result) => !!result.methods,
239+
(result) => ({
240+
methods: result.methods.map(
241+
(key) =>
242+
nip47ToWeblnRequestMap[
243+
key as keyof typeof nip47ToWeblnRequestMap
244+
],
245+
),
246+
node: {
247+
alias: result.alias,
248+
pubkey: result.pubkey,
249+
color: result.color,
250+
} as WebLNNode,
251+
supports,
252+
version,
253+
}),
254+
);
255+
} catch (error) {
256+
console.error("Failed to request get_info", error);
257+
return {
258+
methods: ["sendPayment"],
259+
node: {} as WebLNNode,
260+
supports,
261+
version,
262+
};
263+
}
230264
}
231265

232266
getBalance() {

0 commit comments

Comments
 (0)