Nostr Series — Part 5: Zapped By Lightning!
What is Lightning Network?
Lightning Network is a layer two scaling solution built on top of Bitcoin to enable near-instant, cheap, secure, trust-minimized transactions. It exists as a network of bidirectional payment channels, separated from the main Bitcoin network so that it doesn’t have to wait or pay for confirmations from miners while still utilizing the Bitcoin blockchain for final settlement. These channels enable peer-to-peer transfer of value, eliminating the need for any trusted third party.
Lightning Wallets
There are two distinct types of Lightning wallets, custodial and non-custodial. Custodial means you do not hold your private keys, non-custodial means that you do. Of course, using non-custodial wallets is recommended, however custodial wallets are very easy to get started and is fine provided you do not keep too many funds on there.
Non-custodial:
Custodial:
Zaps
Nostr enabled lightning payments on it’s protocol with NIP57. This enhancement added two extra event kinds, 9734 for zap requests and 9375 for zap receipts. These two kinds combined enable Nostr clients to request lightning invoices from LNURL servers as well as pay them. An LNURL server allows lightning clients to communicate via the LNURL protocol, built on top of everyday HTTP. These URLs are encoded in bech32, and contain information related to the lightning network, for instance a payment request. When the LNURL server receives a request, it will perform the desired actions on lightning and then respond appropriately. In short, it provides a simplified interface for interacting with the lightning network. The protocol is open-source (of course!) and available on GitHub.
The most common use of zaps currently is for tips, which can be conveniently sent instantly with negligible transaction fees. It is the main way that value is transferred on the network; Nostr and Lightning fit together like hand in glove ??
Implementation
We are going to use the getalby/lightning-tools library to facilitate lightning network zaps as tips for notes with the following sendZap function:
import { LightningAddress } from "@getalby/lightning-tools";
export interface User {
name: string;
image: string | undefined;
pubkey: string;
nip05: string | undefined;
}
...
export async function sendZap(user: User, id: string) {
if (user.nip05) {
const ln = new LightningAddress(user.nip05);
await ln.fetch();
const event = {
satoshi: 10,
comment: "Awesome post!",
relays: RELAYS,
e: id
};
await ln.zap(event);
}
}
This will transfer 10 satoshis, which is the equivalent of 0.00000010 Bitcoin, or $0.0067 USD at the time of writing (yes you certainly can send amounts as small as this!) to the author of the post’s lightning wallet, given that both you and the recipient have it set up on the metadata nip05 property.
Additional Applications
Micropayments with Lightning Network on the Nostr protocol enables a wide range of different applications and use cases, some of which are built and some which are currently being built. One interesting application is a streaming video service, similar to YouTube, where a user can seamlessly tip a content creator with a stream of satoshis, let’s say 1 satoshi per second, in real-time as they watch a video.
Another interesting use case is payment for services. We could envision a world where any service provider no longer gets paid at the end of a job or at the end of the month, having to wait even longer for final settlement, but as a stream, instantly paid to their wallet as they perform work. There is no longer any inherent risk for the provider of never receiving their payment for an invoice after the final service has already been delivered, or funds being caught up in escrow through some other third party (upwork, fiverr and so on). Further friction is also alleviated in currency exchange, which often means exorbitant fees and more wait times. This kind of streaming payment functionality with near-instant settlement has not been possible with traditional payment rails; that is until now with the Lightning Network. Utilizing the decentralized and censorship resistant properties of Nostr also enables anyone in the world to provide these peer-to-peer services and be paid in satoshis with no third-party interference, which in many cases means that third party taking a significant cut of the earnings in fees, without having done any of the work (read; rent-seeking).
The ability to tip users Bitcoin for their posts is not only useful but really fun! The Nostr community is full of Bitcoiners who have latched onto this functionality, and I highly recommend getting onto a Nostr client with zap support and trying it out.
Further Reading
Like this post and want to support the series? Tip me some sats on lightning at [email protected]: