From 952932c24b9c23389fb7286ea5f2e22e8dc5f479 Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Thu, 19 Oct 2023 14:18:03 -0400 Subject: [PATCH] feat: add Transaction and PartiallySignedTransaction types --- bdk-ffi/src/bdk.udl | 30 +- bdk-ffi/src/bitcoin.rs | 320 ++++++++++++++++++ bdk-ffi/src/lib.rs | 201 +---------- bdk-ffi/src/psbt.rs | 119 ------- bdk-ffi/src/wallet.rs | 14 +- .../kotlin/org/bitcoindevkit/JvmLibTest.kt | 40 +-- 6 files changed, 386 insertions(+), 338 deletions(-) create mode 100644 bdk-ffi/src/bitcoin.rs delete mode 100644 bdk-ffi/src/psbt.rs diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index f73dc3a9..b689ab54 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -94,7 +94,7 @@ interface TxBuilder { TxBuilder fee_rate(float sat_per_vbyte); [Throws=BdkError] - string finish([ByRef] Wallet wallet); + PartiallySignedTransaction finish([ByRef] Wallet wallet); }; // ------------------------------------------------------------------------ @@ -231,3 +231,31 @@ interface Script { sequence to_bytes(); }; + +interface Transaction { + [Throws=BdkError] + constructor(sequence transaction_bytes); + + string txid(); + + u64 size(); + + u64 vsize(); + + boolean is_coin_base(); + + boolean is_explicitly_rbf(); + + boolean is_lock_time_enabled(); + + i32 version(); +}; + +interface PartiallySignedTransaction { + [Throws=BdkError] + constructor(string psbt_base64); + + string serialize(); + + Transaction extract_tx(); +}; diff --git a/bdk-ffi/src/bitcoin.rs b/bdk-ffi/src/bitcoin.rs new file mode 100644 index 00000000..efc0c6b5 --- /dev/null +++ b/bdk-ffi/src/bitcoin.rs @@ -0,0 +1,320 @@ +use bdk::bitcoin::address::{NetworkChecked, NetworkUnchecked}; +use bdk::bitcoin::blockdata::script::ScriptBuf as BdkScriptBuf; +use bdk::bitcoin::blockdata::transaction::Transaction as BdkTransaction; +use bdk::bitcoin::consensus::Decodable; +use bdk::bitcoin::psbt::PartiallySignedTransaction as BdkPartiallySignedTransaction; +use bdk::bitcoin::Address as BdkAddress; +use bdk::bitcoin::Network as BdkNetwork; +use bdk::Error as BdkError; +use std::io::Cursor; +use std::str::FromStr; +use std::sync::{Arc, Mutex}; + +pub enum Network { + /// Mainnet Bitcoin. + Bitcoin, + /// Bitcoin's testnet network. + Testnet, + /// Bitcoin's signet network. + Signet, + /// Bitcoin's regtest network. + Regtest, +} + +impl From for BdkNetwork { + fn from(network: Network) -> Self { + match network { + Network::Bitcoin => BdkNetwork::Bitcoin, + Network::Testnet => BdkNetwork::Testnet, + Network::Signet => BdkNetwork::Signet, + Network::Regtest => BdkNetwork::Regtest, + } + } +} + +impl From for Network { + fn from(network: BdkNetwork) -> Self { + match network { + BdkNetwork::Bitcoin => Network::Bitcoin, + BdkNetwork::Testnet => Network::Testnet, + BdkNetwork::Signet => Network::Signet, + BdkNetwork::Regtest => Network::Regtest, + _ => panic!("Network {} not supported", network), + } + } +} + +/// A Bitcoin address. +#[derive(Debug, PartialEq, Eq)] +pub struct Address { + inner: BdkAddress, +} + +impl Address { + pub fn new(address: String, network: Network) -> Result { + Ok(Address { + inner: address + .parse::>() + .unwrap() // TODO 11: Handle error correctly by rethrowing it as a BdkError + .require_network(network.into()) + .map_err(|e| BdkError::Generic(e.to_string()))?, + }) + } + + /// alternative constructor + // fn from_script(script: Arc