Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
50 changes: 29 additions & 21 deletions payjoin-cli/src/app/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ use hyper_util::rt::TokioIo;
use payjoin::bitcoin::psbt::Psbt;
use payjoin::bitcoin::{self, FeeRate};
use payjoin::receive::v1::{PayjoinProposal, UncheckedProposal};
use payjoin::receive::Error;
use payjoin::send::v1::SenderBuilder;
use payjoin::{Error, Uri, UriExt};
use payjoin::{Uri, UriExt};
use tokio::net::TcpListener;

use super::config::AppConfig;
Expand Down Expand Up @@ -224,7 +225,7 @@ impl App {
.handle_payjoin_post(req)
.await
.map_err(|e| match e {
Error::BadRequest(e) => {
Error::Validation(e) => {
log::error!("Error handling request: {}", e);
Response::builder().status(400).body(full(e.to_string())).unwrap()
}
Expand All @@ -248,9 +249,9 @@ impl App {
) -> Result<Response<BoxBody<Bytes, hyper::Error>>, Error> {
let address = self
.bitcoind()
.map_err(|e| Error::Server(e.into()))?
.map_err(|e| Error::Implementation(e.into()))?
.get_new_address(None, None)
.map_err(|e| Error::Server(e.into()))?
.map_err(|e| Error::Implementation(e.into()))?
.assume_checked();
let uri_string = if let Some(amount) = amount {
format!(
Expand All @@ -262,8 +263,9 @@ impl App {
} else {
format!("{}?pj={}", address.to_qr_uri(), self.config.pj_endpoint)
};
let uri = Uri::try_from(uri_string.clone())
.map_err(|_| Error::Server(anyhow!("Could not parse payjoin URI string.").into()))?;
let uri = Uri::try_from(uri_string.clone()).map_err(|_| {
Error::Implementation(anyhow!("Could not parse payjoin URI string.").into())
})?;
let _ = uri.assume_checked(); // we just got it from bitcoind above

Ok(Response::new(full(uri_string)))
Expand All @@ -276,7 +278,8 @@ impl App {
let (parts, body) = req.into_parts();
let headers = Headers(&parts.headers);
let query_string = parts.uri.query().unwrap_or("");
let body = body.collect().await.map_err(|e| Error::Server(e.into()))?.aggregate().reader();
let body =
body.collect().await.map_err(|e| Error::Implementation(e.into()))?.aggregate().reader();
let proposal = UncheckedProposal::from_request(body, query_string, headers)?;

let payjoin_proposal = self.process_v1_proposal(proposal)?;
Expand All @@ -290,22 +293,24 @@ impl App {
}

fn process_v1_proposal(&self, proposal: UncheckedProposal) -> Result<PayjoinProposal, Error> {
let bitcoind = self.bitcoind().map_err(|e| Error::Server(e.into()))?;
let bitcoind = self.bitcoind().map_err(|e| Error::Implementation(e.into()))?;

// in a payment processor where the sender could go offline, this is where you schedule to broadcast the original_tx
let _to_broadcast_in_failure_case = proposal.extract_tx_to_schedule_broadcast();

// The network is used for checks later
let network = bitcoind.get_blockchain_info().map_err(|e| Error::Server(e.into()))?.chain;
let network =
bitcoind.get_blockchain_info().map_err(|e| Error::Implementation(e.into()))?.chain;

// Receive Check 1: Can Broadcast
let proposal = proposal.check_broadcast_suitability(None, |tx| {
let raw_tx = bitcoin::consensus::encode::serialize_hex(&tx);
let mempool_results =
bitcoind.test_mempool_accept(&[raw_tx]).map_err(|e| Error::Server(e.into()))?;
let mempool_results = bitcoind
.test_mempool_accept(&[raw_tx])
.map_err(|e| Error::Implementation(e.into()))?;
match mempool_results.first() {
Some(result) => Ok(result.allowed),
None => Err(Error::Server(
None => Err(Error::Implementation(
anyhow!("No mempool results returned on broadcast check").into(),
)),
}
Expand All @@ -318,7 +323,7 @@ impl App {
bitcoind
.get_address_info(&address)
.map(|info| info.is_mine.unwrap_or(false))
.map_err(|e| Error::Server(e.into()))
.map_err(|e| Error::Implementation(e.into()))
} else {
Ok(false)
}
Expand All @@ -327,7 +332,7 @@ impl App {

// Receive Check 3: have we seen this input before? More of a check for non-interactive i.e. payment processor receivers.
let payjoin = proposal.check_no_inputs_seen_before(|input| {
self.db.insert_input_seen_before(*input).map_err(|e| Error::Server(e.into()))
self.db.insert_input_seen_before(*input).map_err(|e| Error::Implementation(e.into()))
})?;
log::trace!("check3");

Expand All @@ -336,7 +341,7 @@ impl App {
bitcoind
.get_address_info(&address)
.map(|info| info.is_mine.unwrap_or(false))
.map_err(|e| Error::Server(e.into()))
.map_err(|e| Error::Implementation(e.into()))
} else {
Ok(false)
}
Expand All @@ -346,12 +351,12 @@ impl App {
.substitute_receiver_script(
&bitcoind
.get_new_address(None, None)
.map_err(|e| Error::Server(e.into()))?
.map_err(|e| Error::Implementation(e.into()))?
.require_network(network)
.map_err(|e| Error::Server(e.into()))?
.map_err(|e| Error::Implementation(e.into()))?
.script_pubkey(),
)
.map_err(|e| Error::Server(e.into()))?
.map_err(|e| Error::Implementation(e.into()))?
.commit_outputs();

let provisional_payjoin = try_contributing_inputs(payjoin.clone(), &bitcoind)
Expand All @@ -364,12 +369,15 @@ impl App {
|psbt: &Psbt| {
bitcoind
.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()))?
.map(|res| {
Psbt::from_str(&res.psbt).map_err(|e| Error::Implementation(e.into()))
})
.map_err(|e| Error::Implementation(e.into()))?
},
Some(bitcoin::FeeRate::MIN),
self.config.max_fee_rate.map_or(Ok(FeeRate::ZERO), |fee_rate| {
FeeRate::from_sat_per_vb(fee_rate).ok_or(Error::Server("Invalid fee rate".into()))
FeeRate::from_sat_per_vb(fee_rate)
.ok_or(Error::Implementation("Invalid fee rate".into()))
})?,
)?;
Ok(payjoin_proposal)
Expand Down
30 changes: 18 additions & 12 deletions payjoin-cli/src/app/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use payjoin::bitcoin::consensus::encode::serialize_hex;
use payjoin::bitcoin::psbt::Psbt;
use payjoin::bitcoin::{Amount, FeeRate};
use payjoin::receive::v2::{Receiver, UncheckedProposal};
use payjoin::receive::Error;
use payjoin::send::v2::{Sender, SenderBuilder};
use payjoin::{bitcoin, Error, Uri};
use payjoin::{bitcoin, Uri};
use tokio::signal;
use tokio::sync::watch;

Expand Down Expand Up @@ -251,21 +252,23 @@ impl App {
&self,
proposal: payjoin::receive::v2::UncheckedProposal,
) -> Result<payjoin::receive::v2::PayjoinProposal, Error> {
let bitcoind = self.bitcoind().map_err(|e| Error::Server(e.into()))?;
let bitcoind = self.bitcoind().map_err(|e| Error::Implementation(e.into()))?;

// in a payment processor where the sender could go offline, this is where you schedule to broadcast the original_tx
let _to_broadcast_in_failure_case = proposal.extract_tx_to_schedule_broadcast();

// The network is used for checks later
let network = bitcoind.get_blockchain_info().map_err(|e| Error::Server(e.into()))?.chain;
let network =
bitcoind.get_blockchain_info().map_err(|e| Error::Implementation(e.into()))?.chain;
// Receive Check 1: Can Broadcast
let proposal = proposal.check_broadcast_suitability(None, |tx| {
let raw_tx = bitcoin::consensus::encode::serialize_hex(&tx);
let mempool_results =
bitcoind.test_mempool_accept(&[raw_tx]).map_err(|e| Error::Server(e.into()))?;
let mempool_results = bitcoind
.test_mempool_accept(&[raw_tx])
.map_err(|e| Error::Implementation(e.into()))?;
match mempool_results.first() {
Some(result) => Ok(result.allowed),
None => Err(Error::Server(
None => Err(Error::Implementation(
anyhow!("No mempool results returned on broadcast check").into(),
)),
}
Expand All @@ -278,7 +281,7 @@ impl App {
bitcoind
.get_address_info(&address)
.map(|info| info.is_mine.unwrap_or(false))
.map_err(|e| Error::Server(e.into()))
.map_err(|e| Error::Implementation(e.into()))
} else {
Ok(false)
}
Expand All @@ -287,7 +290,7 @@ impl App {

// Receive Check 3: have we seen this input before? More of a check for non-interactive i.e. payment processor receivers.
let payjoin = proposal.check_no_inputs_seen_before(|input| {
self.db.insert_input_seen_before(*input).map_err(|e| Error::Server(e.into()))
self.db.insert_input_seen_before(*input).map_err(|e| Error::Implementation(e.into()))
})?;
log::trace!("check3");

Expand All @@ -297,7 +300,7 @@ impl App {
bitcoind
.get_address_info(&address)
.map(|info| info.is_mine.unwrap_or(false))
.map_err(|e| Error::Server(e.into()))
.map_err(|e| Error::Implementation(e.into()))
} else {
Ok(false)
}
Expand All @@ -314,12 +317,15 @@ impl App {
|psbt: &Psbt| {
bitcoind
.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()))?
.map(|res| {
Psbt::from_str(&res.psbt).map_err(|e| Error::Implementation(e.into()))
})
.map_err(|e| Error::Implementation(e.into()))?
},
Some(bitcoin::FeeRate::MIN),
self.config.max_fee_rate.map_or(Ok(FeeRate::ZERO), |fee_rate| {
FeeRate::from_sat_per_vb(fee_rate).ok_or(Error::Server("Invalid fee rate".into()))
FeeRate::from_sat_per_vb(fee_rate)
.ok_or(Error::Implementation("Invalid fee rate".into()))
})?,
)?;
let payjoin_proposal_psbt = payjoin_proposal.psbt();
Expand Down
2 changes: 0 additions & 2 deletions payjoin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ pub extern crate bitcoin;

#[cfg(feature = "receive")]
pub mod receive;
#[cfg(feature = "receive")]
pub use crate::receive::Error;

#[cfg(feature = "send")]
pub mod send;
Expand Down
Loading