From f05a6648a7b71d028e1952e7aaed050d634e601c Mon Sep 17 00:00:00 2001 From: Steve Myers Date: Mon, 5 Sep 2022 11:02:29 -0700 Subject: [PATCH] Refactor TransactionDetails to include confirmation_time (#190) --- CHANGELOG.md | 6 +++++- src/bdk.udl | 15 +++++---------- src/lib.rs | 44 ++++++++++---------------------------------- 3 files changed, 20 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 96958a6..a6776cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Remove `generate_extended_key`, returned ExtendedKeyInfo [#154] - Remove `restore_extended_key`, returned ExtendedKeyInfo [#154] - Remove dictionary `ExtendedKeyInfo {mnenonic, xprv, fingerprint}` [#154] + - Remove interface `Transaction` [#190] + - Changed `Wallet` interface `list_transaction()` to return array of `TransactionDetails` [#190] - APIs Added [#154] - `generate_mnemonic()`, returns string mnemonic - `interface DescriptorSecretKey` @@ -25,7 +27,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `extend(DerivationPath)` extends and returns DescriptorPublicKey - `as_string()` returns DescriptorPublicKey as String - Add to `interface Blockchain` the `get_height()` and `get_block_hash()` methods [#184] - - Add to `interface TxBuilder` the `set_recipients(recipient: Vec)` method [#186] + - Add to `interface TxBuilder` the `set_recipients(recipient: Vec)` method [#186] + - Add to `dictionary TransactionDetails` the `confirmation_time` field [#190] - Interfaces Added [#154] - `DescriptorSecretKey` - `DescriptorPublicKey` @@ -46,6 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#158]: https://github.com/bitcoindevkit/bdk-ffi/pull/158 [#164]: https://github.com/bitcoindevkit/bdk-ffi/pull/164 [#169]: https://github.com/bitcoindevkit/bdk-ffi/pull/169 +[#190]: https://github.com/bitcoindevkit/bdk-ffi/pull/190 ## [v0.7.0] - Update BDK to version 0.19.0 diff --git a/src/bdk.udl b/src/bdk.udl index cca7474..50411e3 100644 --- a/src/bdk.udl +++ b/src/bdk.udl @@ -85,6 +85,7 @@ dictionary TransactionDetails { u64 received; u64 sent; string txid; + BlockTime? confirmation_time; }; dictionary BlockTime { @@ -92,12 +93,6 @@ dictionary BlockTime { u64 timestamp; }; -[Enum] -interface Transaction { - Unconfirmed(TransactionDetails details); - Confirmed(TransactionDetails details, BlockTime confirmation); -}; - enum WordCount { "Words12", "Words15", @@ -187,7 +182,7 @@ interface Wallet { boolean sign([ByRef] PartiallySignedBitcoinTransaction psbt); [Throws=BdkError] - sequence list_transactions(); + sequence list_transactions(); Network network(); @@ -239,9 +234,9 @@ interface TxBuilder { TxBuilder enable_rbf_with_sequence(u32 nsequence); TxBuilder add_data(sequence data); - + TxBuilder set_recipients(sequence recipients); - + [Throws=BdkError] PartiallySignedBitcoinTransaction finish([ByRef] Wallet wallet); }; @@ -285,4 +280,4 @@ interface DescriptorPublicKey { DescriptorPublicKey extend(DerivationPath path); string as_string(); -}; \ No newline at end of file +}; diff --git a/src/lib.rs b/src/lib.rs index d7d5030..3976b0f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -155,23 +155,9 @@ pub struct TransactionDetails { /// Server backend, but it could be None with a Bitcoin RPC node without txindex that receive /// funds while offline. pub fee: Option, -} - -/// A transaction, either of type Confirmed or Unconfirmed -#[derive(Debug, Clone, PartialEq, Eq)] -pub enum Transaction { - /// A transaction that has yet to be included in a block - Unconfirmed { - /// The details of wallet transaction. - details: TransactionDetails, - }, - /// A transaction that has been mined and is part of a block - Confirmed { - /// The details of wallet transaction - details: TransactionDetails, - /// Timestamp and block height of the block in which this transaction was mined - confirmation: BlockTime, - }, + /// If the transaction is confirmed, contains height and timestamp of the block containing the + /// transaction, unconfirmed transaction contains `None`. + pub confirmation_time: Option, } impl From<&bdk::TransactionDetails> for TransactionDetails { @@ -181,20 +167,7 @@ impl From<&bdk::TransactionDetails> for TransactionDetails { txid: x.txid.to_string(), received: x.received, sent: x.sent, - } - } -} - -impl From<&bdk::TransactionDetails> for Transaction { - fn from(x: &bdk::TransactionDetails) -> Transaction { - match x.confirmation_time.clone() { - Some(block_time) => Transaction::Confirmed { - details: TransactionDetails::from(x), - confirmation: block_time, - }, - None => Transaction::Unconfirmed { - details: TransactionDetails::from(x), - }, + confirmation_time: x.confirmation_time.clone(), } } } @@ -440,9 +413,12 @@ impl Wallet { } /// Return the list of transactions made and received by the wallet. Note that this method only operate on the internal database, which first needs to be [Wallet.sync] manually. - fn list_transactions(&self) -> Result, Error> { - let transactions = self.get_wallet().list_transactions(true)?; - Ok(transactions.iter().map(Transaction::from).collect()) + fn list_transactions(&self) -> Result, Error> { + let transaction_details = self.get_wallet().list_transactions(true)?; + Ok(transaction_details + .iter() + .map(TransactionDetails::from) + .collect()) } /// Return the list of unspent outputs of this wallet. Note that this method only operates on the internal database,