Skip to content

Commit ce3fedc

Browse files
authored
Merge pull request #163 from getAlby/task-nwc-keysend
feat: add NWC payKeysend method
2 parents 1b9c51f + bc37f18 commit ce3fedc

File tree

3 files changed

+68
-11
lines changed

3 files changed

+68
-11
lines changed

examples/nwc/keysend.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 { 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+
const destination =
16+
(await rl.question("Enter destination pubkey: ")) ||
17+
"030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3";
18+
const amount = await rl.question("Enter amount: ");
19+
rl.close();
20+
21+
const webln = new providers.NostrWebLNProvider({
22+
nostrWalletConnectUrl: nwcUrl,
23+
});
24+
await webln.enable();
25+
const response = await webln.keysend({
26+
amount,
27+
destination,
28+
customRecords: {
29+
696969: "1KOZHzhLs2U7JIx3BmEY",
30+
},
31+
});
32+
33+
console.info(response);
34+
35+
webln.close();

examples/nwc/send-payment.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,18 @@ import { webln as providers } from "../../dist/index.module.js";
99

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

12-
const nwcUrl = await rl.question(
13-
"Nostr Wallet Connect URL (nostr+walletconnect://...): ",
14-
);
12+
const nwcUrl =
13+
process.env.NWC_URL ||
14+
(await rl.question("Nostr Wallet Connect URL (nostr+walletconnect://...): "));
1515
const invoice = await rl.question("Lightning invoice: ");
1616
rl.close();
1717

1818
const webln = new providers.NostrWebLNProvider({
1919
nostrWalletConnectUrl: nwcUrl,
2020
});
2121
await webln.enable();
22-
const sendPaymentResponse = await webln.sendPayment(invoice);
23-
console.log(sendPaymentResponse);
22+
const response = await webln.sendPayment(invoice);
2423

25-
const getBalanceResponse = await webln.getBalance();
26-
console.log(getBalanceResponse);
24+
console.info(response);
2725

2826
webln.close();

src/webln/NostrWeblnProvider.ts

+28-4
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,16 @@ type Nip47GetInfoResponse = {
5858
methods: string[];
5959
};
6060

61+
type Nip47PayResponse = {
62+
preimage: string;
63+
};
64+
6165
const nip47ToWeblnRequestMap = {
6266
get_info: "getInfo",
6367
get_balance: "getBalance",
6468
make_invoice: "makeInvoice",
6569
pay_invoice: "sendPayment",
70+
pay_keysend: "payKeysend",
6671
lookup_invoice: "lookupInvoice",
6772
};
6873

@@ -281,7 +286,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
281286
sendPayment(invoice: string) {
282287
this.checkConnected();
283288

284-
return this.executeNip47Request<SendPaymentResponse, { preimage: string }>(
289+
return this.executeNip47Request<SendPaymentResponse, Nip47PayResponse>(
285290
"pay_invoice",
286291
{
287292
invoice,
@@ -291,10 +296,29 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
291296
);
292297
}
293298

294-
// not-yet implemented WebLN interface methods
295-
keysend(args: KeysendArgs): Promise<SendPaymentResponse> {
296-
throw new Error("Method not implemented.");
299+
keysend(args: KeysendArgs) {
300+
this.checkConnected();
301+
302+
return this.executeNip47Request<SendPaymentResponse, Nip47PayResponse>(
303+
"pay_keysend",
304+
{
305+
amount: +args.amount * 1000, // NIP-47 uses msat
306+
pubkey: args.destination,
307+
tlv_records: args.customRecords
308+
? Object.entries(args.customRecords).map((v) => ({
309+
type: parseInt(v[0]),
310+
value: v[1],
311+
}))
312+
: [],
313+
// TODO: support optional preimage
314+
// preimage?: "123",
315+
},
316+
(result) => !!result.preimage,
317+
(result) => ({ preimage: result.preimage }),
318+
);
297319
}
320+
321+
// not-yet implemented WebLN interface methods
298322
lnurl(
299323
lnurl: string,
300324
): Promise<{ status: "OK" } | { status: "ERROR"; reason: string }> {

0 commit comments

Comments
 (0)