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 29, 2024
1 parent 34a2fa2 commit 6df3799
Show file tree
Hide file tree
Showing 15 changed files with 713 additions and 807 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
60 changes: 60 additions & 0 deletions src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,57 @@ impl From<bitcoin::OutPoint> for OutPoint {
}
}

#[derive(Debug, Clone)]
pub struct PsbtInput {
pub witness_utxo: TxOut,
pub redeem_script: ScriptBuf,
pub witness_script: ScriptBuf,
}

impl PsbtInput {
pub fn new(witness_utxo: TxOut, redeem_script: ScriptBuf, witness_script: ScriptBuf) -> Self {
Self { witness_utxo, redeem_script, witness_script }
}
}

impl From<bitcoin::psbt::Input> for PsbtInput {
fn from(psbt_input: bitcoin::psbt::Input) -> Self {
Self {
witness_utxo: psbt_input.witness_utxo.unwrap().into(),
redeem_script: psbt_input.redeem_script.clone().unwrap().into(),
witness_script: psbt_input.witness_script.clone().unwrap().into(),
}
}
}

impl From<PsbtInput> for bitcoin::psbt::Input {
fn from(psbt_input: PsbtInput) -> Self {
Self {
witness_utxo: Some(psbt_input.witness_utxo.into()),
redeem_script: Some(psbt_input.redeem_script.0.clone()),
witness_script: Some(psbt_input.witness_script.0.clone()),
..Default::default()
}
}
}

#[derive(Debug, Clone)]
pub struct TxIn {
pub previous_output: OutPoint,
}

impl From<TxIn> for bitcoin::TxIn {
fn from(tx_in: TxIn) -> Self {
bitcoin::TxIn { previous_output: tx_in.previous_output.into(), ..Default::default() }
}
}

impl From<bitcoin::TxIn> for TxIn {
fn from(tx_in: bitcoin::TxIn) -> Self {
TxIn { previous_output: tx_in.previous_output.into() }
}
}

#[derive(Debug, Clone)]
pub struct TxOut {
/// The value of the output, in satoshis.
Expand Down Expand Up @@ -72,3 +123,12 @@ impl From<Network> for bitcoin::Network {
}
}
}

#[derive(Clone, Debug)]
pub struct ScriptBuf(pub payjoin::bitcoin::ScriptBuf);

impl From<payjoin::bitcoin::ScriptBuf> for ScriptBuf {
fn from(value: payjoin::bitcoin::ScriptBuf) -> Self {
Self(value)
}
}
16 changes: 15 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::fmt::Debug;

use payjoin::bitcoin::psbt::PsbtParseError;
use payjoin::receive::{RequestError, SelectionError};
use payjoin::receive::{
InputContributionError, OutputSubstitutionError, PsbtInputError, RequestError, SelectionError,
};
use payjoin::send::{CreateRequestError, ResponseError as PdkResponseError, ValidationError};

#[derive(Debug, PartialEq, Eq, thiserror::Error)]
Expand Down Expand Up @@ -62,6 +64,15 @@ pub enum PayjoinError {

#[error("{message}")]
IoError { message: String },

#[error("{message}")]
OutputSubstitutionError { message: String },

#[error("{message}")]
InputContributionError { message: String },

#[error("{message}")]
InputPairError { message: String },
}

macro_rules! impl_from_error {
Expand All @@ -86,6 +97,9 @@ impl_from_error! {
ValidationError => ValidationError,
CreateRequestError => CreateRequestError,
uniffi::UnexpectedUniFFICallbackError => UnexpectedError,
OutputSubstitutionError => OutputSubstitutionError,
InputContributionError => InputContributionError,
PsbtInputError => InputPairError,
}

impl From<SelectionError> for PayjoinError {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod uri;

pub use crate::bitcoin::*;
use crate::error::PayjoinError;
pub use crate::ohttp::OhttpKeys;
pub use crate::ohttp::*;
pub use crate::request::Request;
pub use crate::uri::{PjUri, PjUriBuilder, Uri, Url};

Expand Down
17 changes: 17 additions & 0 deletions src/ohttp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,20 @@ impl OhttpKeys {
payjoin::OhttpKeys::decode(bytes.as_slice()).map(|e| e.into()).map_err(|e| e.into())
}
}

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)))
}
}
Loading

0 comments on commit 6df3799

Please sign in to comment.