Skip to content

Commit fd057ad

Browse files
committed
chore: add examples for all nwc client methods
1 parent 74df445 commit fd057ad

9 files changed

+304
-3
lines changed

examples/nwc/client/get-balance.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 { nwc } 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 client = new nwc.NWCClient({
18+
nostrWalletConnectUrl: nwcUrl,
19+
});
20+
const response = await client.getBalance();
21+
22+
console.info(response);
23+
24+
client.close();
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 { nwc } 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 client = new nwc.NWCClient({
18+
nostrWalletConnectUrl: nwcUrl,
19+
});
20+
21+
const ONE_WEEK_IN_SECONDS = 60 * 60 * 24 * 7;
22+
const response = await client.listTransactions({
23+
from: Math.floor(new Date().getTime() / 1000 - ONE_WEEK_IN_SECONDS),
24+
until: Math.ceil(new Date().getTime() / 1000),
25+
limit: 30,
26+
// type: "incoming",
27+
// unpaid: true,
28+
});
29+
30+
console.info(
31+
response.transactions.length + " transactions, ",
32+
response.transactions.filter((t) => t.type === "incoming").length +
33+
" incoming",
34+
response,
35+
);
36+
37+
client.close();

examples/nwc/client/lookup-invoice.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 { nwc } 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+
16+
const invoiceOrPaymentHash = await rl.question("Invoice or payment hash: ");
17+
rl.close();
18+
19+
const client = new nwc.NWCClient({
20+
nostrWalletConnectUrl: nwcUrl,
21+
});
22+
23+
const response = await client.lookupInvoice({
24+
// provide one of the below
25+
invoice: invoiceOrPaymentHash.startsWith("ln")
26+
? invoiceOrPaymentHash
27+
: undefined,
28+
payment_hash: !invoiceOrPaymentHash.startsWith("ln")
29+
? invoiceOrPaymentHash
30+
: undefined,
31+
});
32+
33+
console.info(response);
34+
35+
client.close();

examples/nwc/client/make-invoice.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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 { nwc } 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 client = new nwc.NWCClient({
18+
nostrWalletConnectUrl: nwcUrl,
19+
});
20+
21+
const response = await client.makeInvoice({
22+
amount: 1000, // in millisats
23+
description: "NWC Client example",
24+
});
25+
26+
console.info(response);
27+
28+
client.close();
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 { LightningAddress } from "@getalby/lightning-tools";
6+
7+
import * as readline from "node:readline/promises";
8+
import { stdin as input, stdout as output } from "node:process";
9+
10+
import { nwc } from "../../../dist/index.module.js";
11+
12+
const rl = readline.createInterface({ input, output });
13+
14+
const ln = new LightningAddress(process.env.LN_ADDRESS || "[email protected]");
15+
// fetch the LNURL data
16+
await ln.fetch();
17+
18+
// generate 2 invoices to pay
19+
const invoices = (
20+
await Promise.all(
21+
[1, 2].map((v) =>
22+
ln.requestInvoice({
23+
satoshi: 1,
24+
comment: `Multi-pay invoice #${v}`,
25+
}),
26+
),
27+
)
28+
).map((invoice) => invoice.paymentRequest);
29+
30+
console.info("Generated two invoices", invoices);
31+
32+
const nwcUrl =
33+
process.env.NWC_URL ||
34+
(await rl.question("Nostr Wallet Connect URL (nostr+walletconnect://...): "));
35+
rl.close();
36+
37+
const client = new nwc.NWCClient({
38+
nostrWalletConnectUrl: nwcUrl,
39+
});
40+
41+
try {
42+
const response = await client.multiPayInvoice({
43+
invoices: invoices.map((invoice) => ({
44+
invoice,
45+
})),
46+
});
47+
console.info(response);
48+
} catch (error) {
49+
console.error("multi_pay_invoice failed", error);
50+
}
51+
52+
client.close();
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 { nwc } 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 client = new nwc.NWCClient({
18+
nostrWalletConnectUrl: nwcUrl,
19+
});
20+
21+
const keysends = [
22+
{
23+
pubkey:
24+
"030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3",
25+
amount: 1000, // millisats
26+
tlv_records: [
27+
{
28+
type: 696969,
29+
value: "017rsl75kNnSke4mMHYE", // hello@getalby.com
30+
},
31+
{
32+
type: 34349334,
33+
value: "first keysend message",
34+
},
35+
],
36+
},
37+
{
38+
pubkey:
39+
"030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3",
40+
amount: 1000, // millisats
41+
tlv_records: [
42+
{
43+
type: 696969,
44+
value: "1KOZHzhLs2U7JIx3BmEY", // another Alby account
45+
},
46+
{
47+
type: 34349334,
48+
value: "second keysend message",
49+
},
50+
],
51+
},
52+
];
53+
54+
try {
55+
const response = await client.multiPayKeysend({ keysends });
56+
console.info(JSON.stringify(response));
57+
} catch (error) {
58+
console.error("multi_pay_keysend failed", error);
59+
}
60+
61+
client.close();

examples/nwc/client/pay-invoice.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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 { nwc } 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+
const invoice = await rl.question("Lightning invoice: ");
16+
rl.close();
17+
18+
const client = new nwc.NWCClient({
19+
nostrWalletConnectUrl: nwcUrl,
20+
});
21+
22+
const response = await client.payInvoice({ invoice });
23+
24+
console.info(response);
25+
26+
client.close();

examples/nwc/client/pay-keysend.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 { nwc } 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+
16+
rl.close();
17+
18+
const client = new nwc.NWCClient({
19+
nostrWalletConnectUrl: nwcUrl,
20+
});
21+
const response = await client.payKeysend({
22+
amount: 1000, // millisats
23+
pubkey: "030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3",
24+
tlv_records: [
25+
{
26+
type: 696969,
27+
value: "017rsl75kNnSke4mMHYE", // hello@getalby.com
28+
},
29+
{
30+
type: 34349334,
31+
value: "example keysend message",
32+
},
33+
],
34+
});
35+
36+
console.info(response);
37+
38+
client.close();

src/NWCClient.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export type Nip47MultiPayKeysendResponse = {
7070
errors: []; // TODO: add error handling
7171
};
7272

73-
export interface Nip47ListTransactionsArgs {
73+
export interface Nip47ListTransactionsRequest {
7474
from?: number;
7575
until?: number;
7676
limit?: number;
@@ -510,14 +510,14 @@ export class NWCClient {
510510
}
511511

512512
async listTransactions(
513-
args: Nip47ListTransactionsArgs,
513+
request: Nip47ListTransactionsRequest,
514514
): Promise<Nip47ListTransactionsResponse> {
515515
try {
516516
// maybe we can tailor the response to our needs
517517
const result =
518518
await this.executeNip47Request<Nip47ListTransactionsResponse>(
519519
"list_transactions",
520-
args,
520+
request,
521521
(response) => !!response.transactions,
522522
);
523523

0 commit comments

Comments
 (0)