Skip to content

Commit 744689b

Browse files
authored
Merge pull request #197 from getAlby/feat/nwc-client
feat: Split NWC client from NostrWeblnProvider
2 parents 87b8e2d + 409213b commit 744689b

17 files changed

+1460
-663
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();

examples/nwc/client/get-info.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.getInfo();
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();

examples/nwc/keysend.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,20 @@ const rl = readline.createInterface({ input, output });
1212
const nwcUrl =
1313
process.env.NWC_URL ||
1414
(await rl.question("Nostr Wallet Connect URL (nostr+walletconnect://...): "));
15-
const destination =
16-
(await rl.question("Enter destination pubkey: ")) ||
17-
"030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3";
18-
const amount = await rl.question("Enter amount: ");
15+
1916
rl.close();
2017

2118
const webln = new providers.NostrWebLNProvider({
2219
nostrWalletConnectUrl: nwcUrl,
2320
});
2421
await webln.enable();
2522
const response = await webln.keysend({
26-
amount,
27-
destination,
23+
amount: 1,
24+
destination:
25+
"030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3",
2826
customRecords: {
29-
696969: "1KOZHzhLs2U7JIx3BmEY",
27+
696969: "017rsl75kNnSke4mMHYE", // hello@getalby.com
28+
34349334: "example keysend message",
3029
},
3130
});
3231

0 commit comments

Comments
 (0)