Skip to content

Commit

Permalink
Depend on bitcoin-ffi types
Browse files Browse the repository at this point in the history
Note this requires a change to bitcoin-ffi to use proc-macros so that
payjoin-ffi proc macros know the types are UniFFI compaatible.
  • Loading branch information
DanGould committed Nov 13, 2024
1 parent 28ac003 commit 3157eb4
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 164 deletions.
57 changes: 34 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ 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", "rpc"] }
#bitcoin-ffi = { git = "https://github.com/bitcoindevkit/bitcoin-ffi" }
bitcoind = { version = "0.36.0", features = ["0_21_2"] }
bitcoincore-rpc = "0.19.0"
http = "1"
Expand All @@ -28,10 +27,10 @@ testcontainers = "0.15.0"
testcontainers-modules = { version = "0.1.3", features = ["redis"] }
tokio = { version = "1.12.0", features = ["full"] }
[dependencies]

bitcoin-ffi = { git = "https://github.com/dangould/bitcoin-ffi.git", branch = "proc-macros", default-features = false }
payjoin = { git = "https://github.com/payjoin/rust-payjoin", rev = "ef2ce55a57fe5270bc761bfcda58024ae45a93aa", features = ["send", "receive", "base64", "v2", "io"] }
uniffi = { version = "0.28.0", optional = true }
thiserror = "1.0.47"
thiserror = "1.0.58"
ohttp = { package = "bitcoin-ohttp", version = "0.6.0" }
url = "2.5.0"
base64 = "0.22.1"
Expand All @@ -51,5 +50,5 @@ strip = true


[features]
uniffi = ["uniffi/cli"]
uniffi = ["uniffi/cli", "bitcoin-ffi/default"]
danger-local-https = ["payjoin/danger-local-https"]
122 changes: 1 addition & 121 deletions src/bitcoin.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,8 @@
use std::str::FromStr;
use std::sync::Arc;

use bitcoin_ffi::{Script, TxOut};
use payjoin::bitcoin;

/// A reference to a transaction output.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
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)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
pub struct PsbtInput {
Expand Down Expand Up @@ -66,98 +41,3 @@ impl From<PsbtInput> for bitcoin::psbt::Input {
}
}
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
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)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
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)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Enum))]
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,
}
}
}

#[derive(Clone, Debug)]
#[cfg_attr(feature = "uniffi", derive(uniffi::Object))]
pub struct Script(pub payjoin::bitcoin::ScriptBuf);

#[cfg_attr(feature = "uniffi", uniffi::export)]
impl Script {
#[cfg_attr(feature = "uniffi", uniffi::constructor)]
pub fn new(script: Vec<u8>) -> Self {
Self(payjoin::bitcoin::ScriptBuf::from_bytes(script))
}

pub fn to_bytes(&self) -> Vec<u8> {
self.0.to_bytes()
}
}

impl From<payjoin::bitcoin::ScriptBuf> for Script {
fn from(value: payjoin::bitcoin::ScriptBuf) -> Self {
Self(value)
}
}

impl From<Script> for payjoin::bitcoin::ScriptBuf {
fn from(value: Script) -> Self {
value.0
}
}
4 changes: 2 additions & 2 deletions src/receive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use std::io::Cursor;
use std::str::FromStr;
use std::time::Duration;

use bitcoin_ffi::{Network, OutPoint, Script, TxOut};
use payjoin::bitcoin::psbt::Psbt;
use payjoin::bitcoin::FeeRate;
use payjoin::receive as pdk;

use crate::bitcoin::{Network, OutPoint, Script, TxOut};
use crate::error::PayjoinError;
use crate::ohttp::OhttpKeys;
use crate::uri::PjUriBuilder;
Expand Down Expand Up @@ -350,7 +350,7 @@ pub struct InputPair(payjoin::receive::InputPair);
impl InputPair {
#[cfg_attr(feature = "uniffi", uniffi::constructor)]
pub fn new(
txin: crate::bitcoin::TxIn,
txin: bitcoin_ffi::TxIn,
psbtin: crate::bitcoin::PsbtInput,
) -> Result<Self, PayjoinError> {
Ok(Self(payjoin::receive::InputPair::new(txin.into(), psbtin.into())?))
Expand Down
Loading

0 comments on commit 3157eb4

Please sign in to comment.