Skip to content

Commit

Permalink
Upgrade to unreleased payjoin-0.21
Browse files Browse the repository at this point in the history
Still rely on git head rather than 0.21 release.
  • Loading branch information
DanGould committed Nov 1, 2024
1 parent 34a2fa2 commit 3bf8a7e
Show file tree
Hide file tree
Showing 16 changed files with 1,158 additions and 908 deletions.
684 changes: 393 additions & 291 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ uniffi = { version = "0.28.0", features = ["build"] }

[dev-dependencies]
uniffi = { version = "0.28.0", features = ["bindgen-tests"] }
bdk = { version = "0.29.0", features = ["all-keys", "use-esplora-ureq", "keys-bip39"] }
bdk = { version = "0.29.0", features = ["all-keys", "use-esplora-ureq", "keys-bip39", "rpc"] }
bitcoind = { version = "0.36.0", features = ["0_21_2"] }
bitcoincore-rpc = "0.19.0"
http = "1"
payjoin-directory = { git = "https://github.com/payjoin/rust-payjoin", features = ["danger-local-https"] }
Expand All @@ -27,10 +28,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 = { git = "https://github.com/payjoin/rust-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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ The integration tests illustrates and verify integration using bitcoin core and
```shell

# Run the integration test
cargo test --package payjoin_ffi --test bitcoin_core_integration_test v1_to_v1_full_cycle
cargo test --package payjoin_ffi --test bitcoin_core_integration v1_to_v1_full_cycle
cargo test --package payjoin_ffi --test bdk_integration_test v1_to_v1_full_cycle
cargo test --package payjoin_ffi --test bdk_integration_test v2_to_v2_full_cycle --features danger-local-https

Expand Down
70 changes: 70 additions & 0 deletions src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,61 @@ impl From<bitcoin::OutPoint> for OutPoint {
}
}

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

impl PsbtInput {
pub fn new(
witness_utxo: Option<TxOut>,
redeem_script: Option<ScriptBuf>,
witness_script: Option<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.map(|s| s.into()),
redeem_script: psbt_input.redeem_script.clone().map(|s| s.into()),
witness_script: psbt_input.witness_script.clone().map(|s| s.into()),
}
}
}

impl From<PsbtInput> for bitcoin::psbt::Input {
fn from(psbt_input: PsbtInput) -> Self {
Self {
witness_utxo: psbt_input.witness_utxo.map(|s| s.into()),
redeem_script: psbt_input.redeem_script.map(|s| s.into()),
witness_script: psbt_input.witness_script.map(|s| s.into()),
..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 +127,18 @@ 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)
}
}

impl From<ScriptBuf> for payjoin::bitcoin::ScriptBuf {
fn from(value: ScriptBuf) -> Self {
value.0
}
}
25 changes: 22 additions & 3 deletions 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 All @@ -15,7 +17,7 @@ pub enum PayjoinError {
#[error("Error encountered while decoding PSBT: {message} ")]
PsbtParseError { message: String },

#[error("Response error: {message}")]
#[error("Response error: {message:?}")]
ResponseError { message: String },

///Error that may occur when the request from sender is malformed.
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 @@ -82,10 +93,18 @@ impl_from_error! {
payjoin::bitcoin::consensus::encode::Error => TransactionError,
payjoin::bitcoin::address::ParseError => InvalidAddress,
RequestError => RequestError,
PdkResponseError => ResponseError,
ValidationError => ValidationError,
CreateRequestError => CreateRequestError,
uniffi::UnexpectedUniFFICallbackError => UnexpectedError,
OutputSubstitutionError => OutputSubstitutionError,
InputContributionError => InputContributionError,
PsbtInputError => InputPairError,
}

impl From<PdkResponseError> for PayjoinError {
fn from(value: PdkResponseError) -> Self {
PayjoinError::ResponseError { message: format!("{:?}", value) }
}
}

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 3bf8a7e

Please sign in to comment.