ln_gateway/lightning/
cln.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use std::fmt::Debug;
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use bitcoin::Address;
use fedimint_core::encoding::Encodable;
use fedimint_core::task::{sleep, TaskGroup};
use fedimint_core::util::SafeUrl;
use fedimint_core::{secp256k1, Amount, BitcoinAmountOrAll};
use fedimint_ln_common::PrunedInvoice;
use tonic::transport::{Channel, Endpoint};
use tonic::Request;
use tracing::info;

use super::{ChannelInfo, ILnRpcClient, LightningRpcError, RouteHtlcStream};
use crate::gateway_lnrpc::gateway_lightning_client::GatewayLightningClient;
use crate::gateway_lnrpc::{
    self, CloseChannelsWithPeerRequest, CloseChannelsWithPeerResponse, CreateInvoiceRequest,
    CreateInvoiceResponse, EmptyRequest, EmptyResponse, GetBalancesResponse,
    GetLnOnchainAddressResponse, GetNodeInfoResponse, GetRouteHintsRequest, GetRouteHintsResponse,
    InterceptHtlcResponse, OpenChannelRequest, OpenChannelResponse, PayInvoiceResponse,
    PayPrunedInvoiceRequest, WithdrawOnchainRequest, WithdrawOnchainResponse,
};
use crate::lightning::MAX_LIGHTNING_RETRIES;

/// An `ILnRpcClient` that wraps around `GatewayLightningClient` for
/// convenience, and makes real RPC requests over the wire to a remote lightning
/// node. The lightning node is exposed via a corresponding
/// `GatewayLightningServer`.
#[derive(Debug)]
pub struct NetworkLnRpcClient {
    connection_url: SafeUrl,
}

impl NetworkLnRpcClient {
    pub fn new(url: SafeUrl) -> Self {
        info!(
            "Gateway configured to connect to remote LnRpcClient at \n cln extension address: {} ",
            url.to_string()
        );
        NetworkLnRpcClient {
            connection_url: url,
        }
    }

    async fn connect(&self) -> Result<GatewayLightningClient<Channel>, LightningRpcError> {
        let mut retries = 0;
        let client = loop {
            if retries >= MAX_LIGHTNING_RETRIES {
                return Err(LightningRpcError::FailedToConnect);
            }

            retries += 1;

            if let Ok(endpoint) = Endpoint::from_shared(self.connection_url.to_string()) {
                if let Ok(client) = GatewayLightningClient::connect(endpoint.clone()).await {
                    break client;
                }
            }

            tracing::debug!("Couldn't connect to CLN extension, retrying in 1 second...");
            sleep(Duration::from_secs(1)).await;
        };

        Ok(client)
    }
}

#[async_trait]
impl ILnRpcClient for NetworkLnRpcClient {
    async fn info(&self) -> Result<GetNodeInfoResponse, LightningRpcError> {
        let req = Request::new(EmptyRequest {});
        let mut client = self.connect().await?;
        let res = client.get_node_info(req).await.map_err(|status| {
            LightningRpcError::FailedToGetNodeInfo {
                failure_reason: status.message().to_string(),
            }
        })?;
        Ok(res.into_inner())
    }

    async fn routehints(
        &self,
        num_route_hints: usize,
    ) -> Result<GetRouteHintsResponse, LightningRpcError> {
        let req = Request::new(GetRouteHintsRequest {
            num_route_hints: num_route_hints as u64,
        });
        let mut client = self.connect().await?;
        let res = client.get_route_hints(req).await.map_err(|status| {
            LightningRpcError::FailedToGetRouteHints {
                failure_reason: status.message().to_string(),
            }
        })?;
        Ok(res.into_inner())
    }

    async fn pay_private(
        &self,
        invoice: PrunedInvoice,
        max_delay: u64,
        max_fee: Amount,
    ) -> Result<PayInvoiceResponse, LightningRpcError> {
        let req = Request::new(PayPrunedInvoiceRequest {
            pruned_invoice: Some(gateway_lnrpc::PrunedInvoice {
                amount_msat: invoice.amount.msats,
                destination: invoice.destination.consensus_encode_to_vec(),
                destination_features: invoice.destination_features,
                payment_hash: invoice.payment_hash.consensus_encode_to_vec(),
                payment_secret: invoice.payment_secret.to_vec(),
                min_final_cltv_delta: invoice.min_final_cltv_delta,
            }),
            max_delay,
            max_fee_msat: max_fee.msats,
        });
        let mut client = self.connect().await?;
        let res = client.pay_pruned_invoice(req).await.map_err(|status| {
            LightningRpcError::FailedPayment {
                failure_reason: status.message().to_string(),
            }
        })?;
        Ok(res.into_inner())
    }

    fn supports_private_payments(&self) -> bool {
        true
    }

    async fn route_htlcs<'a>(
        self: Box<Self>,
        _task_group: &TaskGroup,
    ) -> Result<(RouteHtlcStream<'a>, Arc<dyn ILnRpcClient>), LightningRpcError> {
        let mut client = self.connect().await?;
        let res = client
            .route_htlcs(EmptyRequest {})
            .await
            .map_err(|status| LightningRpcError::FailedToRouteHtlcs {
                failure_reason: status.message().to_string(),
            })?;
        Ok((
            Box::pin(res.into_inner()),
            Arc::new(Self::new(self.connection_url.clone())),
        ))
    }

    async fn complete_htlc(
        &self,
        htlc: InterceptHtlcResponse,
    ) -> Result<EmptyResponse, LightningRpcError> {
        let mut client = self.connect().await?;
        let res = client.complete_htlc(htlc).await.map_err(|status| {
            LightningRpcError::FailedToCompleteHtlc {
                failure_reason: status.message().to_string(),
            }
        })?;
        Ok(res.into_inner())
    }

    async fn create_invoice(
        &self,
        create_invoice_request: CreateInvoiceRequest,
    ) -> Result<CreateInvoiceResponse, LightningRpcError> {
        let mut client = self.connect().await?;
        let res = client
            .create_invoice(create_invoice_request)
            .await
            .map_err(|status| LightningRpcError::FailedToGetInvoice {
                failure_reason: status.message().to_string(),
            })?;
        Ok(res.into_inner())
    }

    async fn get_ln_onchain_address(
        &self,
    ) -> Result<GetLnOnchainAddressResponse, LightningRpcError> {
        let mut client = self.connect().await?;
        let res = client
            .get_ln_onchain_address(EmptyRequest {})
            .await
            .map_err(|status| LightningRpcError::FailedToGetLnOnchainAddress {
                failure_reason: status.message().to_string(),
            })?;
        Ok(res.into_inner())
    }

    async fn withdraw_onchain(
        &self,
        address: Address,
        amount: BitcoinAmountOrAll,
        fee_rate_sats_per_vbyte: u64,
    ) -> Result<WithdrawOnchainResponse, LightningRpcError> {
        let mut client = self.connect().await?;
        let res = client
            .withdraw_onchain(WithdrawOnchainRequest {
                address: address.to_string(),
                amount_sats: match amount {
                    // This leaves the field empty, which is interpreted as "all funds".
                    BitcoinAmountOrAll::All => None,
                    BitcoinAmountOrAll::Amount(amount) => Some(amount.to_sat()),
                },
                fee_rate_sats_per_vbyte,
            })
            .await
            .map_err(|status| LightningRpcError::FailedToWithdrawOnchain {
                failure_reason: status.message().to_string(),
            })?;
        Ok(res.into_inner())
    }

    async fn open_channel(
        &self,
        pubkey: secp256k1::PublicKey,
        host: String,
        channel_size_sats: u64,
        push_amount_sats: u64,
    ) -> Result<OpenChannelResponse, LightningRpcError> {
        let mut client = self.connect().await?;

        let res = client
            .open_channel(OpenChannelRequest {
                pubkey: pubkey.serialize().to_vec(),
                host,
                channel_size_sats,
                push_amount_sats,
            })
            .await
            .map_err(|status| LightningRpcError::FailedToOpenChannel {
                failure_reason: status.message().to_string(),
            })?;
        Ok(res.into_inner())
    }

    async fn close_channels_with_peer(
        &self,
        pubkey: secp256k1::PublicKey,
    ) -> Result<CloseChannelsWithPeerResponse, LightningRpcError> {
        let mut client = self.connect().await?;
        let res = client
            .close_channels_with_peer(CloseChannelsWithPeerRequest {
                pubkey: pubkey.serialize().to_vec(),
            })
            .await
            .map_err(|status| LightningRpcError::FailedToCloseChannelsWithPeer {
                failure_reason: status.message().to_string(),
            })?;
        Ok(res.into_inner())
    }

    async fn list_active_channels(&self) -> Result<Vec<ChannelInfo>, LightningRpcError> {
        let mut client = self.connect().await?;
        let res = client
            .list_active_channels(EmptyRequest {})
            .await
            .map_err(|status| LightningRpcError::FailedToListActiveChannels {
                failure_reason: status.message().to_string(),
            })?;
        Ok(res
            .into_inner()
            .channels
            .into_iter()
            .map(|channel| ChannelInfo {
                remote_pubkey: secp256k1::PublicKey::from_slice(&channel.remote_pubkey)
                    .expect("Lightning node returned invalid remote channel pubkey"),
                channel_size_sats: channel.channel_size_sats,
                outbound_liquidity_sats: channel.outbound_liquidity_sats,
                inbound_liquidity_sats: channel.inbound_liquidity_sats,
                short_channel_id: channel.short_channel_id,
            })
            .collect())
    }

    async fn get_balances(&self) -> Result<GetBalancesResponse, LightningRpcError> {
        let mut client = self.connect().await?;

        Ok(client
            .get_balances(EmptyRequest {})
            .await
            .map_err(|status| LightningRpcError::FailedToGetBalances {
                failure_reason: status.message().to_string(),
            })?
            .into_inner())
    }

    async fn sync_to_chain(&self, _block_height: u32) -> Result<EmptyResponse, LightningRpcError> {
        // We don't need to do anything here, as CLN automatically syncs to chain.
        Ok(EmptyResponse {})
    }
}