Skip to content

Commit

Permalink
Add deserializer for ohttp keys
Browse files Browse the repository at this point in the history
  • Loading branch information
shinghim committed Dec 20, 2024
1 parent e220bbd commit 549a664
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 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 @@ -92,24 +93,17 @@ impl AppConfig {
)?
};

#[cfg(feature = "v2")]
let ohttp_keys = matches
.get_one::<String>("ohttp_keys")
.map(std::fs::read)
.transpose()
.map_err(|e| {
log::error!("Failed to read ohttp_keys file: {}", e);
ConfigError::Message(format!("Failed to read ohttp_keys file: {}", e))
})?;

#[cfg(feature = "v2")]
let builder = {
builder
.set_override_option(
"pj_directory",
matches.get_one::<Url>("pj_directory").map(|s| s.as_str()),
)?
.set_override_option("ohttp_keys", ohttp_keys)?
.set_override_option(
"ohttp_keys",
matches.get_one::<String>("ohttp_keys").map(|s| s.as_str()),
)?
};

let max_fee_rate = matches
Expand All @@ -132,3 +126,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),
}
}

0 comments on commit 549a664

Please sign in to comment.