Skip to content

Commit

Permalink
DRAFT upgrade to payjoin-0.21
Browse files Browse the repository at this point in the history
  • Loading branch information
DanGould committed Oct 16, 2024
1 parent 9e79770 commit 3b08d11
Show file tree
Hide file tree
Showing 11 changed files with 360 additions and 480 deletions.
400 changes: 89 additions & 311 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ uniffi = { version = "0.28.0", features = ["bindgen-tests"] }
bdk = { version = "0.29.0", features = ["all-keys", "use-esplora-ureq", "keys-bip39"] }
bitcoincore-rpc = "0.19.0"
http = "1"
payjoin-directory = { git = "https://github.com/payjoin/rust-payjoin", features = ["danger-local-https"] }
payjoin-directory = { path = "../payjoin/payjoin-directory", features = ["danger-local-https"] }
ohttp-relay = "0.0.8"
rcgen = { version = "0.11" }
reqwest = { version = "0.12", default-features = false, features = ["rustls-tls"] }
Expand All @@ -27,10 +27,10 @@ testcontainers-modules = { version = "0.1.3", features = ["redis"] }
tokio = { version = "1.12.0", features = ["full"] }
[dependencies]

payjoin = {version = "=0.20.0", features = ["send", "receive", "base64", "v2", "io"] }
payjoin = {path = "../payjoin/payjoin", features = ["send", "receive", "base64", "v2", "io"] }
uniffi = { version = "0.28.0" }
thiserror = "1.0.47"
ohttp = { version = "0.5.1" }
ohttp = { package = "bitcoin-ohttp", version = "0.6.0" }
url = "2.5.0"
base64 = "0.22.1"
hex = "0.4.3"
Expand Down
14 changes: 6 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

pub mod error;
pub mod io;
pub mod ohttp;
pub mod receive;
pub mod send;
pub mod types;
pub mod uri;

use crate::error::PayjoinError;
#[allow(unused_imports)]
use crate::ohttp::ClientResponse;
#[cfg(feature = "uniffi")]
use crate::receive::v1::{
CanBroadcast, GenerateScript, IsOutputKnown, IsScriptOwned, ProcessPartiallySignedTransaction,
Expand All @@ -19,16 +22,11 @@ use crate::receive::v1::{
};
#[allow(unused_imports)]
use crate::receive::v2::{
ActiveSession, ClientResponse, RequestResponse, SessionInitializer, V2MaybeInputsOwned,
V2MaybeInputsSeen, V2MaybeMixedInputScripts, V2OutputsUnknown, V2PayjoinProposal,
V2ProvisionalProposal, V2UncheckedProposal,
};
#[allow(unused_imports)]
use crate::send::v1::{
ContextV1, RequestBuilder, RequestContext, RequestContextV1, RequestContextV2,
Receiver, RequestResponse, V2MaybeInputsOwned, V2MaybeInputsSeen, V2MaybeMixedInputScripts,
V2OutputsUnknown, V2PayjoinProposal, V2ProvisionalProposal, V2UncheckedProposal,
};
#[allow(unused_imports)]
use crate::send::v2::ContextV2;
use crate::send::*;
#[allow(unused_imports)]
use crate::types::{Network, OhttpKeys, OutPoint, Request, TxOut};
#[allow(unused_imports)]
Expand Down
15 changes: 15 additions & 0 deletions src/ohttp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::sync::Mutex;

pub struct ClientResponse(Mutex<Option<ohttp::ClientResponse>>);

impl From<&ClientResponse> for ohttp::ClientResponse {
fn from(value: &ClientResponse) -> Self {
let mut data_guard = value.0.lock().unwrap();
Option::take(&mut *data_guard).expect("ClientResponse moved out of memory")
}
}
impl From<ohttp::ClientResponse> for ClientResponse {
fn from(value: ohttp::ClientResponse) -> Self {
Self(Mutex::new(Some(value)))
}
}
52 changes: 50 additions & 2 deletions src/receive/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use payjoin::bitcoin::FeeRate;
use payjoin::receive as pdk;

use crate::error::PayjoinError;
use crate::types::{OutPoint, TxOut};
use crate::types::{OutPoint, Script, TxOut};

pub trait CanBroadcast {
fn callback(&self, tx: Vec<u8>) -> Result<bool, PayjoinError>;
Expand Down Expand Up @@ -280,7 +280,7 @@ impl OutputsUnknown {
pub fn identify_receiver_outputs(
&self,
is_receiver_output: impl Fn(&Vec<u8>) -> Result<bool, PayjoinError>,
) -> Result<ProvisionalProposal, PayjoinError> {
) -> Result<WantsOutputs, PayjoinError> {
self.0
.clone()
.identify_receiver_outputs(|input| {
Expand All @@ -292,6 +292,50 @@ impl OutputsUnknown {
}
}

pub struct WantsOutputs(payjoin::receive::WantsOutputs);

impl From<payjoin::receive::WantsOutputs> for WantsOutputs {
fn from(value: payjoin::receive::WantsOutputs) -> Self {
Self(value)
}
}
impl WantsOutputs {
pub fn replace_receiver_outputs(
&self,
replacement_outputs: Vec<TxOut>,
drain_script: &Script,
) -> Result<WantsOutputs, PayjoinError> {
self.0
.clone()
.replace_receiver_outputs(replacement_outputs.into(), drain_script.clone().into())
}

pub fn commit_outputs(&self) -> Result<WantsInputs, PayjoinError> {
self.0.clone().commit_outputs().map_err(|e| e.into())
}
}

pub struct WantsInputs(payjoin::receive::WantsInputs);

impl From<payjoin::receive::WantsInputs> for WantsInputs {
fn from(value: payjoin::receive::WantsInputs) -> Self {
Self(value)
}
}

impl WantsInputs {
pub fn contribute_witness_inputs(
&self,
replacement_inputs: Vec<(OutPoint, TxOut)>,
) -> Result<WantsInputs, PayjoinError> {
self.0.clone().replace_receiver_inputs(replacement_inputs.into())
}

pub fn commit_inputs(&self) -> Result<ProvisionalProposal, PayjoinError> {
self.0.clone().commit_inputs().map_err(|e| e.into())
}
}

///A mutable checked proposal that the receiver may contribute inputs to make a payjoin.
pub struct ProvisionalProposal(Mutex<pdk::ProvisionalProposal>);

Expand Down Expand Up @@ -370,6 +414,7 @@ impl ProvisionalProposal {
&self,
process_psbt: Box<dyn ProcessPartiallySignedTransaction>,
min_feerate_sat_per_vb: Option<u64>,
max_feerate_sat_per_vb: u64,
) -> Result<Arc<PayjoinProposal>, PayjoinError> {
self.mutex_guard()
.clone()
Expand All @@ -381,6 +426,7 @@ impl ProvisionalProposal {
.map_err(|e| pdk::Error::Server(Box::new(e)))
},
min_feerate_sat_per_vb.and_then(|x| FeeRate::from_sat_per_vb(x)),
FeeRate::from_sat_per_vb(max_feerate_sat_per_vb),
)
.map(|e| Arc::new(e.into()))
.map_err(|e| e.into())
Expand All @@ -390,6 +436,7 @@ impl ProvisionalProposal {
&self,
process_psbt: impl Fn(String) -> Result<String, PayjoinError>,
min_feerate_sat_per_vb: Option<u64>,
max_feerate_sat_per_vb: u64,
) -> Result<Arc<PayjoinProposal>, PayjoinError> {
self.mutex_guard()
.clone()
Expand All @@ -400,6 +447,7 @@ impl ProvisionalProposal {
.map_err(|e| pdk::Error::Server(Box::new(e)))
},
min_feerate_sat_per_vb.and_then(|x| FeeRate::from_sat_per_vb(x)),
FeeRate::from_sat_per_vb(max_feerate_sat_per_vb),
)
.map(|e| Arc::new(e.into()))
.map_err(|e| e.into())
Expand Down
Loading

0 comments on commit 3b08d11

Please sign in to comment.