breez_sdk_core/greenlight/
node_api.rs

1use std::cmp::{min, Reverse};
2use std::collections::{HashMap, HashSet};
3use std::iter::Iterator;
4use std::pin::Pin;
5use std::str::FromStr;
6use std::sync::atomic::{AtomicU16, Ordering};
7use std::sync::Arc;
8use std::time::{Duration, SystemTime, UNIX_EPOCH};
9
10use anyhow::{anyhow, Result};
11use ecies::symmetric::{sym_decrypt, sym_encrypt};
12use futures::{Future, Stream};
13use gl_client::credentials::{Device, Nobody};
14use gl_client::node;
15use gl_client::node::ClnClient;
16use gl_client::pb::cln::delinvoice_request::DelinvoiceStatus;
17use gl_client::pb::cln::listinvoices_invoices::ListinvoicesInvoicesStatus;
18use gl_client::pb::cln::listinvoices_request::ListinvoicesIndex;
19use gl_client::pb::cln::listpays_pays::ListpaysPaysStatus;
20use gl_client::pb::cln::listsendpays_request::ListsendpaysIndex;
21use gl_client::pb::cln::{
22    self, Amount, ChannelState::*, DelinvoiceRequest, GetrouteRequest, GetrouteRoute,
23    ListchannelsRequest, ListclosedchannelsClosedchannels, ListpaysPays, ListpeerchannelsChannels,
24    ListsendpaysPayments, PreapproveinvoiceRequest, SendpayRequest, SendpayRoute,
25    WaitsendpayRequest,
26};
27use gl_client::pb::{incoming_payment, TrampolinePayRequest};
28use gl_client::scheduler::Scheduler;
29use gl_client::signer::model::greenlight::{amount, scheduler};
30use gl_client::signer::Signer;
31use sdk_common::prelude::*;
32use serde::{Deserialize, Serialize};
33use serde_json::{json, Map, Value};
34use strum_macros::{Display, EnumString};
35use tokio::join;
36use tokio::sync::{mpsc, watch, Mutex};
37use tokio::time::{sleep, Instant, MissedTickBehavior};
38use tokio_stream::StreamExt;
39
40use crate::bitcoin::bech32::{u5, ToBase32};
41use crate::bitcoin::bip32::{ChildNumber, ExtendedPrivKey};
42use crate::bitcoin::blockdata::constants::WITNESS_SCALE_FACTOR;
43use crate::bitcoin::hashes::Hash;
44use crate::bitcoin::secp256k1::ecdsa::{RecoverableSignature, RecoveryId};
45use crate::bitcoin::secp256k1::PublicKey;
46use crate::bitcoin::secp256k1::Secp256k1;
47use crate::bitcoin::{
48    Address, OutPoint, ScriptBuf, Sequence, Transaction, TxIn, TxOut, Txid, Witness,
49};
50use crate::lightning::util::message_signing::verify;
51use crate::lightning_invoice::{RawBolt11Invoice, SignedRawBolt11Invoice};
52use crate::node_api::{
53    CreateInvoiceRequest, FetchBolt11Result, IncomingPayment, NodeAPI, NodeError, NodeResult,
54};
55use crate::persist::cache::NodeStateStorage;
56use crate::persist::db::SqliteStorage;
57use crate::persist::send_pays::{SendPay, SendPayStatus};
58use crate::{models::*, LspInformation};
59use crate::{NodeConfig, PrepareRedeemOnchainFundsRequest, PrepareRedeemOnchainFundsResponse};
60
61const KEY_GL_CREDENTIALS: &str = "gl_credentials";
62const MAX_PAYMENT_AMOUNT_MSAT: u64 = 4294967000;
63const MAX_INBOUND_LIQUIDITY_MSAT: u64 = 4000000000;
64const TRAMPOLINE_BASE_FEE_MSAT: u64 = 4000;
65const TRAMPOLINE_FEE_PPM: u64 = 5000;
66const PAYMENT_STATE_PENDING: u8 = 1;
67const PAYMENT_STATE_COMPLETE: u8 = 2;
68const PAYMENT_STATE_FAILED: u8 = 4;
69
70pub(crate) struct Greenlight {
71    sdk_config: Config,
72    signer: Mutex<Arc<Signer>>,
73    device: Device,
74    gl_client: Mutex<Option<node::Client>>,
75    node_client: Mutex<Option<ClnClient>>,
76    persister: Arc<SqliteStorage>,
77    inprogress_payments: AtomicU16,
78}
79
80#[derive(Serialize, Deserialize)]
81struct InvoiceLabel {
82    pub unix_milli: u128,
83    pub payer_amount_msat: Option<u64>,
84}
85
86#[derive(Serialize, Deserialize)]
87struct PaymentLabel {
88    pub unix_nano: u128,
89    pub trampoline: bool,
90    pub client_label: Option<String>,
91    pub amount_msat: u64,
92}
93
94impl Greenlight {
95    /// Connects to a live node using the provided seed and config.
96    /// If the node is not registered, it will try to recover it using the seed.
97    /// If the node is not created, it will register it using the provided partner credentials
98    /// or invite code
99    /// If the node is already registered and an existing credentials were found, it will try to
100    /// connect to the node using these credentials.
101    pub async fn connect(
102        config: Config,
103        seed: Vec<u8>,
104        restore_only: Option<bool>,
105        persister: Arc<SqliteStorage>,
106    ) -> NodeResult<Self> {
107        // Derive the encryption key from the seed
108        let temp_signer = Arc::new(Signer::new(
109            seed.clone(),
110            config.network.into(),
111            Nobody::new(),
112        )?);
113        let encryption_key = Self::derive_bip32_key(
114            config.network,
115            &temp_signer,
116            vec![ChildNumber::from_hardened_idx(140)?, ChildNumber::from(0)],
117        )?
118        .to_priv()
119        .to_bytes();
120        let encryption_key_slice = encryption_key.as_slice();
121
122        let register_credentials = match config.node_config.clone() {
123            NodeConfig::Greenlight { config } => config,
124        };
125
126        // Query for the existing credentials
127        let mut parsed_credentials =
128            Self::get_node_credentials(config.network, &temp_signer, persister.clone())?
129                .ok_or(NodeError::credentials("No credentials found"));
130        if parsed_credentials.is_err() {
131            info!("No credentials found, trying to recover existing node");
132            parsed_credentials = match Self::recover(config.network, seed.clone()).await {
133                Ok(creds) => Ok(creds),
134                Err(_) => {
135                    match restore_only.unwrap_or(false) {
136                        false => {
137                            // If we got here it means we failed to recover so we need to register a new node
138                            info!("Failed to recover node, registering new one");
139                            let credentials = Self::register(
140                                config.clone().network,
141                                seed.clone(),
142                                register_credentials.partner_credentials,
143                                register_credentials.invite_code,
144                            )
145                            .await?;
146                            Ok(credentials)
147                        }
148                        true => {
149                            return Err(NodeError::RestoreOnly("Node does not exist".to_string()));
150                        }
151                    }
152                }
153            }
154        }
155
156        // Persist the connection credentials for future use and return the node instance
157        match parsed_credentials {
158            Ok(creds) => {
159                let temp_scheduler = Scheduler::new(config.network.into(), creds.clone()).await?;
160                debug!("upgrading credentials");
161                let creds = creds.upgrade(&temp_scheduler, &temp_signer).await?;
162                debug!("upgrading credentials succeeded");
163                let encrypted_creds = sym_encrypt(encryption_key_slice, &creds.to_bytes());
164                match encrypted_creds {
165                    Some(c) => {
166                        persister.update_cached_item(KEY_GL_CREDENTIALS, hex::encode(c))?;
167                        Greenlight::new(config, seed, creds.clone(), persister)
168                    }
169                    None => Err(NodeError::generic("Failed to encrypt credentials")),
170                }
171            }
172            Err(_) => Err(NodeError::credentials("Failed to get gl credentials")),
173        }
174    }
175
176    fn new(
177        sdk_config: Config,
178        seed: Vec<u8>,
179        device: Device,
180        persister: Arc<SqliteStorage>,
181    ) -> NodeResult<Greenlight> {
182        let greenlight_network = sdk_config.network.into();
183        let signer = Signer::new(seed.clone(), greenlight_network, device.clone())?;
184        Ok(Greenlight {
185            sdk_config,
186            signer: Mutex::new(Arc::new(signer)),
187            device,
188            gl_client: Mutex::new(None),
189            node_client: Mutex::new(None),
190            persister,
191            inprogress_payments: AtomicU16::new(0),
192        })
193    }
194
195    async fn get_signer(&self) -> Arc<Signer> {
196        Arc::clone(&*self.signer.lock().await)
197    }
198
199    fn derive_bip32_key(
200        network: Network,
201        signer: &Arc<Signer>,
202        path: Vec<ChildNumber>,
203    ) -> NodeResult<ExtendedPrivKey> {
204        Ok(
205            ExtendedPrivKey::new_master(network.into(), &signer.bip32_ext_key())?
206                .derive_priv(&Secp256k1::new(), &path)?,
207        )
208    }
209
210    fn legacy_derive_bip32_key(
211        network: Network,
212        signer: &Arc<Signer>,
213        path: Vec<ChildNumber>,
214    ) -> NodeResult<ExtendedPrivKey> {
215        Ok(
216            ExtendedPrivKey::new_master(network.into(), &signer.legacy_bip32_ext_key())?
217                .derive_priv(&Secp256k1::new(), &path)?,
218        )
219    }
220
221    async fn register(
222        network: Network,
223        seed: Vec<u8>,
224        register_credentials: Option<GreenlightCredentials>,
225        invite_code: Option<String>,
226    ) -> Result<Device> {
227        if invite_code.is_some() && register_credentials.is_some() {
228            return Err(anyhow!("Cannot specify both invite code and credentials"));
229        }
230        let greenlight_network = network.into();
231        let creds = match register_credentials {
232            Some(creds) => {
233                debug!("registering with credentials");
234                Nobody {
235                    cert: creds.developer_cert,
236                    key: creds.developer_key,
237                    ..Default::default()
238                }
239            }
240            None => Nobody::new(),
241        };
242
243        let signer = Signer::new(seed, greenlight_network, creds.clone())?;
244        let scheduler = Scheduler::new(greenlight_network, creds).await?;
245
246        let register_res: scheduler::RegistrationResponse =
247            scheduler.register(&signer, invite_code).await?;
248
249        Ok(Device::from_bytes(register_res.creds))
250    }
251
252    async fn recover(network: Network, seed: Vec<u8>) -> Result<Device> {
253        let greenlight_network = network.into();
254        let credentials = Nobody::new();
255        let signer = Signer::new(seed, greenlight_network, credentials.clone())?;
256        let scheduler = Scheduler::new(greenlight_network, credentials).await?;
257        let recover_res: scheduler::RecoveryResponse = scheduler.recover(&signer).await?;
258
259        Ok(Device::from_bytes(recover_res.creds))
260    }
261
262    async fn get_client(&self) -> NodeResult<node::Client> {
263        let mut gl_client = self.gl_client.lock().await;
264        if gl_client.is_none() {
265            let scheduler = Scheduler::new(self.sdk_config.network.into(), self.device.clone())
266                .await
267                .map_err(|e| NodeError::ServiceConnectivity(e.to_string()))?;
268            *gl_client = Some(scheduler.node().await?);
269        }
270        Ok(gl_client.clone().unwrap())
271    }
272
273    pub(crate) async fn get_node_client(&self) -> NodeResult<node::ClnClient> {
274        let mut node_client = self.node_client.lock().await;
275        if node_client.is_none() {
276            let scheduler = Scheduler::new(self.sdk_config.network.into(), self.device.clone())
277                .await
278                .map_err(|e| NodeError::ServiceConnectivity(e.to_string()))?;
279            *node_client = Some(scheduler.node().await?);
280        }
281        Ok(node_client.clone().unwrap())
282    }
283
284    fn get_node_credentials(
285        network: Network,
286        signer: &Arc<Signer>,
287        persister: Arc<SqliteStorage>,
288    ) -> NodeResult<Option<Device>> {
289        // Derive the encryption key from the seed
290        let encryption_key = Self::derive_bip32_key(
291            network,
292            signer,
293            vec![ChildNumber::from_hardened_idx(140)?, ChildNumber::from(0)],
294        )?
295        .to_priv()
296        .to_bytes();
297        let encryption_key_slice = encryption_key.as_slice();
298
299        let legacy_encryption_key = Self::legacy_derive_bip32_key(
300            network,
301            signer,
302            vec![ChildNumber::from_hardened_idx(140)?, ChildNumber::from(0)],
303        )?
304        .to_priv()
305        .to_bytes();
306        let legacy_encryption_key_slice = legacy_encryption_key.as_slice();
307
308        match persister.get_cached_item(KEY_GL_CREDENTIALS)? {
309            Some(encrypted_creds) => {
310                let encrypted_creds = hex::decode(encrypted_creds)?;
311                let mut decrypted_credentials =
312                    sym_decrypt(encryption_key_slice, encrypted_creds.as_slice());
313                if decrypted_credentials.is_none() {
314                    info!("Failed to decrypt credentials, trying legacy key");
315                    decrypted_credentials =
316                        sym_decrypt(legacy_encryption_key_slice, encrypted_creds.as_slice());
317                }
318                match decrypted_credentials {
319                    Some(decrypted_creds) => {
320                        let credentials = Device::from_bytes(decrypted_creds.as_slice());
321                        if credentials.cert.is_empty() {
322                            Err(NodeError::credentials("Unable to parse credentials"))
323                        } else {
324                            Ok(Some(credentials))
325                        }
326                    }
327                    None => Err(NodeError::credentials(
328                        "Failed to decrypt credentials, seed doesn't match existing node",
329                    )),
330                }
331            }
332            None => Ok(None),
333        }
334    }
335
336    async fn fetch_outgoing_payment_with_retry(
337        client: node::ClnClient,
338        payment_hash: Vec<u8>,
339    ) -> Result<cln::ListpaysPays> {
340        let mut response = cln::ListpaysResponse::default();
341        let mut retry = 0;
342        let max_retries = 20;
343
344        while response.pays.is_empty() && retry < max_retries {
345            let req = cln::ListpaysRequest {
346                payment_hash: Some(payment_hash.clone()),
347                status: Some(cln::listpays_request::ListpaysStatus::Complete.into()),
348                ..cln::ListpaysRequest::default()
349            };
350            let mut client = client.clone();
351            response = with_connection_retry!(client.list_pays(req.clone()))
352                .await?
353                .into_inner();
354            if response.pays.is_empty() {
355                debug!("fetch outgoing payment failed, retrying in 100ms...");
356                sleep(Duration::from_millis(100)).await;
357            }
358            retry += 1;
359        }
360
361        // CLN also returns failed ListpaysPays along with the complete one
362        debug!("list_pays: {:?}", response.pays);
363        let pays: Vec<ListpaysPays> = response
364            .pays
365            .into_iter()
366            .filter(|pay| pay.status() == cln::listpays_pays::ListpaysPaysStatus::Complete)
367            .collect();
368
369        if pays.is_empty() {
370            return Err(anyhow!("Payment not found"));
371        }
372        Ok(pays[0].clone())
373    }
374
375    async fn fetch_channels_and_balance_with_retry(
376        cln_client: node::ClnClient,
377        persister: Arc<SqliteStorage>,
378        match_local_balance: bool,
379    ) -> NodeResult<(
380        Vec<cln::ListpeerchannelsChannels>,
381        Vec<cln::ListpeerchannelsChannels>,
382        Vec<String>,
383        u64,
384    )> {
385        let (mut all_channels, mut opened_channels, mut connected_peers, mut channels_balance) =
386            Greenlight::fetch_channels_and_balance(cln_client.clone()).await?;
387        if match_local_balance {
388            let node_state = persister.get_node_state()?;
389            if let Some(state) = node_state {
390                let mut retry_count = 0;
391                while state.channels_balance_msat != channels_balance && retry_count < 10 {
392                    warn!("balance matching local state is required and not yet satisfied, retrying in 100ms...");
393                    sleep(Duration::from_millis(100)).await;
394                    (
395                        all_channels,
396                        opened_channels,
397                        connected_peers,
398                        channels_balance,
399                    ) = Greenlight::fetch_channels_and_balance(cln_client.clone()).await?;
400                    retry_count += 1;
401                }
402            }
403        }
404        Ok((
405            all_channels,
406            opened_channels,
407            connected_peers,
408            channels_balance,
409        ))
410    }
411
412    async fn fetch_channels_and_balance(
413        mut client: node::ClnClient,
414    ) -> NodeResult<(
415        Vec<cln::ListpeerchannelsChannels>,
416        Vec<cln::ListpeerchannelsChannels>,
417        Vec<String>,
418        u64,
419    )> {
420        // list all channels
421        let req = cln::ListpeerchannelsRequest::default();
422        let peerchannels = with_connection_retry!(client.list_peer_channels(req.clone()))
423            .await?
424            .into_inner();
425
426        // filter only connected peers
427        let connected_peers: Vec<String> = peerchannels
428            .channels
429            .iter()
430            .filter(|channel| channel.peer_connected)
431            .map(|channel| hex::encode(&channel.peer_id))
432            .collect::<HashSet<_>>()
433            .into_iter()
434            .collect();
435
436        // filter only opened channels
437        let opened_channels: Vec<cln::ListpeerchannelsChannels> = peerchannels
438            .channels
439            .iter()
440            .filter(|c| c.state() == ChanneldNormal)
441            .cloned()
442            .collect();
443
444        // calculate channels balance only from opened channels
445        let channels_balance = opened_channels
446            .iter()
447            .map(|c| Channel::from(c.clone()))
448            .map(|c| c.spendable_msat)
449            .sum::<u64>();
450        Ok((
451            peerchannels.channels,
452            opened_channels,
453            connected_peers,
454            channels_balance,
455        ))
456    }
457
458    async fn list_funds(&self) -> Result<cln::ListfundsResponse> {
459        let mut client = self.get_node_client().await?;
460        let req = cln::ListfundsRequest::default();
461        let funds: cln::ListfundsResponse = with_connection_retry!(client.list_funds(req.clone()))
462            .await?
463            .into_inner();
464        Ok(funds)
465    }
466
467    async fn on_chain_balance(&self, funds: &cln::ListfundsResponse) -> Result<u64> {
468        let on_chain_balance = funds.outputs.iter().fold(0, |a, b| {
469            if b.reserved {
470                return a;
471            }
472            a + b.amount_msat.clone().unwrap_or_default().msat
473        });
474        Ok(on_chain_balance)
475    }
476
477    async fn pending_onchain_balance(
478        &self,
479        peer_channels: &[cln::ListpeerchannelsChannels],
480    ) -> Result<u64> {
481        let pending_onchain_balance = peer_channels.iter().fold(0, |a, b| match b.state() {
482            ChanneldShuttingDown | ClosingdSigexchange | ClosingdComplete | AwaitingUnilateral
483            | FundingSpendSeen => a + b.to_us_msat.clone().unwrap_or_default().msat,
484
485            // When we  unilaterally close the channel it will get status as `AwaitingUnilateral`
486            // first, but when the closing transaction is confirmed onchain the funds receive status
487            // as `Onchain`. Though if we closed the channel we'll have to wait for the timelock to
488            // pass before the funds can be spent.
489            Onchain => {
490                if b.closer() == cln::ChannelSide::Local
491                    && b.status
492                        .last()
493                        .is_some_and(|status| status.contains("DELAYED_OUTPUT_TO_US"))
494                {
495                    a + b.to_us_msat.clone().unwrap_or_default().msat
496                } else {
497                    a
498                }
499            }
500            _ => a,
501        });
502        info!("pending_onchain_balance is {pending_onchain_balance}");
503        Ok(pending_onchain_balance)
504    }
505
506    // Collect utxos from onchain funds
507    async fn utxos(&self, funds: cln::ListfundsResponse) -> Result<Vec<UnspentTransactionOutput>> {
508        let utxos: Vec<UnspentTransactionOutput> = funds
509            .outputs
510            .iter()
511            .map(|output| UnspentTransactionOutput {
512                txid: output.txid.clone(),
513                outnum: output.output,
514                amount_millisatoshi: output
515                    .amount_msat
516                    .as_ref()
517                    .map(|a| a.msat)
518                    .unwrap_or_default(),
519                address: output.address.clone().unwrap_or_default(),
520                reserved: output.reserved,
521            })
522            .collect();
523        Ok(utxos)
524    }
525
526    async fn build_payment_path(
527        &self,
528        route: &Vec<GetrouteRoute>,
529        first_edge: PaymentPathEdge,
530    ) -> NodeResult<PaymentPath> {
531        let client = self.get_node_client().await?;
532        let mut hops = vec![first_edge];
533
534        for hop in route {
535            let req = ListchannelsRequest {
536                short_channel_id: Some(hop.channel.clone()),
537                source: None,
538                destination: None,
539            };
540            let mut client = client.clone();
541            let hopchannels = with_connection_retry!(client.list_channels(req.clone()))
542                .await?
543                .into_inner()
544                .channels;
545
546            let first_channel = hopchannels.first().ok_or(NodeError::RouteNotFound(format!(
547                "Channel not found {}",
548                hop.channel.clone()
549            )))?;
550
551            info!("found channel in route: {first_channel:?}");
552            hops.push(PaymentPathEdge {
553                base_fee_msat: first_channel.base_fee_millisatoshi as u64,
554                fee_per_millionth: first_channel.fee_per_millionth as u64,
555                node_id: hop.id.clone(),
556                short_channel_id: hop.channel.clone(),
557                channel_delay: first_channel.delay as u64,
558            });
559        }
560        Ok(PaymentPath { edges: hops })
561    }
562
563    async fn max_sendable_amount_from_peer(
564        &self,
565        via_peer_id: Vec<u8>,
566        via_peer_channels: Vec<ListpeerchannelsChannels>,
567        payee_node_id: Option<Vec<u8>>,
568        max_hops: u32,
569        last_hop_hint: Option<&RouteHintHop>,
570    ) -> NodeResult<Vec<MaxChannelAmount>> {
571        let mut client = self.get_node_client().await?;
572
573        // Consider the hints as part of the route. If there is a routing hint we will
574        // attempt to calculate the path until the last hop in the hint and then add
575        // the last hop to the path.
576        let (last_node, max_hops) = match last_hop_hint {
577            Some(hop) => (hex::decode(&hop.src_node_id)?, max_hops - 1),
578            None => match payee_node_id.clone() {
579                Some(node_id) => (node_id, max_hops),
580                None => {
581                    return Err(NodeError::RouteNotFound(
582                        "No payee node id or last hop hints provided, cannot calculate max amount"
583                            .to_string(),
584                    ));
585                }
586            },
587        };
588
589        // fetch a route from greenlight
590        info!(
591            "calling get_route for peer {} to node {}, max_hops: {}",
592            hex::encode(via_peer_id.clone()),
593            hex::encode(last_node.clone()),
594            max_hops - 1
595        );
596        let req = GetrouteRequest {
597            id: last_node.clone(),
598            amount_msat: Some(Amount { msat: 0 }),
599            riskfactor: 0,
600            cltv: None,
601            fromid: Some(via_peer_id.clone()),
602            fuzzpercent: Some(0),
603            exclude: vec![],
604            // we deduct the first hop that we calculate manually
605            maxhops: Some(max_hops - 1),
606        };
607        let route_result = with_connection_retry!(client.get_route(req.clone())).await;
608
609        // In case we have no route better to return no amounts for this peer's channels.
610        if let Err(e) = route_result {
611            error!(
612                "Failed to get route for peer {}: {}",
613                hex::encode(via_peer_id.clone()),
614                e
615            );
616            return Ok(vec![]);
617        }
618
619        let route_response = route_result?.into_inner();
620        info!(
621            "max_sendable_amount: route response = {:?}",
622            route_response
623                .route
624                .iter()
625                .map(|r| format!(
626                    "{{node_id: {}, channel: {}}}",
627                    hex::encode(&r.id),
628                    r.channel
629                ))
630                .collect::<Vec<_>>()
631        );
632
633        // We fetch the opened channels so can calculate max amount to send for each channel
634        let opened_channels: Vec<cln::ListpeerchannelsChannels> = via_peer_channels
635            .iter()
636            .filter(|c| c.state() == ChanneldNormal)
637            .cloned()
638            .collect();
639
640        let mut max_per_channel = vec![];
641        for c in opened_channels {
642            let chan_id = c
643                .clone()
644                .channel_id
645                .ok_or(NodeError::generic("Empty channel id"))?;
646
647            // First hop is forwarding so no fees and delays.
648            let first_edge = PaymentPathEdge {
649                base_fee_msat: 0,
650                fee_per_millionth: 0,
651                node_id: via_peer_id.clone(),
652                short_channel_id: c.clone().short_channel_id.unwrap_or_default(),
653                channel_delay: 0,
654            };
655
656            // convert the route to a payment path so we can calculate the amount to forward for each hop
657            let mut payment_path = self
658                .build_payment_path(&route_response.route, first_edge)
659                .await?;
660
661            // Add the last hop hints (if any) to the route
662            if let Some(hint) = last_hop_hint {
663                payment_path.edges.extend(vec![PaymentPathEdge {
664                    base_fee_msat: hint.fees_base_msat as u64,
665                    fee_per_millionth: hint.fees_proportional_millionths as u64,
666                    node_id: payee_node_id.clone().unwrap_or_default(),
667                    short_channel_id: hint.short_channel_id.clone(),
668                    channel_delay: hint.cltv_expiry_delta,
669                }])
670            }
671
672            info!(
673                "max_sendable_amount: route_hops = {:?}",
674                payment_path
675                    .edges
676                    .iter()
677                    .map(|e| format!(
678                        "{{node_id: {}, channel: {}}}",
679                        hex::encode(&e.node_id),
680                        e.short_channel_id
681                    ))
682                    .collect::<Vec<_>>()
683            );
684
685            // go over each hop and calculate the amount to forward.
686            let max_payment_amount =
687                payment_path.final_hop_amount(c.clone().spendable_msat.unwrap_or_default().msat);
688            max_per_channel.push(MaxChannelAmount {
689                channel_id: hex::encode(chan_id),
690                amount_msat: max_payment_amount,
691                path: payment_path,
692            });
693        }
694
695        Ok(max_per_channel)
696    }
697
698    /// Get open peer channels (private and public) as raw protobuf structs, indexed by peer pubkey
699    async fn get_open_peer_channels_pb(
700        &self,
701    ) -> NodeResult<HashMap<Vec<u8>, cln::ListpeerchannelsChannels>> {
702        let mut client = self.get_node_client().await?;
703        // Get the peer channels
704        let req = cln::ListpeerchannelsRequest::default();
705        let peer_channels = with_connection_retry!(client.list_peer_channels(req.clone()))
706            .await?
707            .into_inner();
708
709        let open_peer_channels: HashMap<Vec<u8>, cln::ListpeerchannelsChannels> = peer_channels
710            .channels
711            .into_iter()
712            .filter(|c| c.state() == ChanneldNormal)
713            .map(|c| (c.peer_id.clone(), c))
714            .collect();
715        Ok(open_peer_channels)
716    }
717
718    async fn with_keep_alive<T, F>(&self, f: F) -> T
719    where
720        F: Future<Output = T>,
721    {
722        _ = self.inprogress_payments.fetch_add(1, Ordering::Relaxed);
723        let res = f.await;
724        _ = self.inprogress_payments.fetch_sub(1, Ordering::Relaxed);
725        res
726    }
727
728    // pulls transactions from greenlight based on last sync timestamp.
729    // greenlight gives us the payments via API and for received payments we are looking for settled invoices.
730    async fn pull_transactions(
731        &self,
732        sync_state: &SyncState,
733        htlc_list: Vec<Htlc>,
734    ) -> NodeResult<(SyncState, Vec<Payment>)> {
735        let (receive_payments_res, send_payments_res) = join!(
736            self.pull_receive_payments(&sync_state.list_invoices_index),
737            self.pull_send_payments(&sync_state.send_pays_index, htlc_list),
738        );
739
740        let (receive_payments, list_invoices_index) = receive_payments_res?;
741        let (send_payments, send_pays_index) = send_payments_res?;
742        let mut new_state = sync_state.clone();
743        new_state.list_invoices_index = list_invoices_index;
744        new_state.send_pays_index = send_pays_index;
745
746        let mut payments: Vec<Payment> = Vec::new();
747        payments.extend(receive_payments);
748        payments.extend(send_payments);
749
750        Ok((new_state, payments))
751    }
752
753    async fn pull_receive_payments(
754        &self,
755        state: &SyncIndex,
756    ) -> NodeResult<(Vec<Payment>, SyncIndex)> {
757        let mut client = self.get_node_client().await?;
758
759        let req = cln::ListinvoicesRequest {
760            index: Some(ListinvoicesIndex::Created.into()),
761            start: Some(state.created),
762            ..Default::default()
763        };
764        let mut clone = client.clone();
765        let created_invoices = with_connection_retry!(clone.list_invoices(req.clone()))
766            .await?
767            .into_inner();
768        let req = cln::ListinvoicesRequest {
769            index: Some(ListinvoicesIndex::Updated.into()),
770            start: Some(state.updated),
771            ..Default::default()
772        };
773
774        let updated_invoices = with_connection_retry!(client.list_invoices(req.clone()))
775            .await?
776            .into_inner();
777        let mut new_state = state.clone();
778        if let Some(last) = created_invoices.invoices.last() {
779            new_state.created = last.created_index()
780        }
781        if let Some(last) = updated_invoices.invoices.last() {
782            new_state.updated = last.updated_index()
783        }
784
785        let received_payments: NodeResult<Vec<Payment>> = created_invoices
786            .invoices
787            .into_iter()
788            .chain(updated_invoices.invoices.into_iter())
789            .filter(|i| i.status() == ListinvoicesInvoicesStatus::Paid)
790            .map(TryInto::try_into)
791            .collect();
792
793        Ok((received_payments?, new_state))
794    }
795
796    async fn pull_send_payments(
797        &self,
798        state: &SyncIndex,
799        htlc_list: Vec<Htlc>,
800    ) -> NodeResult<(Vec<Payment>, SyncIndex)> {
801        let mut client = self.get_node_client().await?;
802        let req = cln::ListsendpaysRequest {
803            index: Some(ListsendpaysIndex::Created.into()),
804            start: Some(state.created),
805            ..Default::default()
806        };
807        let mut clone = client.clone();
808        let created_send_pays = with_connection_retry!(clone.list_send_pays(req.clone()))
809            .await?
810            .into_inner();
811        let req = cln::ListsendpaysRequest {
812            index: Some(ListsendpaysIndex::Updated.into()),
813            start: Some(state.updated),
814            ..Default::default()
815        };
816        let updated_send_pays = with_connection_retry!(client.list_send_pays(req.clone()))
817            .await?
818            .into_inner();
819
820        let mut new_state = state.clone();
821        if let Some(last) = created_send_pays.payments.last() {
822            new_state.created = last.created_index()
823        }
824        if let Some(last) = updated_send_pays.payments.last() {
825            new_state.updated = last.updated_index()
826        }
827
828        let hash_groups: HashMap<_, _> = created_send_pays
829            .payments
830            .iter()
831            .chain(updated_send_pays.payments.iter())
832            .map(|p| {
833                let mut key = hex::encode(&p.payment_hash);
834                key.push('|');
835                key.push_str(&p.groupid.to_string());
836                (key, (p.payment_hash.clone(), p.groupid.to_string()))
837            })
838            .collect();
839        let hash_group_values: Vec<_> = hash_groups.values().cloned().collect();
840
841        self.persister.insert_send_pays(
842            &created_send_pays
843                .payments
844                .into_iter()
845                .map(TryInto::try_into)
846                .collect::<Result<Vec<_>, _>>()?,
847        )?;
848        self.persister.insert_send_pays(
849            &updated_send_pays
850                .payments
851                .into_iter()
852                .map(TryInto::try_into)
853                .collect::<Result<Vec<_>, _>>()?,
854        )?;
855
856        // Now all new send_pays are persisted. Retrieve the send_pays for the
857        // payment hashes, to ensure any send_pays belonging to the same payment
858        // that were not fetched in this round are also included.
859        let send_pays = self.persister.list_send_pays(&hash_group_values)?;
860
861        // Now that all send_pays belonging to all payments are here, aggregate
862        // the send_pays into payments. This is a copy of what core lightning's
863        // listpays function does under the hood.
864        let mut outbound_payments: HashMap<String, SendPayAgg> = HashMap::new();
865        for send_pay in send_pays {
866            let mut key = hex::encode(&send_pay.payment_hash);
867            key.push('|');
868            key.push_str(&send_pay.groupid);
869            let payment = outbound_payments.entry(key).or_insert(SendPayAgg {
870                state: 0,
871                created_at: send_pay.created_at,
872                payment_hash: send_pay.payment_hash,
873                bolt11: None,
874                destination: None,
875                label: None,
876                description: None,
877                preimage: None,
878                amount_sent: 0,
879                amount: Some(0),
880                num_nonfailed_parts: 0,
881            });
882            if payment.bolt11.is_none() {
883                payment.bolt11 = send_pay.bolt11;
884            }
885            if payment.destination.is_none() {
886                payment.destination = send_pay.destination;
887            }
888            if payment.description.is_none() {
889                payment.description = send_pay.description;
890            }
891            if payment.label.is_none() {
892                payment.label = send_pay.label;
893            }
894            if payment.preimage.is_none() {
895                payment.preimage = send_pay.payment_preimage;
896            }
897            if send_pay.created_at < payment.created_at {
898                payment.created_at = send_pay.created_at;
899            }
900
901            match send_pay.status {
902                SendPayStatus::Pending => {
903                    add_amount_sent(payment, send_pay.amount_sent_msat, send_pay.amount_msat);
904                    payment.num_nonfailed_parts += 1;
905                    payment.state |= PAYMENT_STATE_PENDING;
906                }
907                SendPayStatus::Failed => {
908                    payment.state |= PAYMENT_STATE_FAILED;
909                }
910                SendPayStatus::Complete => {
911                    add_amount_sent(payment, send_pay.amount_sent_msat, send_pay.amount_msat);
912                    payment.num_nonfailed_parts += 1;
913                    payment.state |= PAYMENT_STATE_COMPLETE;
914                }
915            }
916        }
917
918        let outbound_payments: Vec<Payment> = outbound_payments
919            .into_values()
920            .map(TryInto::try_into)
921            .collect::<Result<Vec<_>, _>>()?;
922        let outbound_payments = update_payment_expirations(outbound_payments, htlc_list)?;
923        Ok((outbound_payments, new_state))
924    }
925
926    async fn wait_channel_reestablished(&self, path: &PaymentPath) -> NodeResult<()> {
927        let deadline =
928            Instant::now()
929                .checked_add(Duration::from_secs(20))
930                .ok_or(NodeError::generic(
931                    "Failed to set channel establishment deadline",
932                ))?;
933
934        while Instant::now().le(&deadline) && !self.poll_channel_reestablished(path).await? {
935            tokio::time::sleep(Duration::from_millis(50)).await
936        }
937
938        Ok(())
939    }
940
941    async fn poll_channel_reestablished(&self, path: &PaymentPath) -> NodeResult<bool> {
942        let edge = match path.edges.first() {
943            Some(edge) => edge,
944            None => return Err(NodeError::generic("Channel not found")),
945        };
946        let mut client = self.get_node_client().await?;
947        let req = cln::ListpeerchannelsRequest {
948            id: Some(edge.node_id.clone()),
949        };
950        let res = with_connection_retry!(client.list_peer_channels(req.clone()))
951            .await?
952            .into_inner();
953        let channel = match res.channels.iter().find(|c| {
954            match (
955                c.alias.as_ref().and_then(|a| a.local.as_ref()),
956                c.short_channel_id.as_ref(),
957            ) {
958                (Some(alias), Some(short_channel_id)) => {
959                    *alias == edge.short_channel_id || *short_channel_id == edge.short_channel_id
960                }
961                (Some(alias), None) => *alias == edge.short_channel_id,
962                (None, Some(short_channel_id)) => *short_channel_id == edge.short_channel_id,
963                (None, None) => false,
964            }
965        }) {
966            Some(channel) => channel,
967            None => return Err(NodeError::generic("Channel not found")),
968        };
969
970        if !channel.peer_connected {
971            return Ok(false);
972        }
973
974        if !channel
975            .status
976            .iter()
977            .any(|s| s.contains("Channel ready") || s.contains("Reconnected, and reestablished"))
978        {
979            return Ok(false);
980        }
981
982        Ok(true)
983    }
984}
985
986fn add_amount_sent(
987    agg: &mut SendPayAgg,
988    send_pay_amount_sent_msat: Option<u64>,
989    send_pay_amount_msat: Option<u64>,
990) {
991    if let Some(amount_sent_msat) = send_pay_amount_sent_msat {
992        agg.amount_sent += amount_sent_msat;
993    }
994
995    let amount_msat = match send_pay_amount_msat {
996        Some(amount_msat) => amount_msat,
997        None => {
998            agg.amount = None;
999            return;
1000        }
1001    };
1002
1003    if let Some(amount) = agg.amount {
1004        agg.amount = Some(amount + amount_msat);
1005    }
1006}
1007
1008#[derive(Clone, Debug, Default, Deserialize, Serialize)]
1009struct SyncIndex {
1010    pub created: u64,
1011    pub updated: u64,
1012}
1013
1014#[derive(Clone, Debug, Default, Deserialize, Serialize)]
1015struct SyncState {
1016    pub send_pays_index: SyncIndex,
1017    pub list_invoices_index: SyncIndex,
1018}
1019
1020#[tonic::async_trait]
1021impl NodeAPI for Greenlight {
1022    async fn node_credentials(&self) -> NodeResult<Option<NodeCredentials>> {
1023        Ok(Self::get_node_credentials(
1024            self.sdk_config.network,
1025            &self.get_signer().await,
1026            self.persister.clone(),
1027        )?
1028        .map(|credentials| NodeCredentials::Greenlight {
1029            credentials: GreenlightDeviceCredentials {
1030                device: credentials.to_bytes(),
1031            },
1032        }))
1033    }
1034
1035    async fn configure_node(&self, close_to_address: Option<String>) -> NodeResult<()> {
1036        match close_to_address {
1037            Some(close_to_addr) => {
1038                let mut client = self.get_client().await?;
1039                let req = gl_client::pb::GlConfig { close_to_addr };
1040                with_connection_retry!(client.configure(req.clone()))
1041                    .await
1042                    .map_err(|e| NodeError::Generic(format!("Unable to set node config: {e}")))?;
1043            }
1044            None => {
1045                let mut client = self.get_node_client().await?;
1046                let req = cln::DeldatastoreRequest {
1047                    key: vec!["glconf".to_string(), "request".to_string()],
1048                    generation: None,
1049                };
1050                with_connection_retry!(client.del_datastore(req.clone()))
1051                    .await
1052                    .map_err(|e| {
1053                        NodeError::Generic(format!("Unable to delete node config: {e}"))
1054                    })?;
1055            }
1056        }
1057        Ok(())
1058    }
1059
1060    async fn create_invoice(&self, request: CreateInvoiceRequest) -> NodeResult<String> {
1061        let mut client = self.get_node_client().await?;
1062        let label = serde_json::to_string(&InvoiceLabel {
1063            unix_milli: SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis(),
1064            payer_amount_msat: request.payer_amount_msat,
1065        })?;
1066        let cln_request = cln::InvoiceRequest {
1067            amount_msat: Some(cln::AmountOrAny {
1068                value: Some(cln::amount_or_any::Value::Amount(cln::Amount {
1069                    msat: request.amount_msat,
1070                })),
1071            }),
1072            label,
1073            description: request.description,
1074            exposeprivatechannels: vec![],
1075            preimage: request.preimage,
1076            deschashonly: request.use_description_hash,
1077            expiry: request.expiry.map(|e| e as u64),
1078            fallbacks: vec![],
1079            cltv: request.cltv,
1080        };
1081
1082        let res = with_connection_retry!(client.invoice(cln_request.clone()))
1083            .await?
1084            .into_inner();
1085        Ok(res.bolt11)
1086    }
1087
1088    async fn delete_invoice(&self, bolt11: String) -> NodeResult<()> {
1089        let mut client = self.get_node_client().await?;
1090        let invoice_request = cln::ListinvoicesRequest {
1091            invstring: Some(bolt11),
1092            ..Default::default()
1093        };
1094        let invoice_result = with_connection_retry!(client.list_invoices(invoice_request.clone()))
1095            .await?
1096            .into_inner();
1097        let invoice_result = invoice_result.invoices.first();
1098        let result = match invoice_result {
1099            Some(result) => result,
1100            None => return Ok(()),
1101        };
1102
1103        let status = match result.status() {
1104            ListinvoicesInvoicesStatus::Unpaid => DelinvoiceStatus::Unpaid,
1105            ListinvoicesInvoicesStatus::Paid => return Err(NodeError::InvoiceAlreadyPaid),
1106            ListinvoicesInvoicesStatus::Expired => DelinvoiceStatus::Expired,
1107        };
1108        with_connection_retry!(client.del_invoice(DelinvoiceRequest {
1109            label: result.label.clone(),
1110            status: status.into(),
1111            desconly: Some(false),
1112        }))
1113        .await?;
1114        Ok(())
1115    }
1116
1117    async fn fetch_bolt11(&self, payment_hash: Vec<u8>) -> NodeResult<Option<FetchBolt11Result>> {
1118        let mut client = self.get_node_client().await?;
1119        let request = cln::ListinvoicesRequest {
1120            payment_hash: Some(payment_hash),
1121            ..Default::default()
1122        };
1123
1124        let result = with_connection_retry!(client.list_invoices(request.clone()))
1125            .await?
1126            .into_inner()
1127            .invoices
1128            .first()
1129            .cloned()
1130            .and_then(|invoice| {
1131                invoice.bolt11.map(|bolt11| FetchBolt11Result {
1132                    bolt11,
1133                    payer_amount_msat: serde_json::from_str::<InvoiceLabel>(&invoice.label)
1134                        .map(|label| label.payer_amount_msat)
1135                        .ok()
1136                        .flatten(),
1137                })
1138            });
1139
1140        Ok(result)
1141    }
1142
1143    // implement pull changes from greenlight
1144    async fn pull_changed(
1145        &self,
1146        sync_state: Option<Value>,
1147        match_local_balance: bool,
1148    ) -> NodeResult<SyncResponse> {
1149        let sync_state: SyncState = match sync_state {
1150            Some(sync_state) => serde_json::from_value(sync_state)?,
1151            None => SyncState::default(),
1152        };
1153
1154        let client = self.get_node_client().await?;
1155
1156        // get node info
1157        let mut client_clone1 = client.clone();
1158        let node_info_future =
1159            with_connection_retry!(client_clone1.getinfo(cln::GetinfoRequest::default()));
1160
1161        // list both off chain funds and on chain fudns
1162        let funds_future = self.list_funds();
1163
1164        // Fetch closed channels from greenlight
1165        let mut client_clone2 = client.clone();
1166        let closed_channels_future = with_connection_retry!(
1167            client_clone2.list_closed_channels(cln::ListclosedchannelsRequest { id: None })
1168        );
1169
1170        // calculate the node new balance and in case the caller signals balance has changed
1171        // keep polling until the balance is updated
1172        let balance_future = Greenlight::fetch_channels_and_balance_with_retry(
1173            client.clone(),
1174            self.persister.clone(),
1175            match_local_balance,
1176        );
1177
1178        let (node_info_res, funds_res, closed_channels_res, balance_res) = tokio::join!(
1179            node_info_future,
1180            funds_future,
1181            closed_channels_future,
1182            balance_future
1183        );
1184
1185        let node_info = node_info_res?.into_inner();
1186        let funds = funds_res?;
1187        let closed_channels = closed_channels_res?.into_inner().closedchannels;
1188        let (all_channels, opened_channels, connected_peers, channels_balance) = balance_res?;
1189        let forgotten_closed_channels: NodeResult<Vec<Channel>> = closed_channels
1190            .into_iter()
1191            .filter(|cc| {
1192                all_channels
1193                    .iter()
1194                    .all(|ac| ac.funding_txid != Some(cc.funding_txid.clone()))
1195            })
1196            .map(TryInto::try_into)
1197            .collect();
1198        info!("forgotten_closed_channels {:?}", forgotten_closed_channels);
1199
1200        let mut all_channel_models: Vec<Channel> =
1201            all_channels.clone().into_iter().map(|c| c.into()).collect();
1202        all_channel_models.extend(forgotten_closed_channels?);
1203
1204        // calculate onchain balance
1205        let onchain_balance = self.on_chain_balance(&funds).await?;
1206        let pending_onchain_balance = self.pending_onchain_balance(&all_channels).await?;
1207        let utxos: Vec<UnspentTransactionOutput> = self.utxos(funds).await?;
1208
1209        // calculate payment limits and inbound liquidity
1210        let mut max_payable: u64 = 0;
1211        let mut max_receivable_single_channel: u64 = 0;
1212        let mut total_inbound_liquidity_msats: u64 = 0;
1213        opened_channels.iter().try_for_each(|c| -> Result<()> {
1214            max_payable += c
1215                .spendable_msat
1216                .as_ref()
1217                .map(|a| a.msat)
1218                .unwrap_or_default();
1219            let receivable_amount = c
1220                .receivable_msat
1221                .as_ref()
1222                .map(|a| a.msat)
1223                .unwrap_or_default();
1224            total_inbound_liquidity_msats += receivable_amount;
1225            if receivable_amount > max_receivable_single_channel {
1226                max_receivable_single_channel = receivable_amount;
1227            }
1228            Ok(())
1229        })?;
1230
1231        let max_allowed_to_receive_msats =
1232            MAX_INBOUND_LIQUIDITY_MSAT.saturating_sub(channels_balance);
1233        let node_pubkey = hex::encode(node_info.id);
1234        // construct the node state
1235        let node_state = NodeState {
1236            id: node_pubkey.clone(),
1237            block_height: node_info.blockheight,
1238            channels_balance_msat: channels_balance,
1239            onchain_balance_msat: onchain_balance,
1240            pending_onchain_balance_msat: pending_onchain_balance,
1241            utxos,
1242            max_payable_msat: max_payable,
1243            max_receivable_msat: max_allowed_to_receive_msats,
1244            max_single_payment_amount_msat: MAX_PAYMENT_AMOUNT_MSAT,
1245            max_chan_reserve_msats: channels_balance - min(max_payable, channels_balance),
1246            connected_peers,
1247            max_receivable_single_payment_amount_msat: max_receivable_single_channel,
1248            total_inbound_liquidity_msats,
1249        };
1250        let mut htlc_list: Vec<Htlc> = Vec::new();
1251        for channel in all_channel_models.clone() {
1252            htlc_list.extend(channel.htlcs);
1253        }
1254
1255        let (new_sync_state, payments) = self.pull_transactions(&sync_state, htlc_list).await?;
1256
1257        Ok(SyncResponse {
1258            sync_state: serde_json::to_value(new_sync_state)?,
1259            node_state,
1260            payments,
1261            channels: all_channel_models,
1262        })
1263    }
1264
1265    async fn send_pay(&self, bolt11: String, max_hops: u32) -> NodeResult<PaymentResponse> {
1266        let invoice = parse_invoice(&bolt11)?;
1267        let last_hop = invoice.routing_hints.first().and_then(|rh| rh.hops.first());
1268        let mut client = self.get_node_client().await?;
1269
1270        // Valid the invoice network against the config network
1271        validate_network(invoice.clone(), self.sdk_config.network)?;
1272
1273        // We first calculate for each channel the max amount to pay (at the receiver)
1274        let mut max_amount_per_channel = self
1275            .max_sendable_amount(Some(hex::decode(invoice.payee_pubkey)?), max_hops, last_hop)
1276            .await?;
1277        info!("send_pay: routes: {:?}", max_amount_per_channel);
1278
1279        // Calculate the total amount to pay
1280        let total_msat: u64 = max_amount_per_channel.iter().map(|m| m.amount_msat).sum();
1281
1282        // Sort the channels by max amount descending so we can build the route in a way that it
1283        // drains the largest channels first
1284        max_amount_per_channel.sort_by_key(|m| Reverse(m.amount_msat));
1285
1286        let amount_to_pay_msat = match invoice.amount_msat {
1287            Some(amount) => Ok(amount),
1288            None => Err(NodeError::generic("Invoice has no amount")),
1289        }?;
1290
1291        if amount_to_pay_msat > total_msat {
1292            return Err(NodeError::RouteNotFound(format!(
1293                "Amount too high, max amount is {total_msat} msat"
1294            )));
1295        }
1296
1297        // This is needed in greenlight for the signer to recognize this invoice.
1298        client
1299            .pre_approve_invoice(PreapproveinvoiceRequest {
1300                bolt11: bolt11.clone(),
1301            })
1302            .await?;
1303
1304        // We need to allocate a part id for each part that we are sending.
1305        let mut part_id = 1;
1306        // The total amount we sent. i.e. what the recipient received + fees
1307        let mut amount_sent_msat = 0;
1308        // The total amount received by the recipient
1309        let mut amount_received_msat = 0;
1310        // Generate a random group_id for the payment
1311        let group_id = rand::random::<u64>();
1312
1313        // The algorithm goes over each channel and drains it until the received amount
1314        // equals to the amount to pay defined in the bolt11 invoice.
1315        for max in max_amount_per_channel {
1316            // calculating the incoming amount for the remaining amount to pay.
1317            let left_to_pay_msat = amount_to_pay_msat - amount_received_msat;
1318            // Whether we draining the whole channel balance or only what is left to pay
1319            let to_pay_msat = std::cmp::min(left_to_pay_msat, max.amount_msat);
1320
1321            // We convert our payment path to an actual route that can be sent to the node.
1322            // This requires calculating the right fees and cltv delta in each hop.
1323            let (route, sent_msat) = convert_to_send_pay_route(
1324                max.path.clone(),
1325                to_pay_msat,
1326                invoice.min_final_cltv_expiry_delta,
1327            );
1328            info!("send_pay route to pay: {route:?}, received_amount = {to_pay_msat}");
1329            self.wait_channel_reestablished(&max.path).await?;
1330            // We send the part using the node API
1331            let req = SendpayRequest {
1332                route,
1333                payment_hash: hex::decode(invoice.payment_hash.clone())?,
1334                label: None,
1335                amount_msat: Some(Amount {
1336                    msat: amount_to_pay_msat,
1337                }),
1338                bolt11: Some(bolt11.clone()),
1339                payment_secret: Some(invoice.payment_secret.clone()),
1340                payment_metadata: None,
1341                partid: Some(part_id),
1342                localinvreqid: None,
1343                groupid: Some(group_id),
1344                description: None,
1345            };
1346            let mut client = client.clone();
1347            with_connection_retry!(client.send_pay(req.clone())).await?;
1348            part_id += 1;
1349            amount_sent_msat += sent_msat;
1350            amount_received_msat += to_pay_msat;
1351            if amount_received_msat == amount_to_pay_msat {
1352                break;
1353            }
1354        }
1355
1356        // Now we wait for the first part to be completed as a way to wait for the payment
1357        // to complete.
1358        let req = WaitsendpayRequest {
1359            payment_hash: hex::decode(invoice.payment_hash.clone())?,
1360            partid: Some(1),
1361            timeout: Some(self.sdk_config.payment_timeout_sec),
1362            groupid: Some(group_id),
1363        };
1364        let response = self
1365            .with_keep_alive(with_connection_retry!(client.wait_send_pay(req.clone())))
1366            .await?
1367            .into_inner();
1368        Ok(PaymentResponse {
1369            payment_time: response.completed_at.unwrap_or(response.created_at as f64) as i64,
1370            amount_msat: amount_received_msat,
1371            fee_msat: amount_sent_msat - amount_received_msat,
1372            payment_hash: invoice.payment_hash,
1373            payment_preimage: hex::encode(response.payment_preimage.unwrap_or_default()),
1374        })
1375    }
1376
1377    async fn send_payment(
1378        &self,
1379        bolt11: String,
1380        amount_msat: Option<u64>,
1381        label: Option<String>,
1382    ) -> NodeResult<Payment> {
1383        let mut description = None;
1384        if !bolt11.is_empty() {
1385            let invoice = parse_invoice(&bolt11)?;
1386            validate_network(invoice.clone(), self.sdk_config.network)?;
1387            description = invoice.description;
1388        }
1389
1390        let mut client = self.get_node_client().await?;
1391        let request = cln::PayRequest {
1392            bolt11,
1393            amount_msat: amount_msat.map(|amt| cln::Amount { msat: amt }),
1394            maxfeepercent: Some(self.sdk_config.maxfee_percent),
1395            retry_for: Some(self.sdk_config.payment_timeout_sec),
1396            label,
1397            maxdelay: None,
1398            riskfactor: None,
1399            localinvreqid: None,
1400            exclude: vec![],
1401            maxfee: None,
1402            description,
1403            exemptfee: Some(cln::Amount {
1404                msat: self.sdk_config.exemptfee_msat,
1405            }),
1406            partial_msat: None,
1407        };
1408        let result: cln::PayResponse = self
1409            .with_keep_alive(with_connection_retry!(client.pay(request.clone())))
1410            .await?
1411            .into_inner();
1412
1413        // Before returning from send_payment we need to make sure it is persisted in the backend node.
1414        // We do so by polling for the payment.
1415        let payment = Self::fetch_outgoing_payment_with_retry(client, result.payment_hash).await?;
1416        payment.try_into()
1417    }
1418
1419    async fn send_trampoline_payment(
1420        &self,
1421        bolt11: String,
1422        amount_msat: u64,
1423        label: Option<String>,
1424        trampoline_node_id: Vec<u8>,
1425    ) -> NodeResult<Payment> {
1426        let invoice = parse_invoice(&bolt11)?;
1427        validate_network(invoice.clone(), self.sdk_config.network)?;
1428        let label = serde_json::to_string(&PaymentLabel {
1429            trampoline: true,
1430            client_label: label,
1431            unix_nano: SystemTime::now().duration_since(UNIX_EPOCH)?.as_nanos(),
1432            amount_msat,
1433        })?;
1434        let fee_msat =
1435            (amount_msat.saturating_mul(TRAMPOLINE_FEE_PPM) / 1_000_000) + TRAMPOLINE_BASE_FEE_MSAT;
1436        let fee_percent = ((fee_msat as f64 / amount_msat as f64) * 100.) as f32;
1437        debug!("using fee msat {} fee percent {}", fee_msat, fee_percent);
1438        let mut client = self.get_client().await?;
1439        let request = TrampolinePayRequest {
1440            bolt11,
1441            trampoline_node_id,
1442            amount_msat,
1443            label,
1444            maxdelay: u32::default(),
1445            description: String::default(),
1446            maxfeepercent: fee_percent,
1447        };
1448        let result = self
1449            .with_keep_alive(with_connection_retry!(
1450                client.trampoline_pay(request.clone())
1451            ))
1452            .await?
1453            .into_inner();
1454
1455        let client = self.get_node_client().await?;
1456
1457        // Before returning from send_payment we need to make sure it is
1458        // persisted in the backend node. We do so by polling for the payment.
1459        // TODO: Ensure this works with trampoline payments
1460        // NOTE: If this doesn't work with trampoline payments, the sync also
1461        // needs updating.
1462        let payment = Self::fetch_outgoing_payment_with_retry(client, result.payment_hash).await?;
1463        payment.try_into()
1464    }
1465
1466    async fn send_spontaneous_payment(
1467        &self,
1468        node_id: String,
1469        amount_msat: u64,
1470        extra_tlvs: Option<Vec<TlvEntry>>,
1471        label: Option<String>,
1472    ) -> NodeResult<Payment> {
1473        let mut client: node::ClnClient = self.get_node_client().await?;
1474        let request = cln::KeysendRequest {
1475            destination: hex::decode(node_id)?,
1476            amount_msat: Some(cln::Amount { msat: amount_msat }),
1477            label: label.or(Some(format!(
1478                "breez-{}",
1479                SystemTime::now().duration_since(UNIX_EPOCH)?.as_millis()
1480            ))),
1481            extratlvs: extra_tlvs.map(|tlvs| cln::TlvStream {
1482                entries: tlvs
1483                    .into_iter()
1484                    .map(|tlv| cln::TlvEntry {
1485                        r#type: tlv.field_number,
1486                        value: tlv.value,
1487                    })
1488                    .collect(),
1489            }),
1490            routehints: None,
1491            maxfeepercent: Some(self.sdk_config.maxfee_percent),
1492            exemptfee: None,
1493            retry_for: Some(self.sdk_config.payment_timeout_sec),
1494            maxdelay: None,
1495            maxfee: None,
1496        };
1497
1498        // Not wrapped with connection retry, in case it causes to send twice.
1499        let result = self
1500            .with_keep_alive(client.key_send(request))
1501            .await?
1502            .into_inner();
1503
1504        // Before returning from send_payment we need to make sure it is persisted in the backend node.
1505        // We do so by polling for the payment.
1506        let payment = Self::fetch_outgoing_payment_with_retry(client, result.payment_hash).await?;
1507        payment.try_into()
1508    }
1509
1510    async fn node_id(&self) -> NodeResult<String> {
1511        Ok(hex::encode(self.get_signer().await.node_id()))
1512    }
1513
1514    async fn redeem_onchain_funds(
1515        &self,
1516        to_address: String,
1517        sat_per_vbyte: u32,
1518    ) -> NodeResult<Vec<u8>> {
1519        let mut client = self.get_node_client().await?;
1520
1521        let request = cln::WithdrawRequest {
1522            feerate: Some(cln::Feerate {
1523                style: Some(cln::feerate::Style::Perkw(sat_per_vbyte * 250)),
1524            }),
1525            satoshi: Some(cln::AmountOrAll {
1526                value: Some(cln::amount_or_all::Value::All(true)),
1527            }),
1528            destination: to_address,
1529            minconf: None,
1530            utxos: vec![],
1531        };
1532
1533        Ok(with_connection_retry!(client.withdraw(request.clone()))
1534            .await?
1535            .into_inner()
1536            .txid)
1537    }
1538
1539    async fn prepare_redeem_onchain_funds(
1540        &self,
1541        req: PrepareRedeemOnchainFundsRequest,
1542    ) -> NodeResult<PrepareRedeemOnchainFundsResponse> {
1543        let funds = self.list_funds().await?;
1544        let utxos = self.utxos(funds).await?;
1545
1546        let mut amount_msat: u64 = 0;
1547        let txins: Vec<TxIn> = utxos
1548            .iter()
1549            .map(|utxo| {
1550                amount_msat += utxo.amount_millisatoshi;
1551                TxIn {
1552                    previous_output: OutPoint {
1553                        txid: Txid::from_slice(&utxo.txid).unwrap(),
1554                        vout: 0,
1555                    },
1556                    script_sig: ScriptBuf::new(),
1557                    sequence: Sequence(0),
1558                    witness: Witness::default(),
1559                }
1560            })
1561            .collect();
1562
1563        let amount_sat = amount_msat / 1_000;
1564        let btc_address = Address::from_str(&req.to_address)?;
1565        let tx_out: Vec<TxOut> = vec![TxOut {
1566            value: amount_sat,
1567            script_pubkey: btc_address.payload.script_pubkey(),
1568        }];
1569        let tx = Transaction {
1570            version: 2,
1571            lock_time: crate::bitcoin::absolute::LockTime::ZERO,
1572            input: txins.clone(),
1573            output: tx_out,
1574        };
1575
1576        let witness_input_size: u64 = 110;
1577        let tx_weight = tx.strippedsize() as u64 * WITNESS_SCALE_FACTOR as u64
1578            + witness_input_size * txins.len() as u64;
1579        let fee: u64 = tx_weight * req.sat_per_vbyte as u64 / WITNESS_SCALE_FACTOR as u64;
1580        if fee >= amount_sat {
1581            return Err(NodeError::InsufficientFunds(
1582                "Insufficient funds to pay fees".to_string(),
1583            ));
1584        }
1585
1586        return Ok(PrepareRedeemOnchainFundsResponse {
1587            tx_weight,
1588            tx_fee_sat: fee,
1589        });
1590    }
1591
1592    /// Starts the signer that listens in a loop until the shutdown signal is received
1593    async fn start(&self, shutdown: mpsc::Receiver<()>) {
1594        match self.get_signer().await.run_forever(shutdown).await {
1595            Ok(_) => info!("signer exited gracefully"),
1596            Err(e) => error!("signer exited with error: {e}"),
1597        }
1598    }
1599
1600    async fn start_keep_alive(&self, mut shutdown: watch::Receiver<()>) {
1601        info!("keep alive started");
1602        let mut interval = tokio::time::interval(Duration::from_secs(15));
1603        interval.set_missed_tick_behavior(MissedTickBehavior::Skip);
1604        loop {
1605            tokio::select! {
1606                  _ = shutdown.changed() => {
1607                    info!("keep alive exited");
1608                    break;
1609                  }
1610                  _ = interval.tick() => {
1611                    let inprogress_payments = self.inprogress_payments.load(Ordering::Relaxed);
1612                    if inprogress_payments == 0 {
1613                      continue
1614                    }
1615                    let client_res = self.get_node_client().await;
1616                    match client_res {
1617                      Ok(mut client) => {
1618                        let res = client.getinfo(cln::GetinfoRequest {}).await;
1619                        match res {
1620                          Ok(_) => {
1621                            info!("keep alive ping sent, in progress payments: {inprogress_payments}");
1622                          }
1623                          Err(e) => {
1624                            error!("keep alive ping failed: {e}");
1625                          }
1626                        }
1627                      }
1628                      Err(e) => {
1629                        error!("keep alive ping failed to create client: {e}");
1630                      }
1631                    }
1632                  }
1633            }
1634        }
1635    }
1636
1637    async fn connect_peer(&self, id: String, addr: String) -> NodeResult<()> {
1638        let mut client = self.get_node_client().await?;
1639        let connect_req = cln::ConnectRequest {
1640            id: format!("{id}@{addr}"),
1641            host: None,
1642            port: None,
1643        };
1644        with_connection_retry!(client.connect_peer(connect_req.clone())).await?;
1645        Ok(())
1646    }
1647
1648    async fn sign_message(&self, message: &str) -> NodeResult<String> {
1649        let (sig, recovery_id) = self
1650            .get_signer()
1651            .await
1652            .sign_message(message.as_bytes().to_vec())?;
1653        let mut complete_signature = vec![31 + recovery_id];
1654        complete_signature.extend_from_slice(&sig);
1655        Ok(zbase32::encode_full_bytes(&complete_signature))
1656    }
1657
1658    async fn check_message(
1659        &self,
1660        message: &str,
1661        pubkey: &str,
1662        signature: &str,
1663    ) -> NodeResult<bool> {
1664        let pk = PublicKey::from_str(pubkey)?;
1665        Ok(verify(message.as_bytes(), signature, &pk))
1666    }
1667
1668    async fn sign_invoice(&self, invoice: RawBolt11Invoice) -> NodeResult<String> {
1669        let hrp_bytes = invoice.hrp.to_string().as_bytes().to_vec();
1670        let data_bytes = invoice.data.to_base32();
1671
1672        // create the message for the signer
1673        let msg_type: u16 = 8;
1674        let data_len: u16 = data_bytes.len().try_into()?;
1675        let mut data_len_bytes = data_len.to_be_bytes().to_vec();
1676        let mut data_buf = data_bytes.iter().copied().map(u5::to_u8).collect();
1677
1678        let hrp_len: u16 = hrp_bytes.len().try_into()?;
1679        let mut hrp_len_bytes = hrp_len.to_be_bytes().to_vec();
1680        let mut hrp_buf = hrp_bytes.to_vec();
1681
1682        let mut buf = msg_type.to_be_bytes().to_vec();
1683        buf.append(&mut data_len_bytes);
1684        buf.append(&mut data_buf);
1685        buf.append(&mut hrp_len_bytes);
1686        buf.append(&mut hrp_buf);
1687        // Sign the invoice using the signer
1688        let raw_result = self.get_signer().await.sign_invoice(buf)?;
1689        info!(
1690            "recover id: {:?} raw = {:?}",
1691            raw_result, raw_result[64] as i32
1692        );
1693        // contruct the RecoveryId
1694        let rid = RecoveryId::from_i32(raw_result[64] as i32).expect("recovery ID");
1695        let sig = &raw_result[0..64];
1696        let recoverable_sig = RecoverableSignature::from_compact(sig, rid)?;
1697
1698        let signed_invoice: Result<SignedRawBolt11Invoice> = invoice.sign(|_| Ok(recoverable_sig));
1699        Ok(signed_invoice?.to_string())
1700    }
1701
1702    async fn close_peer_channels(&self, node_id: String) -> NodeResult<Vec<String>> {
1703        let mut client = self.get_node_client().await?;
1704        let req = cln::ListpeerchannelsRequest {
1705            id: Some(hex::decode(node_id)?),
1706        };
1707        let closed_channels = with_connection_retry!(client.list_peer_channels(req.clone()))
1708            .await?
1709            .into_inner();
1710        let mut tx_ids = vec![];
1711        for channel in closed_channels.channels {
1712            let should_close = matches!(
1713                channel.state(),
1714                Openingd
1715                    | ChanneldAwaitingLockin
1716                    | ChanneldNormal
1717                    | ChanneldShuttingDown
1718                    | FundingSpendSeen
1719                    | DualopendOpenInit
1720                    | DualopendAwaitingLockin
1721            );
1722
1723            if should_close {
1724                let chan_id = channel.channel_id.ok_or(anyhow!("Empty channel id"))?;
1725                let req = cln::CloseRequest {
1726                    id: hex::encode(chan_id),
1727                    unilateraltimeout: None,
1728                    destination: None,
1729                    fee_negotiation_step: None,
1730                    wrong_funding: None,
1731                    force_lease_closed: None,
1732                    feerange: vec![],
1733                };
1734                let response = with_connection_retry!(client.close(req.clone())).await;
1735                match response {
1736                    Ok(res) => {
1737                        tx_ids.push(hex::encode(
1738                            res.into_inner()
1739                                .txid
1740                                .ok_or(anyhow!("Empty txid in close response"))?,
1741                        ));
1742                    }
1743                    Err(e) => Err(anyhow!("Empty closing channel: {e}"))?,
1744                };
1745            }
1746        }
1747        Ok(tx_ids)
1748    }
1749
1750    async fn stream_incoming_payments(
1751        &self,
1752    ) -> NodeResult<Pin<Box<dyn Stream<Item = IncomingPayment> + Send>>> {
1753        let mut client = self.get_client().await?;
1754        let req = gl_client::signer::model::greenlight::StreamIncomingFilter {};
1755        let stream = with_connection_retry!(client.stream_incoming(req.clone()))
1756            .await?
1757            .into_inner();
1758        Ok(Box::pin(stream.filter_map(|msg| match msg {
1759            Ok(msg) => match msg.details {
1760                Some(incoming_payment::Details::Offchain(p)) => Some(IncomingPayment {
1761                    label: p.label,
1762                    payment_hash: p.payment_hash,
1763                    preimage: p.preimage,
1764                    amount_msat: amount_to_msat(&p.amount.unwrap_or_default()),
1765                    bolt11: p.bolt11,
1766                }),
1767                _ => None,
1768            },
1769            Err(e) => {
1770                debug!("failed to receive message: {e}");
1771                None
1772            }
1773        })))
1774    }
1775
1776    async fn stream_log_messages(&self) -> NodeResult<Pin<Box<dyn Stream<Item = String> + Send>>> {
1777        let mut client = self.get_client().await?;
1778        let req = gl_client::signer::model::greenlight::StreamLogRequest {};
1779        let stream = with_connection_retry!(client.stream_log(req.clone()))
1780            .await?
1781            .into_inner();
1782        Ok(Box::pin(stream.filter_map(|msg| match msg {
1783            Ok(msg) => Some(msg.line),
1784            Err(e) => {
1785                debug!("failed to receive log message: {e}");
1786                None
1787            }
1788        })))
1789    }
1790
1791    async fn static_backup(&self) -> NodeResult<Vec<String>> {
1792        let mut client = self.get_node_client().await?;
1793        let req = cln::StaticbackupRequest {};
1794        let res = with_connection_retry!(client.static_backup(req.clone()))
1795            .await?
1796            .into_inner();
1797        let hex_vec: Vec<String> = res.scb.into_iter().map(hex::encode).collect();
1798        Ok(hex_vec)
1799    }
1800
1801    async fn generate_diagnostic_data(&self) -> NodeResult<Value> {
1802        let all_commands = vec![
1803            NodeCommand::GetInfo.to_string(),
1804            NodeCommand::ListPeerChannels.to_string(),
1805            NodeCommand::ListFunds.to_string(),
1806            NodeCommand::ListPayments.to_string(),
1807            NodeCommand::ListInvoices.to_string(),
1808        ];
1809
1810        let mut result = Map::new();
1811        for command in all_commands {
1812            let command_name = command.clone();
1813            let res = self
1814                .execute_command(command)
1815                .await
1816                .unwrap_or_else(|e| json!({ "error": e.to_string() }));
1817            result.insert(command_name, res);
1818        }
1819        Ok(Value::Object(result))
1820    }
1821
1822    async fn execute_command(&self, command: String) -> NodeResult<Value> {
1823        let node_cmd =
1824            NodeCommand::from_str(&command).map_err(|_| anyhow!("Command not found: {command}"))?;
1825
1826        let mut client = self.get_node_client().await?;
1827        match node_cmd {
1828            NodeCommand::ListPeers => {
1829                let req = cln::ListpeersRequest::default();
1830                let resp = with_connection_retry!(client.list_peers(req.clone()))
1831                    .await?
1832                    .into_inner();
1833
1834                Ok(crate::serializer::value::to_value(&resp)?)
1835            }
1836            NodeCommand::ListPeerChannels => {
1837                let req = cln::ListpeerchannelsRequest::default();
1838                let resp = with_connection_retry!(client.list_peer_channels(req.clone()))
1839                    .await?
1840                    .into_inner();
1841                Ok(crate::serializer::value::to_value(&resp)?)
1842            }
1843            NodeCommand::ListFunds => {
1844                let req = cln::ListfundsRequest::default();
1845                let resp = with_connection_retry!(client.list_funds(req.clone()))
1846                    .await?
1847                    .into_inner();
1848                Ok(crate::serializer::value::to_value(&resp)?)
1849            }
1850            NodeCommand::ListPayments => {
1851                let req = cln::ListpaysRequest::default();
1852                let resp = with_connection_retry!(client.list_pays(req.clone()))
1853                    .await?
1854                    .into_inner();
1855                Ok(crate::serializer::value::to_value(&resp)?)
1856            }
1857            NodeCommand::ListInvoices => {
1858                let req = cln::ListinvoicesRequest::default();
1859                let resp = with_connection_retry!(client.list_invoices(req.clone()))
1860                    .await?
1861                    .into_inner();
1862
1863                Ok(crate::serializer::value::to_value(&resp)?)
1864            }
1865            NodeCommand::CloseAllChannels => {
1866                let req = cln::ListpeersRequest::default();
1867                let resp = with_connection_retry!(client.list_peers(req.clone()))
1868                    .await?
1869                    .into_inner();
1870                for p in resp.peers {
1871                    self.close_peer_channels(hex::encode(p.id)).await?;
1872                }
1873
1874                Ok(Value::String("All channels were closed".to_string()))
1875            }
1876            NodeCommand::GetInfo => {
1877                let req = cln::GetinfoRequest::default();
1878                let resp = with_connection_retry!(client.getinfo(req.clone()))
1879                    .await?
1880                    .into_inner();
1881                Ok(crate::serializer::value::to_value(&resp)?)
1882            }
1883            NodeCommand::Stop => {
1884                let req = cln::StopRequest::default();
1885                let resp = with_connection_retry!(client.stop(req.clone()))
1886                    .await?
1887                    .into_inner();
1888                Ok(crate::serializer::value::to_value(&resp)?)
1889            }
1890        }
1891    }
1892
1893    async fn max_sendable_amount<'a>(
1894        &self,
1895        payee_node_id: Option<Vec<u8>>,
1896        max_hops: u32,
1897        last_hop_hint: Option<&'a RouteHintHop>,
1898    ) -> NodeResult<Vec<MaxChannelAmount>> {
1899        let mut client = self.get_node_client().await?;
1900
1901        let mut peers = HashMap::new();
1902        let req = cln::ListpeerchannelsRequest::default();
1903        with_connection_retry!(client.list_peer_channels(req.clone()))
1904            .await?
1905            .into_inner()
1906            .channels
1907            .into_iter()
1908            .for_each(|channel| {
1909                peers
1910                    .entry(channel.peer_id.clone())
1911                    .or_insert(Vec::new())
1912                    .push(channel)
1913            });
1914
1915        let mut max_channel_amounts = vec![];
1916        for (peer, channels) in peers {
1917            let max_amounts_for_peer = self
1918                .max_sendable_amount_from_peer(
1919                    peer,
1920                    channels,
1921                    payee_node_id.clone(),
1922                    max_hops,
1923                    last_hop_hint,
1924                )
1925                .await?;
1926            max_channel_amounts.extend_from_slice(max_amounts_for_peer.as_slice());
1927        }
1928        Ok(max_channel_amounts)
1929    }
1930
1931    async fn derive_bip32_key(&self, path: Vec<ChildNumber>) -> NodeResult<ExtendedPrivKey> {
1932        Self::derive_bip32_key(self.sdk_config.network, &self.get_signer().await, path)
1933    }
1934
1935    async fn legacy_derive_bip32_key(&self, path: Vec<ChildNumber>) -> NodeResult<ExtendedPrivKey> {
1936        Self::legacy_derive_bip32_key(self.sdk_config.network, &self.get_signer().await, path)
1937    }
1938
1939    async fn stream_custom_messages(
1940        &self,
1941    ) -> NodeResult<Pin<Box<dyn Stream<Item = Result<CustomMessage>> + Send>>> {
1942        let stream = {
1943            let mut client = self.get_client().await?;
1944            let req = gl_client::signer::model::greenlight::StreamCustommsgRequest {};
1945            with_connection_retry!(client.stream_custommsg(req.clone()))
1946                .await?
1947                .into_inner()
1948        };
1949
1950        Ok(Box::pin(stream.filter_map(|msg| {
1951            let msg = match msg {
1952                Ok(msg) => msg,
1953                Err(e) => return Some(Err(anyhow!("failed to receive message: {e}"))),
1954            };
1955
1956            if msg.payload.len() < 2 {
1957                debug!(
1958                    "received too short custom message payload: {:?}",
1959                    &msg.payload
1960                );
1961                return None;
1962            }
1963
1964            let msg_type = u16::from_be_bytes([msg.payload[0], msg.payload[1]]);
1965
1966            Some(Ok(CustomMessage {
1967                peer_id: msg.peer_id,
1968                message_type: msg_type,
1969                payload: msg.payload[2..].to_vec(),
1970            }))
1971        })))
1972    }
1973
1974    async fn send_custom_message(&self, message: CustomMessage) -> NodeResult<()> {
1975        let mut client = self.get_node_client().await?;
1976
1977        let mut msg = message.message_type.to_be_bytes().to_vec();
1978        msg.extend(message.payload);
1979        let req = cln::SendcustommsgRequest {
1980            msg,
1981            node_id: message.peer_id,
1982        };
1983        let resp = with_connection_retry!(client.send_custom_msg(req.clone()))
1984            .await?
1985            .into_inner();
1986        debug!("send_custom_message returned status {:?}", resp.status);
1987        Ok(())
1988    }
1989
1990    // Gets the routing hints related to all private channels that the node has
1991    async fn get_routing_hints(
1992        &self,
1993        lsp_info: &LspInformation,
1994    ) -> NodeResult<(Vec<RouteHint>, bool)> {
1995        let mut client = self.get_node_client().await?;
1996
1997        let open_peer_channels = self.get_open_peer_channels_pb().await?;
1998        let (open_peer_channels_private, open_peer_channels_public): (
1999            HashMap<Vec<u8>, ListpeerchannelsChannels>,
2000            HashMap<Vec<u8>, ListpeerchannelsChannels>,
2001        ) = open_peer_channels
2002            .into_iter()
2003            .partition(|(_, c)| c.private.unwrap_or_default());
2004        let has_public_channel = !open_peer_channels_public.is_empty();
2005
2006        let mut hints: Vec<RouteHint> = vec![];
2007
2008        // Get channels where our node is the destination
2009        let pubkey = self
2010            .persister
2011            .get_node_state()?
2012            .map(|n| n.id)
2013            .ok_or(NodeError::generic("Node info not found"))?;
2014        let req = cln::ListchannelsRequest {
2015            destination: Some(hex::decode(pubkey)?),
2016            ..Default::default()
2017        };
2018        let channels: HashMap<Vec<u8>, cln::ListchannelsChannels> =
2019            with_connection_retry!(client.list_channels(req.clone()))
2020                .await?
2021                .into_inner()
2022                .channels
2023                .into_iter()
2024                .map(|c| (c.source.clone(), c))
2025                .collect();
2026
2027        // Create a routing hint from each private channel.
2028        for (peer_id, peer_channel) in open_peer_channels_private {
2029            let peer_id_str = hex::encode(&peer_id);
2030            let optional_channel_id = peer_channel
2031                .alias
2032                .and_then(|a| a.remote)
2033                .or(peer_channel.short_channel_id);
2034
2035            if let Some(channel_id) = optional_channel_id {
2036                // The remote fee policy
2037                let maybe_policy = match channels.get(&peer_id) {
2038                    Some(channel) => Some((
2039                        channel.base_fee_millisatoshi,
2040                        channel.fee_per_millionth,
2041                        channel.delay,
2042                    )),
2043                    None if peer_id_str == lsp_info.pubkey => Some((
2044                        lsp_info.base_fee_msat as u32,
2045                        (lsp_info.fee_rate * 1000000.0) as u32,
2046                        lsp_info.time_lock_delta,
2047                    )),
2048                    _ => None,
2049                };
2050                match maybe_policy {
2051                    Some((fees_base_msat, fees_proportional_millionths, cltv_delta)) => {
2052                        debug!(
2053                            "For peer {peer_id_str}: remote base {fees_base_msat} proportional {fees_proportional_millionths} cltv_delta {cltv_delta}",
2054                        );
2055                        let hint = RouteHint {
2056                            hops: vec![RouteHintHop {
2057                                src_node_id: peer_id_str,
2058                                short_channel_id: channel_id,
2059                                fees_base_msat,
2060                                fees_proportional_millionths,
2061                                cltv_expiry_delta: cltv_delta as u64,
2062                                htlc_minimum_msat: Some(
2063                                    peer_channel
2064                                        .minimum_htlc_in_msat
2065                                        .clone()
2066                                        .unwrap_or_default()
2067                                        .msat,
2068                                ),
2069                                htlc_maximum_msat: None,
2070                            }],
2071                        };
2072                        info!("Generating hint hop as routing hint: {hint:?}");
2073                        hints.push(hint);
2074                    }
2075                    _ => debug!("No source channel found for peer: {peer_id_str:?}"),
2076                };
2077            }
2078        }
2079        Ok((hints, has_public_channel))
2080    }
2081
2082    async fn get_open_peers(&self) -> NodeResult<HashSet<Vec<u8>>> {
2083        let open_peer_channels = self.get_open_peer_channels_pb().await?;
2084        let open_peers: HashSet<Vec<u8>> = open_peer_channels.into_keys().collect();
2085        Ok(open_peers)
2086    }
2087}
2088
2089#[derive(Clone, PartialEq, Eq, Debug, EnumString, Display, Deserialize, Serialize)]
2090enum NodeCommand {
2091    /// Closes all channels of all peers.
2092    #[strum(serialize = "closeallchannels")]
2093    CloseAllChannels,
2094
2095    /// See <https://docs.corelightning.org/reference/lightning-getinfo>
2096    #[strum(serialize = "getinfo")]
2097    GetInfo,
2098
2099    /// See <https://docs.corelightning.org/reference/lightning-listfunds>
2100    #[strum(serialize = "listfunds")]
2101    ListFunds,
2102
2103    /// See <https://docs.corelightning.org/reference/lightning-listinvoices>
2104    #[strum(serialize = "listinvoices")]
2105    ListInvoices,
2106
2107    /// See <https://docs.corelightning.org/reference/lightning-listpays>
2108    #[strum(serialize = "listpayments")]
2109    ListPayments,
2110
2111    /// See <https://docs.corelightning.org/reference/lightning-listpeers>
2112    #[strum(serialize = "listpeers")]
2113    ListPeers,
2114
2115    /// See <https://docs.corelightning.org/reference/lightning-listpeerchannels>
2116    #[strum(serialize = "listpeerchannels")]
2117    ListPeerChannels,
2118
2119    /// Stops the node.
2120    ///
2121    /// Note that this command will return an error, as the node is stopped before it can reply.
2122    ///
2123    /// See <https://docs.corelightning.org/reference/lightning-stop>
2124    #[strum(serialize = "stop")]
2125    Stop,
2126}
2127
2128struct SendPayAgg {
2129    state: u8,
2130    created_at: u64,
2131    payment_hash: Vec<u8>,
2132    bolt11: Option<String>,
2133    destination: Option<Vec<u8>>,
2134    label: Option<String>,
2135    description: Option<String>,
2136    preimage: Option<Vec<u8>>,
2137    amount_sent: u64,
2138    amount: Option<u64>,
2139    num_nonfailed_parts: u64,
2140}
2141
2142fn update_payment_expirations(
2143    payments: Vec<Payment>,
2144    htlc_list: Vec<Htlc>,
2145) -> NodeResult<Vec<Payment>> {
2146    if htlc_list.is_empty() {
2147        return Ok(payments);
2148    }
2149
2150    let mut payments_res: Vec<Payment> = Vec::new();
2151    for mut payment in payments {
2152        if payment.status == PaymentStatus::Pending {
2153            let new_data = payment.clone().details;
2154            if let PaymentDetails::Ln { data } = new_data {
2155                for htlc in &htlc_list {
2156                    let payment_hash = hex::encode(htlc.clone().payment_hash);
2157                    if payment_hash == data.payment_hash
2158                        && data.pending_expiration_block < Some(htlc.expiry)
2159                    {
2160                        payment.details.add_pending_expiration_block(htlc.clone())
2161                    }
2162                }
2163            }
2164        }
2165        payments_res.push(payment);
2166    }
2167    info!("pending htlc payments {payments_res:?}");
2168    Ok(payments_res)
2169}
2170
2171impl TryFrom<ListsendpaysPayments> for SendPay {
2172    type Error = NodeError;
2173
2174    fn try_from(value: ListsendpaysPayments) -> std::result::Result<Self, Self::Error> {
2175        Ok(SendPay {
2176            created_index: value
2177                .created_index
2178                .ok_or(NodeError::generic("missing created index"))?,
2179            updated_index: value.updated_index,
2180            groupid: value.groupid.to_string(),
2181            partid: value.partid,
2182            payment_hash: value.payment_hash,
2183            status: value.status.try_into()?,
2184            amount_msat: value.amount_msat.map(|a| a.msat),
2185            destination: value.destination,
2186            created_at: value.created_at,
2187            amount_sent_msat: value.amount_sent_msat.map(|a| a.msat),
2188            label: value.label,
2189            bolt11: value.bolt11,
2190            description: value.description,
2191            bolt12: value.bolt12,
2192            payment_preimage: value.payment_preimage,
2193            erroronion: value.erroronion,
2194        })
2195    }
2196}
2197
2198impl TryFrom<i32> for SendPayStatus {
2199    type Error = NodeError;
2200
2201    fn try_from(value: i32) -> std::result::Result<Self, Self::Error> {
2202        match value {
2203            0 => Ok(Self::Pending),
2204            1 => Ok(Self::Failed),
2205            2 => Ok(Self::Complete),
2206            _ => Err(NodeError::generic("invalid send_pay status")),
2207        }
2208    }
2209}
2210
2211impl TryFrom<SendPayAgg> for Payment {
2212    type Error = NodeError;
2213
2214    fn try_from(value: SendPayAgg) -> std::result::Result<Self, Self::Error> {
2215        let ln_invoice = value
2216            .bolt11
2217            .as_ref()
2218            .ok_or(InvoiceError::generic("No bolt11 invoice"))
2219            .and_then(|b| parse_invoice(b));
2220
2221        // For trampoline payments the amount_msat doesn't match the actual
2222        // amount. If it's a trampoline payment, take the amount from the label.
2223        let (payment_amount, client_label) =
2224            serde_json::from_str::<PaymentLabel>(&value.label.clone().unwrap_or_default())
2225                .ok()
2226                .and_then(|label| {
2227                    label
2228                        .trampoline
2229                        .then_some((label.amount_msat, label.client_label))
2230                })
2231                .unwrap_or((value.amount.unwrap_or_default(), value.label));
2232        let fee_msat = value.amount_sent.saturating_sub(payment_amount);
2233        let status = if value.state & PAYMENT_STATE_COMPLETE > 0 {
2234            PaymentStatus::Complete
2235        } else if value.state & PAYMENT_STATE_PENDING > 0 {
2236            PaymentStatus::Pending
2237        } else {
2238            PaymentStatus::Failed
2239        };
2240        Ok(Self {
2241            id: hex::encode(&value.payment_hash),
2242            payment_type: PaymentType::Sent,
2243            payment_time: value.created_at as i64,
2244            amount_msat: match status {
2245                PaymentStatus::Complete => payment_amount,
2246                _ => ln_invoice
2247                    .as_ref()
2248                    .map_or(0, |i| i.amount_msat.unwrap_or_default()),
2249            },
2250            fee_msat,
2251            status,
2252            error: None,
2253            description: ln_invoice
2254                .as_ref()
2255                .map(|i| i.description.clone())
2256                .unwrap_or_default(),
2257            details: PaymentDetails::Ln {
2258                data: LnPaymentDetails {
2259                    payment_hash: hex::encode(&value.payment_hash),
2260                    label: client_label.unwrap_or_default(),
2261                    destination_pubkey: ln_invoice.map_or(
2262                        value.destination.map(hex::encode).unwrap_or_default(),
2263                        |i| i.payee_pubkey,
2264                    ),
2265                    payment_preimage: value.preimage.map(hex::encode).unwrap_or_default(),
2266                    keysend: value.bolt11.is_none(),
2267                    bolt11: value.bolt11.unwrap_or_default(),
2268                    open_channel_bolt11: None,
2269                    lnurl_success_action: None,
2270                    lnurl_pay_domain: None,
2271                    lnurl_pay_comment: None,
2272                    ln_address: None,
2273                    lnurl_metadata: None,
2274                    lnurl_withdraw_endpoint: None,
2275                    swap_info: None,
2276                    reverse_swap_info: None,
2277                    pending_expiration_block: None,
2278                },
2279            },
2280            metadata: None,
2281        })
2282    }
2283}
2284
2285/// Construct a lightning transaction from an invoice
2286impl TryFrom<cln::ListinvoicesInvoices> for Payment {
2287    type Error = NodeError;
2288
2289    fn try_from(invoice: cln::ListinvoicesInvoices) -> std::result::Result<Self, Self::Error> {
2290        let ln_invoice = invoice
2291            .bolt11
2292            .as_ref()
2293            .ok_or(InvoiceError::generic("No bolt11 invoice"))
2294            .and_then(|b| parse_invoice(b))?;
2295        Ok(Payment {
2296            id: hex::encode(invoice.payment_hash.clone()),
2297            payment_type: PaymentType::Received,
2298            payment_time: invoice.paid_at.map(|i| i as i64).unwrap_or_default(),
2299            amount_msat: invoice
2300                .amount_received_msat
2301                .or(invoice.amount_msat)
2302                .map(|a| a.msat)
2303                .unwrap_or_default(),
2304            fee_msat: 0,
2305            status: PaymentStatus::Complete,
2306            error: None,
2307            description: ln_invoice.description,
2308            details: PaymentDetails::Ln {
2309                data: LnPaymentDetails {
2310                    payment_hash: hex::encode(invoice.payment_hash),
2311                    label: invoice.label,
2312                    destination_pubkey: ln_invoice.payee_pubkey,
2313                    payment_preimage: invoice
2314                        .payment_preimage
2315                        .map(hex::encode)
2316                        .unwrap_or_default(),
2317                    keysend: false,
2318                    bolt11: invoice.bolt11.unwrap_or_default(),
2319                    lnurl_success_action: None, // For received payments, this is None
2320                    lnurl_pay_domain: None,     // For received payments, this is None
2321                    lnurl_pay_comment: None,    // For received payments, this is None
2322                    lnurl_metadata: None,       // For received payments, this is None
2323                    ln_address: None,
2324                    lnurl_withdraw_endpoint: None,
2325                    swap_info: None,
2326                    reverse_swap_info: None,
2327                    pending_expiration_block: None,
2328                    open_channel_bolt11: None,
2329                },
2330            },
2331            metadata: None,
2332        })
2333    }
2334}
2335
2336impl From<ListpaysPaysStatus> for PaymentStatus {
2337    fn from(value: ListpaysPaysStatus) -> Self {
2338        match value {
2339            ListpaysPaysStatus::Pending => PaymentStatus::Pending,
2340            ListpaysPaysStatus::Complete => PaymentStatus::Complete,
2341            ListpaysPaysStatus::Failed => PaymentStatus::Failed,
2342        }
2343    }
2344}
2345
2346impl TryFrom<cln::ListpaysPays> for Payment {
2347    type Error = NodeError;
2348
2349    fn try_from(payment: cln::ListpaysPays) -> NodeResult<Self, Self::Error> {
2350        let ln_invoice = payment
2351            .bolt11
2352            .as_ref()
2353            .ok_or(InvoiceError::generic("No bolt11 invoice"))
2354            .and_then(|b| parse_invoice(b));
2355        let payment_amount_sent = payment
2356            .amount_sent_msat
2357            .clone()
2358            .map(|a| a.msat)
2359            .unwrap_or_default();
2360
2361        // For trampoline payments the amount_msat doesn't match the actual
2362        // amount. If it's a trampoline payment, take the amount from the label.
2363        let (payment_amount, client_label) = serde_json::from_str::<PaymentLabel>(payment.label())
2364            .ok()
2365            .and_then(|label| {
2366                label
2367                    .trampoline
2368                    .then_some((label.amount_msat, label.client_label))
2369            })
2370            .unwrap_or((
2371                payment
2372                    .amount_msat
2373                    .clone()
2374                    .map(|a| a.msat)
2375                    .unwrap_or_default(),
2376                payment.label.clone(),
2377            ));
2378        let status = payment.status().into();
2379
2380        Ok(Payment {
2381            id: hex::encode(payment.payment_hash.clone()),
2382            payment_type: PaymentType::Sent,
2383            payment_time: payment.completed_at.unwrap_or(payment.created_at) as i64,
2384            amount_msat: match status {
2385                PaymentStatus::Complete => payment_amount,
2386                _ => ln_invoice
2387                    .as_ref()
2388                    .map_or(0, |i| i.amount_msat.unwrap_or_default()),
2389            },
2390            fee_msat: payment_amount_sent.saturating_sub(payment_amount),
2391            status,
2392            error: None,
2393            description: ln_invoice
2394                .as_ref()
2395                .map(|i| i.description.clone())
2396                .unwrap_or_default(),
2397            details: PaymentDetails::Ln {
2398                data: LnPaymentDetails {
2399                    payment_hash: hex::encode(payment.payment_hash),
2400                    label: client_label.unwrap_or_default(),
2401                    destination_pubkey: ln_invoice.map_or(
2402                        payment.destination.map(hex::encode).unwrap_or_default(),
2403                        |i| i.payee_pubkey,
2404                    ),
2405                    payment_preimage: payment.preimage.map(hex::encode).unwrap_or_default(),
2406                    keysend: payment.bolt11.is_none(),
2407                    bolt11: payment.bolt11.unwrap_or_default(),
2408                    lnurl_success_action: None,
2409                    lnurl_pay_domain: None,
2410                    lnurl_pay_comment: None,
2411                    lnurl_metadata: None,
2412                    ln_address: None,
2413                    lnurl_withdraw_endpoint: None,
2414                    swap_info: None,
2415                    reverse_swap_info: None,
2416                    pending_expiration_block: None,
2417                    open_channel_bolt11: None,
2418                },
2419            },
2420            metadata: None,
2421        })
2422    }
2423}
2424
2425impl TryFrom<cln::PayResponse> for PaymentResponse {
2426    type Error = NodeError;
2427
2428    fn try_from(payment: cln::PayResponse) -> std::result::Result<Self, Self::Error> {
2429        let payment_amount = payment.amount_msat.unwrap_or_default().msat;
2430        let payment_amount_sent = payment.amount_sent_msat.unwrap_or_default().msat;
2431
2432        Ok(PaymentResponse {
2433            payment_time: payment.created_at as i64,
2434            amount_msat: payment_amount,
2435            fee_msat: payment_amount_sent - payment_amount,
2436            payment_hash: hex::encode(payment.payment_hash),
2437            payment_preimage: hex::encode(payment.payment_preimage),
2438        })
2439    }
2440}
2441
2442impl TryFrom<cln::KeysendResponse> for PaymentResponse {
2443    type Error = NodeError;
2444
2445    fn try_from(payment: cln::KeysendResponse) -> std::result::Result<Self, Self::Error> {
2446        let payment_amount = payment.amount_msat.unwrap_or_default().msat;
2447        let payment_amount_sent = payment.amount_sent_msat.unwrap_or_default().msat;
2448
2449        Ok(PaymentResponse {
2450            payment_time: payment.created_at as i64,
2451            amount_msat: payment_amount,
2452            fee_msat: payment_amount_sent - payment_amount,
2453            payment_hash: hex::encode(payment.payment_hash),
2454            payment_preimage: hex::encode(payment.payment_preimage),
2455        })
2456    }
2457}
2458
2459fn amount_to_msat(amount: &gl_client::pb::greenlight::Amount) -> u64 {
2460    match amount.unit {
2461        Some(amount::Unit::Millisatoshi(val)) => val,
2462        Some(amount::Unit::Satoshi(val)) => val * 1000,
2463        Some(amount::Unit::Bitcoin(val)) => val * 100000000,
2464        Some(_) => 0,
2465        None => 0,
2466    }
2467}
2468
2469/// Conversion for an open channel
2470impl From<cln::ListpeerchannelsChannels> for Channel {
2471    fn from(c: cln::ListpeerchannelsChannels) -> Self {
2472        let state = match c.state() {
2473            Openingd | ChanneldAwaitingLockin | DualopendOpenInit | DualopendAwaitingLockin => {
2474                ChannelState::PendingOpen
2475            }
2476            ChanneldNormal => ChannelState::Opened,
2477            _ => ChannelState::PendingClose,
2478        };
2479
2480        let (alias_remote, alias_local) = match c.alias {
2481            Some(a) => (a.remote, a.local),
2482            None => (None, None),
2483        };
2484
2485        Channel {
2486            short_channel_id: c.short_channel_id,
2487            state,
2488            funding_txid: c.funding_txid.map(hex::encode).unwrap_or_default(),
2489            spendable_msat: c.spendable_msat.unwrap_or_default().msat,
2490            local_balance_msat: c.to_us_msat.unwrap_or_default().msat,
2491            receivable_msat: c.receivable_msat.unwrap_or_default().msat,
2492            closed_at: None,
2493            funding_outnum: c.funding_outnum,
2494            alias_remote,
2495            alias_local,
2496            closing_txid: None,
2497            htlcs: c
2498                .htlcs
2499                .into_iter()
2500                .map(|c| Htlc::from(c.expiry, c.payment_hash))
2501                .collect(),
2502        }
2503    }
2504}
2505
2506fn convert_to_send_pay_route(
2507    route: PaymentPath,
2508    to_pay_msat: u64,
2509    final_cltv_delta: u64,
2510) -> (Vec<SendpayRoute>, u64) {
2511    let mut sendpay_route = vec![];
2512    let mut to_forward = to_pay_msat;
2513    let mut cltv_delay = 0;
2514    let hops_arr = route.edges.as_slice();
2515
2516    let reverse_hops: Vec<&PaymentPathEdge> = hops_arr.iter().rev().collect();
2517
2518    // Iterating over the path in a reverse order so we can calculate
2519    // the cltv deltas and fees.
2520    for (reverse_index, hop) in reverse_hops.iter().enumerate() {
2521        //let hop = h.clone();
2522        (to_forward, cltv_delay) = match reverse_index == 0 {
2523            // last hop should not take any fees and should use the final_cltv_delta.
2524            true => (to_forward, final_cltv_delta),
2525
2526            // all other hops are forwarding therefore should take fees and increase the cltv delta.
2527            false => (
2528                reverse_hops[reverse_index - 1].amount_from_forward(to_forward),
2529                cltv_delay + reverse_hops[reverse_index - 1].channel_delay,
2530            ),
2531        };
2532
2533        sendpay_route.insert(
2534            0,
2535            SendpayRoute {
2536                amount_msat: Some(gl_client::pb::cln::Amount { msat: to_forward }),
2537                id: hop.node_id.clone(),
2538                delay: cltv_delay as u32,
2539                channel: hop.short_channel_id.clone(),
2540            },
2541        );
2542    }
2543
2544    (sendpay_route, to_forward)
2545}
2546
2547impl TryFrom<ListclosedchannelsClosedchannels> for Channel {
2548    type Error = NodeError;
2549
2550    fn try_from(
2551        c: cln::ListclosedchannelsClosedchannels,
2552    ) -> std::result::Result<Self, Self::Error> {
2553        let (alias_remote, alias_local) = match c.alias {
2554            Some(a) => (a.remote, a.local),
2555            None => (None, None),
2556        };
2557
2558        // To keep the conversion simple and fast, some closing-related fields (closed_at, closing_txid)
2559        // are left empty here in the conversion, but populated later (via chain service lookup, or DB lookup)
2560        let local_balance_msat = c
2561            .final_to_us_msat
2562            .ok_or(anyhow!("final_to_us_msat is missing"))?
2563            .msat;
2564        Ok(Channel {
2565            short_channel_id: c.short_channel_id,
2566            state: ChannelState::Closed,
2567            funding_txid: hex::encode(c.funding_txid),
2568            spendable_msat: local_balance_msat,
2569            local_balance_msat,
2570            receivable_msat: 0,
2571            closed_at: None,
2572            funding_outnum: Some(c.funding_outnum),
2573            alias_remote,
2574            alias_local,
2575            closing_txid: None,
2576            htlcs: Vec::new(),
2577        })
2578    }
2579}
2580
2581#[cfg(test)]
2582mod tests {
2583    use crate::greenlight::node_api::convert_to_send_pay_route;
2584    use crate::{models, PaymentPath, PaymentPathEdge};
2585    use anyhow::Result;
2586    use gl_client::pb::cln::ChannelState::*;
2587    use gl_client::pb::cln::{Amount, ChannelState};
2588    use gl_client::pb::{self, cln};
2589
2590    #[test]
2591    fn test_convert_route() -> Result<()> {
2592        let path = PaymentPath {
2593            edges: vec![
2594                PaymentPathEdge {
2595                    node_id: vec![1],
2596                    short_channel_id: "807189x2048x0".into(),
2597                    channel_delay: 34,
2598                    base_fee_msat: 1000,
2599                    fee_per_millionth: 10,
2600                },
2601                PaymentPathEdge {
2602                    node_id: vec![2],
2603                    short_channel_id: "811871x2726x1".into(),
2604                    channel_delay: 34,
2605                    base_fee_msat: 0,
2606                    fee_per_millionth: 0,
2607                },
2608                PaymentPathEdge {
2609                    node_id: vec![3],
2610                    short_channel_id: "16000000x0x18087".into(),
2611                    channel_delay: 40,
2612                    base_fee_msat: 1000,
2613                    fee_per_millionth: 1,
2614                },
2615            ],
2616        };
2617
2618        let (r, sent) = convert_to_send_pay_route(path, 50000000, 144);
2619        assert_eq!(
2620            r,
2621            vec![
2622                pb::cln::SendpayRoute {
2623                    amount_msat: Some(gl_client::pb::cln::Amount { msat: 50001050 }),
2624                    id: vec![1],
2625                    delay: 218,
2626                    channel: "807189x2048x0".into(),
2627                },
2628                pb::cln::SendpayRoute {
2629                    amount_msat: Some(gl_client::pb::cln::Amount { msat: 50001050 }),
2630                    id: vec![2],
2631                    delay: 184,
2632                    channel: "811871x2726x1".into(),
2633                },
2634                pb::cln::SendpayRoute {
2635                    amount_msat: Some(gl_client::pb::cln::Amount { msat: 50000000 }),
2636                    id: vec![3],
2637                    delay: 144,
2638                    channel: "16000000x0x18087".into(),
2639                }
2640            ]
2641        );
2642        assert_eq!(sent, 50001050);
2643
2644        let path = PaymentPath {
2645            edges: vec![
2646                PaymentPathEdge {
2647                    node_id: vec![1],
2648                    short_channel_id: "807189x2048x0".into(),
2649                    channel_delay: 34,
2650                    base_fee_msat: 1000,
2651                    fee_per_millionth: 10,
2652                },
2653                PaymentPathEdge {
2654                    node_id: vec![2],
2655                    short_channel_id: "811871x2726x1".into(),
2656                    channel_delay: 34,
2657                    base_fee_msat: 0,
2658                    fee_per_millionth: 0,
2659                },
2660                PaymentPathEdge {
2661                    node_id: vec![3],
2662                    short_channel_id: "16000000x0x18087".into(),
2663                    channel_delay: 40,
2664                    base_fee_msat: 0,
2665                    fee_per_millionth: 2000,
2666                },
2667            ],
2668        };
2669        let (r, sent) = convert_to_send_pay_route(path, 50000000, 144);
2670        assert_eq!(
2671            r,
2672            vec![
2673                pb::cln::SendpayRoute {
2674                    amount_msat: Some(gl_client::pb::cln::Amount { msat: 50100000 }),
2675                    id: vec![1],
2676                    delay: 218,
2677                    channel: "807189x2048x0".into(),
2678                },
2679                pb::cln::SendpayRoute {
2680                    amount_msat: Some(gl_client::pb::cln::Amount { msat: 50100000 }),
2681                    id: vec![2],
2682                    delay: 184,
2683                    channel: "811871x2726x1".into(),
2684                },
2685                pb::cln::SendpayRoute {
2686                    amount_msat: Some(gl_client::pb::cln::Amount { msat: 50000000 }),
2687                    id: vec![3],
2688                    delay: 144,
2689                    channel: "16000000x0x18087".into(),
2690                }
2691            ]
2692        );
2693        assert_eq!(sent, 50100000);
2694
2695        Ok(())
2696    }
2697
2698    #[test]
2699    fn test_channel_states() -> Result<()> {
2700        for s in &[Openingd, ChanneldAwaitingLockin] {
2701            let c: models::Channel = cln_channel(s).into();
2702            assert_eq!(c.state, models::ChannelState::PendingOpen);
2703        }
2704
2705        let s = ChanneldNormal;
2706        let c: models::Channel = cln_channel(&s).into();
2707        assert_eq!(c.state, models::ChannelState::Opened);
2708
2709        for s in &[
2710            ChanneldShuttingDown,
2711            ClosingdSigexchange,
2712            ClosingdComplete,
2713            AwaitingUnilateral,
2714            FundingSpendSeen,
2715        ] {
2716            let c: models::Channel = cln_channel(s).into();
2717            assert_eq!(c.state, models::ChannelState::PendingClose);
2718        }
2719
2720        let c: models::Channel = cln_channel(&Onchain).into();
2721        assert_eq!(c.state, models::ChannelState::PendingClose);
2722
2723        Ok(())
2724    }
2725
2726    fn cln_channel(state: &ChannelState) -> cln::ListpeerchannelsChannels {
2727        cln::ListpeerchannelsChannels {
2728            state: (*state).into(),
2729            scratch_txid: None,
2730            feerate: None,
2731            owner: None,
2732            short_channel_id: None,
2733            channel_id: None,
2734            funding_txid: None,
2735            funding_outnum: None,
2736            initial_feerate: None,
2737            last_feerate: None,
2738            next_feerate: None,
2739            next_fee_step: None,
2740            inflight: vec![],
2741            close_to: None,
2742            private: Some(true),
2743            opener: 0,
2744            closer: None,
2745            funding: None,
2746            to_us_msat: None,
2747            min_to_us_msat: None,
2748            max_to_us_msat: None,
2749            total_msat: Some(Amount { msat: 1_000 }),
2750            fee_base_msat: None,
2751            fee_proportional_millionths: None,
2752            dust_limit_msat: Some(Amount { msat: 10 }),
2753            max_total_htlc_in_msat: None,
2754            their_reserve_msat: None,
2755            our_reserve_msat: None,
2756            spendable_msat: Some(Amount { msat: 20_000 }),
2757            receivable_msat: Some(Amount { msat: 960_000 }),
2758            minimum_htlc_in_msat: None,
2759            minimum_htlc_out_msat: None,
2760            maximum_htlc_out_msat: None,
2761            their_to_self_delay: Some(144),
2762            our_to_self_delay: Some(144),
2763            max_accepted_htlcs: None,
2764            alias: None,
2765            status: vec![],
2766            in_payments_offered: None,
2767            in_offered_msat: None,
2768            in_payments_fulfilled: None,
2769            in_fulfilled_msat: None,
2770            out_payments_offered: None,
2771            out_offered_msat: None,
2772            out_payments_fulfilled: None,
2773            out_fulfilled_msat: None,
2774            htlcs: vec![],
2775            close_to_addr: None,
2776            peer_id: vec![],
2777            peer_connected: false,
2778            updates: None,
2779            ignore_fee_limits: None,
2780            lost_state: None,
2781            last_stable_connection: None,
2782            reestablished: None,
2783            direction: None,
2784            last_tx_fee_msat: None,
2785            our_max_htlc_value_in_flight_msat: None,
2786            their_max_htlc_value_in_flight_msat: None,
2787        }
2788    }
2789}