Skip to content

Commit

Permalink
start implementing finalize_proposal
Browse files Browse the repository at this point in the history
  • Loading branch information
jbesraa committed Aug 18, 2023
1 parent 2cd21ec commit 1438451
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
12 changes: 3 additions & 9 deletions payjoin-cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,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 @@ -493,6 +489,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
43 changes: 31 additions & 12 deletions payjoin/src/receive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,13 +268,16 @@

use std::cmp::{max, min};
use std::collections::{BTreeMap, HashMap};
use std::str::FromStr;

use bitcoin::psbt::Psbt;
use bitcoin::{Amount, FeeRate, OutPoint, Script, TxOut};

mod error;
mod optional_parameters;

use bitcoind::bitcoincore_rpc;
use bitcoind::bitcoincore_rpc::core_rpc_json::WalletProcessPsbtResult;
pub use error::{Error, RequestError, SelectionError};
use error::{InternalRequestError, InternalSelectionError};
use optional_parameters::Params;
Expand Down Expand Up @@ -426,9 +429,8 @@ impl MaybeInputsOwned {
})
.find_map(|script| match is_owned(&script) {
Ok(false) => None,
Ok(true) => {
Some(Error::BadRequest(InternalRequestError::InputOwned(script).into()))
}
Ok(true) =>
Some(Error::BadRequest(InternalRequestError::InputOwned(script).into())),
Err(e) => Some(Error::Server(e.into())),
})
{
Expand Down Expand Up @@ -567,13 +569,9 @@ impl PayjoinProposal {
self.params.disable_output_substitution
}

pub fn get_owned_vouts(&self) -> Vec<usize> {
self.owned_vouts.clone()
}
pub fn get_owned_vouts(&self) -> Vec<usize> { self.owned_vouts.clone() }

pub fn get_payjoin_psbt(&self) -> Psbt {
self.payjoin_psbt.clone()
}
pub fn get_payjoin_psbt(&self) -> Psbt { self.payjoin_psbt.clone() }
}

/// A mutable checked proposal that the receiver may contribute inputs to to make a payjoin.
Expand Down Expand Up @@ -816,6 +814,29 @@ impl ProvisionalProposal {
params: self.params,
})
}

pub fn finalize_proposal(
mut self,
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)?;
let payjoin_base64_string = bitcoin::base64::encode(&payjoin_proposal_psbt.serialize());
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)
}
}

#[cfg(test)]
Expand All @@ -828,9 +849,7 @@ mod test {

impl MockHeaders {
#[cfg(test)]
fn new(length: u64) -> MockHeaders {
MockHeaders { length: length.to_string() }
}
fn new(length: u64) -> MockHeaders { MockHeaders { length: length.to_string() } }
}

impl Headers for MockHeaders {
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 1438451

Please sign in to comment.