Skip to content

Commit

Permalink
Simplify base64 dependency usage
Browse files Browse the repository at this point in the history
- use psbt.to_string() to get base64
- use base64::encode/decode_config with const configs
  • Loading branch information
DanGould committed Jul 2, 2024
1 parent cc2dc5b commit af92319
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 32 deletions.
7 changes: 2 additions & 5 deletions payjoin-cli/src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@ use anyhow::{anyhow, Context, Result};
use bitcoincore_rpc::bitcoin::Amount;
use bitcoincore_rpc::RpcApi;
use payjoin::bitcoin::psbt::Psbt;
use payjoin::bitcoin::{self, base64};
use payjoin::send::RequestContext;
use payjoin::PjUri;
use payjoin::{bitcoin, PjUri};

pub mod config;
use crate::app::config::AppConfig;
Expand Down Expand Up @@ -77,7 +76,7 @@ pub trait App {
log::debug!("Proposed psbt: {:#?}", psbt);
let psbt = self
.bitcoind()?
.wallet_process_psbt(&serialize_psbt(&psbt), None, None, None)
.wallet_process_psbt(&psbt.to_string(), None, None, None)
.with_context(|| "Failed to process PSBT")?
.psbt;
let tx = self
Expand Down Expand Up @@ -134,8 +133,6 @@ impl payjoin::receive::Headers for Headers<'_> {
}
}

fn serialize_psbt(psbt: &Psbt) -> String { base64::encode(psbt.serialize()) }

#[cfg(feature = "danger-local-https")]
fn http_agent() -> Result<reqwest::Client> { Ok(http_agent_builder()?.build()?) }

Expand Down
6 changes: 3 additions & 3 deletions payjoin-cli/src/app/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use hyper::{Body, Method, Request, Response, Server, StatusCode};
use payjoin::bitcoin::psbt::Psbt;
use payjoin::bitcoin::{self};
use payjoin::receive::{PayjoinProposal, UncheckedProposal};
use payjoin::{base64, Error, PjUriBuilder, Uri, UriExt};
use payjoin::{Error, PjUriBuilder, Uri, UriExt};

use super::config::AppConfig;
use super::App as AppTrait;
Expand Down Expand Up @@ -242,7 +242,7 @@ impl App {

let payjoin_proposal = self.process_v1_proposal(proposal)?;
let psbt = payjoin_proposal.psbt();
let body = base64::encode(psbt.serialize());
let body = psbt.to_string();
println!("Responded with Payjoin proposal {}", psbt.clone().extract_tx().txid());
Ok(Response::new(Body::from(body)))
}
Expand Down Expand Up @@ -325,7 +325,7 @@ impl App {
let payjoin_proposal = provisional_payjoin.finalize_proposal(
|psbt: &Psbt| {
bitcoind
.wallet_process_psbt(&base64::encode(psbt.serialize()), None, None, Some(false))
.wallet_process_psbt(&psbt.to_string(), None, None, Some(false))
.map(|res| Psbt::from_str(&res.psbt).map_err(|e| Error::Server(e.into())))
.map_err(|e| Error::Server(e.into()))?
},
Expand Down
4 changes: 2 additions & 2 deletions payjoin-cli/src/app/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use payjoin::bitcoin::psbt::Psbt;
use payjoin::bitcoin::Amount;
use payjoin::receive::v2::ActiveSession;
use payjoin::send::RequestContext;
use payjoin::{base64, bitcoin, Error, Uri};
use payjoin::{bitcoin, Error, Uri};
use tokio::signal;
use tokio::sync::watch;

Expand Down Expand Up @@ -341,7 +341,7 @@ impl App {
let payjoin_proposal = provisional_payjoin.finalize_proposal(
|psbt: &Psbt| {
bitcoind
.wallet_process_psbt(&base64::encode(psbt.serialize()), None, None, Some(false))
.wallet_process_psbt(&psbt.to_string(), None, None, Some(false))
.map(|res| Psbt::from_str(&res.psbt).map_err(|e| Error::Server(e.into())))
.map_err(|e| Error::Server(e.into()))?
},
Expand Down
8 changes: 2 additions & 6 deletions payjoin-directory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ fn init_ohttp() -> Result<ohttp::Server> {
// create or read from file
let server_config = ohttp::KeyConfig::new(KEY_ID, KEM, Vec::from(SYMMETRIC))?;
let encoded_config = server_config.encode()?;
let b64_config = base64::encode_config(
encoded_config,
base64::Config::new(base64::CharacterSet::UrlSafe, false),
);
let b64_config = base64::encode_config(encoded_config, base64::URL_SAFE_NO_PAD);
info!("ohttp-keys server config base64 UrlSafe: {:?}", b64_config);
Ok(ohttp::Server::new(server_config)?)
}
Expand Down Expand Up @@ -242,12 +239,11 @@ impl From<hyper::http::Error> for HandlerError {
}

async fn post_session(body: Body) -> Result<Response<Body>, HandlerError> {
let b64_config = base64::Config::new(base64::CharacterSet::UrlSafe, false);
let bytes =
hyper::body::to_bytes(body).await.map_err(|e| HandlerError::BadRequest(e.into()))?;
let base64_id =
String::from_utf8(bytes.to_vec()).map_err(|e| HandlerError::BadRequest(e.into()))?;
let pubkey_bytes: Vec<u8> = base64::decode_config(base64_id, b64_config)
let pubkey_bytes: Vec<u8> = base64::decode_config(base64_id, base64::URL_SAFE_NO_PAD)
.map_err(|e| HandlerError::BadRequest(e.into()))?;
let pubkey = bitcoin::secp256k1::PublicKey::from_slice(&pubkey_bytes)
.map_err(|e| HandlerError::BadRequest(e.into()))?;
Expand Down
6 changes: 2 additions & 4 deletions payjoin/src/receive/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,7 @@ impl SessionInitializer {
}

fn subdir_path_from_pubkey(pubkey: &bitcoin::secp256k1::PublicKey) -> String {
let pubkey = pubkey.serialize();
let b64_config = base64::Config::new(base64::CharacterSet::UrlSafe, false);
base64::encode_config(pubkey, b64_config)
base64::encode_config(pubkey.serialize(), base64::URL_SAFE_NO_PAD)
}

/// An active payjoin V2 session, allowing for polled requests to the
Expand Down Expand Up @@ -443,7 +441,7 @@ impl PayjoinProposal {

pub fn psbt(&self) -> &Psbt { self.inner.psbt() }

pub fn extract_v1_req(&self) -> String { base64::encode(self.inner.payjoin_psbt.serialize()) }
pub fn extract_v1_req(&self) -> String { self.inner.payjoin_psbt.to_string() }

#[cfg(feature = "v2")]
pub fn extract_v2_req(&mut self) -> Result<(Request, ohttp::ClientResponse), Error> {
Expand Down
7 changes: 3 additions & 4 deletions payjoin/src/send/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,9 @@ impl RequestContext {
subdirectory = path_and_query;
}

let b64_config =
bitcoin::base64::Config::new(bitcoin::base64::CharacterSet::UrlSafe, false);
let pubkey_bytes = bitcoin::base64::decode_config(subdirectory, b64_config)
.map_err(InternalCreateRequestError::SubdirectoryNotBase64)?;
let pubkey_bytes =
bitcoin::base64::decode_config(subdirectory, bitcoin::base64::URL_SAFE_NO_PAD)
.map_err(InternalCreateRequestError::SubdirectoryNotBase64)?;
Ok(bitcoin::secp256k1::PublicKey::from_slice(&pubkey_bytes)
.map_err(InternalCreateRequestError::SubdirectoryInvalidPubkey)?)
}
Expand Down
13 changes: 5 additions & 8 deletions payjoin/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ mod integration {

#[cfg(not(feature = "v2"))]
mod v1 {
use bitcoin::base64;
use log::debug;
use payjoin::receive::{Headers, PayjoinProposal, UncheckedProposal};
use payjoin::{PjUri, PjUriBuilder, UriExt};
Expand Down Expand Up @@ -103,7 +102,7 @@ mod integration {
let proposal = handle_proposal(proposal, receiver);
let psbt = proposal.psbt();
debug!("Receiver's Payjoin proposal PSBT: {:#?}", &psbt);
base64::encode(&psbt.serialize())
psbt.to_string()
}

fn handle_proposal(
Expand Down Expand Up @@ -179,7 +178,7 @@ mod integration {
|psbt: &Psbt| {
Ok(receiver
.wallet_process_psbt(
&bitcoin::base64::encode(psbt.serialize()),
&psbt.to_string(),
None,
None,
Some(true), // check that the receiver properly clears keypaths
Expand Down Expand Up @@ -270,9 +269,8 @@ mod integration {
sender: &bitcoincore_rpc::Client,
psbt: Psbt,
) -> Result<bitcoin::Transaction, Box<dyn std::error::Error>> {
let payjoin_base64_string = base64::encode(&psbt.serialize());
let payjoin_psbt =
sender.wallet_process_psbt(&payjoin_base64_string, None, None, None)?.psbt;
sender.wallet_process_psbt(&psbt.to_string(), None, None, None)?.psbt;
let payjoin_psbt = sender.finalize_psbt(&payjoin_psbt, Some(false))?.psbt.unwrap();
let payjoin_psbt = Psbt::from_str(&payjoin_psbt)?;
debug!("Sender's Payjoin PSBT: {:#?}", payjoin_psbt);
Expand Down Expand Up @@ -703,7 +701,7 @@ mod integration {
|psbt: &Psbt| {
Ok(receiver
.wallet_process_psbt(
&bitcoin::base64::encode(psbt.serialize()),
&psbt.to_string(),
None,
None,
Some(true), // check that the receiver properly clears keypaths
Expand Down Expand Up @@ -832,9 +830,8 @@ mod integration {
sender: &bitcoincore_rpc::Client,
psbt: Psbt,
) -> Result<bitcoin::Transaction, Box<dyn std::error::Error>> {
let payjoin_base64_string = bitcoin::base64::encode(&psbt.serialize());
let payjoin_psbt =
sender.wallet_process_psbt(&payjoin_base64_string, None, None, None)?.psbt;
sender.wallet_process_psbt(&psbt.to_string(), None, None, None)?.psbt;
let payjoin_psbt = sender.finalize_psbt(&payjoin_psbt, Some(false))?.psbt.unwrap();
let payjoin_psbt = Psbt::from_str(&payjoin_psbt)?;

Expand Down

0 comments on commit af92319

Please sign in to comment.