example_electrum/
main.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
use std::io::{self, Write};

use bdk_chain::{
    bitcoin::Network,
    collections::BTreeSet,
    indexed_tx_graph,
    spk_client::{FullScanRequest, SyncRequest},
    ConfirmationBlockTime, Merge,
};
use bdk_electrum::{
    electrum_client::{self, Client, ElectrumApi},
    BdkElectrumClient,
};
use example_cli::{
    self,
    anyhow::{self, Context},
    clap::{self, Parser, Subcommand},
    ChangeSet, Keychain,
};

const DB_MAGIC: &[u8] = b"bdk_example_electrum";
const DB_PATH: &str = ".bdk_example_electrum.db";

#[derive(Subcommand, Debug, Clone)]
enum ElectrumCommands {
    /// Scans the addresses in the wallet using the electrum API.
    Scan {
        /// When a gap this large has been found for a keychain, it will stop.
        #[clap(long, default_value = "5")]
        stop_gap: usize,
        #[clap(flatten)]
        scan_options: ScanOptions,
        #[clap(flatten)]
        electrum_args: ElectrumArgs,
    },
    /// Scans particular addresses using the electrum API.
    Sync {
        /// Scan all the unused addresses.
        #[clap(long)]
        unused_spks: bool,
        /// Scan every address that you have derived.
        #[clap(long)]
        all_spks: bool,
        /// Scan unspent outpoints for spends or changes to confirmation status of residing tx.
        #[clap(long)]
        utxos: bool,
        /// Scan unconfirmed transactions for updates.
        #[clap(long)]
        unconfirmed: bool,
        #[clap(flatten)]
        scan_options: ScanOptions,
        #[clap(flatten)]
        electrum_args: ElectrumArgs,
    },
}

impl ElectrumCommands {
    fn electrum_args(&self) -> ElectrumArgs {
        match self {
            ElectrumCommands::Scan { electrum_args, .. } => electrum_args.clone(),
            ElectrumCommands::Sync { electrum_args, .. } => electrum_args.clone(),
        }
    }
}

#[derive(clap::Args, Debug, Clone)]
pub struct ElectrumArgs {
    /// The electrum url to use to connect to. If not provided it will use a default electrum server
    /// for your chosen network.
    electrum_url: Option<String>,
}

impl ElectrumArgs {
    pub fn client(&self, network: Network) -> anyhow::Result<Client> {
        let electrum_url = self.electrum_url.as_deref().unwrap_or(match network {
            Network::Bitcoin => "ssl://electrum.blockstream.info:50002",
            Network::Testnet => "ssl://electrum.blockstream.info:60002",
            Network::Regtest => "tcp://localhost:60401",
            Network::Signet => "tcp://signet-electrumx.wakiyamap.dev:50001",
            _ => panic!("Unknown network"),
        });
        let config = electrum_client::Config::builder()
            .validate_domain(matches!(network, Network::Bitcoin))
            .build();

        Ok(electrum_client::Client::from_config(electrum_url, config)?)
    }
}

#[derive(Parser, Debug, Clone, PartialEq)]
pub struct ScanOptions {
    /// Set batch size for each script_history call to electrum client.
    #[clap(long, default_value = "25")]
    pub batch_size: usize,
}

fn main() -> anyhow::Result<()> {
    let example_cli::Init {
        args,
        graph,
        chain,
        db,
        network,
    } = match example_cli::init_or_load::<ElectrumCommands, ElectrumArgs>(DB_MAGIC, DB_PATH)? {
        Some(init) => init,
        None => return Ok(()),
    };

    let electrum_cmd = match &args.command {
        example_cli::Commands::ChainSpecific(electrum_cmd) => electrum_cmd,
        general_cmd => {
            return example_cli::handle_commands(
                &graph,
                &chain,
                &db,
                network,
                |electrum_args, tx| {
                    let client = electrum_args.client(network)?;
                    client.transaction_broadcast(tx)?;
                    Ok(())
                },
                general_cmd.clone(),
            );
        }
    };

    let client = BdkElectrumClient::new(electrum_cmd.electrum_args().client(network)?);

    // Tell the electrum client about the txs we've already got locally so it doesn't re-download them
    client.populate_tx_cache(
        graph
            .lock()
            .unwrap()
            .graph()
            .full_txs()
            .map(|tx_node| tx_node.tx),
    );

    let (chain_update, tx_update, keychain_update) = match electrum_cmd.clone() {
        ElectrumCommands::Scan {
            stop_gap,
            scan_options,
            ..
        } => {
            let request = {
                let graph = &*graph.lock().unwrap();
                let chain = &*chain.lock().unwrap();

                FullScanRequest::builder()
                    .chain_tip(chain.tip())
                    .spks_for_keychain(
                        Keychain::External,
                        graph
                            .index
                            .unbounded_spk_iter(Keychain::External)
                            .into_iter()
                            .flatten(),
                    )
                    .spks_for_keychain(
                        Keychain::Internal,
                        graph
                            .index
                            .unbounded_spk_iter(Keychain::Internal)
                            .into_iter()
                            .flatten(),
                    )
                    .inspect({
                        let mut once = BTreeSet::new();
                        move |k, spk_i, _| {
                            if once.insert(k) {
                                eprint!("\nScanning {}: {} ", k, spk_i);
                            } else {
                                eprint!("{} ", spk_i);
                            }
                            io::stdout().flush().expect("must flush");
                        }
                    })
            };

            let res = client
                .full_scan::<_>(request, stop_gap, scan_options.batch_size, false)
                .context("scanning the blockchain")?;
            (
                res.chain_update,
                res.tx_update,
                Some(res.last_active_indices),
            )
        }
        ElectrumCommands::Sync {
            mut unused_spks,
            all_spks,
            mut utxos,
            mut unconfirmed,
            scan_options,
            ..
        } => {
            // Get a short lock on the tracker to get the spks we're interested in
            let graph = graph.lock().unwrap();
            let chain = chain.lock().unwrap();

            if !(all_spks || unused_spks || utxos || unconfirmed) {
                unused_spks = true;
                unconfirmed = true;
                utxos = true;
            } else if all_spks {
                unused_spks = false;
            }

            let chain_tip = chain.tip();
            let mut request =
                SyncRequest::builder()
                    .chain_tip(chain_tip.clone())
                    .inspect(|item, progress| {
                        let pc = (100 * progress.consumed()) as f32 / progress.total() as f32;
                        eprintln!("[ SCANNING {:03.0}% ] {}", pc, item);
                    });

            if all_spks {
                request = request.spks_with_indexes(graph.index.revealed_spks(..));
            }
            if unused_spks {
                request = request.spks_with_indexes(graph.index.unused_spks());
            }
            if utxos {
                let init_outpoints = graph.index.outpoints();
                request = request.outpoints(
                    graph
                        .graph()
                        .filter_chain_unspents(
                            &*chain,
                            chain_tip.block_id(),
                            init_outpoints.iter().cloned(),
                        )
                        .map(|(_, utxo)| utxo.outpoint),
                );
            };
            if unconfirmed {
                request = request.txids(
                    graph
                        .graph()
                        .list_canonical_txs(&*chain, chain_tip.block_id())
                        .filter(|canonical_tx| !canonical_tx.chain_position.is_confirmed())
                        .map(|canonical_tx| canonical_tx.tx_node.txid),
                );
            }

            let res = client
                .sync(request, scan_options.batch_size, false)
                .context("scanning the blockchain")?;

            // drop lock on graph and chain
            drop((graph, chain));

            (res.chain_update, res.tx_update, None)
        }
    };

    let db_changeset = {
        let mut chain = chain.lock().unwrap();
        let mut graph = graph.lock().unwrap();

        let chain_changeset = chain.apply_update(chain_update.expect("request has chain tip"))?;

        let mut indexed_tx_graph_changeset =
            indexed_tx_graph::ChangeSet::<ConfirmationBlockTime, _>::default();
        if let Some(keychain_update) = keychain_update {
            let keychain_changeset = graph.index.reveal_to_target_multi(&keychain_update);
            indexed_tx_graph_changeset.merge(keychain_changeset.into());
        }
        indexed_tx_graph_changeset.merge(graph.apply_update(tx_update));

        ChangeSet {
            local_chain: chain_changeset,
            tx_graph: indexed_tx_graph_changeset.tx_graph,
            indexer: indexed_tx_graph_changeset.indexer,
            ..Default::default()
        }
    };

    let mut db = db.lock().unwrap();
    db.append_changeset(&db_changeset)?;
    Ok(())
}