Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate error if the fs read for ohttp keys fails during config #435

Merged
merged 1 commit into from
Dec 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions payjoin-cli/src/app/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct AppConfig {

// v2 only
#[cfg(feature = "v2")]
#[serde(deserialize_with = "deserialize_ohttp_keys_from_path")]
pub ohttp_keys: Option<payjoin::OhttpKeys>,
#[cfg(feature = "v2")]
pub ohttp_relay: Url,
Expand Down Expand Up @@ -72,7 +73,8 @@ impl AppConfig {
"ohttp_relay",
matches.get_one::<Url>("ohttp_relay").map(|s| s.as_str()),
)?
.set_default("pj_directory", "https://payjo.in")?;
.set_default("pj_directory", "https://payjo.in")?
.set_default("ohttp_keys", None::<String>)?;

let builder = match matches.subcommand() {
Some(("send", _)) => builder,
Expand Down Expand Up @@ -101,17 +103,7 @@ impl AppConfig {
)?
.set_override_option(
"ohttp_keys",
matches.get_one::<String>("ohttp_keys").and_then(|s| {
std::fs::read(s)
.map_err(|e| {
log::error!("Failed to read ohttp_keys file: {}", e);
ConfigError::Message(format!(
"Failed to read ohttp_keys file: {}",
e
))
})
.ok()
}),
matches.get_one::<String>("ohttp_keys").map(|s| s.as_str()),
)?
};

Expand All @@ -135,3 +127,25 @@ impl AppConfig {
Ok(app_config)
}
}

#[cfg(feature = "v2")]
fn deserialize_ohttp_keys_from_path<'de, D>(
deserializer: D,
) -> Result<Option<payjoin::OhttpKeys>, D::Error>
where
D: serde::Deserializer<'de>,
{
let path_str: Option<String> = Option::deserialize(deserializer)?;

match path_str {
None => Ok(None),
Some(path) => std::fs::read(path)
.map_err(|e| serde::de::Error::custom(format!("Failed to read ohttp_keys file: {}", e)))
.and_then(|bytes| {
payjoin::OhttpKeys::decode(&bytes).map_err(|e| {
serde::de::Error::custom(format!("Failed to decode ohttp keys: {}", e))
})
})
.map(Some),
}
}
Loading