Skip to content

Commit 9c23b58

Browse files
authored
Added docker compose file for btc and lnd signet. Updated readme and code demo. (#1354)
1 parent 851d902 commit 9c23b58

File tree

7 files changed

+180
-60
lines changed

7 files changed

+180
-60
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,7 @@ docfx/*
176176
.fake
177177
.ionide
178178

179-
**/appsettings.Development.json
179+
**/appsettings.Development.json
180+
**/btc-data
181+
**/lnd-data
182+
**/rtl-data

examples/WebRTCLightningExamples/README.md

100644100755
+144-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,144 @@
1-
The examples in this folder contain sample code to demonstrate WebRTC and Bitcoin Lightning interoperating.
1+
# WebRTC Lightning Payment Video Stream Demo
2+
3+
This demo project showcases a real-time WebRTC video stream whose source is controlled by Bitcoin Lightning payments. Viewers can trigger changes in the video stream by making micropayments over the Lightning Network.
4+
5+
## Overview
6+
7+
This project integrates two cutting-edge technologies:
8+
- **WebRTC Video Streaming:** Enabling low-latency, real-time video communications.
9+
- **Bitcoin Lightning Payments:** Allowing instant, low-cost micropayments that trigger changes in the video source.
10+
11+
When a viewer makes a payment, the system processes and verifies the Lightning payment. Once confirmed, the video source is updated accordingly—demonstrating a pay-per-view or dynamic content control model.
12+
13+
## Features
14+
15+
- **Real-Time Video Streaming:** Built using WebRTC for low-latency video delivery.
16+
- **Micropayment Control:** Use Bitcoin Lightning payments to trigger video content updates.
17+
- **Dynamic Integration:** Seamless integration between payment events and WebRTC streaming.
18+
- **Demo-Ready:** Easy-to-run demo for testing and showcasing the technology.
19+
20+
## Prerequisites
21+
22+
Before running the demo, ensure you have:
23+
- **Docker** and **Docker Compose** installed on your system.
24+
- A modern web browser with WebRTC support (Chrome, Firefox, etc.).
25+
26+
## Running the Demo
27+
28+
### 1. Clone the Repository
29+
30+
Clone the repository and navigate to the project directory:
31+
32+
```bash
33+
git clone [email protected]:sipsorcery-org/sipsorcery.git
34+
cd sipsorcery/examples/WebRTCLightningExamples/WebRTCLightningGetStarted
35+
```
36+
37+
### 2. Set Up the Docker Environment
38+
39+
This demo uses a `docker-compose` file to set up a Bitcoin Signet node, an LND Lightning node, and the RTL (Ride the Lightning) web interface.
40+
41+
1. **Start the Docker Containers**
42+
43+
Run the following command to start the Bitcoin Signet node, LND, and RTL:
44+
45+
```bash
46+
docker-compose up -d
47+
```
48+
49+
This will spin up three services:
50+
51+
- bitcoind: A Bitcoin Signet node.
52+
- lnd: A Lightning Network node connected to the Bitcoin Signet network.
53+
- rtl: A web interface for managing your LND node.
54+
55+
2. **Wait for Initialization**
56+
57+
The LND node will take a few minutes to sync with the Bitcoin Signet network. You can monitor the progress by checking the logs:
58+
59+
```bash
60+
docker logs -f lnd
61+
```
62+
63+
3. **Access RTL (Optional)**
64+
65+
Once the LND node is synced, you can access the RTL web interface at [http://localhost:3000](http://localhost:3000) to manage your Lightning node.
66+
67+
### 3. Configure the Demo Application
68+
69+
1. **Update appSettings.json:**
70+
71+
Ensure the LND connection settings in your appSettings.Development.json file match the credentials and ports used in the docker-compose setup. For example:
72+
73+
```json
74+
{
75+
"LndSettings": {
76+
"Url": "localhost:10009",
77+
"MacaroonPath": "../lnd-data/data/chain/bitcoin/signet/admin.macaroon",
78+
"CertificatePath": "../lnd-data/tls.cert"
79+
}
80+
}
81+
```
82+
83+
If you're using the default `docker-compose` setup, the paths and ports should already be preconfigured.
84+
85+
2. **Run the Demo Application**
86+
87+
Start the demo application using the following command:
88+
89+
```bash
90+
dotnet run
91+
```
92+
93+
### 4. Make Payments
94+
95+
1. **Scan the QR Code**
96+
97+
The video stream will display a payment request QR code when it transitions to certain states. Use a mobile Lightning Network client (e.g. Zeus) to scan the QR code and make a payment.
98+
99+
2. **Trigger Video Updates**
100+
101+
Once the payment is detected, the video source will update dynamically, demonstrating the pay-per-view or dynamic content control model.
102+
103+
### 5. Stopping the Demo
104+
105+
To stop the Docker containers, run:
106+
107+
```bash
108+
docker-compose down
109+
```
110+
111+
This will gracefully shut down the Bitcoin Signet node, LND, and RTL services.
112+
113+
### Docker Compose Configuration
114+
115+
The `docker-compose.yml` file provided sets up the following services:
116+
117+
- **Bitcoin Signet Node (bitcoind):**
118+
119+
- Runs a Bitcoin Signet node for testing purposes.
120+
- Exposes RPC and ZMQ ports for communication with LND.
121+
122+
- **Lightning Network Node (lnd):**
123+
124+
- Connects to the Bitcoin Signet node.
125+
- Exposes gRPC, REST, and peer-to-peer ports for Lightning Network operations.
126+
127+
- **RTL (Ride the Lightning) Interface (rtl):**
128+
129+
- Provides a web-based interface for managing the LND node.
130+
- Accessible at http://localhost:3000 default password `sipsorcery`.
131+
132+
For advanced configuration, refer to the comments in the `docker-compose.yml` file.
133+
134+
### Troubleshooting
135+
136+
- **LND Node Not Syncing:** Ensure the Bitcoin Signet node is fully synced before using LND. Check the logs with:
137+
138+
```bash
139+
docker logs -f bitcoind
140+
```
141+
142+
- **Connection Issues:** Verify that the `appSettings.json`, or `appSettings.Development.json`, file has the correct LND connection details.
143+
144+
- **Payment Not Triggering Video Updates:** Ensure the payment is confirmed and the demo application is correctly processing payment events.

examples/WebRTCLightningExamples/WebRTCLightningGetStarted/BackgroundServices/LndInvoiceListener.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ protected override async Task ExecuteAsync(CancellationToken cancelToken)
8484
_logger.LogError($"{nameof(LndInvoiceListener)} listener exception. {ex}");
8585
}
8686

87-
_logger.LogDebug($"{nameof(LndInvoiceListener)} delaying {RESTART_DELAY_SECONDS}s before attempting to reconnect the grpc listener.");
87+
_logger.LogDebug($"{nameof(LndInvoiceListener)} delaying {RESTART_DELAY_SECONDS}s before attempting to reconnect the grpc listener at.");
8888
await Task.Delay(1000 * RESTART_DELAY_SECONDS);
8989
}
9090
}

examples/WebRTCLightningExamples/WebRTCLightningGetStarted/ConfigKeys.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public class ConfigKeys
1919
{
2020
public const string LND_URL = "Lnd:Url";
2121

22-
public const string LND_MACAROON = "Lnd:Macaroon";
22+
public const string LND_MACAROON_PATH = "Lnd:MacaroonPath";
23+
24+
public const string LND_MACAROON_HEX = "Lnd:MacaroonHex";
2325

2426
public const string LND_CERTIFICATE_PATH = "Lnd:CertificatePath";
2527

examples/WebRTCLightningExamples/WebRTCLightningGetStarted/README.md

-47
This file was deleted.

examples/WebRTCLightningExamples/WebRTCLightningGetStarted/Services/LightningClientFactory.cs

+23-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
using Grpc.Net.Client;
1818
using Microsoft.Extensions.Configuration;
1919
using Microsoft.Extensions.Logging;
20+
using NBitcoin.DataEncoders;
2021
using System;
2122
using System.IO;
2223
using System.Linq;
@@ -42,7 +43,7 @@ public class LightningClientFactory : ILightningClientFactory
4243
private IConfiguration _config;
4344

4445
private readonly string _lndUrl;
45-
private readonly string _lndMacaroon;
46+
private readonly string _lndMacaroonHex;
4647
private readonly X509Certificate2 _lndCertificate;
4748

4849
public LightningClientFactory(
@@ -53,10 +54,28 @@ public LightningClientFactory(
5354
_config = config;
5455

5556
_lndUrl = config[ConfigKeys.LND_URL] ?? string.Empty;
56-
_lndMacaroon = config[ConfigKeys.LND_MACAROON] ?? string.Empty;
57+
_lndMacaroonHex = LoadLndMacaroonAsHex();
5758
_lndCertificate = LoadLndPublicCertificate() ?? new X509Certificate2([]);
59+
}
60+
61+
private string LoadLndMacaroonAsHex()
62+
{
63+
var macaroonFilePath = _config[ConfigKeys.LND_MACAROON_PATH];
64+
65+
if (!string.IsNullOrWhiteSpace(macaroonFilePath))
66+
{
67+
if(!File.Exists(macaroonFilePath))
68+
{
69+
_logger.LogError($"Macaroon file does not exist at {macaroonFilePath}.");
70+
}
71+
else
72+
{
73+
var macaroonBytes = File.ReadAllBytes(macaroonFilePath);
74+
return Encoders.Hex.EncodeData(macaroonBytes);
75+
}
76+
}
5877

59-
//_logger.LogInformation($"{nameof(LightningClientFactory)} initialised with url {_lndUrl} and certificate thumbprint {_lndCertificate.Thumbprint}.");
78+
return _config[ConfigKeys.LND_MACAROON_HEX] ?? string.Empty;
6079
}
6180

6281
private X509Certificate2? LoadLndPublicCertificate()
@@ -144,7 +163,7 @@ public Routerrpc.Router.RouterClient GetRouterClient()
144163

145164
private Task AddMacaroon(AuthInterceptorContext context, Metadata metadata)
146165
{
147-
metadata.Add(new Metadata.Entry("macaroon", _lndMacaroon));
166+
metadata.Add(new Metadata.Entry("macaroon", _lndMacaroonHex));
148167
return Task.CompletedTask;
149168
}
150169

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"Lnd": {
3-
//"Url": "https://x.x.x.x:10009",
4-
// To get the macaroon in base64 format, run the following command: xxd -p -c 1000 admin.macaroon | tr -d '\n'
5-
//"Macaroon": "" // Secret store.
6-
//"CertificatePath": "c:/dev/certs/lnd-tls.cert",
3+
"Url": "https://localhost:10009",
4+
"MacaroonPath": "../lnd-data/data/chain/bitcoin/signet/admin.macaroon",
5+
// To get the macaroon in hex format, run the following command: xxd -p -c 1000 admin.macaroon | tr -d '\n'
6+
//"MacaroonHex": "",
7+
"CertificatePath": "../lnd-data/tls.cert"
78
// To get the certificate in base64 format, run the following command: cat .lnd/tls.cert | base64 -w 0
89
// "CertificateBase64": "base64 cert"
9-
"DefaultInvoiceExpirySeconds": 900
1010
}
1111
}

0 commit comments

Comments
 (0)