bp-wallet 0.12.0-rc.2

Modern, minimalistic & standard-compliant bitcoin wallet
Documentation
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
// Modern, minimalistic & standard-compliant cold wallet library.
//
// SPDX-License-Identifier: Apache-2.0
//
// Written in 2020-2024 by
//     Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
//
// Copyright (C) 2020-2024 LNP/BP Standards Association. All rights reserved.
// Copyright (C) 2020-2024 Dr Maxim Orlovsky. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::fs::File;
use std::path::{Path, PathBuf};
use std::process::exit;
use std::{fs, io};

use amplify::IoError;
use bpstd::psbt::{Beneficiary, TxParams};
use bpstd::{ConsensusEncode, Derive, IdxBase, Keychain, NormalIndex, Sats, Tx, XpubDerivable};
use colored::Colorize;
use descriptors::Descriptor;
use nonasync::persistence::PersistenceError;
use psbt::{ConstructionError, Payment, Psbt, PsbtConstructor, PsbtVer, UnfinalizedInputs};
use strict_encoding::Ident;

use crate::cli::{Args, Config, DescriptorOpts, Exec};
use crate::fs::FsTextStore;
use crate::{coinselect, AnyIndexerError, Indexer, OpType, Wallet, WalletAddr, WalletUtxo};

#[derive(Subcommand, Clone, PartialEq, Eq, Debug, Display)]
pub enum Command {
    /// List known named wallets
    #[display("list")]
    List,

    /// Get or set default wallet
    #[display("default")]
    Default {
        /// Name of the wallet to make it default
        default: Option<Ident>,
    },

    /// Create a named wallet
    #[display("create")]
    Create {
        /// The name for the new wallet
        name: Ident,
    },

    /// Generate a new wallet address(es)
    #[display("address")]
    Address {
        /// Use change keychain
        #[clap(short = '1', long)]
        change: bool,

        /// Use custom keychain
        #[clap(short, long, conflicts_with = "change")]
        keychain: Option<Keychain>,

        /// Use custom address index
        #[clap(short, long)]
        index: Option<NormalIndex>,

        /// Do not shift the last used index
        #[clap(short = 'D', long, conflicts_with_all = ["change", "index"])]
        dry_run: bool,

        /// Number of addresses to generate
        #[clap(short = 'C', long, default_value = "1")]
        count: u8,
    },

    /// Finalize a PSBT, optionally extracting and publishing the signed transaction
    #[display("finalize")]
    Finalize {
        /// Extract and send the signed transaction to the network.
        #[clap(short, long)]
        publish: bool,

        /// Name of PSBT file to finalize.
        psbt: PathBuf,

        /// File to save the extracted signed transaction.
        tx: Option<PathBuf>,
    },

    /// Extract a signed transaction from PSBT. The PSBT file itself is not modified
    #[display("finalize")]
    Extract {
        /// Send the extracted transaction to the network.
        #[clap(short, long)]
        publish: bool,

        /// Name of PSBT file to take the transaction from
        psbt: PathBuf,

        /// File to save the extracted signed transaction. If not provided, the transaction is
        /// print to STDOUT.
        tx: Option<PathBuf>,
    },
}

#[derive(Subcommand, Clone, PartialEq, Eq, Debug, Display)]
pub enum BpCommand {
    #[clap(flatten)]
    #[display(inner)]
    General(Command),

    /// List wallet balance and UTXOs
    #[display("balance")]
    Balance {
        /// Print balance for each individual address
        #[clap(short, long)]
        addr: bool,

        /// Print information about individual UTXOs
        #[clap(short, long)]
        utxo: bool,
    },

    /// Display history of wallet operations
    #[display("history")]
    History {
        /// Print full transaction ids
        #[clap(long)]
        txid: bool,

        /// Print operation details
        #[clap(long)]
        details: bool,
    },

    /// Inspect transaction
    Tx { tx: Tx },

    /// Inspect PSBT file
    Inspect {
        /// Name of a PSBT file to inspect
        psbt: PathBuf,
    },

    /// Compose a new PSBT for bitcoin payment
    #[display("construct")]
    Construct {
        /// Encode PSBT as V2
        #[clap(short = '2')]
        v2: bool,

        /// Bitcoin invoice in form of `<sats>@<address>`. To spend full wallet balance use
        /// `MAX` for the amount.
        ///
        /// If multiple `MAX` addresses provided the wallet balance is split between them in equal
        /// proportions.
        #[clap(long)]
        to: Vec<Beneficiary>,

        /// Fee
        fee: Sats,

        /// Name of a PSBT file to save. If not given, prints PSBT to STDOUT
        psbt: Option<PathBuf>,
    },
}

#[derive(Debug, Display, Error, From)]
#[non_exhaustive]
#[display(inner)]
pub enum ExecError {
    #[from]
    #[from(io::Error)]
    Io(IoError),

    #[from]
    Store(PersistenceError),

    #[from]
    ConstructPsbt(ConstructionError),

    #[from]
    DecodePsbt(psbt::DecodeError),

    #[from]
    Unfinalized(UnfinalizedInputs),

    /// indexer failed with {0}
    #[from]
    #[cfg_attr(feature = "electrum", from(electrum::Error))]
    #[cfg_attr(feature = "electrum", from(crate::indexers::electrum::ElectrumError))]
    #[cfg_attr(feature = "esplora", from(esplora::Error))]
    #[display(doc_comments)]
    Indexer(AnyIndexerError),
}

impl<O: DescriptorOpts> Exec for Args<Command, O> {
    type Error = ExecError;
    const CONF_FILE_NAME: &'static str = "bp.toml";

    fn exec(self, mut config: Config, conf_filename: &'static str) -> Result<(), Self::Error> {
        match &self.command {
            Command::List => {
                let dir = self.general.base_dir();
                let Ok(dir) = fs::read_dir(dir).inspect_err(|err| {
                    error!("Error reading wallet directory: {err:?}");
                    eprintln!("System directory is not initialized");
                    println!("no wallets found");
                }) else {
                    return Ok(());
                };
                println!("Known wallets:");
                let mut count = 0usize;
                for wallet in dir {
                    let Ok(entry) = wallet else {
                        continue;
                    };
                    let Ok(meta) = entry.metadata() else {
                        continue;
                    };
                    if !meta.is_dir() {
                        continue;
                    }
                    count += 1;
                    let name = entry.file_name().into_string().expect("invalid directory name");
                    print!(
                        "{name}{}",
                        if config.default_wallet == name { "\t[default]\t" } else { "\t\t" }
                    );
                    let provider = FsTextStore::new(entry.path().clone())?;
                    let wallet = match Wallet::<XpubDerivable, O::Descr>::load(provider, true) {
                        Err(err) => {
                            error!("Error loading wallet descriptor: {err}");
                            println!("# broken wallet descriptor");
                            continue;
                        }
                        Ok(wallet) => wallet,
                    };
                    println!("\t{}", wallet.descriptor());
                }
                if count == 0 {
                    println!("no wallets found");
                }
            }
            Command::Default { default } => {
                if let Some(default) = default {
                    config.default_wallet = default.to_string();
                    config.store(&self.conf_path(conf_filename));
                } else {
                    println!("Default wallet is '{}'", config.default_wallet);
                }
            }
            Command::Create { name } => {
                if !self.wallet.descriptor_opts.is_some() {
                    eprintln!("Error: you must provide an argument specifying wallet descriptor");
                    exit(1);
                }
                print!("Saving the wallet as '{name}' ... ");
                let mut wallet = self.bp_wallet::<O::Descr>(&config)?;
                let name = name.to_string();
                let provider = FsTextStore::new(self.general.wallet_dir(&name))?;
                wallet.make_persistent(provider, true)?;
                wallet.set_name(name);
                if let Err(err) = wallet.store() {
                    println!("error: {err}");
                } else {
                    println!("success");
                }
            }
            Command::Address {
                change,
                keychain,
                index,
                dry_run: no_shift,
                count: no,
            } => {
                let mut wallet = self.bp_wallet::<O::Descr>(&config)?;
                let keychain = match (change, keychain) {
                    (false, None) => wallet.default_keychain(),
                    (true, None) => (*change as u8).into(),
                    (false, Some(keychain)) => *keychain,
                    _ => unreachable!(),
                };
                if !wallet.keychains().contains(&keychain) {
                    eprintln!(
                        "Error: the specified keychain {keychain} is not a part of the descriptor"
                    );
                    exit(1);
                }
                let index =
                    index.unwrap_or_else(|| wallet.next_derivation_index(keychain, !*no_shift));
                println!("\nTerm.\tAddress");
                for derived_addr in
                    wallet.addresses(keychain).skip(index.index() as usize).take(*no as usize)
                {
                    println!("{}\t{}", derived_addr.terminal, derived_addr.addr);
                }
            }
            Command::Finalize {
                publish,
                psbt: psbt_path,
                tx,
            } => {
                let mut psbt = psbt_read(psbt_path)?;
                if psbt.is_finalized() {
                    eprintln!("The PSBT is already finalized");
                } else {
                    let wallet = self.bp_wallet::<O::Descr>(&config)?;
                    psbt_finalize(&mut psbt, wallet.descriptor())?;
                }

                psbt_write(&psbt, psbt_path)?;
                if let Ok(tx) = psbt_extract(&psbt, *publish, tx.as_deref()) {
                    if *publish {
                        let indexer = self.indexer()?;
                        eprint!("Publishing transaction via {} ... ", indexer.name());
                        indexer.broadcast(&tx)?;
                        eprintln!("success");
                    }
                }
            }
            Command::Extract {
                publish,
                psbt: psbt_path,
                tx,
            } => {
                let mut psbt = psbt_read(psbt_path)?;
                if !psbt.is_finalized() {
                    let wallet = self.bp_wallet::<O::Descr>(&config)?;
                    psbt_finalize(&mut psbt, wallet.descriptor())?;
                }

                if let Ok(tx) = psbt_extract(&psbt, *publish, tx.as_deref()) {
                    if *publish {
                        let indexer = self.indexer()?;
                        eprint!("Publishing transaction via {} ... ", indexer.name());
                        indexer.broadcast(&tx)?;
                        eprintln!("success");
                    }
                }
            }
        }

        Ok(())
    }
}

impl<O: DescriptorOpts> Exec for Args<BpCommand, O> {
    type Error = ExecError;
    const CONF_FILE_NAME: &'static str = "bp.toml";

    fn exec(mut self, config: Config, conf_filename: &'static str) -> Result<(), Self::Error> {
        match &self.command {
            BpCommand::General(cmd) => self.translate(cmd).exec(config, conf_filename)?,
            BpCommand::Balance {
                addr: false,
                utxo: false,
            } => {
                let runtime = self.bp_wallet::<O::Descr>(&config)?;
                println!("\nWallet total balance: {} ṩ", runtime.balance());
            }
            BpCommand::Balance {
                addr: true,
                utxo: false,
            } => {
                let wallet = self.bp_wallet::<O::Descr>(&config)?;
                println!("\nTerm.\t{:62}\t# used\tVol., ṩ\tBalance, ṩ", "Address");
                for info in wallet.address_balance() {
                    let WalletAddr {
                        addr,
                        terminal,
                        used,
                        volume,
                        balance,
                    } = info;
                    println!("{terminal}\t{:62}\t{used}\t{volume}\t{balance}", addr.to_string());
                }
                self.command = BpCommand::Balance {
                    addr: false,
                    utxo: false,
                };
                self.sync = false;
                self.exec(config, conf_filename)?;
            }
            BpCommand::Balance {
                addr: false,
                utxo: true,
            } => {
                let wallet = self.bp_wallet::<O::Descr>(&config)?;
                println!("Balance of {}", wallet.descriptor());
                println!("\nHeight\t{:>12}\t{:68}\tAddress", "Amount, ṩ", "Outpoint");
                for row in wallet.coins() {
                    println!(
                        "{}\t{: >12}\t{:68}\t{}",
                        row.height, row.amount, row.outpoint, row.address
                    );
                }
                self.command = BpCommand::Balance {
                    addr: false,
                    utxo: false,
                };
                self.sync = false;
                self.exec(config, conf_filename)?;
            }
            BpCommand::Balance {
                addr: true,
                utxo: true,
            } => {
                let wallet = self.bp_wallet::<O::Descr>(&config)?;
                println!("Balance of {}", wallet.descriptor());
                println!("\nHeight\t{:>12}\t{:68}", "Amount, ṩ", "Outpoint");
                for (derived_addr, utxos) in wallet.address_coins() {
                    println!("{}\t{}", derived_addr.addr, derived_addr.terminal);
                    for row in utxos {
                        println!("{}\t{: >12}\t{:68}", row.height, row.amount, row.outpoint);
                    }
                    println!()
                }
                self.command = BpCommand::Balance {
                    addr: false,
                    utxo: false,
                };
                self.sync = false;
                self.exec(config, conf_filename)?;
            }
            BpCommand::History { txid, details } => {
                let wallet = self.bp_wallet::<O::Descr>(&config)?;
                println!("History of {}", wallet.descriptor());
                println!(
                    "\nHeight\t{:<1$}\t    Amount, ṩ\tFee rate, ṩ/vbyte",
                    "Txid",
                    if *txid { 64 } else { 18 }
                );
                let mut rows = wallet.history().collect::<Vec<_>>();
                rows.sort_by_key(|row| row.height);
                for row in rows {
                    println!(
                        "{}\t{}\t{}{: >12}\t{: >8.2}",
                        row.height,
                        if *txid { row.txid.to_string() } else { format!("{:#}", row.txid) },
                        row.operation,
                        row.amount,
                        row.fee.sats() as f64 * 4.0 / row.weight as f64
                    );
                    if *details {
                        for (cp, value) in &row.own {
                            println!(
                                "\t* {value: >-12}ṩ\t{}\t{cp}",
                                if *value < 0 {
                                    "taken from"
                                } else if row.operation == OpType::Credit {
                                    "moved to  "
                                } else {
                                    "change    "
                                }
                            );
                        }
                        for (cp, value) in &row.counterparties {
                            println!(
                                "\t* {value: >-12}ṩ\t{}\t{cp}",
                                if *value > 0 {
                                    "received  "
                                } else if row.operation == OpType::Credit {
                                    "change?   "
                                } else {
                                    "paid to   "
                                }
                            );
                        }
                        println!("\t* {: >-12}ṩ\tminer fee", -row.fee.sats_i64());
                        println!();
                    }
                }
            }
            BpCommand::Tx { tx } => {
                println!(
                    "{}",
                    serde_yaml::to_string(&tx).expect("unable to generate YAML representation")
                );
            }
            BpCommand::Inspect { psbt } => {
                let psbt = psbt_read(psbt)?;
                println!(
                    "{}",
                    serde_yaml::to_string(&psbt).expect("unable to generate YAML representation")
                );
            }
            BpCommand::Construct {
                v2,
                to: beneficiaries,
                fee,
                psbt: psbt_file,
            } => {
                let mut wallet = self.bp_wallet::<O::Descr>(&config)?;

                // Do coin selection
                let total_amount =
                    beneficiaries.iter().try_fold(Sats::ZERO, |sats, b| match b.amount {
                        Payment::Max => Err(()),
                        Payment::Fixed(s) => sats.checked_add(s).ok_or(()),
                    });
                let coins: Vec<_> = match total_amount {
                    Ok(sats) if sats > Sats::ZERO => {
                        wallet.coinselect(sats + *fee, coinselect::all).collect()
                    }
                    _ => {
                        eprintln!(
                            "Warning: you are not paying to anybody but just aggregating all your \
                             balances to a single UTXO",
                        );
                        wallet.utxos().map(WalletUtxo::into_outpoint).collect()
                    }
                };

                // TODO: Support lock time and RBFs
                let params = TxParams::with(*fee);
                let (mut psbt, _) =
                    wallet.construct_psbt(coins, beneficiaries.iter().copied(), params)?;
                psbt.version = if *v2 { PsbtVer::V2 } else { PsbtVer::V0 };
                psbt_write_or_print(&psbt, psbt_file.as_deref())?;
            }
        };

        println!();

        Ok(())
    }
}

fn psbt_read(psbt_path: &Path) -> Result<Psbt, ExecError> {
    eprint!("Reading PSBT from file {} ... ", psbt_path.display());
    let mut psbt_file = File::open(psbt_path)?;
    let psbt = Psbt::decode(&mut psbt_file)?;
    eprintln!("success");
    Ok(psbt)
}

fn psbt_write(psbt: &Psbt, psbt_path: &Path) -> Result<(), ExecError> {
    eprint!("Saving PSBT to file {} ... ", psbt_path.display());
    let mut psbt_file = File::create(psbt_path)?;
    psbt.encode(psbt.version, &mut psbt_file)?;
    eprintln!("success");
    Ok(())
}

fn psbt_write_or_print(psbt: &Psbt, psbt_path: Option<&Path>) -> Result<(), ExecError> {
    match psbt_path {
        Some(file_name) => {
            psbt_write(psbt, file_name)?;
        }
        None => match psbt.version {
            PsbtVer::V0 => println!("{psbt}"),
            PsbtVer::V2 => println!("{psbt:#}"),
        },
    }
    Ok(())
}

fn psbt_finalize<D: Descriptor<K, V>, K, V>(
    psbt: &mut Psbt,
    descriptor: &D,
) -> Result<(), ExecError> {
    eprint!("Finalizing PSBT ... ");
    let inputs = psbt.finalize(descriptor);
    eprint!(
        "{} of {} inputs were finalized",
        inputs.to_string().bright_green(),
        psbt.inputs().count()
    );
    if psbt.is_finalized() {
        eprintln!(", transaction is ready for the extraction");
    } else {
        eprintln!(" and some non-finalized inputs remains");
    }
    Ok(())
}

fn psbt_extract(psbt: &Psbt, publish: bool, tx: Option<&Path>) -> Result<Tx, ExecError> {
    eprint!("Extracting signed transaction ... ");
    match psbt.extract() {
        Ok(extracted) => {
            eprintln!("success");
            if !publish && tx.is_none() {
                println!("{extracted}");
            }
            if let Some(file) = tx {
                eprint!("Saving transaction to file {} ...", file.display());
                let mut file = File::create(file)?;
                extracted.consensus_encode(&mut file)?;
                eprintln!("success");
            }
            Ok(extracted)
        }
        Err(e) if publish || tx.is_some() => {
            eprintln!(
                "PSBT still contains {} non-finalized inputs, failing to extract transaction",
                e.0.to_string().bright_red()
            );
            Err(e.into())
        }
        Err(e) => {
            eprintln!("{} more inputs still have to be finalized", e.0);
            Err(e.into())
        }
    }
}