NWC decouples wallets from apps though a universal and open messaging interface. Sending or receiving payments becomes a simple message call. No wallet complexity is required and users can connect their preferred wallet.
The @getalby/sdk allows you to use NWC in your application. Here are some examples on how to implement common features.
Let's say your app wants to enable a p2p payment between Bob and Alice. Once both Bob and Alice have "connected" their wallets with the required permissions your app can request a lightning invoice from Bob's wallet and give it Alice's wallet to pay through NWC.
const bobNwc = "nostr+walletconnect://...";
const bob = new nwc.NWCClient({
nostrWalletConnectUrl: bobNwc,
});
const response = await bob.makeInvoice({
amount, // in millisats
description: "NWC Client example",
});
const aliceNwc = "nostr+walletconnect://...";
const alice = new nwc.NWCClient({
nostrWalletConnectUrl: aliceNwc,
});
const invoice = "lnbc...";
const response = await alice.payInvoice({ invoice });
Charge a user for a certain action:
const client = new LN(nwcUrl);
// request a lightning invoice that we show the user to pay
const request = await client.requestPayment(USD(0.1), {
description: "payment for xyz",
});
request
.onPaid(() => {
console.info("received payment!");
client.close(); // when done and no longer needed close the wallet connection
})
Full example: https://github.com/getAlby/js-sdk/blob/master/examples/lnclient/paywall.js
You can find many more examples in the GitHub repository of the js-sdk: