From 3abdbf5ba0e29b034bf29ad642ce86f69721b682 Mon Sep 17 00:00:00 2001 From: FadedCoder Date: Sun, 27 Feb 2022 11:19:53 +0530 Subject: [PATCH 1/2] Add support for custom sorting and deprecate BIP69 --- src/wallet/mod.rs | 1 + src/wallet/tx_builder.rs | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/wallet/mod.rs b/src/wallet/mod.rs index a24f922dc..103c69f9c 100644 --- a/src/wallet/mod.rs +++ b/src/wallet/mod.rs @@ -2145,6 +2145,7 @@ pub(crate) mod test { let (wallet, _, _) = get_funded_wallet(get_test_wpkh()); let addr = wallet.get_address(New).unwrap(); let mut builder = wallet.build_tx(); + #[allow(deprecated)] builder .add_recipient(addr.script_pubkey(), 30_000) .add_recipient(addr.script_pubkey(), 10_000) diff --git a/src/wallet/tx_builder.rs b/src/wallet/tx_builder.rs index 66b037854..9fa009350 100644 --- a/src/wallet/tx_builder.rs +++ b/src/wallet/tx_builder.rs @@ -36,11 +36,14 @@ //! # Ok::<(), bdk::Error>(()) //! ``` +use std::cmp::Ordering; use std::collections::BTreeMap; use std::collections::HashSet; use std::default::Default; use std::marker::PhantomData; +use std::sync::Arc; +use bitcoin::blockdata::transaction::{TxIn, TxOut}; use bitcoin::util::psbt::{self, PartiallySignedTransaction as Psbt}; use bitcoin::{OutPoint, Script, SigHashType, Transaction}; @@ -646,14 +649,22 @@ impl<'a, B, D: BatchDatabase> TxBuilder<'a, B, D, DefaultCoinSelectionAlgorithm, } /// Ordering of the transaction's inputs and outputs -#[derive(Debug, Ord, PartialOrd, Eq, PartialEq, Hash, Clone, Copy)] +#[derive(Clone)] pub enum TxOrdering { /// Randomized (default) Shuffle, /// Unchanged Untouched, /// BIP69 / Lexicographic + #[deprecated = "BIP69 does not improve privacy as was the intention of the BIP"] Bip69Lexicographic, + /// Provide custom comparison functions for sorting + Custom { + /// Transaction inputs sort function + input_sort: Arc Ordering>, + /// Transaction outputs sort function + output_sort: Arc Ordering>, + }, } impl Default for TxOrdering { @@ -662,6 +673,18 @@ impl Default for TxOrdering { } } +impl std::fmt::Debug for TxOrdering { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + TxOrdering::Shuffle => write!(f, "Randomized"), + TxOrdering::Untouched => write!(f, "Unchanged"), + #[allow(deprecated)] + TxOrdering::Bip69Lexicographic => write!(f, "BIP69 / Lexicographic"), + TxOrdering::Custom { .. } => write!(f, "Custom"), + } + } +} + impl TxOrdering { /// Sort transaction inputs and outputs by [`TxOrdering`] variant pub fn sort_tx(&self, tx: &mut Transaction) { @@ -679,6 +702,7 @@ impl TxOrdering { tx.output.shuffle(&mut rng); } + #[allow(deprecated)] TxOrdering::Bip69Lexicographic => { tx.input.sort_unstable_by_key(|txin| { (txin.previous_output.txid, txin.previous_output.vout) @@ -686,6 +710,13 @@ impl TxOrdering { tx.output .sort_unstable_by_key(|txout| (txout.value, txout.script_pubkey.clone())); } + TxOrdering::Custom { + input_sort, + output_sort, + } => { + tx.input.sort_unstable_by(|a, b| input_sort(a, b)); + tx.output.sort_unstable_by(|a, b| output_sort(a, b)); + } } } } @@ -769,7 +800,7 @@ mod test { #[test] fn test_output_ordering_default_shuffle() { - assert_eq!(TxOrdering::default(), TxOrdering::Shuffle); + assert!(std::matches!(TxOrdering::default(), TxOrdering::Shuffle)); } #[test] @@ -800,6 +831,7 @@ mod test { let original_tx = ordering_test_tx!(); let mut tx = original_tx; + #[allow(deprecated)] TxOrdering::Bip69Lexicographic.sort_tx(&mut tx); assert_eq!( From 946819f8e7a7059fb1b812f3422ce6187718949e Mon Sep 17 00:00:00 2001 From: Soham Sen Date: Sun, 27 Feb 2022 11:46:10 +0530 Subject: [PATCH 2/2] Updated CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17bf16242..4550eb1c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Removed default verification from `wallet::sync`. sync-time verification is added in `script_sync` and is activated by `verify` feature flag. - `verify` flag removed from `TransactionDetails`. +- Deprecated `TxOrdering::Bip69Lexicographic` and added `TxOrdering::Custom`. ## [v0.16.1] - [v0.16.0]