Skip to content

Commit

Permalink
feat: use bdk names for CanonicalTx and ChainPosition exposed structures
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Apr 9, 2024
1 parent ab9763b commit c87602f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 60 deletions.
24 changes: 12 additions & 12 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ dictionary TxOut {
Script script_pubkey;
};

[Enum]
interface ChainPosition {
Confirmed(u32 height, u64 timestamp);
Unconfirmed(u64 timestamp);
};

dictionary CanonicalTx {
Transaction transaction;
ChainPosition confirmation;
};

// ------------------------------------------------------------------------
// bdk crate - wallet module
// ------------------------------------------------------------------------
Expand Down Expand Up @@ -147,7 +158,7 @@ interface Wallet {

SentAndReceivedValues sent_and_received([ByRef] Transaction tx);

sequence<TransactionDetails> transactions();
sequence<CanonicalTx> transactions();

[Throws=CalculateFeeError]
u64 calculate_fee([ByRef] Transaction tx);
Expand Down Expand Up @@ -363,17 +374,6 @@ interface Address {
boolean is_valid_for_network(Network network);
};

[Enum]
interface ConfirmationDetails {
Confirmed(u32 height, u64 timestamp);
Unconfirmed(u64 timestamp);
};

dictionary TransactionDetails {
Transaction transaction;
ConfirmationDetails confirmation;
};

interface Transaction {
[Throws=Alpha3Error]
constructor(sequence<u8> transaction_bytes);
Expand Down
35 changes: 2 additions & 33 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::error::Alpha3Error;

use bdk::bitcoin::address::{NetworkChecked, NetworkUnchecked};
use bdk::bitcoin::blockdata::script::ScriptBuf as BdkScriptBuf;
use bdk::bitcoin::blockdata::transaction::TxOut as BdkTxOut;
Expand All @@ -8,10 +10,7 @@ use bdk::bitcoin::Network;
use bdk::bitcoin::OutPoint as BdkOutPoint;
use bdk::bitcoin::Transaction as BdkTransaction;
use bdk::bitcoin::Txid;
use bdk::chain::tx_graph::CanonicalTx as BdkCanonicalTx;

use crate::error::Alpha3Error;
use bdk::chain::{ChainPosition, ConfirmationTimeHeightAnchor};
use std::io::Cursor;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
Expand Down Expand Up @@ -116,36 +115,6 @@ impl From<BdkAddress> for Address {
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConfirmationDetails {
Confirmed { height: u32, timestamp: u64 },
Unconfirmed { timestamp: u64 },
}

pub struct TransactionDetails {
pub transaction: Arc<Transaction>,
pub confirmation: ConfirmationDetails,
}

impl<'a> From<BdkCanonicalTx<'a, BdkTransaction, ConfirmationTimeHeightAnchor>>
for TransactionDetails
{
fn from(tx: BdkCanonicalTx<'a, BdkTransaction, ConfirmationTimeHeightAnchor>) -> Self {
let confirmation = match tx.chain_position {
ChainPosition::Confirmed(anchor) => ConfirmationDetails::Confirmed {
height: anchor.confirmation_height,
timestamp: anchor.confirmation_time,
},
ChainPosition::Unconfirmed(timestamp) => ConfirmationDetails::Unconfirmed { timestamp },
};

TransactionDetails {
transaction: Arc::new(Transaction::from(tx.tx_node.tx)),
confirmation,
}
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Transaction {
inner: BdkTransaction,
Expand Down
9 changes: 4 additions & 5 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use crate::error::Alpha3Error;
use crate::error::CalculateFeeError;
use crate::error::EsploraError;
use crate::error::FeeRateError;
use crate::error::PersistenceError;
use crate::error::WalletCreationError;
use crate::esplora::EsploraClient;
use crate::keys::DerivationPath;
use crate::keys::DescriptorPublicKey;
Expand All @@ -25,6 +27,8 @@ use crate::keys::Mnemonic;
use crate::types::AddressIndex;
use crate::types::AddressInfo;
use crate::types::Balance;
use crate::types::CanonicalTx;
use crate::types::ChainPosition;
use crate::types::FeeRate;
use crate::types::LocalOutput;
use crate::types::ScriptAmount;
Expand All @@ -34,11 +38,6 @@ use crate::wallet::TxBuilder;
use crate::wallet::Update;
use crate::wallet::Wallet;

use crate::bitcoin::ConfirmationDetails;
use crate::bitcoin::TransactionDetails;

use crate::error::PersistenceError;
use crate::error::WalletCreationError;
use bdk::bitcoin::Network;
use bdk::keys::bip39::WordCount;
use bdk::wallet::tx_builder::ChangeSpendPolicy;
Expand Down
38 changes: 34 additions & 4 deletions bdk-ffi/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
use crate::bitcoin::{Address, OutPoint, Script, TxOut};
use crate::bitcoin::{Address, OutPoint, Script, Transaction, TxOut};
use crate::error::FeeRateError;

use bdk::bitcoin::FeeRate as BdkFeeRate;
use bdk::bitcoin::Transaction as BdkTransaction;
use bdk::chain::tx_graph::CanonicalTx as BdkCanonicalTx;
use bdk::chain::{ChainPosition as BdkChainPosition, ConfirmationTimeHeightAnchor};
use bdk::wallet::AddressIndex as BdkAddressIndex;
use bdk::wallet::AddressInfo as BdkAddressInfo;
use bdk::wallet::Balance as BdkBalance;
use bdk::KeychainKind;
use bdk::LocalOutput as BdkLocalOutput;

use bdk::bitcoin::FeeRate as BdkFeeRate;

use crate::error::FeeRateError;
use std::sync::Arc;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ChainPosition {
Confirmed { height: u32, timestamp: u64 },
Unconfirmed { timestamp: u64 },
}

pub struct CanonicalTx {
pub transaction: Arc<Transaction>,
pub confirmation: ChainPosition,
}

impl From<BdkCanonicalTx<'_, BdkTransaction, ConfirmationTimeHeightAnchor>> for CanonicalTx {
fn from(tx: BdkCanonicalTx<'_, BdkTransaction, ConfirmationTimeHeightAnchor>) -> Self {
let confirmation = match tx.chain_position {
BdkChainPosition::Confirmed(anchor) => ChainPosition::Confirmed {
height: anchor.confirmation_height,
timestamp: anchor.confirmation_time,
},
BdkChainPosition::Unconfirmed(timestamp) => ChainPosition::Unconfirmed { timestamp },
};

CanonicalTx {
transaction: Arc::new(Transaction::from(tx.tx_node.tx)),
confirmation,
}
}
}

#[derive(Clone, Debug)]
pub struct FeeRate(pub BdkFeeRate);

Expand Down
9 changes: 3 additions & 6 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use crate::bitcoin::{OutPoint, PartiallySignedTransaction, Transaction, TransactionDetails};
use crate::bitcoin::{OutPoint, PartiallySignedTransaction, Script, Transaction};
use crate::descriptor::Descriptor;
use crate::error::{Alpha3Error, CalculateFeeError, PersistenceError, WalletCreationError};
use crate::types::ScriptAmount;
use crate::types::{Balance, FeeRate};
use crate::Script;
use crate::{AddressIndex, AddressInfo};
use crate::types::{AddressIndex, AddressInfo, Balance, CanonicalTx, FeeRate, ScriptAmount};

use bdk::bitcoin::blockdata::script::ScriptBuf as BdkScriptBuf;
use bdk::bitcoin::psbt::PartiallySignedTransaction as BdkPartiallySignedTransaction;
Expand Down Expand Up @@ -104,7 +101,7 @@ impl Wallet {
SentAndReceivedValues { sent, received }
}

pub fn transactions(&self) -> Vec<TransactionDetails> {
pub fn transactions(&self) -> Vec<CanonicalTx> {
self.get_wallet()
.transactions()
.map(|tx| tx.into())
Expand Down

0 comments on commit c87602f

Please sign in to comment.