Skip to content

Commit 0b05917

Browse files
committed
WIP
1 parent 2fceda8 commit 0b05917

File tree

17 files changed

+757
-244
lines changed

17 files changed

+757
-244
lines changed

examples/example_wallet_electrum/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ edition = "2021"
66
[dependencies]
77
bdk_wallet = { path = "../../wallet", features = ["file_store"] }
88
#bdk_electrum = { version = "0.21" }
9-
bdk_electrum = { git = "https://github.com/bitcoindevkit/bdk", rev = "b70758652d1c819c88d92765905d09e26b951ee2" }
9+
bdk_electrum = { git = "https://github.com/evanlinjin/bdk", branch = "superimposed_canonicalization" }
1010
anyhow = "1"

examples/example_wallet_electrum/src/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn main() -> Result<(), anyhow::Error> {
4141
wallet.persist(&mut db)?;
4242
println!("Generated Address: {}", address);
4343

44-
let balance = wallet.balance();
44+
let balance = wallet.balance(wallet.include_unbroadcasted());
4545
println!("Wallet balance before syncing: {}", balance.total());
4646

4747
print!("Syncing...");
@@ -70,7 +70,7 @@ fn main() -> Result<(), anyhow::Error> {
7070
wallet.apply_update(update)?;
7171
wallet.persist(&mut db)?;
7272

73-
let balance = wallet.balance();
73+
let balance = wallet.balance(wallet.include_unbroadcasted());
7474
println!("Wallet balance after syncing: {}", balance.total());
7575

7676
if balance.total() < SEND_AMOUNT {
@@ -81,14 +81,17 @@ fn main() -> Result<(), anyhow::Error> {
8181
std::process::exit(0);
8282
}
8383

84-
let mut tx_builder = wallet.build_tx();
84+
let canonicalization_params = wallet.include_unbroadcasted();
85+
let mut tx_builder = wallet.build_tx(canonicalization_params);
8586
tx_builder.add_recipient(address.script_pubkey(), SEND_AMOUNT);
8687

8788
let mut psbt = tx_builder.finish()?;
8889
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
8990
assert!(finalized);
9091

9192
let tx = psbt.extract_tx()?;
93+
wallet.insert_unbroadcasted(tx.clone());
94+
9295
client.transaction_broadcast(&tx)?;
9396
println!("Tx broadcasted! Txid: {}", tx.compute_txid());
9497

examples/example_wallet_esplora_async/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edition = "2021"
88
[dependencies]
99
bdk_wallet = { path = "../../wallet", features = ["rusqlite"] }
1010
#bdk_esplora = { version = "0.20", features = ["async-https", "tokio"] }
11-
bdk_esplora = { git = "https://github.com/bitcoindevkit/bdk", rev = "b70758652d1c819c88d92765905d09e26b951ee2", features = ["async-https", "tokio"] }
12-
bdk_testenv = { git = "https://github.com/bitcoindevkit/bdk", rev = "b70758652d1c819c88d92765905d09e26b951ee2" }
11+
bdk_esplora = { git = "https://github.com/evanlinjin/bdk", branch = "superimposed_canonicalization", features = ["async-https", "tokio"] }
12+
bdk_testenv = { git = "https://github.com/evanlinjin/bdk", branch = "superimposed_canonicalization" }
1313
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] }
1414
anyhow = "1"

examples/example_wallet_esplora_async/src/main.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,14 @@ async fn main() -> Result<(), anyhow::Error> {
154154
wallet.apply_update(resp)?;
155155
wallet.persist(&mut conn)?;
156156

157-
assert_eq!(wallet.balance().total(), Amount::ONE_BTC);
158-
println!("Balance after send tx1: {}", wallet.balance().total());
157+
assert_eq!(
158+
wallet.balance(wallet.include_unbroadcasted()).total(),
159+
Amount::ONE_BTC
160+
);
161+
println!(
162+
"Balance after send tx1: {}",
163+
wallet.balance(wallet.include_unbroadcasted()).total()
164+
);
159165
// We should expect tx1 to occur in a future sync
160166
let exp_spk_txids = wallet
161167
.tx_graph()
@@ -189,8 +195,14 @@ async fn main() -> Result<(), anyhow::Error> {
189195
wallet.apply_update(resp)?;
190196
wallet.persist(&mut conn)?;
191197

192-
println!("Balance after send tx2: {}", wallet.balance().total());
193-
assert_eq!(wallet.balance().total(), Amount::ZERO);
198+
println!(
199+
"Balance after send tx2: {}",
200+
wallet.balance(wallet.include_unbroadcasted()).total()
201+
);
202+
assert_eq!(
203+
wallet.balance(wallet.include_unbroadcasted()).total(),
204+
Amount::ZERO
205+
);
194206

195207
// Load the persisted wallet
196208
{
@@ -201,13 +213,21 @@ async fn main() -> Result<(), anyhow::Error> {
201213
// tx1 is there, but is not canonical
202214
assert!(wallet.tx_graph().full_txs().any(|node| node.txid == txid1));
203215
assert!(wallet
204-
.tx_graph()
205-
.list_canonical_txs(wallet.local_chain(), wallet.local_chain().tip().block_id())
216+
.transactions(wallet.include_unbroadcasted())
217+
.next()
218+
.is_none());
219+
assert!(wallet
220+
.list_unspent(wallet.include_unbroadcasted())
206221
.next()
207222
.is_none());
208-
assert!(wallet.list_unspent().next().is_none());
209-
assert_eq!(wallet.balance().total(), Amount::ZERO);
210-
println!("Balance after load wallet: {}", wallet.balance().total());
223+
assert_eq!(
224+
wallet.balance(wallet.include_unbroadcasted()).total(),
225+
Amount::ZERO
226+
);
227+
println!(
228+
"Balance after load wallet: {}",
229+
wallet.balance(wallet.include_unbroadcasted()).total()
230+
);
211231
}
212232

213233
Ok(())

examples/example_wallet_esplora_blocking/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ publish = false
99
[dependencies]
1010
bdk_wallet = { path = "../../wallet", features = ["file_store"] }
1111
#bdk_esplora = { version = "0.20", features = ["blocking"] }
12-
bdk_esplora = { git = "https://github.com/bitcoindevkit/bdk", rev = "b70758652d1c819c88d92765905d09e26b951ee2", features = ["blocking"] }
12+
bdk_esplora = { git = "https://github.com/evanlinjin/bdk", branch = "superimposed_canonicalization", features = ["blocking"] }
1313
anyhow = "1"

examples/example_wallet_esplora_blocking/src/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn main() -> Result<(), anyhow::Error> {
4141
address.index, address.address
4242
);
4343

44-
let balance = wallet.balance();
44+
let balance = wallet.balance(wallet.include_unbroadcasted());
4545
println!("Wallet balance before syncing: {}", balance.total());
4646

4747
print!("Syncing...");
@@ -65,7 +65,7 @@ fn main() -> Result<(), anyhow::Error> {
6565
wallet.persist(&mut db)?;
6666
println!();
6767

68-
let balance = wallet.balance();
68+
let balance = wallet.balance(wallet.include_unbroadcasted());
6969
println!("Wallet balance after syncing: {}", balance.total());
7070

7171
if balance.total() < SEND_AMOUNT {
@@ -76,14 +76,17 @@ fn main() -> Result<(), anyhow::Error> {
7676
std::process::exit(0);
7777
}
7878

79-
let mut tx_builder = wallet.build_tx();
79+
let params = wallet.include_unbroadcasted();
80+
let mut tx_builder = wallet.build_tx(params);
8081
tx_builder.add_recipient(address.script_pubkey(), SEND_AMOUNT);
8182

8283
let mut psbt = tx_builder.finish()?;
8384
let finalized = wallet.sign(&mut psbt, SignOptions::default())?;
8485
assert!(finalized);
8586

8687
let tx = psbt.extract_tx()?;
88+
wallet.insert_unbroadcasted(tx.clone());
89+
8790
client.broadcast(&tx)?;
8891
println!("Tx broadcasted! Txid: {}", tx.compute_txid());
8992

examples/example_wallet_rpc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ edition = "2021"
88
[dependencies]
99
bdk_wallet = { path = "../../wallet", features = ["file_store"] }
1010
#bdk_bitcoind_rpc = { version = "0.18" }
11-
bdk_bitcoind_rpc = { git = "https://github.com/bitcoindevkit/bdk", rev = "b70758652d1c819c88d92765905d09e26b951ee2" }
11+
bdk_bitcoind_rpc = { git = "https://github.com/evanlinjin/bdk", branch = "superimposed_canonicalization" }
1212

1313
anyhow = "1"
1414
clap = { version = "4.5.17", features = ["derive", "env"] }

examples/example_wallet_rpc/src/main.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ fn main() -> anyhow::Result<()> {
110110
start_load_wallet.elapsed().as_secs_f32()
111111
);
112112

113-
let balance = wallet.balance();
113+
let balance = wallet.balance(wallet.include_unbroadcasted());
114114
println!("Wallet balance before syncing: {}", balance.total());
115115

116116
let wallet_tip = wallet.latest_checkpoint();
@@ -173,7 +173,7 @@ fn main() -> anyhow::Result<()> {
173173
}
174174
}
175175
let wallet_tip_end = wallet.latest_checkpoint();
176-
let balance = wallet.balance();
176+
let balance = wallet.balance(wallet.include_unbroadcasted());
177177
println!(
178178
"Synced {} blocks in {}s",
179179
blocks_received,
@@ -187,8 +187,8 @@ fn main() -> anyhow::Result<()> {
187187
println!("Wallet balance is {}", balance.total());
188188
println!(
189189
"Wallet has {} transactions and {} utxos",
190-
wallet.transactions().count(),
191-
wallet.list_unspent().count()
190+
wallet.transactions(wallet.include_unbroadcasted()).count(),
191+
wallet.list_unspent(wallet.include_unbroadcasted()).count()
192192
);
193193

194194
Ok(())

wallet/Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ serde_json = { version = "^1.0" }
2828
bip39 = { version = "2.0", optional = true }
2929

3030
[dependencies.bdk_chain]
31-
git = "https://github.com/bitcoindevkit/bdk"
32-
rev = "b70758652d1c819c88d92765905d09e26b951ee2"
31+
git = "https://github.com/evanlinjin/bdk"
32+
branch = "superimposed_canonicalization"
3333
default-features = false
3434
features = ["miniscript", "serde"]
3535

3636
[dependencies.bdk_file_store]
37-
git = "https://github.com/bitcoindevkit/bdk"
38-
rev = "b70758652d1c819c88d92765905d09e26b951ee2"
37+
git = "https://github.com/evanlinjin/bdk"
38+
branch = "superimposed_canonicalization"
3939
optional = true
4040

4141
[features]
@@ -59,8 +59,8 @@ anyhow = "1"
5959
rand = "^0.8"
6060

6161
[dev-dependencies.bdk_chain]
62-
git = "https://github.com/bitcoindevkit/bdk"
63-
rev = "b70758652d1c819c88d92765905d09e26b951ee2"
62+
git = "https://github.com/evanlinjin/bdk"
63+
branch = "superimposed_canonicalization"
6464
features = ["rusqlite"]
6565

6666
[package.metadata.docs.rs]

wallet/src/wallet/changeset.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use bdk_chain::{
33
};
44
use miniscript::{Descriptor, DescriptorPublicKey};
55

6+
use super::unbroadcasted;
7+
68
type IndexedTxGraphChangeSet =
79
indexed_tx_graph::ChangeSet<ConfirmationBlockTime, keychain_txout::ChangeSet>;
810

@@ -21,6 +23,8 @@ pub struct ChangeSet {
2123
pub tx_graph: tx_graph::ChangeSet<ConfirmationBlockTime>,
2224
/// Changes to [`KeychainTxOutIndex`](keychain_txout::KeychainTxOutIndex).
2325
pub indexer: keychain_txout::ChangeSet,
26+
/// Changes to [`Unbroadcasted`](crate::unbroadcasted::Unbroadcasted).
27+
pub unbroadcasted: unbroadcasted::ChangeSet,
2428
}
2529

2630
impl Merge for ChangeSet {

wallet/src/wallet/coin_selection.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@
9191
//! .require_network(Network::Testnet)
9292
//! .unwrap();
9393
//! let psbt = {
94-
//! let mut builder = wallet.build_tx().coin_selection(AlwaysSpendEverything);
94+
//! let params = wallet.include_unbroadcasted();
95+
//! let mut builder = wallet
96+
//! .build_tx(params)
97+
//! .coin_selection(AlwaysSpendEverything);
9598
//! builder.add_recipient(to_address.script_pubkey(), Amount::from_sat(50_000));
9699
//! builder.finish()?
97100
//! };

wallet/src/wallet/export.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,15 @@ impl FullyNodedExport {
128128
Self::is_compatible_with_core(&descriptor)?;
129129

130130
let blockheight = if include_blockheight {
131-
wallet.transactions().next().map_or(0, |canonical_tx| {
132-
canonical_tx
133-
.chain_position
134-
.confirmation_height_upper_bound()
135-
.unwrap_or(0)
136-
})
131+
wallet
132+
.transactions(wallet.include_unbroadcasted())
133+
.next()
134+
.map_or(0, |canonical_tx| {
135+
canonical_tx
136+
.chain_position
137+
.confirmation_height_upper_bound()
138+
.unwrap_or(0)
139+
})
137140
} else {
138141
0
139142
};

0 commit comments

Comments
 (0)