Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode payjoin 2 subdirectory pubkey and ohttp= param in ur::bytewords #226

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Abstract bytewords coding with helper function
DanGould committed Mar 28, 2024
commit 78e24d6bb7473461d77b3cef3d82e8306a7ea0de
8 changes: 4 additions & 4 deletions payjoin/src/receive/v2.rs
Original file line number Diff line number Diff line change
@@ -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.
@@ -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") }
@@ -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)]
@@ -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)
}
}
16 changes: 7 additions & 9 deletions payjoin/src/send/mod.rs
Original file line number Diff line number Diff line change
@@ -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};

@@ -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)
@@ -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)?;
@@ -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)?,
)
7 changes: 4 additions & 3 deletions payjoin/src/uri.rs
Original file line number Diff line number Diff line change
@@ -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;

@@ -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");
@@ -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)?;
13 changes: 10 additions & 3 deletions payjoin/src/v2.rs
Original file line number Diff line number Diff line change
@@ -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
@@ -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)?))
}
}
@@ -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)
}
}