|
| 1 | +# LSPS4 + BOLT12 JIT Channel Implementation Plan |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | +Enable BOLT12 offers to work with LSPS4 JIT channels for nodes that have no existing channels. |
| 5 | + |
| 6 | +## Background |
| 7 | + |
| 8 | +### How BOLT12 Works |
| 9 | +1. Recipient creates an **Offer** (contains blinded MESSAGE paths for receiving invoice requests) |
| 10 | +2. Payer sends **InvoiceRequest** via onion message to recipient |
| 11 | +3. Recipient responds with **Bolt12Invoice** (contains blinded PAYMENT paths) |
| 12 | +4. Payer pays the invoice using the blinded payment paths |
| 13 | + |
| 14 | +### How LSPS4 Works |
| 15 | +1. Client registers with LSP, receives `intercept_scid` and `cltv_expiry_delta` |
| 16 | +2. Client creates invoices with route hint pointing to LSP via `intercept_scid` |
| 17 | +3. When payment arrives at `intercept_scid`, LSP intercepts HTLC and opens JIT channel |
| 18 | +4. LSP forwards payment to client |
| 19 | + |
| 20 | +### The Gap |
| 21 | +- **Message paths**: Work fine - LSP is a connected peer, can forward onion messages |
| 22 | +- **Payment paths**: Fail - `Router::create_blinded_payment_paths` requires usable channels |
| 23 | + |
| 24 | +## Solution |
| 25 | + |
| 26 | +### Architecture |
| 27 | +Create a wrapper router (`LSPS4Router`) that: |
| 28 | +1. Delegates to `DefaultRouter` for normal operations |
| 29 | +2. Falls back to LSPS4 blinded payment paths when no channels exist |
| 30 | + |
| 31 | +### Shared State |
| 32 | +```rust |
| 33 | +pub struct LSPS4BlindedPathConfig { |
| 34 | + pub lsp_node_id: PublicKey, |
| 35 | + pub intercept_scid: u64, |
| 36 | + pub cltv_expiry_delta: u32, |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +This state is: |
| 41 | +- Written by: LiquiditySource after LSPS4 registration |
| 42 | +- Read by: LSPS4Router when creating blinded payment paths |
| 43 | + |
| 44 | +### LSPS4Router Implementation |
| 45 | +```rust |
| 46 | +pub struct LSPS4Router<G, L, ES, S, SP, Sc> { |
| 47 | + inner: DefaultRouter<G, L, ES, S, SP, Sc>, |
| 48 | + lsps4_config: Arc<RwLock<Option<LSPS4BlindedPathConfig>>>, |
| 49 | + entropy_source: ES, |
| 50 | +} |
| 51 | + |
| 52 | +impl Router for LSPS4Router { |
| 53 | + fn create_blinded_payment_paths(...) -> Result<Vec<BlindedPaymentPath>, ()> { |
| 54 | + // 1. Try normal path creation |
| 55 | + if let Ok(paths) = self.inner.create_blinded_payment_paths(...) { |
| 56 | + if !paths.is_empty() { |
| 57 | + return Ok(paths); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + // 2. Fall back to LSPS4 if configured |
| 62 | + if let Some(config) = self.lsps4_config.read().unwrap().as_ref() { |
| 63 | + return self.create_lsps4_blinded_path(config, recipient, tlvs, ...); |
| 64 | + } |
| 65 | + |
| 66 | + Err(()) |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +### LSPS4 Blinded Path Creation |
| 72 | +```rust |
| 73 | +fn create_lsps4_blinded_path(&self, config: &LSPS4BlindedPathConfig, ...) { |
| 74 | + let forward_node = PaymentForwardNode { |
| 75 | + node_id: config.lsp_node_id, |
| 76 | + tlvs: ForwardTlvs { |
| 77 | + short_channel_id: config.intercept_scid, |
| 78 | + payment_relay: PaymentRelay { |
| 79 | + cltv_expiry_delta: config.cltv_expiry_delta as u16, |
| 80 | + fee_base_msat: 0, // LSPS4 charges via channel opening |
| 81 | + fee_proportional_millionths: 0, |
| 82 | + }, |
| 83 | + payment_constraints: PaymentConstraints { |
| 84 | + max_cltv_expiry: tlvs.tlvs().payment_constraints.max_cltv_expiry + config.cltv_expiry_delta, |
| 85 | + htlc_minimum_msat: 0, |
| 86 | + }, |
| 87 | + next_blinding_override: None, |
| 88 | + features: BlindedHopFeatures::empty(), |
| 89 | + }, |
| 90 | + htlc_maximum_msat: u64::MAX, |
| 91 | + }; |
| 92 | + |
| 93 | + BlindedPaymentPath::new( |
| 94 | + &[forward_node], |
| 95 | + recipient, |
| 96 | + tlvs, |
| 97 | + u64::MAX, |
| 98 | + MIN_FINAL_CLTV_EXPIRY_DELTA, |
| 99 | + &self.entropy_source, |
| 100 | + secp_ctx, |
| 101 | + ).map(|path| vec![path]) |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +## Implementation Steps |
| 106 | + |
| 107 | +### Step 1: Create router module (src/router.rs) - DONE |
| 108 | +- [x] Define `LSPS4BlindedPathConfig` struct |
| 109 | +- [x] Create `LSPS4Router` struct wrapping `DefaultRouter` |
| 110 | +- [x] Implement `Router` trait for `LSPS4Router` |
| 111 | +- [x] Add LSPS4 blinded path fallback logic |
| 112 | + |
| 113 | +### Step 2: Update types.rs - DONE |
| 114 | +- [x] Change `Router` type alias to use `LSPS4Router` |
| 115 | +- [x] Export `LSPS4BlindedPathConfig` |
| 116 | + |
| 117 | +### Step 3: Update builder.rs - DONE |
| 118 | +- [x] Create `LSPS4BlindedPathConfig` shared state |
| 119 | +- [x] Create `LSPS4Router` instead of `DefaultRouter` |
| 120 | +- [x] Pass shared state to both router and liquidity source |
| 121 | + |
| 122 | +### Step 4: Update liquidity.rs - DONE |
| 123 | +- [x] Add method to set LSPS4 config after registration |
| 124 | +- [x] Store reference to shared config |
| 125 | +- [x] Update config when LSPS4 registration succeeds |
| 126 | +- [x] Add `lsps4_register_for_bolt12()` method for BOLT12 use case |
| 127 | + |
| 128 | +### Step 5: Add receive_via_lsps4_jit_channel to Bolt12Payment - DONE |
| 129 | +- [x] Add required fields to Bolt12Payment (runtime, connection_manager, liquidity_source, peer_store) |
| 130 | +- [x] Update constructor and call sites in lib.rs |
| 131 | +- [x] Add `receive_via_lsps4_jit_channel` method |
| 132 | +- [x] Add `receive_variable_amount_via_lsps4_jit_channel` method |
| 133 | + |
| 134 | +### Step 6: Testing - DONE |
| 135 | +- [ ] Unit test for LSPS4Router fallback (optional) |
| 136 | +- [x] Integration test for full BOLT12 + LSPS4 flow (`lsps4_bolt12_jit_channel` in integration_tests_rust.rs) |
| 137 | + |
| 138 | +## Files Modified |
| 139 | + |
| 140 | +1. **NEW: src/router.rs** - LSPS4Router implementation |
| 141 | +2. **src/types.rs** - Update Router type alias |
| 142 | +3. **src/builder.rs** - Wire up LSPS4Router and shared state |
| 143 | +4. **src/liquidity.rs** - Populate LSPS4 config after registration, add lsps4_register_for_bolt12() |
| 144 | +5. **src/payment/bolt12.rs** - Add receive_via_lsps4_jit_channel methods |
| 145 | +6. **src/lib.rs** - Add router module, update Bolt12Payment construction |
| 146 | + |
| 147 | +## Dependencies |
| 148 | + |
| 149 | +From rust-lightning: |
| 150 | +- `lightning::blinded_path::payment::{BlindedPaymentPath, PaymentForwardNode, ForwardTlvs, PaymentRelay, PaymentConstraints}` |
| 151 | +- `lightning::routing::router::Router` |
| 152 | +- `lightning::types::features::BlindedHopFeatures` |
| 153 | +- `lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA` |
| 154 | + |
| 155 | +## Notes |
| 156 | + |
| 157 | +### Normal BOLT12 still works |
| 158 | +The `LSPS4Router` first tries normal path creation via `DefaultRouter`. LSPS4 fallback only triggers when: |
| 159 | +1. Normal path creation fails or returns empty, AND |
| 160 | +2. LSPS4 config is populated |
| 161 | + |
| 162 | +So existing BOLT12 functionality is completely preserved for nodes with channels. |
0 commit comments