From 4e15badb14d34db4911641f345e99987d132a81c Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Thu, 1 Sep 2022 13:32:13 -0400 Subject: [PATCH 1/2] Update BDK to version 0.22 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index d9eb03ee..274cde58 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,7 +14,7 @@ crate-type = ["staticlib", "cdylib"] name = "bdkffi" [dependencies] -bdk = { version = "0.20", features = ["all-keys", "use-esplora-ureq", "sqlite-bundled"] } +bdk = { version = "0.22", features = ["all-keys", "use-esplora-ureq", "sqlite-bundled"] } uniffi_macros = { version = "0.19.3", features = ["builtin-bindgen"] } uniffi = { version = "0.19.3", features = ["builtin-bindgen"] } From 3c6075ad96afa238dc3ceca71ba82cb10088bb90 Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Thu, 1 Sep 2022 14:04:36 -0400 Subject: [PATCH 2/2] Add Balance struct and conversion from BdkBalance --- CHANGELOG.md | 2 ++ src/bdk.udl | 12 +++++++++++- src/lib.rs | 38 +++++++++++++++++++++++++++++++++----- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a6776cc2..5b695445 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Remove dictionary `ExtendedKeyInfo {mnenonic, xprv, fingerprint}` [#154] - Remove interface `Transaction` [#190] - Changed `Wallet` interface `list_transaction()` to return array of `TransactionDetails` [#190] + - Update `bdk` dependency version to 0.22 [#193] - APIs Added [#154] - `generate_mnemonic()`, returns string mnemonic - `interface DescriptorSecretKey` @@ -37,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#154]: https://github.com/bitcoindevkit/bdk-ffi/pull/154 [#184]: https://github.com/bitcoindevkit/bdk-ffi/pull/184 [#185]: https://github.com/bitcoindevkit/bdk-ffi/pull/185 +[#193]: https://github.com/bitcoindevkit/bdk-ffi/pull/193 ## [v0.8.0] - Update BDK to version 0.20.0 [#169] diff --git a/src/bdk.udl b/src/bdk.udl index 50411e35..cf68b8ad 100644 --- a/src/bdk.udl +++ b/src/bdk.udl @@ -7,6 +7,7 @@ namespace bdk { enum BdkError { "InvalidU32Bytes", "Generic", + "MissingCachedScripts", "ScriptDoesntHaveAddressForm", "NoRecipients", "NoUtxosSelected", @@ -73,6 +74,15 @@ dictionary SqliteDbConfiguration { string path; }; +dictionary Balance { + u64 immature; + u64 trusted_pending; + u64 untrusted_pending; + u64 confirmed; + u64 spendable; + u64 total; +}; + [Enum] interface DatabaseConfig { Memory(); @@ -176,7 +186,7 @@ interface Wallet { AddressInfo get_address(AddressIndex address_index); [Throws=BdkError] - u64 get_balance(); + Balance get_balance(); [Throws=BdkError] boolean sign([ByRef] PartiallySignedBitcoinTransaction psbt); diff --git a/src/lib.rs b/src/lib.rs index 3976b0f9..b1545561 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,8 +23,8 @@ use bdk::wallet::tx_builder::ChangeSpendPolicy; use bdk::wallet::AddressIndex as BdkAddressIndex; use bdk::wallet::AddressInfo as BdkAddressInfo; use bdk::{ - BlockTime, Error, FeeRate, KeychainKind, SignOptions, SyncOptions as BdkSyncOptions, - Wallet as BdkWallet, + Balance as BdkBalance, BlockTime, Error, FeeRate, KeychainKind, SignOptions, + SyncOptions as BdkSyncOptions, Wallet as BdkWallet, }; use std::collections::HashSet; use std::convert::{From, TryFrom}; @@ -150,7 +150,7 @@ pub struct TransactionDetails { /// Sent value (sats) /// Sum of owned inputs of this transaction. pub sent: u64, - /// Fee value (sats) if available. + /// Fee value (sats) if confirmed. /// The availability of the fee depends on the backend. It's never None with an Electrum /// Server backend, but it could be None with a Bitcoin RPC node without txindex that receive /// funds while offline. @@ -246,6 +246,34 @@ impl From<&OutPoint> for BdkOutPoint { } } +pub struct Balance { + // All coinbase outputs not yet matured + pub immature: u64, + /// Unconfirmed UTXOs generated by a wallet tx + pub trusted_pending: u64, + /// Unconfirmed UTXOs received from an external wallet + pub untrusted_pending: u64, + /// Confirmed and immediately spendable balance + pub confirmed: u64, + /// Get sum of trusted_pending and confirmed coins + pub spendable: u64, + /// Get the whole balance visible to the wallet + pub total: u64, +} + +impl From for Balance { + fn from(bdk_balance: BdkBalance) -> Self { + Balance { + immature: bdk_balance.immature, + trusted_pending: bdk_balance.trusted_pending, + untrusted_pending: bdk_balance.untrusted_pending, + confirmed: bdk_balance.confirmed, + spendable: bdk_balance.get_spendable(), + total: bdk_balance.get_total(), + } + } +} + /// A transaction output, which defines new coins to be created from old ones. pub struct TxOut { /// The value of the output, in satoshis. @@ -402,8 +430,8 @@ impl Wallet { /// Return the balance, meaning the sum of this wallet’s unspent outputs’ values. Note that this method only operates /// on the internal database, which first needs to be Wallet.sync manually. - fn get_balance(&self) -> Result { - self.get_wallet().get_balance() + fn get_balance(&self) -> Result { + self.get_wallet().get_balance().map(|b| b.into()) } /// Sign a transaction with all the wallet’s signers.