Skip to content

Commit 55d3484

Browse files
committed
Add deserializer for ohttp keys
1 parent e220bbd commit 55d3484

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

payjoin-cli/src/app/config.rs

Lines changed: 29 additions & 12 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,
@@ -72,7 +73,8 @@ impl AppConfig {
7273
"ohttp_relay",
7374
matches.get_one::<Url>("ohttp_relay").map(|s| s.as_str()),
7475
)?
75-
.set_default("pj_directory", "https://payjo.in")?;
76+
.set_default("pj_directory", "https://payjo.in")?
77+
.set_default("ohttp_keys", None::<String>)?;
7678

7779
let builder = match matches.subcommand() {
7880
Some(("send", _)) => builder,
@@ -92,24 +94,17 @@ impl AppConfig {
9294
)?
9395
};
9496

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-
10597
#[cfg(feature = "v2")]
10698
let builder = {
10799
builder
108100
.set_override_option(
109101
"pj_directory",
110102
matches.get_one::<Url>("pj_directory").map(|s| s.as_str()),
111103
)?
112-
.set_override_option("ohttp_keys", ohttp_keys)?
104+
.set_override_option(
105+
"ohttp_keys",
106+
matches.get_one::<String>("ohttp_keys").map(|s| s.as_str()),
107+
)?
113108
};
114109

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

0 commit comments

Comments
 (0)