From deb22b07348b66a1679248cacef5382bb7a7f7ae Mon Sep 17 00:00:00 2001 From: DanGould Date: Wed, 13 Nov 2024 13:09:41 -0500 Subject: [PATCH] Depend on bitcoin-ffi types Note this requires a change to bitcoin-ffi to use proc-macros so that payjoin-ffi proc macros know the types are UniFFI compaatible. --- Cargo.lock | 57 +++++++----- Cargo.toml | 7 +- src/bitcoin.rs | 163 ---------------------------------- src/bitcoin_ffi.rs | 58 ++++++++++++ src/lib.rs | 4 +- src/receive/mod.rs | 6 +- src/receive/uni.rs | 21 +++-- src/send/mod.rs | 5 +- src/send/uni.rs | 2 +- src/uri.rs | 10 +-- tests/bdk_integration_test.rs | 6 +- 11 files changed, 122 insertions(+), 217 deletions(-) delete mode 100644 src/bitcoin.rs create mode 100644 src/bitcoin_ffi.rs diff --git a/Cargo.lock b/Cargo.lock index a0e384b..e88e112 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -166,7 +166,7 @@ dependencies = [ "proc-macro2", "quote", "serde", - "syn 2.0.48", + "syn 2.0.87", ] [[package]] @@ -183,7 +183,7 @@ checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9" dependencies = [ "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.87", ] [[package]] @@ -371,6 +371,16 @@ dependencies = [ "serde", ] +[[package]] +name = "bitcoin-ffi" +version = "0.1.2" +source = "git+https://github.com/bitcoindevkit/bitcoin-ffi.git?branch=master#4cd8e644dbf4e001d71d5fffb232480fa5ff2246" +dependencies = [ + "bitcoin 0.32.2", + "thiserror", + "uniffi", +] + [[package]] name = "bitcoin-hpke" version = "0.13.0" @@ -748,7 +758,7 @@ dependencies = [ "heck 0.4.1", "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.87", ] [[package]] @@ -1130,7 +1140,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.87", ] [[package]] @@ -1851,6 +1861,7 @@ version = "0.20.0" dependencies = [ "base64 0.22.1", "bdk", + "bitcoin-ffi", "bitcoin-ohttp", "bitcoincore-rpc", "bitcoind", @@ -1921,7 +1932,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.87", ] [[package]] @@ -1996,9 +2007,9 @@ checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" [[package]] name = "proc-macro2" -version = "1.0.76" +version = "1.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" +checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" dependencies = [ "unicode-ident", ] @@ -2340,7 +2351,7 @@ checksum = "7f81c2fde025af7e69b1d1420531c8a8811ca898919db177141a85313b1cb932" dependencies = [ "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.87", ] [[package]] @@ -2444,7 +2455,7 @@ checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.87", ] [[package]] @@ -2669,9 +2680,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.48" +version = "2.0.87" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" +checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" dependencies = [ "proc-macro2", "quote", @@ -2744,22 +2755,22 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.47" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "97a802ec30afc17eee47b2855fc72e0c4cd62be9b4efe6591edde0ec5bd68d8f" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.47" +version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6bb623b56e39ab7dcd4b1b98bb6c8f8d907ed255b18de254088016b27a8ee19b" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" dependencies = [ "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.87", ] [[package]] @@ -2833,7 +2844,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.87", ] [[package]] @@ -2927,7 +2938,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.87", ] [[package]] @@ -3087,7 +3098,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2fcfa22f55829d3aaa7acfb1c5150224188fe0f27c59a8a3eddcaa24d1ffbe58" dependencies = [ "quote", - "syn 2.0.48", + "syn 2.0.87", ] [[package]] @@ -3118,7 +3129,7 @@ dependencies = [ "proc-macro2", "quote", "serde", - "syn 2.0.48", + "syn 2.0.87", "toml", "uniffi_meta", ] @@ -3284,7 +3295,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.87", "wasm-bindgen-shared", ] @@ -3318,7 +3329,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.87", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -3611,7 +3622,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.87", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index cd3651c..657fd7a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" @@ -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/bitcoindevkit/bitcoin-ffi.git", branch = "master" } 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" @@ -51,5 +50,5 @@ strip = true [features] -uniffi = ["uniffi/cli"] +uniffi = ["uniffi/cli", "bitcoin-ffi/default"] danger-local-https = ["payjoin/danger-local-https"] diff --git a/src/bitcoin.rs b/src/bitcoin.rs deleted file mode 100644 index 2b51a19..0000000 --- a/src/bitcoin.rs +++ /dev/null @@ -1,163 +0,0 @@ -use std::str::FromStr; -use std::sync::Arc; - -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 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 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 { - pub witness_utxo: Option, - pub redeem_script: Option>, - pub witness_script: Option>, -} - -impl PsbtInput { - pub fn new( - witness_utxo: Option, - redeem_script: Option>, - witness_script: Option>, - ) -> Self { - Self { witness_utxo, redeem_script, witness_script } - } -} - -impl From 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| Arc::new(s.into())), - witness_script: psbt_input.witness_script.clone().map(|s| Arc::new(s.into())), - } - } -} - -impl From 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| Arc::unwrap_or_clone(s).into()), - witness_script: psbt_input.witness_script.map(|s| Arc::unwrap_or_clone(s).into()), - ..Default::default() - } - } -} - -#[derive(Debug, Clone)] -#[cfg_attr(feature = "uniffi", derive(uniffi::Record))] -pub struct TxIn { - pub previous_output: OutPoint, -} - -impl From for bitcoin::TxIn { - fn from(tx_in: TxIn) -> Self { - bitcoin::TxIn { previous_output: tx_in.previous_output.into(), ..Default::default() } - } -} - -impl From 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, -} - -impl From 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 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 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) -> Self { - Self(payjoin::bitcoin::ScriptBuf::from_bytes(script)) - } - - pub fn to_bytes(&self) -> Vec { - self.0.to_bytes() - } -} - -impl From for Script { - fn from(value: payjoin::bitcoin::ScriptBuf) -> Self { - Self(value) - } -} - -impl From