Skip to content

Commit 3854590

Browse files
committed
feat: convert between FFI/Rust changesets
1 parent 4cec06e commit 3854590

File tree

6 files changed

+77
-8
lines changed

6 files changed

+77
-8
lines changed

bdk-ffi/src/bdk.udl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,6 @@ interface FullScanScriptInspector {
304304
void inspect(KeychainKind keychain, u32 index, Script script);
305305
};
306306

307-
/// A changeset for [`Wallet`].
308-
[Remote]
309-
interface ChangeSet {};
310-
311307
// ------------------------------------------------------------------------
312308
// bdk_wallet crate - wallet module
313309
// ------------------------------------------------------------------------

bdk-ffi/src/bitcoin.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,22 @@ impl From<OutPoint> for BdkOutPoint {
7272
}
7373
}
7474

75-
/// An [`OutPoint`] suitable as a key in a hash map.
75+
/// An [`OutPoint`] used as a key in a hash map.
76+
///
77+
/// Due to limitations in generating the foreign language bindings, we cannot use [`OutPoint`] as a
78+
/// key for hash maps.
7679
#[derive(Debug, PartialEq, Eq, std::hash::Hash, uniffi::Object)]
7780
#[uniffi::export(Debug, Eq, Hash)]
7881
pub struct HashableOutPoint(pub(crate) OutPoint);
7982

8083
#[uniffi::export]
8184
impl HashableOutPoint {
85+
/// Create a key for a key-value store from an [`OutPoint`]
86+
#[uniffi::constructor]
87+
pub fn from_outpoint(outpoint: OutPoint) -> Self {
88+
Self(outpoint)
89+
}
90+
8291
/// Get the internal [`OutPoint`]
8392
pub fn outpoint(&self) -> OutPoint {
8493
self.0.clone()

bdk-ffi/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use crate::types::SyncScriptInspector;
4343

4444
use bdk_wallet::bitcoin::Network;
4545
use bdk_wallet::keys::bip39::WordCount;
46-
use bdk_wallet::ChangeSet;
4746
use bdk_wallet::KeychainKind;
4847

4948
uniffi::include_scaffolding!("bdk");

bdk-ffi/src/types.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::bitcoin::{
22
Address, Amount, BlockHash, DescriptorId, HashableOutPoint, OutPoint, Script, Transaction,
33
TxOut, Txid,
44
};
5+
use crate::descriptor::Descriptor;
56
use crate::error::{CreateTxError, RequestBuilderError};
67

78
use bdk_core::bitcoin::absolute::LockTime as BdkLockTime;
@@ -902,3 +903,69 @@ impl From<TxGraphChangeSet> for bdk_wallet::chain::tx_graph::ChangeSet<BdkConfir
902903
}
903904
}
904905
}
906+
907+
#[derive(Debug, Clone, uniffi::Record)]
908+
pub struct ChangeSet {
909+
pub descriptor: Option<Arc<Descriptor>>,
910+
pub change_descriptor: Option<Arc<Descriptor>>,
911+
pub network: Option<bdk_wallet::bitcoin::Network>,
912+
pub local_chain: LocalChainChangeSet,
913+
pub tx_graph: TxGraphChangeSet,
914+
pub indexer: IndexerChangeSet,
915+
}
916+
917+
impl From<ChangeSet> for bdk_wallet::ChangeSet {
918+
fn from(value: ChangeSet) -> Self {
919+
let descriptor = value.descriptor.map(|d| {
920+
let str_repr = d.to_string();
921+
str_repr.parse::<bdk_wallet::miniscript::Descriptor<bdk_wallet::miniscript::DescriptorPublicKey>>().unwrap()
922+
});
923+
let change_descriptor = value.change_descriptor.map(|d| {
924+
let str_repr = d.to_string();
925+
str_repr.parse::<bdk_wallet::miniscript::Descriptor<bdk_wallet::miniscript::DescriptorPublicKey>>().unwrap()
926+
});
927+
let network = value.network;
928+
let local_chain = value.local_chain.into();
929+
let tx_graph = value.tx_graph.into();
930+
let indexer = value.indexer.into();
931+
Self {
932+
descriptor,
933+
change_descriptor,
934+
network,
935+
local_chain,
936+
tx_graph,
937+
indexer,
938+
}
939+
}
940+
}
941+
942+
impl From<bdk_wallet::ChangeSet> for ChangeSet {
943+
fn from(value: bdk_wallet::ChangeSet) -> Self {
944+
let descriptor = value.descriptor.map(|d| {
945+
Arc::new(Descriptor {
946+
extended_descriptor: d,
947+
key_map: BTreeMap::new(),
948+
})
949+
});
950+
let change_descriptor = value
951+
.change_descriptor
952+
.map(|d| {
953+
Arc::new(Descriptor {
954+
extended_descriptor: d,
955+
key_map: BTreeMap::new(),
956+
})
957+
});
958+
let network = value.network;
959+
let local_chain = value.local_chain.into();
960+
let tx_graph = value.tx_graph.into();
961+
let indexer = value.indexer.into();
962+
Self {
963+
descriptor,
964+
change_descriptor,
965+
network,
966+
local_chain,
967+
tx_graph,
968+
indexer,
969+
}
970+
}
971+
}

bdk-ffi/tests/bindings/test.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*/
55

66
import org.bitcoindevkit.bitcoin.Network
7-
import org.bitcoindevkit.Condition
87

98
// A type from bitcoin-ffi
109
val network = Network.TESTNET

bdk-ffi/tests/bindings/test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from bdkpython import Condition
21
from bdkpython.bitcoin import Network
32

43
import unittest

0 commit comments

Comments
 (0)