Skip to content

Commit 549a664

Browse files
committed
Add deserializer for ohttp keys
1 parent e220bbd commit 549a664

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

payjoin-cli/src/app/config.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct AppConfig {
2020

2121
// v2 only
2222
#[cfg(feature = "v2")]
23+
#[serde(deserialize_with = "deserialize_ohttp_keys_from_path")]
2324
pub ohttp_keys: Option<payjoin::OhttpKeys>,
2425
#[cfg(feature = "v2")]
2526
pub ohttp_relay: Url,
@@ -92,24 +93,17 @@ impl AppConfig {
9293
)?
9394
};
9495

95-
#[cfg(feature = "v2")]
96-
let ohttp_keys = matches
97-
.get_one::<String>("ohttp_keys")
98-
.map(std::fs::read)
99-
.transpose()
100-
.map_err(|e| {
101-
log::error!("Failed to read ohttp_keys file: {}", e);
102-
ConfigError::Message(format!("Failed to read ohttp_keys file: {}", e))
103-
})?;
104-
10596
#[cfg(feature = "v2")]
10697
let builder = {
10798
builder
10899
.set_override_option(
109100
"pj_directory",
110101
matches.get_one::<Url>("pj_directory").map(|s| s.as_str()),
111102
)?
112-
.set_override_option("ohttp_keys", ohttp_keys)?
103+
.set_override_option(
104+
"ohttp_keys",
105+
matches.get_one::<String>("ohttp_keys").map(|s| s.as_str()),
106+
)?
113107
};
114108

115109
let max_fee_rate = matches
@@ -132,3 +126,25 @@ impl AppConfig {
132126
Ok(app_config)
133127
}
134128
}
129+
130+
#[cfg(feature = "v2")]
131+
fn deserialize_ohttp_keys_from_path<'de, D>(
132+
deserializer: D,
133+
) -> Result<Option<payjoin::OhttpKeys>, D::Error>
134+
where
135+
D: serde::Deserializer<'de>,
136+
{
137+
let path_str: Option<String> = Option::deserialize(deserializer)?;
138+
139+
match path_str {
140+
None => Ok(None),
141+
Some(path) => std::fs::read(path)
142+
.map_err(|e| serde::de::Error::custom(format!("Failed to read ohttp_keys file: {}", e)))
143+
.and_then(|bytes| {
144+
payjoin::OhttpKeys::decode(&bytes).map_err(|e| {
145+
serde::de::Error::custom(format!("Failed to decode ohttp keys: {}", e))
146+
})
147+
})
148+
.map(Some),
149+
}
150+
}

0 commit comments

Comments
 (0)