|
| 1 | +Define the minimal extension that enables atomic swaps even over a 2-hop path `u → m → v`. |
| 2 | + |
| 3 | +## Motivation |
| 4 | + |
| 5 | +PeerSwap can perform atomic swap between direct nodes. However, in real networks many two-hop paths exist where exactly one intermediary node forms the route, `u → m → v`. We therefore want to enable swaps over these paths as well. |
| 6 | + |
| 7 | +* Release liquidity without opening additional channels |
| 8 | +* No cooperation or trust required from the relay node m |
| 9 | +* Keep using the existing one-hop protocol on the endpoint nodes u and v as is |
| 10 | + |
| 11 | +## Network Model |
| 12 | + |
| 13 | +``` |
| 14 | +u──ch₁──m──ch₂──v |
| 15 | +↕ p2p (tcp) ↕ |
| 16 | +``` |
| 17 | + |
| 18 | +* `ch₁` and `ch₂` are public LN channels |
| 19 | +* `u` and `v` are already connected via p2p (TCP) and can exchange BOLT#1 custom messages |
| 20 | +* It is irrelevant whether the relay node `m` supports PeerSwap or not |
| 21 | + |
| 22 | +**Notation** |
| 23 | + |
| 24 | +* `u`: initiator |
| 25 | +* `m`: intermediary |
| 26 | +* `v`: responder |
| 27 | + |
| 28 | +## Messages |
| 29 | + |
| 30 | +Policy |
| 31 | + |
| 32 | +* No new message types are introduced. |
| 33 | +* The existing JSON messages are extended with additional optional fields. |
| 34 | +* The semantics of existing fields are unchanged. |
| 35 | +* All added fields are OPTIONAL; nodes that do not understand them simply ignore them. |
| 36 | +* The JSON container twohop indicates “2-hop discovery mode”; the message is interpreted as 2-hop only when this container is present. |
| 37 | + |
| 38 | +Affected messages (type numbers stay the same) |
| 39 | + |
| 40 | +* swap_in_request (JSON, type = 42069) |
| 41 | +* swap_in_agreement (JSON, type = 42073) |
| 42 | +* swap_out_request (JSON, type = 42071) |
| 43 | +* swap_out_agreement (JSON, type = 42075) |
| 44 | +* opening_tx_broadcasted and the rest remain unchanged |
| 45 | + |
| 46 | +### `swap_out_request` |
| 47 | + |
| 48 | +Additional Fields |
| 49 | + |
| 50 | +* `twohop`: object — A container that signals 2-hop discovery mode. |
| 51 | + The receiver treats the message as 2-hop only when this object is present. |
| 52 | + * `twohop.intermediary_pubkey`: string (33-byte compressed pubkey, hex) — |
| 53 | + the pubkey of the intermediary node **m** |
| 54 | + |
| 55 | +Behaviour |
| 56 | + |
| 57 | +* When `twohop` is present, the receiver **v** derives its local channel |
| 58 | + **`ch₂` (m–v)** from `intermediary_pubkey`, computes its own |
| 59 | + `receivable_msat`, and decides whether the requested amount is within the |
| 60 | + executable range. |
| 61 | + |
| 62 | +Compatibility |
| 63 | + |
| 64 | +* A legacy node that does not understand `twohop` ignores the unknown field and |
| 65 | + will safely reject the request for ordinary reasons (e.g. the `scid` |
| 66 | + is not a direct channel or `amount = 0`). |
| 67 | + |
| 68 | +### `swap_out_agreement` |
| 69 | + |
| 70 | +Additional Fields (all OPTIONAL) |
| 71 | + |
| 72 | +* `twohop`: object — The result of 2-hop discovery. |
| 73 | + * `twohop.incoming_scid`: string (e.g. `"x:y:z"`) — |
| 74 | + the short_channel_id of **`ch₂` (m–v)** |
| 75 | + |
| 76 | +Behaviour |
| 77 | + |
| 78 | +* If `twohop` is present, the receiver **u** pays the `payreq` via a 2-hop route |
| 79 | + that includes `incoming_scid`. |
| 80 | + |
| 81 | +Notes |
| 82 | + |
| 83 | +* Route hints are **not** used; the sender pins the route itself (doable with |
| 84 | + standard LN APIs). |
| 85 | +* Route hints cannot reliably force a fixed route. |
| 86 | + |
| 87 | +### `swap_in_request` |
| 88 | + |
| 89 | +Additional Fields |
| 90 | + |
| 91 | +* `twohop`: object — Container that signals 2-hop discovery mode. |
| 92 | + * `twohop.intermediary_pubkey`: string (33-byte compressed pubkey, hex) — |
| 93 | + the pubkey of the intermediary node **m** |
| 94 | + |
| 95 | +Behaviour |
| 96 | + |
| 97 | +* When `twohop` is present, the receiver **v** identifies its local |
| 98 | + **`ch₂` (m–v)** from `intermediary_pubkey` and checks whether the requested |
| 99 | + **`amount`** can be sent. |
| 100 | + |
| 101 | +Compatibility |
| 102 | + |
| 103 | +* A legacy node that does not understand `twohop` ignores the field and rejects |
| 104 | + the request using the current rules (`scid`, `amount`, etc.). |
| 105 | + |
| 106 | +### `swap_in_agreement` |
| 107 | + |
| 108 | +Additional Fields (all OPTIONAL) |
| 109 | + |
| 110 | +* `twohop`: object — The result of 2-hop discovery. |
| 111 | + * `twohop.incoming_scid`: string (e.g. `"x:y:z"`) — |
| 112 | + the short_channel_id of **`ch₂` (m–v)**, viewed from **v** (m → v) |
| 113 | + |
| 114 | +Behaviour |
| 115 | + |
| 116 | +* If `twohop` is present, the responder **v**, when making the payment, verifies |
| 117 | + that the receivable amount on `incoming_scid`** is sufficient for |
| 118 | + **`amount`. |
| 119 | + |
| 120 | +## Doing the Swap |
| 121 | + |
| 122 | +* When `twohop` is present, the swap maker locates its local |
| 123 | + **`ch₁` (u–m)** using `intermediary_pubkey`, then pays the `payreq` over the |
| 124 | + 2-hop route that includes `scid`. |
| 125 | + |
| 126 | +## Extension of the `poll` Message (Implemented; optional to rely on) |
| 127 | + |
| 128 | +### Purpose |
| 129 | +Let the intermediary **m** periodically advertise its own channel adjacency list |
| 130 | +(public + active channels from **m** to immediate neighbors). |
| 131 | +Doing so makes it easier for **u** and **v** to discover potential 2-hop routes. |
| 132 | + |
| 133 | +### Method |
| 134 | +Add an optional `channel_adjacency` object to the existing `poll` JSON. |
| 135 | +Nodes that do not understand the field simply ignore it, so backward compatibility is preserved. |
| 136 | +In most cases it is sufficient to send it on the same schedule as the ordinary `poll`. |
| 137 | + |
| 138 | +* The broadcast interval is fixed; freshness is **not** guaranteed. |
| 139 | +* Because there is no defence against false reports, using this extension is **optional**. |
| 140 | +* When the swap is actually executed, the amount is finalised with the 2-hop discovery step. |
| 141 | +* Legacy note: older drafts used the name `neighbors_ad` with keys |
| 142 | + `v/public_only/limit/entries`. This repository accepts both representations on |
| 143 | + receive, but emits `channel_adjacency` going forward. |
| 144 | + |
| 145 | +Example of the extended `poll` message: |
| 146 | + |
| 147 | +```json |
| 148 | +{ |
| 149 | + "version": 5, |
| 150 | + "assets": ["BTC", "LBTC"], |
| 151 | + "peer_allowed": true, |
| 152 | + "btc_swap_in_premium_rate_ppm": 100, |
| 153 | + "btc_swap_out_premium_rate_ppm": 200, |
| 154 | + "lbtc_swap_in_premium_rate_ppm": 50, |
| 155 | + "lbtc_swap_out_premium_rate_ppm": 150, |
| 156 | + "channel_adjacency": { |
| 157 | + "schema_version": 1, |
| 158 | + "public_channels_only": true, |
| 159 | + "max_neighbors": 20, |
| 160 | + "truncated": false, |
| 161 | + "neighbors": [ |
| 162 | + { |
| 163 | + "node_id": "<pubkey-of-neighbor>", |
| 164 | + "channels": [ |
| 165 | + { |
| 166 | + "channel_id": 1234567890, |
| 167 | + "short_channel_id": "1x2x3", |
| 168 | + "active": true |
| 169 | + } |
| 170 | + ] |
| 171 | + } |
| 172 | + ] |
| 173 | + } |
| 174 | +} |
| 175 | +``` |
| 176 | + |
| 177 | +### Sending policy |
| 178 | +When transmitting, the LN node extracts public and active neighbours from its channel list. |
| 179 | +By default no balance information is sent (privacy). |
| 180 | +If the number of neighbours exceeds `max_neighbors`, prune deterministically and set `truncated=true`. |
| 181 | +Send triggers: the regular `poll` interval and the reply to an initial `request_poll`. |
| 182 | + |
| 183 | +### Receiver behaviour |
| 184 | +A listener can build a list of 2-hop candidates — even to nodes with which it does **not** share a direct channel. |
| 185 | + |
| 186 | +In this repository, the last received `channel_adjacency` is stored in the peersync database and is surfaced via `listpeers` as `channel_adjacency`. |
| 187 | + |
| 188 | +Conceptual example of a 2-hop candidate list: |
| 189 | + |
| 190 | +```json |
| 191 | +[ |
| 192 | + { |
| 193 | + "nodeid": "<v_pubkey>", |
| 194 | + "intermediary_nodeid": "<m_pubkey>", |
| 195 | + "outgoing_scid": "<ch1 u–m>", |
| 196 | + "incoming_scid": "<ch2 m–v>", |
| 197 | + "spendable_msat": 0, |
| 198 | + "receivable_msat": 0, |
| 199 | + "sent": { |
| 200 | + "total_swaps_out": 2, |
| 201 | + "total_swaps_in": 1, |
| 202 | + "total_sats_swapped_out": 5300000, |
| 203 | + "total_sats_swapped_in": 302938 |
| 204 | + }, |
| 205 | + "received": { |
| 206 | + "total_swaps_out": 1, |
| 207 | + "total_swaps_in": 0, |
| 208 | + "total_sats_swapped_out": 2400000, |
| 209 | + "total_sats_swapped_in": 0 |
| 210 | + }, |
| 211 | + "total_fee_paid": 6082, |
| 212 | + "swap_in_premium_rate_ppm": 100, |
| 213 | + "swap_out_premium_rate_ppm": 100 |
| 214 | + } |
| 215 | +] |
| 216 | +``` |
| 217 | + |
| 218 | +When actually executing a swap, the implementation can pick transparently from this list, but — given equal conditions — a direct 1-hop path should always be preferred. |
| 219 | + |
| 220 | +## Peer-Discovery Strategy |
| 221 | + |
| 222 | +The `poll` extension makes it easier to estimate whether a 2-hop swap is possible and what the rough capacity might be. |
| 223 | +Even without it, discovery can still be done by probing directly, but this is less efficient. |
| 224 | + |
| 225 | +| Strategy | support required on m? | Pros | Cons | |
| 226 | +| ------------------------------------------------------------ | ---------------------- | ---------------- | -------------------------------------- | |
| 227 | +| A – Poll extension (periodic broadcast of `connected_peers`) | yes | Low latency | Useless if m does not support PeerSwap | |
| 228 | +| B – Direct probe | no | Works everywhere | Less efficient | |
0 commit comments