Skip to content

Commit 691c290

Browse files
authored
Merge pull request #1005 from starius/agents
Updated README and architecture.md with Gemini, added AGENTS.md
2 parents ff91b51 + 0c88b05 commit 691c290

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

AGENTS.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
### Project Summary: Lightning Loop
2+
3+
**1. Core Purpose:**
4+
Lightning Loop is a non-custodial service from Lightning Labs for bridging the Bitcoin mainnet (on-chain) and the Lightning Network (off-chain). It enables users to rebalance their channels by moving funds into or out of Lightning — without the need for closing and reopening them. The system is architected to be non-custodial, using atomic swaps to ensure neither the user nor the server can lose funds.
5+
6+
**2. Architecture & Core Components:**
7+
The project is a client daemon (`loopd`) that connects to a user's `lnd` node and communicates with a remote Loop server via gRPC. It is composed of several distinct managers and services that handle different aspects of the swap lifecycle.
8+
9+
* **`loopd` (Daemon):** The main executable that bootstraps and orchestrates all other components. It initializes the database, connects to `lnd`, starts the gRPC/REST servers, and launches all the feature-specific managers (`liquidity`, `instantout`, `staticaddr`, etc.). Its lifecycle and configuration are managed in the `loopd/` and `cmd/loopd/` packages.
10+
11+
* **Finite State Machines (FSM):** The core logic for managing swaps is built around a robust FSM pattern (`fsm/`). Each swap type (Instant Out, Static Address Loop-in, etc.) has its own state machine that defines its lifecycle. This ensures that swaps progress through a defined set of states, with transitions triggered by on-chain or off-chain events, making the system resilient to failures and restarts.
12+
13+
* **Database (`loopdb/`):** Persistence is handled by a database layer that supports both `sqlite3` and `PostgreSQL` (via `sqlc` for query generation). There is also deprecated `bbolt` implementation which is left in the codebase to support migrating old instances. It stores swap contracts, state history, liquidity parameters, and other critical data, allowing the daemon to recover and resume operations after a restart.
14+
15+
* **Notification Manager (`notifications/`):** Manages a persistent gRPC subscription to the Loop server. It listens for asynchronous events, such as a `Reservation` being offered or a request to co-sign a `StaticAddress` sweep, and dispatches them to the appropriate internal manager.
16+
17+
* **On-Chain Transaction Management:**
18+
* **Sweeper (`sweep/`):** A utility library responsible for crafting and signing sweep transactions for various HTLC types (P2WSH, P2TR, etc.).
19+
* **Sweep Batcher (`sweepbatcher/`):** A sophisticated service that batches multiple on-chain sweeps into a single transaction to save on-chain fees. It has its own database store and logic for creating, tracking, and RBF-bumping (Replace-By-Fee) batch transactions until they are confirmed.
20+
21+
**3. Swap Types & Features:**
22+
23+
* #### Standard Loop Out
24+
* **Goal:** Move funds from Lightning to an on-chain address.
25+
* **Mechanism:** The client pays an off-chain Lightning invoice to the Loop server. In return, the server sends an equivalent amount (minus fees) to the user's on-chain address via an on-chain HTLC. The client sweeps this HTLC using the same preimage from the Lightning payment, completing the atomic swap.
26+
27+
* #### Standard Loop In
28+
* **Goal:** Move on-chain funds into the Lightning Network.
29+
* **Mechanism:** The client sends funds to an on-chain HTLC. This HTLC is spendable by the server *if* it knows the swap preimage (which it learns by paying the client's off-chain Lightning invoice), or by the client after a timeout. Once the server sees the on-chain HTLC, it pays the invoice to "loop in" the funds and then sweeps the HTLC with the revealed preimage, often in cooperation with the client to save fees.
30+
31+
* #### Instant Loop-Out (`instantout/`)
32+
* **Goal:** A faster, more efficient Loop-Out that prioritizes a cooperative path.
33+
* **Mechanism:** Built on **Reservations**—on-chain UTXOs controlled by a 2-of-2 MuSig2 key between the client and server. The primary flow is a cooperative "Sweepless Sweep" that spends the reservation directly to the client's destination. A non-cooperative fallback to a standard HTLC ensures the swap remains non-custodial.
34+
35+
* #### Static Address (`staticaddr/`)
36+
* **Goal:** Provide a permanent, reusable on-chain address for receiving funds that can then be looped into Lightning.
37+
* **Mechanism:** The address is a P2TR (Taproot) output with a cooperative MuSig2 keyspend path and a unilateral CSV timeout scriptspend path for safety. This feature is composed of several sub-managers:
38+
* `address/`: Manages the creation and lifecycle of the static address itself.
39+
* `deposit/`: Monitors the address for new UTXOs (deposits) and manages their state (e.g., `Deposited`, `Expired`, `LoopingIn`).
40+
* `loopin/`: Orchestrates the Loop-In process for funds held at the static address.
41+
* `withdraw/`: Handles cooperative withdrawal of funds from the address back to the user's wallet without performing a swap.
42+
43+
* #### Liquidity Manager (`liquidity/`)
44+
* **Goal:** Automate channel liquidity management based on user-defined rules.
45+
* **Mechanism:** Periodically assesses channel balances and can suggest or automatically dispatch swaps to meet target liquidity levels. It supports detailed rules, fee budgets, and an "easyAutoloop" mode for simple balance targeting.
46+
47+
**4. Technology & Cross-Cutting Concerns:**
48+
49+
* **Language:** Go
50+
* **Remote Communication:** gRPC. Definitions are split between `looprpc/` (client-facing) and `swapserverrpc/` (server-facing).
51+
* **Cryptography:** Extensively uses Taproot and MuSig2 for efficiency, privacy, and complex spending conditions, especially in the `instantout` and `staticaddr` features.
52+
* **Labeling (`labels/`):** A utility to create and validate labels for swaps, which helps distinguish between user-initiated and automated swaps (e.g., `[reserved]: autoloop-out`).
53+
* **Assets (`assets/`):** Contains logic for interacting with `tapd` (Taproot Assets Protocol Daemon), allowing Loop to facilitate swaps involving assets other than Bitcoin.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ bitcoin into and out of the Lightning Network.
77
- Automated channel balancing
88
- Privacy-forward non-custodial swaps
99
- Opportunistic transaction batching to save on fees
10+
- Instant Loop-Outs for faster off-chain to on-chain swaps
11+
- Static, reusable addresses for Loop-Ins
1012
- Progress monitoring of in-flight swaps
1113

1214
## Use Cases

docs/architecture.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,49 @@ Phases:
6565
| | | | | |
6666
'--------------------' '--------------' '---------------'
6767
```
68+
69+
### Standard Loop-In (on -> off-chain)
70+
71+
The reverse of a Loop-Out. The client sends funds to an on-chain HTLC with a
72+
2-of-2 MuSig2 keyspend path. Once the server detects that the HTLC transaction
73+
is confirmed, it pays the Lightning invoice provided by the client to "loop in"
74+
the funds to their node's channels and sweeps the HTLC. The client cooperates
75+
with the server to cosign the sweep transaction to save on onchain fees.
76+
77+
### Instant Loop-Out
78+
79+
A faster, more efficient Loop-Out that prioritizes a cooperative path, built on
80+
**Reservations** (on-chain UTXOs controlled by a 2-of-2 MuSig2 key between the
81+
client and server).
82+
83+
1. **Cooperative Path ("Sweepless Sweep"):** After the client pays the
84+
off-chain swap invoice, the client and server cooperatively sign a
85+
transaction that spends the reservation UTXO directly to the client's final
86+
destination address. This avoids publishing an HTLC, saving fees and chain
87+
space.
88+
89+
2. **Fallback Path:** If the cooperative path fails, the system falls back to
90+
creating a standard on-chain HTLC from the reservation UTXO, which is then
91+
swept by the client. This ensures the swap remains non-custodial.
92+
93+
### Static Address (for Loop-In)
94+
95+
Provides a permanent, reusable on-chain address for receiving funds that can
96+
then be swapped for an off-chain Lightning payment.
97+
98+
- **Address Type:** The address is a P2TR (Taproot) output with two spending
99+
paths:
100+
1. **Keyspend Path (Cooperative):** The internal key is a 2-of-2 MuSig2
101+
aggregate key held by the client and server. This path is used to
102+
cooperatively sign transactions for performing a Loop-In or withdrawing
103+
funds.
104+
2. **Scriptspend Path (Timeout):** A tapleaf contains a script that allows
105+
the client to unilaterally sweep their funds after a CSV timeout. This
106+
is a safety mechanism ensuring the client never loses access to their
107+
deposits.
108+
109+
- **Loop-In Flow:** When a user wants to loop in funds from this address, the
110+
client and server use the cooperative keyspend path to create and sign an
111+
HTLC transaction, which then follows a standard Loop-In flow. When the
112+
client gets the LN payment, they cooperate with the server to sweep the
113+
deposit directly to the server's wallet instead of publishing the HTLC tx.

0 commit comments

Comments
 (0)