Skip to content

Commit

Permalink
better error handling in finalize_proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
jbesraa committed Jul 31, 2023
1 parent 255311a commit 9489175
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
12 changes: 3 additions & 9 deletions payjoin-cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,13 +366,9 @@ struct OutPointSet(HashSet<bitcoin::OutPoint>);

use std::fs::OpenOptions;
impl OutPointSet {
fn new() -> Self {
Self(HashSet::new())
}
fn new() -> Self { Self(HashSet::new()) }

fn insert(&mut self, input: bitcoin::OutPoint) -> bool {
self.0.insert(input)
}
fn insert(&mut self, input: bitcoin::OutPoint) -> bool { self.0.insert(input) }
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -483,6 +479,4 @@ impl payjoin::receive::Headers for Headers<'_> {
}
}

fn serialize_psbt(psbt: &Psbt) -> String {
base64::encode(&psbt.serialize())
}
fn serialize_psbt(psbt: &Psbt) -> String { base64::encode(&psbt.serialize()) }
4 changes: 2 additions & 2 deletions payjoin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ bip21 = "0.3.1"
log = { version = "0.4.14"}
rand = { version = "0.8.4", optional = true }
url = "2.2.2"
bitcoind = { version = "0.31.1", features = ["0_21_2"] }

[dev-dependencies]
env_logger = "0.9.0"
bitcoind = { version = "0.31.1", features = ["0_21_2"] }

[package.metadata.docs.rs]
features = ["send", "receive"]
features = ["send", "receive"]
4 changes: 4 additions & 0 deletions payjoin/src/receive/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ pub struct RequestError(InternalRequestError);
#[derive(Debug)]
pub(crate) enum InternalRequestError {
Psbt(bitcoin::psbt::Error),
PsbtParseError(bitcoin::psbt::PsbtParseError),
Base64(bitcoin::base64::DecodeError),
BitcoinCoreRpc(bitcoind::bitcoincore_rpc::Error),
Io(std::io::Error),
MissingHeader(&'static str),
InvalidContentType(String),
Expand Down Expand Up @@ -78,6 +80,8 @@ impl fmt::Display for RequestError {
}

match &self.0 {
InternalRequestError::PsbtParseError(e) => write_error(f, "psbt-parse-error", e),
InternalRequestError::BitcoinCoreRpc(e) => write_error(f, "bitcoin-core-rpc-error", e),
InternalRequestError::Psbt(e) => write_error(f, "psbt-error", e),
InternalRequestError::Base64(e) => write_error(f, "base64-decode-error", e),
InternalRequestError::Io(e) => write_error(f, "io-error", e),
Expand Down
24 changes: 15 additions & 9 deletions payjoin/src/receive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,16 +820,22 @@ impl ProvisionalProposal {
wallet_process_psbt: impl Fn(&str) -> Result<WalletProcessPsbtResult, bitcoincore_rpc::Error>,
min_feerate_sat_per_vb: Option<u64>,
) -> Result<PayjoinProposal, RequestError> {
let payjoin_proposal_psbt: &Psbt = self.apply_fee(min_feerate_sat_per_vb).unwrap();
let payjoin_proposal_psbt: &Psbt = self.apply_fee(min_feerate_sat_per_vb)?;
let payjoin_base64_string = bitcoin::base64::encode(&payjoin_proposal_psbt.serialize());
let payjoin_proposal_psbt = wallet_process_psbt(&payjoin_base64_string)
.map_err(|e| Error::Server(e.into()))
.unwrap()
.psbt;
let payjoin_proposal_psbt =
Psbt::from_str(&payjoin_proposal_psbt).map_err(|e| Error::Server(e.into())).unwrap();

let payjoin_proposal = self.prepare_psbt(payjoin_proposal_psbt).unwrap();
let payjoin_proposal_psbt = match wallet_process_psbt(&payjoin_base64_string) {
Ok(p) => p.psbt,
Err(e) => return Err(RequestError::from(InternalRequestError::BitcoinCoreRpc(e))),
};
let payjoin_proposal_psbt = match Psbt::from_str(&payjoin_proposal_psbt) {
Ok(p) => p,
Err(e) => return Err(RequestError::from(InternalRequestError::PsbtParseError(e))),
};

let payjoin_proposal = match self.prepare_psbt(payjoin_proposal_psbt) {
Ok(p) => p,
Err(e) => return Err(RequestError::from(e)),
};
Ok(payjoin_proposal)
}
}

Expand Down
4 changes: 1 addition & 3 deletions payjoin/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ mod integration {
struct HeaderMock(HashMap<String, String>);

impl Headers for HeaderMock {
fn get_header(&self, key: &str) -> Option<&str> {
self.0.get(key).map(|e| e.as_str())
}
fn get_header(&self, key: &str) -> Option<&str> { self.0.get(key).map(|e| e.as_str()) }
}

impl HeaderMock {
Expand Down

0 comments on commit 9489175

Please sign in to comment.