diff --git a/payjoin-cli/src/app/config.rs b/payjoin-cli/src/app/config.rs index 967c86f8..cdb213cb 100644 --- a/payjoin-cli/src/app/config.rs +++ b/payjoin-cli/src/app/config.rs @@ -20,6 +20,7 @@ pub struct AppConfig { // v2 only #[cfg(feature = "v2")] + #[serde(deserialize_with = "deserialize_ohttp_keys_from_path")] pub ohttp_keys: Option, #[cfg(feature = "v2")] pub ohttp_relay: Url, @@ -92,16 +93,6 @@ impl AppConfig { )? }; - #[cfg(feature = "v2")] - let ohttp_keys = matches - .get_one::("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 @@ -109,7 +100,10 @@ impl AppConfig { "pj_directory", matches.get_one::("pj_directory").map(|s| s.as_str()), )? - .set_override_option("ohttp_keys", ohttp_keys)? + .set_override_option( + "ohttp_keys", + matches.get_one::("ohttp_keys").map(|s| s.as_str()), + )? }; let max_fee_rate = matches @@ -132,3 +126,25 @@ impl AppConfig { Ok(app_config) } } + +#[cfg(feature = "v2")] +fn deserialize_ohttp_keys_from_path<'de, D>( + deserializer: D, +) -> Result, D::Error> +where + D: serde::Deserializer<'de>, +{ + let path_str: Option = 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), + } +}