Skip to content

Commit 78e24d6

Browse files
committed
Abstract bytewords coding with helper function
1 parent ebc0b1c commit 78e24d6

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

payjoin/src/receive/v2.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ use bitcoin::psbt::Psbt;
44
use bitcoin::{base64, Amount, FeeRate, OutPoint, Script, TxOut};
55
use serde::ser::SerializeStruct;
66
use serde::{Deserialize, Serialize, Serializer};
7-
use ur::bytewords;
87
use url::Url;
98

109
use super::{Error, InternalRequestError, RequestError, SelectionError};
1110
use crate::psbt::PsbtExt;
1211
use crate::receive::optional_parameters::Params;
12+
use crate::v2::encode_minimal_bytewords;
1313
use crate::OhttpKeys;
1414

1515
/// Represents data that needs to be transmitted to the payjoin directory.
@@ -59,7 +59,7 @@ impl Enroller {
5959

6060
pub fn subdirectory(&self) -> String {
6161
let pubkey = &self.s.public_key().serialize();
62-
bytewords::encode(pubkey, bytewords::Style::Minimal)
62+
encode_minimal_bytewords(pubkey)
6363
}
6464

6565
pub fn payjoin_subdir(&self) -> String { format!("{}/{}", self.subdirectory(), "payjoin") }
@@ -98,7 +98,7 @@ impl Enroller {
9898
}
9999

100100
fn subdirectory(pubkey: &bitcoin::secp256k1::PublicKey) -> String {
101-
bytewords::encode(&pubkey.serialize(), bytewords::Style::Minimal)
101+
encode_minimal_bytewords(&pubkey.serialize())
102102
}
103103

104104
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -270,7 +270,7 @@ impl Enrolled {
270270

271271
pub fn fallback_target(&self) -> String {
272272
let pubkey = &self.s.public_key().serialize();
273-
let subdirectory = bytewords::encode(pubkey, bytewords::Style::Minimal);
273+
let subdirectory = encode_minimal_bytewords(pubkey);
274274
format!("{}{}", &self.directory, subdirectory)
275275
}
276276
}

payjoin/src/send/mod.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ use serde::{
3737
ser::SerializeStruct,
3838
Deserialize, Deserializer, Serialize, Serializer,
3939
};
40-
use ur::bytewords;
4140
use url::Url;
4241

4342
use crate::input_type::InputType;
4443
use crate::psbt::PsbtExt;
4544
use crate::uri::UriExt;
45+
#[cfg(feature = "v2")]
46+
use crate::v2::{decode_minimal_bytewords, encode_minimal_bytewords};
4647
use crate::weight::{varint_size, ComputeWeight};
4748
use crate::{PjUri, Uri};
4849

@@ -326,7 +327,7 @@ impl RequestContext {
326327
) -> Result<(Request, ContextV2), CreateRequestError> {
327328
let rs_base64 = crate::v2::subdir(self.endpoint.as_str()).to_string();
328329
log::debug!("rs_base64: {:?}", rs_base64);
329-
let rs = bytewords::decode(&rs_base64, bytewords::Style::Minimal)
330+
let rs = decode_minimal_bytewords(&rs_base64)
330331
.map_err(|_| InternalCreateRequestError::PubkeyEncoding)?;
331332
log::debug!("rs: {:?}", rs.len());
332333
let rs = bitcoin::secp256k1::PublicKey::from_slice(&rs)
@@ -382,7 +383,7 @@ impl Serialize for RequestContext {
382383
config
383384
.encode()
384385
.map_err(|e| serde::ser::Error::custom(format!("ohttp-keys encoding error: {}", e)))
385-
.map(|bytes| bytewords::encode(&bytes, bytewords::Style::Minimal))
386+
.map(|bytes| encode_minimal_bytewords(&bytes))
386387
})?;
387388
state.serialize_field("ohttp_keys", &ohttp_string)?;
388389
state.serialize_field("disable_output_substitution", &self.disable_output_substitution)?;
@@ -460,12 +461,9 @@ impl<'de> Deserialize<'de> for RequestContext {
460461
} else {
461462
Some(
462463
crate::v2::OhttpKeys::decode(
463-
bytewords::decode(
464-
&ohttp_encoded,
465-
bytewords::Style::Minimal,
466-
)
467-
.map_err(de::Error::custom)?
468-
.as_slice(),
464+
decode_minimal_bytewords(&ohttp_encoded)
465+
.map_err(de::Error::custom)?
466+
.as_slice(),
469467
)
470468
.map_err(de::Error::custom)?,
471469
)

payjoin/src/uri.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ use std::convert::TryFrom;
33

44
use bitcoin::address::{Error, NetworkChecked, NetworkUnchecked};
55
use bitcoin::{Address, Amount, Network};
6-
use ur::bytewords;
76
use url::Url;
87

8+
#[cfg(feature = "v2")]
9+
use crate::v2::{decode_minimal_bytewords, encode_minimal_bytewords};
910
#[cfg(feature = "v2")]
1011
use crate::OhttpKeys;
1112

@@ -240,7 +241,7 @@ impl<'a> bip21::SerializeParams for &'a PayjoinExtras {
240241
];
241242
#[cfg(feature = "v2")]
242243
if let Some(ohttp_keys) = self.ohttp_keys.clone().and_then(|c| c.encode().ok()) {
243-
let encoded_ohttp_keys = bytewords::encode(&ohttp_keys, bytewords::Style::Minimal);
244+
let encoded_ohttp_keys = encode_minimal_bytewords(&ohttp_keys);
244245
params.push(("ohttp", encoded_ohttp_keys));
245246
} else {
246247
log::warn!("Failed to encode ohttp config, ignoring");
@@ -266,7 +267,7 @@ impl<'a> bip21::de::DeserializationState<'a> for DeserializationState {
266267
#[cfg(feature = "v2")]
267268
"ohttp" if self.ohttp.is_none() => {
268269
let ohttp_encoded = Cow::try_from(value).map_err(InternalPjParseError::NotUtf8)?;
269-
let ohttp_bytes = bytewords::decode(&ohttp_encoded, bytewords::Style::Minimal)
270+
let ohttp_bytes = decode_minimal_bytewords(&ohttp_encoded)
270271
.map_err(|_| InternalPjParseError::BadOhttpEncoding)?;
271272
let ohttp_keys = OhttpKeys::decode(&ohttp_bytes)
272273
.map_err(InternalPjParseError::DecodeOhttpKeys)?;

payjoin/src/v2.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ pub fn subdir(path: &str) -> String {
2828
pubkey_id
2929
}
3030

31+
pub(crate) fn encode_minimal_bytewords(bytes: &[u8]) -> String {
32+
bytewords::encode(bytes, bytewords::Style::Minimal)
33+
}
34+
35+
pub(crate) fn decode_minimal_bytewords(encoded: &str) -> Result<Vec<u8>, bytewords::Error> {
36+
bytewords::decode(encoded, bytewords::Style::Minimal)
37+
}
38+
3139
/// crypto context
3240
///
3341
/// <- Receiver S
@@ -285,8 +293,7 @@ impl<'de> serde::Deserialize<'de> for OhttpKeys {
285293
D: serde::Deserializer<'de>,
286294
{
287295
let encoded = String::deserialize(deserializer)?;
288-
let bytes = bytewords::decode(&encoded, bytewords::Style::Minimal)
289-
.map_err(serde::de::Error::custom)?;
296+
let bytes = decode_minimal_bytewords(&encoded).map_err(serde::de::Error::custom)?;
290297
Ok(OhttpKeys(ohttp::KeyConfig::decode(&bytes).map_err(serde::de::Error::custom)?))
291298
}
292299
}
@@ -297,7 +304,7 @@ impl serde::Serialize for OhttpKeys {
297304
S: serde::Serializer,
298305
{
299306
let bytes = self.0.encode().map_err(serde::ser::Error::custom)?;
300-
let encoded = bytewords::encode(&bytes, bytewords::Style::Minimal);
307+
let encoded = encode_minimal_bytewords(&bytes);
301308
encoded.serialize(serializer)
302309
}
303310
}

0 commit comments

Comments
 (0)