• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

payjoin / rust-payjoin / 17840817924

18 Sep 2025 08:38PM UTC coverage: 79.013% (-5.6%) from 84.625%
17840817924

Pull #1091

github

web-flow
Merge ef714af46 into 0859949d5
Pull Request #1091: Add pki-https feature to prevent local cert validation on all-features

3 of 4 new or added lines in 1 file covered. (75.0%)

539 existing lines in 9 files now uncovered.

7556 of 9563 relevant lines covered (79.01%)

486.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/payjoin-cli/src/app/v2/ohttp.rs
1
use std::sync::{Arc, Mutex};
2

3
use anyhow::{anyhow, Result};
4

5
use super::Config;
6

7
#[derive(Debug, Clone)]
8
pub struct RelayManager {
9
    selected_relay: Option<payjoin::Url>,
10
    failed_relays: Vec<payjoin::Url>,
11
}
12

13
impl RelayManager {
UNCOV
14
    pub fn new() -> Self { RelayManager { selected_relay: None, failed_relays: Vec::new() } }
×
15

UNCOV
16
    pub fn set_selected_relay(&mut self, relay: payjoin::Url) { self.selected_relay = Some(relay); }
×
17

UNCOV
18
    pub fn get_selected_relay(&self) -> Option<payjoin::Url> { self.selected_relay.clone() }
×
19

20
    pub fn add_failed_relay(&mut self, relay: payjoin::Url) { self.failed_relays.push(relay); }
×
21

UNCOV
22
    pub fn get_failed_relays(&self) -> Vec<payjoin::Url> { self.failed_relays.clone() }
×
23
}
24

25
pub(crate) struct ValidatedOhttpKeys {
26
    pub(crate) ohttp_keys: payjoin::OhttpKeys,
27
    pub(crate) relay_url: payjoin::Url,
28
}
29

UNCOV
30
pub(crate) async fn unwrap_ohttp_keys_or_else_fetch(
×
UNCOV
31
    config: &Config,
×
UNCOV
32
    directory: Option<payjoin::Url>,
×
UNCOV
33
    relay_manager: Arc<Mutex<RelayManager>>,
×
UNCOV
34
) -> Result<ValidatedOhttpKeys> {
×
UNCOV
35
    if let Some(ohttp_keys) = config.v2()?.ohttp_keys.clone() {
×
UNCOV
36
        println!("Using OHTTP Keys from config");
×
37
        return Ok(ValidatedOhttpKeys {
UNCOV
38
            ohttp_keys,
×
UNCOV
39
            relay_url: config.v2()?.ohttp_relays[0].clone(),
×
40
        });
41
    } else {
UNCOV
42
        println!("Bootstrapping private network transport over Oblivious HTTP");
×
UNCOV
43
        let fetched_keys = fetch_ohttp_keys(config, directory, relay_manager).await?;
×
44

UNCOV
45
        Ok(fetched_keys)
×
46
    }
UNCOV
47
}
×
48

UNCOV
49
async fn fetch_ohttp_keys(
×
UNCOV
50
    config: &Config,
×
UNCOV
51
    directory: Option<payjoin::Url>,
×
UNCOV
52
    relay_manager: Arc<Mutex<RelayManager>>,
×
UNCOV
53
) -> Result<ValidatedOhttpKeys> {
×
54
    use payjoin::bitcoin::secp256k1::rand::prelude::SliceRandom;
UNCOV
55
    let payjoin_directory = directory.unwrap_or(config.v2()?.pj_directory.clone());
×
UNCOV
56
    let relays = config.v2()?.ohttp_relays.clone();
×
57

58
    loop {
UNCOV
59
        let failed_relays =
×
UNCOV
60
            relay_manager.lock().expect("Lock should not be poisoned").get_failed_relays();
×
61

UNCOV
62
        let remaining_relays: Vec<_> =
×
UNCOV
63
            relays.iter().filter(|r| !failed_relays.contains(r)).cloned().collect();
×
64

UNCOV
65
        if remaining_relays.is_empty() {
×
66
            return Err(anyhow!("No valid relays available"));
×
UNCOV
67
        }
×
68

UNCOV
69
        let selected_relay =
×
UNCOV
70
            match remaining_relays.choose(&mut payjoin::bitcoin::key::rand::thread_rng()) {
×
UNCOV
71
                Some(relay) => relay.clone(),
×
72
                None => return Err(anyhow!("Failed to select from remaining relays")),
×
73
            };
74

UNCOV
75
        relay_manager
×
UNCOV
76
            .lock()
×
UNCOV
77
            .expect("Lock should not be poisoned")
×
UNCOV
78
            .set_selected_relay(selected_relay.clone());
×
79

UNCOV
80
        let ohttp_keys = {
×
81
            #[cfg(all(not(feature = "pki-https"), feature = "_manual-tls"))]
82
            {
83
                if let Some(cert_path) = config.root_certificate.as_ref() {
84
                    let cert_der = std::fs::read(cert_path)?;
85
                    payjoin::io::fetch_ohttp_keys_with_cert(
86
                        selected_relay.as_str(),
87
                        payjoin_directory.as_str(),
88
                        cert_der,
89
                    )
90
                    .await
91
                } else {
92
                    payjoin::io::fetch_ohttp_keys(
93
                        selected_relay.as_str(),
94
                        payjoin_directory.as_str(),
95
                    )
96
                    .await
97
                }
98
            }
99
            #[cfg(feature = "pki-https")]
UNCOV
100
            payjoin::io::fetch_ohttp_keys(selected_relay.as_str(), payjoin_directory.as_str()).await
×
101
        };
102

103
        match ohttp_keys {
×
UNCOV
104
            Ok(keys) =>
×
UNCOV
105
                return Ok(ValidatedOhttpKeys { ohttp_keys: keys, relay_url: selected_relay }),
×
106
            Err(payjoin::io::Error::UnexpectedStatusCode(e)) => {
×
107
                return Err(payjoin::io::Error::UnexpectedStatusCode(e).into());
×
108
            }
109
            Err(e) => {
×
110
                tracing::debug!("Failed to connect to relay: {selected_relay}, {e:?}");
×
111
                relay_manager
×
112
                    .lock()
×
113
                    .expect("Lock should not be poisoned")
×
114
                    .add_failed_relay(selected_relay);
×
115
            }
116
        }
117
    }
UNCOV
118
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc