Skip to content

Commit

Permalink
Abstract bytewords coding with helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
DanGould committed Mar 28, 2024
1 parent ebc0b1c commit 78e24d6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
8 changes: 4 additions & 4 deletions payjoin/src/receive/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use bitcoin::psbt::Psbt;
use bitcoin::{base64, Amount, FeeRate, OutPoint, Script, TxOut};
use serde::ser::SerializeStruct;
use serde::{Deserialize, Serialize, Serializer};
use ur::bytewords;
use url::Url;

use super::{Error, InternalRequestError, RequestError, SelectionError};
use crate::psbt::PsbtExt;
use crate::receive::optional_parameters::Params;
use crate::v2::encode_minimal_bytewords;
use crate::OhttpKeys;

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

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

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

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

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down Expand Up @@ -270,7 +270,7 @@ impl Enrolled {

pub fn fallback_target(&self) -> String {
let pubkey = &self.s.public_key().serialize();
let subdirectory = bytewords::encode(pubkey, bytewords::Style::Minimal);
let subdirectory = encode_minimal_bytewords(pubkey);
format!("{}{}", &self.directory, subdirectory)
}
}
Expand Down
16 changes: 7 additions & 9 deletions payjoin/src/send/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ use serde::{
ser::SerializeStruct,
Deserialize, Deserializer, Serialize, Serializer,
};
use ur::bytewords;
use url::Url;

use crate::input_type::InputType;
use crate::psbt::PsbtExt;
use crate::uri::UriExt;
#[cfg(feature = "v2")]
use crate::v2::{decode_minimal_bytewords, encode_minimal_bytewords};
use crate::weight::{varint_size, ComputeWeight};
use crate::{PjUri, Uri};

Expand Down Expand Up @@ -326,7 +327,7 @@ impl RequestContext {
) -> Result<(Request, ContextV2), CreateRequestError> {
let rs_base64 = crate::v2::subdir(self.endpoint.as_str()).to_string();
log::debug!("rs_base64: {:?}", rs_base64);
let rs = bytewords::decode(&rs_base64, bytewords::Style::Minimal)
let rs = decode_minimal_bytewords(&rs_base64)
.map_err(|_| InternalCreateRequestError::PubkeyEncoding)?;
log::debug!("rs: {:?}", rs.len());
let rs = bitcoin::secp256k1::PublicKey::from_slice(&rs)
Expand Down Expand Up @@ -382,7 +383,7 @@ impl Serialize for RequestContext {
config
.encode()
.map_err(|e| serde::ser::Error::custom(format!("ohttp-keys encoding error: {}", e)))
.map(|bytes| bytewords::encode(&bytes, bytewords::Style::Minimal))
.map(|bytes| encode_minimal_bytewords(&bytes))
})?;
state.serialize_field("ohttp_keys", &ohttp_string)?;
state.serialize_field("disable_output_substitution", &self.disable_output_substitution)?;
Expand Down Expand Up @@ -460,12 +461,9 @@ impl<'de> Deserialize<'de> for RequestContext {
} else {
Some(
crate::v2::OhttpKeys::decode(
bytewords::decode(
&ohttp_encoded,
bytewords::Style::Minimal,
)
.map_err(de::Error::custom)?
.as_slice(),
decode_minimal_bytewords(&ohttp_encoded)
.map_err(de::Error::custom)?
.as_slice(),
)
.map_err(de::Error::custom)?,
)
Expand Down
7 changes: 4 additions & 3 deletions payjoin/src/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use std::convert::TryFrom;

use bitcoin::address::{Error, NetworkChecked, NetworkUnchecked};
use bitcoin::{Address, Amount, Network};
use ur::bytewords;
use url::Url;

#[cfg(feature = "v2")]
use crate::v2::{decode_minimal_bytewords, encode_minimal_bytewords};
#[cfg(feature = "v2")]
use crate::OhttpKeys;

Expand Down Expand Up @@ -240,7 +241,7 @@ impl<'a> bip21::SerializeParams for &'a PayjoinExtras {
];
#[cfg(feature = "v2")]
if let Some(ohttp_keys) = self.ohttp_keys.clone().and_then(|c| c.encode().ok()) {
let encoded_ohttp_keys = bytewords::encode(&ohttp_keys, bytewords::Style::Minimal);
let encoded_ohttp_keys = encode_minimal_bytewords(&ohttp_keys);
params.push(("ohttp", encoded_ohttp_keys));
} else {
log::warn!("Failed to encode ohttp config, ignoring");
Expand All @@ -266,7 +267,7 @@ impl<'a> bip21::de::DeserializationState<'a> for DeserializationState {
#[cfg(feature = "v2")]
"ohttp" if self.ohttp.is_none() => {
let ohttp_encoded = Cow::try_from(value).map_err(InternalPjParseError::NotUtf8)?;
let ohttp_bytes = bytewords::decode(&ohttp_encoded, bytewords::Style::Minimal)
let ohttp_bytes = decode_minimal_bytewords(&ohttp_encoded)
.map_err(|_| InternalPjParseError::BadOhttpEncoding)?;
let ohttp_keys = OhttpKeys::decode(&ohttp_bytes)
.map_err(InternalPjParseError::DecodeOhttpKeys)?;
Expand Down
13 changes: 10 additions & 3 deletions payjoin/src/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ pub fn subdir(path: &str) -> String {
pubkey_id
}

pub(crate) fn encode_minimal_bytewords(bytes: &[u8]) -> String {
bytewords::encode(bytes, bytewords::Style::Minimal)
}

pub(crate) fn decode_minimal_bytewords(encoded: &str) -> Result<Vec<u8>, bytewords::Error> {
bytewords::decode(encoded, bytewords::Style::Minimal)
}

/// crypto context
///
/// <- Receiver S
Expand Down Expand Up @@ -285,8 +293,7 @@ impl<'de> serde::Deserialize<'de> for OhttpKeys {
D: serde::Deserializer<'de>,
{
let encoded = String::deserialize(deserializer)?;
let bytes = bytewords::decode(&encoded, bytewords::Style::Minimal)
.map_err(serde::de::Error::custom)?;
let bytes = decode_minimal_bytewords(&encoded).map_err(serde::de::Error::custom)?;
Ok(OhttpKeys(ohttp::KeyConfig::decode(&bytes).map_err(serde::de::Error::custom)?))
}
}
Expand All @@ -297,7 +304,7 @@ impl serde::Serialize for OhttpKeys {
S: serde::Serializer,
{
let bytes = self.0.encode().map_err(serde::ser::Error::custom)?;
let encoded = bytewords::encode(&bytes, bytewords::Style::Minimal);
let encoded = encode_minimal_bytewords(&bytes);
encoded.serialize(serializer)
}
}
Expand Down

0 comments on commit 78e24d6

Please sign in to comment.