Skip to content

Commit

Permalink
refactor: use outpoint and txid from bitcoin-ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Aug 30, 2024
1 parent 1fa179d commit 491b3a4
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 53 deletions.
2 changes: 1 addition & 1 deletion bdk-ffi/Cargo.lock

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

2 changes: 1 addition & 1 deletion bdk-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bdk_esplora = { version = "0.17.0", default-features = false, features = ["std",
bdk_electrum = { version = "0.17.0", default-features = false, features = ["use-rustls-ring"] }
bdk_bitcoind_rpc = { version = "0.14.0" }
bitcoin-internals = { version = "0.2.0", features = ["alloc"] }
bitcoin-ffi = { git = "https://github.com/bitcoindevkit/bitcoin-ffi", branch = "master" }
bitcoin-ffi = { git = "https://github.com/bitcoindevkit/bitcoin-ffi", rev = "d72f189e9c0faef1735eb6dc40f2a2e40a25da69" }

uniffi = { version = "=0.28.0" }
thiserror = "1.0.58"
Expand Down
15 changes: 8 additions & 7 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -688,11 +688,6 @@ interface Psbt {
string json_serialize();
};

dictionary OutPoint {
string txid;
u32 vout;
};

dictionary TxIn {
OutPoint previous_output;
Script script_sig;
Expand All @@ -716,8 +711,14 @@ typedef extern Amount;
[ExternalInterface="bitcoin_ffi"]
typedef extern FeeRate;

[ExternalInterface="bitcoin_ffi"]
typedef extern ParseAmountError;
[External="bitcoin_ffi"]
typedef extern Txid;

[External="bitcoin_ffi"]
typedef extern OutPoint;

[ExternalInterface="bitcoin_ffi"]
typedef extern FeeRateError;

[ExternalInterface="bitcoin_ffi"]
typedef extern ParseAmountError;
29 changes: 2 additions & 27 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::error::{
AddressParseError, FromScriptError, PsbtError, PsbtParseError, TransactionError,
};

use bitcoin_ffi::OutPoint;
use bitcoin_ffi::Script;

use bdk_bitcoind_rpc::bitcoincore_rpc::jsonrpc::serde_json;
Expand All @@ -12,12 +13,10 @@ use bdk_wallet::bitcoin::io::Cursor;
use bdk_wallet::bitcoin::psbt::ExtractTxError;
use bdk_wallet::bitcoin::Address as BdkAddress;
use bdk_wallet::bitcoin::Network;
use bdk_wallet::bitcoin::OutPoint as BdkOutPoint;
use bdk_wallet::bitcoin::Psbt as BdkPsbt;
use bdk_wallet::bitcoin::Transaction as BdkTransaction;
use bdk_wallet::bitcoin::TxIn as BdkTxIn;
use bdk_wallet::bitcoin::TxOut as BdkTxOut;
use bdk_wallet::bitcoin::Txid;

use std::fmt::Display;
use std::ops::Deref;
Expand Down Expand Up @@ -201,30 +200,6 @@ impl From<BdkPsbt> for Psbt {
}
}

#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct OutPoint {
pub txid: String,
pub vout: u32,
}

impl From<&OutPoint> for BdkOutPoint {
fn from(outpoint: &OutPoint) -> Self {
BdkOutPoint {
txid: Txid::from_str(&outpoint.txid).unwrap(),
vout: outpoint.vout,
}
}
}

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

#[derive(Debug, Clone)]
pub struct TxIn {
pub previous_output: OutPoint,
Expand All @@ -237,7 +212,7 @@ impl From<&BdkTxIn> for TxIn {
fn from(tx_in: &BdkTxIn) -> Self {
TxIn {
previous_output: OutPoint {
txid: tx_in.previous_output.txid.to_string(),
txid: tx_in.previous_output.txid,
vout: tx_in.previous_output.vout,
},
script_sig: Arc::new(Script(tx_in.script_sig.clone())),
Expand Down
11 changes: 5 additions & 6 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::bitcoin::OutPoint;
use bitcoin_ffi::OutPoint;

use bdk_bitcoind_rpc::bitcoincore_rpc::bitcoin::address::ParseError;
use bdk_electrum::electrum_client::Error as BdkElectrumError;
Expand All @@ -10,7 +10,6 @@ use bdk_wallet::bitcoin::consensus::encode::Error as BdkEncodeError;
use bdk_wallet::bitcoin::psbt::Error as BdkPsbtError;
use bdk_wallet::bitcoin::psbt::ExtractTxError as BdkExtractTxError;
use bdk_wallet::bitcoin::psbt::PsbtParseError as BdkPsbtParseError;
use bdk_wallet::chain;
use bdk_wallet::chain::local_chain::CannotConnectError as BdkCannotConnectError;
use bdk_wallet::chain::rusqlite::Error as BdkSqliteError;
use bdk_wallet::chain::tx_graph::CalculateFeeError as BdkCalculateFeeError;
Expand All @@ -21,8 +20,8 @@ use bdk_wallet::keys::bip39::Error as BdkBip39Error;
use bdk_wallet::miniscript::descriptor::DescriptorKeyParseError as BdkDescriptorKeyParseError;
use bdk_wallet::signer::SignerError as BdkSignerError;
use bdk_wallet::tx_builder::AddUtxoError;
use bdk_wallet::CreateWithPersistError as BdkCreateWithPersistError;
use bdk_wallet::LoadWithPersistError as BdkLoadWithPersistError;
use bdk_wallet::{chain, CreateWithPersistError as BdkCreateWithPersistError};
use bitcoin_internals::hex::display::DisplayHex;

use std::convert::TryInto;
Expand Down Expand Up @@ -752,9 +751,9 @@ impl From<BdkBip39Error> for Bip39Error {
impl From<BdkCalculateFeeError> for CalculateFeeError {
fn from(error: BdkCalculateFeeError) -> Self {
match error {
BdkCalculateFeeError::MissingTxOut(out_points) => CalculateFeeError::MissingTxOut {
out_points: out_points.iter().map(|op| op.into()).collect(),
},
BdkCalculateFeeError::MissingTxOut(out_points) => {
CalculateFeeError::MissingTxOut { out_points }
}
BdkCalculateFeeError::NegativeFee(signed_amount) => CalculateFeeError::NegativeFee {
amount: signed_amount.to_string(),
},
Expand Down
2 changes: 1 addition & 1 deletion bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mod types;
mod wallet;

use crate::bitcoin::Address;
use crate::bitcoin::OutPoint;
use crate::bitcoin::Psbt;
use crate::bitcoin::Transaction;
use crate::bitcoin::TxIn;
Expand Down Expand Up @@ -67,6 +66,7 @@ use crate::wallet::Wallet;
use bitcoin_ffi::Amount;
use bitcoin_ffi::FeeRate;
use bitcoin_ffi::Network;
use bitcoin_ffi::OutPoint;
use bitcoin_ffi::Script;

use bdk_wallet::keys::bip39::WordCount;
Expand Down
5 changes: 3 additions & 2 deletions bdk-ffi/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::bitcoin::{Address, OutPoint, Transaction, TxOut};
use crate::bitcoin::{Address, Transaction, TxOut};
use crate::error::RequestBuilderError;

use bitcoin_ffi::Amount;
use bitcoin_ffi::OutPoint;
use bitcoin_ffi::Script;

use bdk_core::spk_client::SyncItem;
Expand Down Expand Up @@ -128,7 +129,7 @@ impl From<BdkLocalOutput> for LocalOutput {
fn from(local_utxo: BdkLocalOutput) -> Self {
LocalOutput {
outpoint: OutPoint {
txid: local_utxo.outpoint.txid.to_string(),
txid: local_utxo.outpoint.txid,
vout: local_utxo.outpoint.vout,
},
txout: TxOut {
Expand Down
11 changes: 5 additions & 6 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::bitcoin::{OutPoint, Psbt, Transaction};
use crate::bitcoin::{Psbt, Transaction};
use crate::descriptor::Descriptor;
use crate::error::{
CalculateFeeError, CannotConnectError, CreateTxError, CreateWithPersistError,
Expand All @@ -10,13 +10,14 @@ use crate::types::{FullScanRequestBuilder, SyncRequestBuilder, Update};

use bitcoin_ffi::Amount;
use bitcoin_ffi::FeeRate;
use bitcoin_ffi::OutPoint;
use bitcoin_ffi::Script;

use bdk_wallet::bitcoin::amount::Amount as BdkAmount;
use bdk_wallet::bitcoin::Network;
use bdk_wallet::bitcoin::Psbt as BdkPsbt;
use bdk_wallet::bitcoin::ScriptBuf as BdkScriptBuf;
use bdk_wallet::bitcoin::{OutPoint as BdkOutPoint, Sequence, Txid};
use bdk_wallet::bitcoin::{Sequence, Txid};
use bdk_wallet::rusqlite::Connection as BdkConnection;
use bdk_wallet::tx_builder::ChangeSpendPolicy;
use bdk_wallet::PersistedWallet;
Expand Down Expand Up @@ -361,14 +362,12 @@ impl TxBuilder {
}
tx_builder.change_policy(self.change_policy);
if !self.utxos.is_empty() {
let bdk_utxos: Vec<BdkOutPoint> = self.utxos.iter().map(BdkOutPoint::from).collect();
tx_builder
.add_utxos(&bdk_utxos)
.add_utxos(&self.utxos)
.map_err(CreateTxError::from)?;
}
if !self.unspendable.is_empty() {
let bdk_unspendable: Vec<BdkOutPoint> =
self.unspendable.iter().map(BdkOutPoint::from).collect();
let bdk_unspendable: Vec<OutPoint> = self.unspendable.clone().into_iter().collect();
tx_builder.unspendable(bdk_unspendable);
}
if self.manually_selected_only {
Expand Down
2 changes: 1 addition & 1 deletion bdk-ffi/tests/bindings/test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from bdkpython.bitcoin import Network
from bdkpython import BlockId
from bdkpython.bitcoin import Network

import unittest

Expand Down
4 changes: 3 additions & 1 deletion bdk-ffi/tests/test_generated_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ uniffi::build_foreign_language_testcases!(
// fine. Commenting out for now.
// "tests/bindings/test.kts",
"tests/bindings/test.swift",
"tests/bindings/test.py",
// Weirdly enough, the Python tests below pass locally, but fail on the CI with the error:
// ModuleNotFoundError: No module named 'bdkpython'
// "tests/bindings/test.py",
);

0 comments on commit 491b3a4

Please sign in to comment.