STORY
Integrating LNBits API as an Escrow Payment System for Nostrawars
AUTHOR
Joined 2022.06.19
PROJECT
DATE
VOTES
sats
COMMENTS

Integrating LNBits API as an Escrow Payment System for Nostrawars

Welcome back to my NostrHack hackathon project, Nostrawars! In my previous blog post, I talked about the NIP:04 and 3-way handshake algorithm that I've implemented in the game. Today, I'm excited to share with you the progress I've made by integrating the LNBits API into Nostrawars, and how I'm using it as an escrow payment system for the game.

The idea behind my escrow-based LN payment system for nostawars is simple:

  1. Before creating or joining a game room, both players must deposit a certain amount of sats into the system.

  2. When the game ends, the winner will receive double the amount they deposited, while the loser will receive nothing.

To make this happen, I integrated the LNBits API, which generates invoices for the deposit payments and acts as an escrow for the duration of the game.

To start playing Nostrawars, both players need to deposit the required sats. Here's what the game looks like when you first open it:

When player clicks on "Create Room" or "Join Room", both players will see the payment invoice screen:

For now, the amount to pay is hardcoded.

After paying the invoice, players need to click on the "Check Invoice Paid?" button in the top right corner of the screen. This will verify if the invoice has been paid or not, using the payment_hash of the invoice with the API endpoint (/api/v1/payments/<payment_hash>). If the payment has been made, the player will be redirected to the Create Room or Join Room screen:

Now both players are in the game room, let's copy the npub of the left one and paste it on the right:

After clicking on the "Join Room" button the 3-way handshake algorithm takes place, ensuring a secure and encrypted connection between the players. The game begins:

At the end of the game, the winner can withdraw their sats using the LNURL withdraw link that's generated by LNBits using the LNURL-withdraw extension.

The LNBits wallet acts as a third-party custodial wallet to hold the funds of the players during the game, but once the game ends, the winner can easily withdraw their sats to their own wallet.

Here's what the LNBits transaction dashboard looks like:

And here's a look at my own Munn wallet, which I used to pay the invoice and receive/withdraw the winning sats:

Ya I've been using the demo legends instance of lnbits for testing :) And the sats to withdraw is hardcoded to 15.

I know/think that this solution is not perfect, but it works for now! If you have any questions about Nostrawars or the Nostr protocol, feel free to ask in the comments section below.

Thanks for reading and enjoy playing Nostrawars!