Skip to content

Commit

Permalink
Merge pull request #163 from getAlby/task-nwc-keysend
Browse files Browse the repository at this point in the history
feat: add NWC payKeysend method
  • Loading branch information
rolznz authored Dec 14, 2023
2 parents 1b9c51f + bc37f18 commit ce3fedc
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 11 deletions.
35 changes: 35 additions & 0 deletions examples/nwc/keysend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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 =
process.env.NWC_URL ||
(await rl.question("Nostr Wallet Connect URL (nostr+walletconnect://...): "));
const destination =
(await rl.question("Enter destination pubkey: ")) ||
"030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3";
const amount = await rl.question("Enter amount: ");
rl.close();

const webln = new providers.NostrWebLNProvider({
nostrWalletConnectUrl: nwcUrl,
});
await webln.enable();
const response = await webln.keysend({
amount,
destination,
customRecords: {
696969: "1KOZHzhLs2U7JIx3BmEY",
},
});

console.info(response);

webln.close();
12 changes: 5 additions & 7 deletions examples/nwc/send-payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,18 @@ import { webln as providers } from "../../dist/index.module.js";

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

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

const webln = new providers.NostrWebLNProvider({
nostrWalletConnectUrl: nwcUrl,
});
await webln.enable();
const sendPaymentResponse = await webln.sendPayment(invoice);
console.log(sendPaymentResponse);
const response = await webln.sendPayment(invoice);

const getBalanceResponse = await webln.getBalance();
console.log(getBalanceResponse);
console.info(response);

webln.close();
32 changes: 28 additions & 4 deletions src/webln/NostrWeblnProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,16 @@ type Nip47GetInfoResponse = {
methods: string[];
};

type Nip47PayResponse = {
preimage: string;
};

const nip47ToWeblnRequestMap = {
get_info: "getInfo",
get_balance: "getBalance",
make_invoice: "makeInvoice",
pay_invoice: "sendPayment",
pay_keysend: "payKeysend",
lookup_invoice: "lookupInvoice",
};

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

return this.executeNip47Request<SendPaymentResponse, { preimage: string }>(
return this.executeNip47Request<SendPaymentResponse, Nip47PayResponse>(
"pay_invoice",
{
invoice,
Expand All @@ -291,10 +296,29 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
);
}

// not-yet implemented WebLN interface methods
keysend(args: KeysendArgs): Promise<SendPaymentResponse> {
throw new Error("Method not implemented.");
keysend(args: KeysendArgs) {
this.checkConnected();

return this.executeNip47Request<SendPaymentResponse, Nip47PayResponse>(
"pay_keysend",
{
amount: +args.amount * 1000, // NIP-47 uses msat
pubkey: args.destination,
tlv_records: args.customRecords
? Object.entries(args.customRecords).map((v) => ({
type: parseInt(v[0]),
value: v[1],
}))
: [],
// TODO: support optional preimage
// preimage?: "123",
},
(result) => !!result.preimage,
(result) => ({ preimage: result.preimage }),
);
}

// not-yet implemented WebLN interface methods
lnurl(
lnurl: string,
): Promise<{ status: "OK" } | { status: "ERROR"; reason: string }> {
Expand Down

0 comments on commit ce3fedc

Please sign in to comment.