From e05b73e37ad1e19c290f6bc75f1cb72f0ca57120 Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Wed, 15 Nov 2023 10:31:29 -0500 Subject: [PATCH] temp --- .../org/bitcoindevkit/LiveWalletTest.kt | 2 + .../bitcoindevkit/OfflineDescriptorTest.kt | 1 + bdk-ffi/src/bdk.udl | 36 +++ bdk-ffi/src/bitcoin.rs | 20 ++ bdk-ffi/src/lib.rs | 10 +- bdk-ffi/src/wallet.rs | 231 +++++++++--------- 6 files changed, 184 insertions(+), 116 deletions(-) diff --git a/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveWalletTest.kt b/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveWalletTest.kt index 0fd4fb30..73153634 100644 --- a/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveWalletTest.kt +++ b/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/LiveWalletTest.kt @@ -15,6 +15,8 @@ class LiveWalletTest { val update = esploraClient.scan(wallet, 10uL, 1uL) wallet.applyUpdate(update) println("Balance: ${wallet.getBalance().total()}") + val balance: Balance = wallet.getBalance() + println("Balance: $balance") assert(wallet.getBalance().total() > 0uL) } diff --git a/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/OfflineDescriptorTest.kt b/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/OfflineDescriptorTest.kt index 84788065..c9e46e12 100644 --- a/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/OfflineDescriptorTest.kt +++ b/bdk-android/lib/src/androidTest/kotlin/org/bitcoindevkit/OfflineDescriptorTest.kt @@ -1,6 +1,7 @@ package org.bitcoindevkit import kotlin.test.Test +import kotlin.test.assertEquals import androidx.test.ext.junit.runners.AndroidJUnit4 import org.junit.runner.RunWith import kotlin.test.assertEquals diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index caabbaaf..d1159834 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -43,6 +43,12 @@ enum BdkError { "Psbt", }; +enum ChangeSpendPolicy { + "ChangeAllowed", + "OnlyChange", + "ChangeForbidden" +}; + interface Balance { u64 immature(); @@ -95,8 +101,24 @@ interface TxBuilder { TxBuilder add_recipient(Script script, u64 amount); + TxBuilder set_recipients(sequence script_amount); + + TxBuilder add_unspendable(OutPoint unspendable); + + TxBuilder add_utxo(OutPoint outpoint); + + TxBuilder change_policy(ChangeSpendPolicy change_policy); + + TxBuilder do_not_spend_change(); + + TxBuilder only_spend_change(); + + TxBuilder manually_selected_only(); + TxBuilder fee_rate(float sat_per_vbyte); + TxBuilder drain_wallet(); + [Throws=BdkError] PartiallySignedTransaction finish([ByRef] Wallet wallet); }; @@ -198,6 +220,15 @@ interface EsploraClient { Update scan(Wallet wallet, u64 stop_gap, u64 parallel_requests); }; +// ------------------------------------------------------------------------ +// bdk-ffi-defined types +// ------------------------------------------------------------------------ + +dictionary ScriptAmount { + Script script; + u64 amount; +}; + // ------------------------------------------------------------------------ // bdk crate - bitcoin re-exports // ------------------------------------------------------------------------ @@ -263,3 +294,8 @@ interface PartiallySignedTransaction { Transaction extract_tx(); }; + +dictionary OutPoint { + string txid; + u32 vout; +}; diff --git a/bdk-ffi/src/bitcoin.rs b/bdk-ffi/src/bitcoin.rs index b6e19de3..739bd99c 100644 --- a/bdk-ffi/src/bitcoin.rs +++ b/bdk-ffi/src/bitcoin.rs @@ -4,7 +4,9 @@ use bdk::bitcoin::consensus::Decodable; use bdk::bitcoin::network::constants::Network as BdkNetwork; use bdk::bitcoin::psbt::PartiallySignedTransaction as BdkPartiallySignedTransaction; use bdk::bitcoin::Address as BdkAddress; +use bdk::bitcoin::OutPoint as BdkOutPoint; use bdk::bitcoin::Transaction as BdkTransaction; +use bdk::bitcoin::Txid; use bdk::Error as BdkError; use std::io::Cursor; @@ -279,3 +281,21 @@ impl From for PartiallySignedTransaction { } } } + +/// A reference to a transaction output. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +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 BdkOutPoint { + fn from(outpoint: &OutPoint) -> Self { + BdkOutPoint { + txid: Txid::from_str(&outpoint.txid).unwrap(), + vout: outpoint.vout, + } + } +} diff --git a/bdk-ffi/src/lib.rs b/bdk-ffi/src/lib.rs index 5bc4e30f..185529b4 100644 --- a/bdk-ffi/src/lib.rs +++ b/bdk-ffi/src/lib.rs @@ -7,6 +7,7 @@ mod wallet; // TODO 6: Why are these imports required? use crate::bitcoin::Address; use crate::bitcoin::Network; +use crate::bitcoin::OutPoint; use crate::bitcoin::PartiallySignedTransaction; use crate::bitcoin::Script; use crate::bitcoin::Transaction; @@ -21,6 +22,7 @@ use crate::wallet::Update; use crate::wallet::Wallet; use bdk::keys::bip39::WordCount; +use bdk::wallet::tx_builder::ChangeSpendPolicy; use bdk::wallet::AddressIndex as BdkAddressIndex; use bdk::wallet::AddressInfo as BdkAddressInfo; use bdk::wallet::Balance as BdkBalance; @@ -32,10 +34,10 @@ use std::sync::Arc; uniffi::include_scaffolding!("bdk"); /// A output script and an amount of satoshis. -// pub struct ScriptAmount { -// pub script: Arc