Skip to content

Commit

Permalink
Replicate rust-payjoin directory structure
Browse files Browse the repository at this point in the history
Add bitcoin, ohttp, request modules. Remove types module.
  • Loading branch information
DanGould committed Nov 12, 2024
1 parent 98b5e13 commit 7586ec6
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 123 deletions.
74 changes: 74 additions & 0 deletions src/bitcoin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use std::str::FromStr;

use payjoin::bitcoin;

/// A reference to a transaction output.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct OutPoint {
/// The referenced transaction's txid.
pub txid: String,
/// The index of the referenced output in its transaction's vout.
pub vout: u32,
}

impl From<OutPoint> for bitcoin::OutPoint {
fn from(outpoint: OutPoint) -> Self {
bitcoin::OutPoint {
txid: bitcoin::Txid::from_str(&outpoint.txid).expect("Invalid txid"),
vout: outpoint.vout,
}
}
}

impl From<bitcoin::OutPoint> for OutPoint {
fn from(outpoint: bitcoin::OutPoint) -> Self {
OutPoint { txid: outpoint.txid.to_string(), vout: outpoint.vout }
}
}

#[derive(Debug, Clone)]
pub struct TxOut {
/// The value of the output, in satoshis.
pub value: u64,
/// The address of the output.
pub script_pubkey: Vec<u8>,
}

impl From<TxOut> for bitcoin::TxOut {
fn from(tx_out: TxOut) -> Self {
bitcoin::TxOut {
value: bitcoin::amount::Amount::from_sat(tx_out.value),
script_pubkey: bitcoin::ScriptBuf::from_bytes(tx_out.script_pubkey),
}
}
}

impl From<bitcoin::TxOut> for TxOut {
fn from(tx_out: bitcoin::TxOut) -> Self {
TxOut { value: tx_out.value.to_sat(), script_pubkey: tx_out.script_pubkey.to_bytes() }
}
}

#[derive(Clone, Default)]
pub enum Network {
///Bitcoin’s testnet
Testnet,
///Bitcoin’s regtest
Regtest,
#[default]
///Classic Bitcoin
Bitcoin,
///Bitcoin’s signet
Signet,
}

impl From<Network> for bitcoin::Network {
fn from(network: Network) -> Self {
match network {
Network::Signet => bitcoin::Network::Signet,
Network::Testnet => bitcoin::Network::Testnet,
Network::Regtest => bitcoin::Network::Regtest,
Network::Bitcoin => bitcoin::Network::Bitcoin,
}
}
}
2 changes: 1 addition & 1 deletion src/io.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::error::PayjoinError;
use crate::types::OhttpKeys;
use crate::ohttp::OhttpKeys;
use crate::uri::Url;

/// Fetch the ohttp keys from the specified payjoin directory via proxy.
Expand Down
27 changes: 25 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,42 @@
#![crate_name = "payjoin_ffi"]

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

pub use crate::bitcoin::*;
use crate::error::PayjoinError;
pub use crate::ohttp::*;
pub use crate::receive::v1::*;
pub use crate::receive::v2::*;
pub use crate::send::v1::*;
pub use crate::send::v2::*;
pub use crate::types::*;
pub use crate::uri::{PjUri, PjUriBuilder, Uri, Url};

#[cfg(feature = "uniffi")]
uniffi::include_scaffolding!("payjoin_ffi");

use std::sync::Arc;
///Represents data that needs to be transmitted to the receiver.
///You need to send this request over HTTP(S) to the receiver.
#[derive(Clone, Debug)]
pub struct Request {
///URL to send the request to.
///
///This is full URL with scheme etc - you can pass it right to reqwest or a similar library.
pub url: Arc<Url>,
///Bytes to be sent to the receiver.
///
///This is properly encoded PSBT, already in base64. You only need to make sure Content-Type is text/plain and Content-Length is body.len() (most libraries do the latter automatically).
pub body: Vec<u8>,
}

impl From<payjoin::Request> for Request {
fn from(value: payjoin::Request) -> Self {
Self { url: Arc::new(value.url.into()), body: value.body }
}
}
20 changes: 20 additions & 0 deletions src/ohttp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::error::PayjoinError;

impl From<payjoin::OhttpKeys> for OhttpKeys {
fn from(value: payjoin::OhttpKeys) -> Self {
Self(value)
}
}
impl From<OhttpKeys> for payjoin::OhttpKeys {
fn from(value: OhttpKeys) -> Self {
value.0
}
}
#[derive(Debug, Clone)]
pub struct OhttpKeys(pub payjoin::OhttpKeys);
impl OhttpKeys {
/// Decode an OHTTP KeyConfig
pub fn decode(bytes: Vec<u8>) -> Result<Self, PayjoinError> {
payjoin::OhttpKeys::decode(bytes.as_slice()).map(|e| e.into()).map_err(|e| e.into())
}
}
2 changes: 1 addition & 1 deletion src/receive/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use payjoin::bitcoin::psbt::Psbt;
use payjoin::bitcoin::FeeRate;
use payjoin::receive as pdk;

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

pub trait CanBroadcast {
fn callback(&self, tx: Vec<u8>) -> Result<bool, PayjoinError>;
Expand Down
5 changes: 3 additions & 2 deletions src/receive/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ use payjoin::bitcoin::psbt::Psbt;
use payjoin::bitcoin::FeeRate;
use payjoin::receive as pdk;

use crate::bitcoin::Network;
use crate::ohttp::OhttpKeys;
#[cfg(feature = "uniffi")]
use crate::receive::v1::{
CanBroadcast, GenerateScript, IsOutputKnown, IsScriptOwned, ProcessPartiallySignedTransaction,
};
use crate::types::Network;
use crate::uri::PjUriBuilder;
use crate::{OhttpKeys, OutPoint, PayjoinError, Request, TxOut, Url};
use crate::{OutPoint, PayjoinError, Request, TxOut, Url};

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

Expand Down
Empty file added src/request.rs
Empty file.
2 changes: 1 addition & 1 deletion src/send/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub use payjoin::send as pdk;

use crate::error::PayjoinError;
use crate::send::v2::ContextV2;
use crate::types::Request;
use crate::uri::{PjUri, Url};
use crate::Request;

///Builder for sender-side payjoin parameters
///
Expand Down
114 changes: 0 additions & 114 deletions src/types.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use payjoin::bitcoin::address::NetworkChecked;
use payjoin::UriExt;

use crate::error::PayjoinError;
use crate::types::OhttpKeys;
use crate::ohttp::OhttpKeys;
#[derive(Clone)]
pub struct Uri(payjoin::Uri<'static, NetworkChecked>);
impl From<Uri> for payjoin::Uri<'static, NetworkChecked> {
Expand Down
3 changes: 2 additions & 1 deletion tests/bitcoin_core_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use std::sync::Arc;

use bitcoincore_rpc::bitcoincore_rpc_json::WalletProcessPsbtResult;
use bitcoincore_rpc::{Auth, Client, RpcApi};
use payjoin_ffi::bitcoin::{OutPoint, Request, TxOut};
use payjoin_ffi::receive::v1::{Headers, PayjoinProposal, UncheckedProposal};
use payjoin_ffi::send::v1::RequestBuilder;
use payjoin_ffi::types::{OutPoint, Request, TxOut};
use payjoin_ffi::uri::{PjUriBuilder, Uri, Url};
use payjoin_ffi::Request;

type BoxError = Box<dyn std::error::Error>;

Expand Down

0 comments on commit 7586ec6

Please sign in to comment.