Skip to content

Commit 579c523

Browse files
authored
Clean subdir api (#228)
There were a couple of functions with similar names that confused me. Some weren't even used. This cleans them up - first commit repalces unnecessary functions with a well named conversion fn - second encapsulates a similarly named private conversion function in the right place - third removes a helper function with no apparent use
2 parents be6c9ef + 636b4a5 commit 579c523

File tree

3 files changed

+32
-42
lines changed

3 files changed

+32
-42
lines changed

payjoin/src/receive/v2.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,9 @@ impl Enroller {
5656
}
5757
}
5858

59-
pub fn subdirectory(&self) -> String {
60-
let pubkey = &self.s.public_key().serialize();
61-
let b64_config = base64::Config::new(base64::CharacterSet::UrlSafe, false);
62-
base64::encode_config(pubkey, b64_config)
63-
}
64-
65-
pub fn payjoin_subdir(&self) -> String { format!("{}/{}", self.subdirectory(), "payjoin") }
66-
6759
pub fn extract_req(&mut self) -> Result<(Request, ohttp::ClientResponse), Error> {
6860
let url = self.ohttp_relay.clone();
69-
let subdirectory = self.subdirectory();
61+
let subdirectory = subdir_path_from_pubkey(&self.s.public_key());
7062
let (body, ctx) = crate::v2::ohttp_encapsulate(
7163
&mut self.ohttp_keys,
7264
"POST",
@@ -97,7 +89,7 @@ impl Enroller {
9789
}
9890
}
9991

100-
fn subdirectory(pubkey: &bitcoin::secp256k1::PublicKey) -> String {
92+
fn subdir_path_from_pubkey(pubkey: &bitcoin::secp256k1::PublicKey) -> String {
10193
let pubkey = pubkey.serialize();
10294
let b64_config = base64::Config::new(base64::CharacterSet::UrlSafe, false);
10395
base64::encode_config(pubkey, b64_config)
@@ -268,8 +260,6 @@ impl Enrolled {
268260
Ok(crate::v2::ohttp_encapsulate(&mut self.ohttp_keys, "GET", &fallback_target, None)?)
269261
}
270262

271-
pub fn pubkey(&self) -> [u8; 33] { self.s.public_key().serialize() }
272-
273263
pub fn fallback_target(&self) -> String {
274264
let pubkey = &self.s.public_key().serialize();
275265
let b64_config = base64::Config::new(base64::CharacterSet::UrlSafe, false);
@@ -518,7 +508,7 @@ impl PayjoinProposal {
518508
let post_payjoin_target = format!(
519509
"{}{}/payjoin",
520510
self.context.directory.as_str(),
521-
subdirectory(&self.context.s.public_key())
511+
subdir_path_from_pubkey(&self.context.s.public_key())
522512
);
523513
log::debug!("Payjoin post target: {}", post_payjoin_target.as_str());
524514
let (body, ctx) = crate::v2::ohttp_encapsulate(

payjoin/src/send/mod.rs

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ use std::str::FromStr;
2828

2929
use bitcoin::address::NetworkChecked;
3030
use bitcoin::psbt::Psbt;
31+
#[cfg(feature = "v2")]
32+
use bitcoin::secp256k1::PublicKey;
3133
use bitcoin::{FeeRate, Script, ScriptBuf, Sequence, TxOut, Weight};
3234
pub use error::{CreateRequestError, ResponseError, ValidationError};
3335
pub(crate) use error::{InternalCreateRequestError, InternalValidationError};
@@ -323,16 +325,7 @@ impl RequestContext {
323325
&mut self,
324326
ohttp_relay: Url,
325327
) -> Result<(Request, ContextV2), CreateRequestError> {
326-
let rs_base64 = crate::v2::subdir(self.endpoint.as_str()).to_string();
327-
log::debug!("rs_base64: {:?}", rs_base64);
328-
let b64_config =
329-
bitcoin::base64::Config::new(bitcoin::base64::CharacterSet::UrlSafe, false);
330-
let rs = bitcoin::base64::decode_config(rs_base64, b64_config)
331-
.map_err(InternalCreateRequestError::SubdirectoryNotBase64)?;
332-
log::debug!("rs: {:?}", rs.len());
333-
let rs = bitcoin::secp256k1::PublicKey::from_slice(&rs)
334-
.map_err(InternalCreateRequestError::SubdirectoryInvalidPubkey)?;
335-
328+
let rs = Self::rs_pubkey_from_dir_endpoint(&self.endpoint)?;
336329
let url = self.endpoint.clone();
337330
let body = serialize_v2_body(
338331
&self.psbt,
@@ -368,6 +361,32 @@ impl RequestContext {
368361
},
369362
))
370363
}
364+
365+
#[cfg(feature = "v2")]
366+
fn rs_pubkey_from_dir_endpoint(endpoint: &Url) -> Result<PublicKey, CreateRequestError> {
367+
let path_and_query: String;
368+
369+
if let Some(pos) = endpoint.as_str().rfind('/') {
370+
path_and_query = endpoint.as_str()[pos + 1..].to_string();
371+
} else {
372+
path_and_query = endpoint.to_string();
373+
}
374+
375+
let subdirectory: String;
376+
377+
if let Some(pos) = path_and_query.find('?') {
378+
subdirectory = path_and_query[..pos].to_string();
379+
} else {
380+
subdirectory = path_and_query;
381+
}
382+
383+
let b64_config =
384+
bitcoin::base64::Config::new(bitcoin::base64::CharacterSet::UrlSafe, false);
385+
let pubkey_bytes = bitcoin::base64::decode_config(subdirectory, b64_config)
386+
.map_err(InternalCreateRequestError::SubdirectoryNotBase64)?;
387+
Ok(bitcoin::secp256k1::PublicKey::from_slice(&pubkey_bytes)
388+
.map_err(InternalCreateRequestError::SubdirectoryInvalidPubkey)?)
389+
}
371390
}
372391

373392
#[cfg(feature = "v2")]

payjoin/src/v2.rs

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,6 @@ use chacha20poly1305::{AeadCore, ChaCha20Poly1305, Nonce};
88

99
pub const PADDED_MESSAGE_BYTES: usize = 7168; // 7KB
1010

11-
pub fn subdir(path: &str) -> String {
12-
let subdirectory: String;
13-
14-
if let Some(pos) = path.rfind('/') {
15-
subdirectory = path[pos + 1..].to_string();
16-
} else {
17-
subdirectory = path.to_string();
18-
}
19-
20-
let pubkey_id: String;
21-
22-
if let Some(pos) = subdirectory.find('?') {
23-
pubkey_id = subdirectory[..pos].to_string();
24-
} else {
25-
pubkey_id = subdirectory;
26-
}
27-
pubkey_id
28-
}
29-
3011
/// crypto context
3112
///
3213
/// <- Receiver S

0 commit comments

Comments
 (0)