bdk-cli 4.0.0

An experimental CLI wallet application and playground, powered by BDK
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
use crate::commands::OfflineWalletSubCommand;
use crate::error::BDKCliError as Error;
use crate::handlers::{AppCommand, AppContext, OfflineOperations};
use crate::utils::output::{FormatOutput, ListResult};
use crate::utils::parse_address;
use crate::utils::types::{
    AddressResult, BalanceResult, KeychainPair, PsbtResult, RawPsbt, TransactionDetails,
    UnspentDetails,
};
use crate::utils::{parse_outpoint, parse_recipient};
use bdk_wallet::bitcoin::base64::Engine;
use bdk_wallet::bitcoin::base64::prelude::BASE64_STANDARD;
use bdk_wallet::bitcoin::script::PushBytesBuf;
use bdk_wallet::bitcoin::{Address, Amount, FeeRate, OutPoint, Psbt, ScriptBuf, Sequence, Txid};
use bdk_wallet::{KeychainKind, SignOptions};
use clap::Parser;
use serde_json::json;
use std::collections::BTreeMap;
#[cfg(feature = "silent-payments")]
use {
    crate::utils::common::parse_sp_code_value_pairs,
    bdk_sp::{
        bitcoin::{PrivateKey, PublicKey},
        encoding::SilentPaymentCode,
        send::psbt::derive_sp,
    },
    bdk_wallet::bitcoin::key::Secp256k1,
    bdk_wallet::keys::{DescriptorPublicKey, DescriptorSecretKey, SinglePubKey},
    std::collections::HashMap,
};
#[cfg(feature = "message_signer")]
use {
    crate::utils::parse_signature_format,
    crate::utils::types::MessageResult,
    bdk_message_signer::{MessageProof, MessageSigner},
};

impl OfflineWalletSubCommand {
    pub fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<(), Error> {
        match self {
            Self::NewAddress(new_address) => new_address.execute(ctx)?.write_out(std::io::stdout()),
            Self::Balance(balance) => balance.execute(ctx)?.write_out(std::io::stdout()),
            Self::UnusedAddress(unused_address_command) => unused_address_command
                .execute(ctx)?
                .write_out(std::io::stdout()),
            Self::Unspent(unspent_command) => {
                unspent_command.execute(ctx)?.write_out(std::io::stdout())
            }
            Self::Transactions(transactions_command) => transactions_command
                .execute(ctx)?
                .write_out(std::io::stdout()),
            Self::CreateTx(createtx_command) => {
                createtx_command.execute(ctx)?.write_out(std::io::stdout())
            }
            #[cfg(feature = "silent-payments")]
            Self::CreateSpTx(cmd) => cmd.execute(ctx)?.write_out(std::io::stdout()),
            Self::BumpFee(bumpfee_command) => {
                bumpfee_command.execute(ctx)?.write_out(std::io::stdout())
            }
            Self::Policies(policies_command) => {
                policies_command.execute(ctx)?.write_out(std::io::stdout())
            }
            Self::PublicDescriptor(public_descriptor_command) => public_descriptor_command
                .execute(ctx)?
                .write_out(std::io::stdout()),
            Self::Sign(sign_command) => sign_command.execute(ctx)?.write_out(std::io::stdout()),
            Self::ExtractPsbt(extract_psbt_command) => extract_psbt_command
                .execute(ctx)?
                .write_out(std::io::stdout()),
            Self::FinalizePsbt(finalize_psbt_command) => finalize_psbt_command
                .execute(ctx)?
                .write_out(std::io::stdout()),
            Self::CombinePsbt(combine_psbt_command) => combine_psbt_command
                .execute(ctx)?
                .write_out(std::io::stdout()),
            #[cfg(feature = "message_signer")]
            Self::SignMessage(sign_message_command) => sign_message_command
                .execute(ctx)?
                .write_out(std::io::stdout()),
            #[cfg(feature = "message_signer")]
            Self::VerifyMessage(verify_message_command) => verify_message_command
                .execute(ctx)?
                .write_out(std::io::stdout()),
            Self::LockUtxo(lock_utxo) => lock_utxo.execute(ctx)?.write_out(std::io::stdout()),
            Self::UnlockUtxo(unlock_utxo) => unlock_utxo.execute(ctx)?.write_out(std::io::stdout()),
            Self::LockedUtxos(locked_utxos) => {
                locked_utxos.execute(ctx)?.write_out(std::io::stdout())
            }
            #[cfg(feature = "dns_payment")]
            Self::CreateDnsTx(_) => Err(Error::Generic(
                "CreateDnsTx is dispatched asynchronously through main".to_string(),
            )),
        }
    }
}

#[derive(Parser, Debug, Clone, PartialEq)]
pub struct NewAddressCommand;

impl AppCommand<AppContext<OfflineOperations<'_>>> for NewAddressCommand {
    type Output = AddressResult;

    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let wallet = &mut ctx.state.wallet;
        let address_info = wallet.reveal_next_address(KeychainKind::External);
        Ok(AddressResult::from(address_info))
    }
}

#[derive(Parser, Debug, PartialEq, Clone)]
pub struct UnusedAddressCommand;

impl AppCommand<AppContext<OfflineOperations<'_>>> for UnusedAddressCommand {
    type Output = AddressResult;

    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let wallet = &mut ctx.state.wallet;
        let address_info = wallet.next_unused_address(KeychainKind::External);
        Ok(AddressResult::from(address_info))
    }
}

#[derive(Parser, Debug, PartialEq, Clone)]
pub struct UnspentCommand;

impl AppCommand<AppContext<OfflineOperations<'_>>> for UnspentCommand {
    type Output = ListResult<UnspentDetails>;

    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let wallet = &mut ctx.state.wallet;
        let outputs: Vec<_> = wallet.list_unspent().collect();
        let utxos = outputs
            .into_iter()
            .map(|utxo| {
                let is_locked = wallet.is_outpoint_locked(utxo.outpoint);
                UnspentDetails::from_local_output(&utxo, ctx.network, is_locked)
            })
            .collect();

        Ok(ListResult::new(utxos))
    }
}

#[derive(Parser, Debug, PartialEq, Clone)]
pub struct TransactionsCommand;

impl AppCommand<AppContext<OfflineOperations<'_>>> for TransactionsCommand {
    type Output = ListResult<TransactionDetails>;

    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let transactions = &mut ctx.state.wallet.transactions();

        let txns: Vec<TransactionDetails> = transactions
            .map(|tx| {
                let total_value = tx
                    .tx_node
                    .output
                    .iter()
                    .map(|output| output.value.to_sat())
                    .sum::<u64>();

                TransactionDetails {
                    txid: tx.tx_node.txid.to_string(),
                    is_coinbase: tx.tx_node.is_coinbase(),
                    wtxid: tx.tx_node.compute_wtxid().to_string(),
                    version: serde_json::to_value(tx.tx_node.version).unwrap_or(json!(1)),
                    version_display: tx.tx_node.version.to_string(),
                    is_rbf: tx.tx_node.is_explicitly_rbf(),
                    inputs: serde_json::to_value(&tx.tx_node.input).unwrap_or_default(),
                    outputs: serde_json::to_value(&tx.tx_node.output).unwrap_or_default(),
                    input_count: tx.tx_node.input.len(),
                    output_count: tx.tx_node.output.len(),
                    total_value,
                }
            })
            .collect();

        Ok(ListResult::new(txns))
    }
}

#[derive(Parser, Debug, PartialEq, Clone)]
pub struct BalanceCommand;

impl AppCommand<AppContext<OfflineOperations<'_>>> for BalanceCommand {
    type Output = BalanceResult;

    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let balance = ctx.state.wallet.balance();
        Ok(BalanceResult::from(balance))
    }
}
#[derive(Debug, Parser, Clone, PartialEq)]
pub struct CreateTxCommand {
    /// Adds a recipient to the transaction.
    #[arg(env = "ADDRESS:SAT", long = "to", required = true, value_parser = parse_recipient)]
    pub recipients: Vec<(ScriptBuf, u64)>,

    /// Sends all the funds (or all the selected utxos). Requires only one recipient with value 0.
    #[arg(long = "send_all", short = 'a')]
    pub send_all: bool,

    /// Enables Replace-By-Fee (BIP125).
    #[arg(long = "enable_rbf", short = 'r', default_value_t = true)]
    pub enable_rbf: bool,

    /// Make a PSBT that can be signed by offline signers and hardware wallets. Forces the addition of `PSBT_GLOBAL_XPUB` and more details to let the signer identify the change output.
    #[arg(long = "offline_signer")]
    pub offline_signer: bool,

    /// Selects which utxos *must* be spent.
    #[arg(env = "MUST_SPEND_TXID:VOUT", long = "utxos", value_parser = parse_outpoint)]
    pub utxos: Option<Vec<OutPoint>>,

    /// Marks a utxo as unspendable.
    #[arg(env = "CANT_SPEND_TXID:VOUT", long = "unspendable", value_parser = parse_outpoint)]
    pub unspendable: Option<Vec<OutPoint>>,

    /// Fee rate to use in sat/vbyte.
    #[arg(env = "SATS_VBYTE", short = 'f', long = "fee_rate")]
    pub fee_rate: Option<f32>,

    /// Selects which policy should be used to satisfy the external descriptor.
    #[arg(env = "EXT_POLICY", long = "external_policy")]
    pub external_policy: Option<String>,

    /// Selects which policy should be used to satisfy the internal descriptor.
    #[arg(env = "INT_POLICY", long = "internal_policy")]
    pub internal_policy: Option<String>,

    /// Optionally create an OP_RETURN output containing given String in utf8 encoding (max 80 bytes)
    #[arg(
        env = "ADD_STRING",
        long = "add_string",
        short = 's',
        conflicts_with = "add_data"
    )]
    pub add_string: Option<String>,

    /// Optionally create an OP_RETURN output containing given base64 encoded String. (max 80 bytes)
    #[arg(
        env = "ADD_DATA",
        long = "add_data",
        short = 'o',
        conflicts_with = "add_string"
    )]
    pub add_data: Option<String>,
}

impl AppCommand<AppContext<OfflineOperations<'_>>> for CreateTxCommand {
    type Output = PsbtResult;

    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let mut tx_builder = ctx.state.wallet.build_tx();

        if self.send_all {
            if self.recipients.len() == 1 {
                tx_builder
                    .drain_wallet()
                    .drain_to(self.recipients[0].0.clone());
            } else {
                return Err(Error::Generic(
                    "Wallet can only be drained to a single output".to_string(),
                ));
            }
        } else {
            let recipients = self
                .recipients
                .clone()
                .into_iter()
                .map(|(script, amount)| (script, Amount::from_sat(amount)))
                .collect();
            tx_builder.set_recipients(recipients);
        }

        if !self.enable_rbf {
            tx_builder.set_exact_sequence(Sequence::MAX);
        }

        if self.offline_signer {
            tx_builder.add_global_xpubs();
        }

        if let Some(fee_rate) = self.fee_rate
            && let Some(fee_rate) = FeeRate::from_sat_per_vb(fee_rate as u64)
        {
            tx_builder.fee_rate(fee_rate);
        }

        if let Some(utxos) = &self.utxos {
            tx_builder.add_utxos(&utxos[..]).unwrap();
        }

        if let Some(unspendable) = &self.unspendable {
            tx_builder.unspendable(unspendable.to_vec());
        }

        if let Some(base64_data) = &self.add_data {
            let op_return_data = BASE64_STANDARD.decode(base64_data).unwrap();
            tx_builder.add_data(&PushBytesBuf::try_from(op_return_data).unwrap());
        } else if let Some(string_data) = &self.add_string {
            let data = PushBytesBuf::try_from(string_data.as_bytes().to_vec()).unwrap();
            tx_builder.add_data(&data);
        }

        let policies = vec![
            self.external_policy
                .clone()
                .map(|p| (p, KeychainKind::External)),
            self.internal_policy
                .clone()
                .map(|p| (p, KeychainKind::Internal)),
        ];

        for (policy, keychain) in policies.into_iter().flatten() {
            let policy = serde_json::from_str::<BTreeMap<String, Vec<usize>>>(&policy)?;
            tx_builder.policy_path(policy, keychain);
        }

        let psbt = tx_builder.finish()?;

        // let psbt_base64 = BASE64_STANDARD.encode(psbt.serialize());

        Ok(PsbtResult::new(&psbt, Some(false)))
    }
}

#[cfg(feature = "silent-payments")]
#[derive(Debug, Parser, Clone, PartialEq)]
pub struct CreateSpTxCommand {
    /// Adds a recipient to the transaction.
    // Clap Doesn't support complex vector parsing https://github.com/clap-rs/clap/issues/1704.
    // Address and amount parsing is done at run time in handler function.
    #[arg(env = "ADDRESS:SAT", long = "to", required = false, value_parser = parse_recipient)]
    pub recipients: Option<Vec<(ScriptBuf, u64)>>,
    /// Parse silent payment recipients
    #[arg(long = "to-sp", required = true, value_parser = parse_sp_code_value_pairs)]
    pub silent_payment_recipients: Vec<(SilentPaymentCode, u64)>,
    /// Sends all the funds (or all the selected utxos). Requires only one recipient with value 0.
    #[arg(long = "send_all", short = 'a')]
    pub send_all: bool,
    /// Make a PSBT that can be signed by offline signers and hardware wallets. Forces the addition of `non_witness_utxo` and more details to let the signer identify the change output.
    #[arg(long = "offline_signer")]
    pub offline_signer: bool,
    /// Selects which utxos *must* be spent.
    #[arg(env = "MUST_SPEND_TXID:VOUT", long = "utxos", value_parser = parse_outpoint)]
    pub utxos: Option<Vec<OutPoint>>,
    /// Marks a utxo as unspendable.
    #[arg(env = "CANT_SPEND_TXID:VOUT", long = "unspendable", value_parser = parse_outpoint)]
    pub unspendable: Option<Vec<OutPoint>>,
    /// Fee rate to use in sat/vbyte.
    #[arg(env = "SATS_VBYTE", short = 'f', long = "fee_rate")]
    pub fee_rate: Option<f32>,
    /// Selects which policy should be used to satisfy the external descriptor.
    #[arg(env = "EXT_POLICY", long = "external_policy")]
    pub external_policy: Option<String>,
    /// Selects which policy should be used to satisfy the internal descriptor.
    #[arg(env = "INT_POLICY", long = "internal_policy")]
    pub internal_policy: Option<String>,
    /// Optionally create an OP_RETURN output containing given String in utf8 encoding (max 80 bytes)
    #[arg(
        env = "ADD_STRING",
        long = "add_string",
        short = 's',
        conflicts_with = "add_data"
    )]
    pub add_string: Option<String>,
    /// Optionally create an OP_RETURN output containing given base64 encoded String. (max 80 bytes)
    #[arg(
        env = "ADD_DATA",
        long = "add_data",
        short = 'o',
        conflicts_with = "add_string"
    )]
    pub add_data: Option<String>, //base 64 econding
}

#[cfg(feature = "silent-payments")]
impl AppCommand<AppContext<OfflineOperations<'_>>> for CreateSpTxCommand {
    type Output = RawPsbt;

    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let mut tx_builder = ctx.state.wallet.build_tx();

        let sp_recipients: Vec<SilentPaymentCode> = self
            .silent_payment_recipients
            .iter()
            .map(|(sp_code, _)| sp_code.clone())
            .collect();

        if self.send_all {
            if sp_recipients.len() == 1 && self.recipients.is_none() {
                tx_builder
                    .drain_wallet()
                    .drain_to(sp_recipients[0].get_placeholder_p2tr_spk());
            } else if let Some(ref recipients) = self.recipients
                && sp_recipients.is_empty()
            {
                if recipients.len() == 1 {
                    tx_builder.drain_wallet().drain_to(recipients[0].0.clone());
                } else {
                    return Err(Error::Generic(
                        "Wallet can only be drain to a single output".to_string(),
                    ));
                }
            } else {
                return Err(Error::Generic(
                    "Wallet can only be drain to a single output".to_string(),
                ));
            }
        } else {
            let mut outputs: Vec<(ScriptBuf, Amount)> = self
                .silent_payment_recipients
                .iter()
                .map(|(sp_code, amount)| {
                    let script = sp_code.get_placeholder_p2tr_spk();
                    (script, Amount::from_sat(*amount))
                })
                .collect();

            if let Some(recipients) = &self.recipients {
                let recipients = recipients
                    .iter()
                    .map(|(script, amount)| (script.clone(), Amount::from_sat(*amount)));

                outputs.extend(recipients);
            }

            tx_builder.set_recipients(outputs);
        }

        // Do not enable RBF for this transaction
        tx_builder.set_exact_sequence(Sequence::MAX);

        if self.offline_signer {
            tx_builder.add_global_xpubs();
        }

        if let Some(fee_rate) = self.fee_rate
            && let Some(fee_rate) = FeeRate::from_sat_per_vb(fee_rate as u64)
        {
            tx_builder.fee_rate(fee_rate);
        }

        if let Some(utxos) = &self.utxos {
            tx_builder
                .add_utxos(&utxos[..])
                .map_err(|_| bdk_wallet::error::CreateTxError::UnknownUtxo)?;
        }

        if let Some(unspendable) = &self.unspendable {
            tx_builder.unspendable(unspendable.to_vec());
        }

        if let Some(base64_data) = &self.add_data {
            let op_return_data = BASE64_STANDARD
                .decode(base64_data)
                .map_err(|e| Error::Generic(e.to_string()))?;
            tx_builder.add_data(
                &PushBytesBuf::try_from(op_return_data)
                    .map_err(|e| Error::Generic(e.to_string()))?,
            );
        } else if let Some(string_data) = &self.add_string {
            let data = PushBytesBuf::try_from(string_data.as_bytes().to_vec())
                .map_err(|e| Error::Generic(e.to_string()))?;
            tx_builder.add_data(&data);
        }

        let policies = vec![
            self.external_policy
                .as_ref()
                .map(|p| (p, KeychainKind::External)),
            self.internal_policy
                .as_ref()
                .map(|p| (p, KeychainKind::Internal)),
        ];

        for (policy, keychain) in policies.into_iter().flatten() {
            let policy = serde_json::from_str::<BTreeMap<String, Vec<usize>>>(policy)?;
            tx_builder.policy_path(policy, keychain);
        }

        let mut psbt = tx_builder.finish()?;

        let unsigned_psbt = psbt.clone();

        let finalized = ctx.state.wallet.sign(&mut psbt, SignOptions::default())?;

        if !finalized {
            return Err(Error::Generic(
                "Cannot produce silent payment outputs without intermediate signing phase."
                    .to_string(),
            ));
        }

        for (full_input, psbt_input) in unsigned_psbt.inputs.iter().zip(psbt.inputs.iter_mut()) {
            // repopulate key derivation data
            psbt_input.bip32_derivation = full_input.bip32_derivation.clone();
            psbt_input.tap_key_origins = full_input.tap_key_origins.clone();
        }

        let secp = Secp256k1::new();
        let mut external_signers = ctx
            .state
            .wallet
            .get_signers(KeychainKind::External)
            .as_key_map(&secp);
        let internal_signers = ctx
            .state
            .wallet
            .get_signers(KeychainKind::Internal)
            .as_key_map(&secp);
        external_signers.extend(internal_signers);

        match external_signers.iter().next().expect("not empty") {
            (DescriptorPublicKey::Single(single_pub), DescriptorSecretKey::Single(prv)) => {
                match single_pub.key {
                    SinglePubKey::FullKey(pk) => {
                        let keys: HashMap<PublicKey, PrivateKey> = [(pk, prv.key)].into();
                        derive_sp(&mut psbt, &keys, &sp_recipients, &secp).expect("will fix later");
                    }
                    SinglePubKey::XOnly(xonly) => {
                        let keys: HashMap<bdk_sp::bitcoin::XOnlyPublicKey, PrivateKey> =
                            [(xonly, prv.key)].into();
                        derive_sp(&mut psbt, &keys, &sp_recipients, &secp).expect("will fix later");
                    }
                };
            }
            (_, DescriptorSecretKey::XPrv(k)) => {
                derive_sp(&mut psbt, &k.xkey, &sp_recipients, &secp).expect("will fix later");
            }
            _ => unimplemented!("multi xkey signer"),
        };

        // Unfinalize PSBT to resign
        for psbt_input in psbt.inputs.iter_mut() {
            psbt_input.final_script_sig = None;
            psbt_input.final_script_witness = None;
        }

        let _resigned = ctx.state.wallet.sign(&mut psbt, SignOptions::default())?;

        let raw_tx = psbt.extract_tx()?;

        Ok(RawPsbt::new(&raw_tx))
    }
}

#[derive(Debug, Parser, Clone, PartialEq)]
pub struct BumpFeeCommand {
    /// TXID of the transaction to update.
    #[arg(env = "TXID", long = "txid")]
    pub txid: Txid,

    /// Allows the wallet to reduce the amount to the specified address in order to increase fees.
    #[arg(env = "SHRINK_ADDRESS", long = "shrink", value_parser = parse_address)]
    pub shrink_address: Option<Address>,

    /// Make a PSBT that can be signed by offline signers and hardware wallets. Forces the addition of `non_witness_utxo` and more details to let the signer identify the change output.
    #[arg(long = "offline_signer")]
    pub offline_signer: bool,

    /// Selects which utxos *must* be added to the tx. Unconfirmed utxos cannot be used.
    #[arg(env = "MUST_SPEND_TXID:VOUT", long = "utxos", value_parser = parse_outpoint)]
    pub utxos: Option<Vec<OutPoint>>,

    /// Marks an utxo as unspendable, in case more inputs are needed to cover the extra fees.
    #[arg(env = "CANT_SPEND_TXID:VOUT", long = "unspendable", value_parser = parse_outpoint)]
    pub unspendable: Option<Vec<OutPoint>>,

    /// The new targeted fee rate in sat/vbyte.
    #[arg(
        env = "SATS_VBYTE",
        short = 'f',
        long = "fee_rate",
        default_value = "1.0"
    )]
    pub fee_rate: f32,
}

impl AppCommand<AppContext<OfflineOperations<'_>>> for BumpFeeCommand {
    type Output = PsbtResult;

    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let wallet = &mut ctx.state.wallet;

        let mut tx_builder = wallet.build_fee_bump(self.txid)?;
        let fee_rate =
            FeeRate::from_sat_per_vb(self.fee_rate as u64).unwrap_or(FeeRate::BROADCAST_MIN);
        tx_builder.fee_rate(fee_rate);

        if let Some(address) = &self.shrink_address {
            let script_pubkey = address.script_pubkey();
            tx_builder.drain_to(script_pubkey);
        }

        if self.offline_signer {
            tx_builder.add_global_xpubs();
        }

        if let Some(utxos) = &self.utxos {
            tx_builder.add_utxos(&utxos[..]).unwrap();
        }

        if let Some(unspendable) = &self.unspendable {
            tx_builder.unspendable(unspendable.to_vec());
        }

        let psbt = tx_builder.finish()?;

        // let psbt_base64 = BASE64_STANDARD.encode(psbt.serialize());

        Ok(PsbtResult::new(&psbt, Some(false)))
    }
}

#[derive(Parser, Debug, PartialEq, Clone)]
pub struct PoliciesCommand;

impl AppCommand<AppContext<OfflineOperations<'_>>> for PoliciesCommand {
    type Output = KeychainPair<serde_json::Value>;

    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let wallet = &mut ctx.state.wallet;
        let external_policy = wallet.policies(KeychainKind::External)?;
        let internal_policy = wallet.policies(KeychainKind::Internal)?;

        Ok(KeychainPair {
            external: serde_json::to_value(&external_policy).unwrap_or(serde_json::json!(null)),
            internal: serde_json::to_value(&internal_policy).unwrap_or(serde_json::json!(null)),
        })
    }
}

#[derive(Parser, Debug, PartialEq, Clone)]
pub struct PublicDescriptorCommand;

impl AppCommand<AppContext<OfflineOperations<'_>>> for PublicDescriptorCommand {
    type Output = KeychainPair<String>;

    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let wallet = &mut ctx.state.wallet;
        Ok(KeychainPair {
            external: wallet.public_descriptor(KeychainKind::External).to_string(),
            internal: wallet.public_descriptor(KeychainKind::Internal).to_string(),
        })
    }
}

#[derive(Debug, Parser, Clone, PartialEq)]
pub struct SignCommand {
    /// Sets the PSBT to sign.
    #[arg(env = "BASE64_PSBT")]
    pub psbt: String,

    /// Assume the blockchain has reached a specific height. This affects the transaction finalization, if there are timelocks in the descriptor.
    #[arg(env = "HEIGHT", long = "assume_height")]
    pub assume_height: Option<u32>,

    /// Whether the signer should trust the witness_utxo, if the non_witness_utxo hasn’t been provided.
    #[arg(env = "WITNESS", long = "trust_witness_utxo")]
    pub trust_witness_utxo: Option<bool>,
}

impl AppCommand<AppContext<OfflineOperations<'_>>> for SignCommand {
    type Output = PsbtResult;

    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let wallet = &mut ctx.state.wallet;
        let psbt_bytes = BASE64_STANDARD
            .decode(&self.psbt)
            .map_err(|e| Error::Generic(e.to_string()))?;
        let mut psbt = Psbt::deserialize(&psbt_bytes).map_err(|e| Error::Generic(e.to_string()))?;

        let signopt = SignOptions {
            assume_height: self.assume_height,
            trust_witness_utxo: self.trust_witness_utxo.unwrap_or(false),
            ..Default::default()
        };
        let finalized = wallet.sign(&mut psbt, signopt)?;
        Ok(PsbtResult::new(&psbt, Some(finalized)))
    }
}

#[derive(Debug, Parser, Clone, PartialEq)]
pub struct ExtractPsbtCommand {
    /// Sets the PSBT to extract
    #[arg(env = "BASE64_PSBT")]
    pub psbt: String,
}

impl AppCommand<AppContext<OfflineOperations<'_>>> for ExtractPsbtCommand {
    type Output = RawPsbt;

    fn execute(&self, _ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let psbt_serialized = BASE64_STANDARD.decode(self.psbt.clone())?;
        let psbt = Psbt::deserialize(&psbt_serialized)?;
        let raw_tx = psbt.extract_tx()?;

        Ok(RawPsbt::new(&raw_tx))
    }
}

#[derive(Debug, Parser, Clone, PartialEq)]
pub struct FinalizePsbtCommand {
    /// Sets the PSBT to finalize.
    #[arg(env = "BASE64_PSBT")]
    pub psbt: String,

    /// Assume the blockchain has reached a specific height.
    #[arg(env = "HEIGHT", long = "assume_height")]
    pub assume_height: Option<u32>,

    /// Whether the signer should trust the witness_utxo, if the non_witness_utxo hasn’t been provided.
    #[arg(env = "WITNESS", long = "trust_witness_utxo")]
    pub trust_witness_utxo: Option<bool>,
}

impl AppCommand<AppContext<OfflineOperations<'_>>> for FinalizePsbtCommand {
    type Output = PsbtResult;

    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let wallet = &mut ctx.state.wallet;
        let psbt_bytes = BASE64_STANDARD
            .decode(&self.psbt)
            .map_err(|e| Error::Generic(e.to_string()))?;
        let mut psbt = Psbt::deserialize(&psbt_bytes).map_err(|e| Error::Generic(e.to_string()))?;

        let signopt = SignOptions {
            assume_height: self.assume_height,
            trust_witness_utxo: self.trust_witness_utxo.unwrap_or(false),
            ..Default::default()
        };

        let finalized = wallet.finalize_psbt(&mut psbt, signopt)?;

        Ok(PsbtResult::new(&psbt, Some(finalized)))
    }
}

#[derive(Debug, Parser, Clone, PartialEq)]
pub struct CombinePsbtCommand {
    /// Add one PSBT to combine. This option can be repeated multiple times, one for each PSBT.
    #[arg(env = "BASE64_PSBT", required = true)]
    pub psbt: Vec<String>,
}

impl AppCommand<AppContext<OfflineOperations<'_>>> for CombinePsbtCommand {
    type Output = PsbtResult;

    fn execute(&self, _ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let mut psbts = self
            .psbt
            .iter()
            .map(|s| {
                let psbt = BASE64_STANDARD.decode(s)?;
                Ok(Psbt::deserialize(&psbt)?)
            })
            .collect::<Result<Vec<_>, Error>>()?;

        let init_psbt = psbts
            .pop()
            .ok_or_else(|| Error::Generic("Invalid PSBT input".to_string()))?;
        let final_psbt =
            psbts
                .into_iter()
                .try_fold::<_, _, Result<Psbt, Error>>(init_psbt, |mut acc, x| {
                    let _ = acc.combine(x);
                    Ok(acc)
                })?;

        Ok(PsbtResult::new(&final_psbt, Some(false)))
    }
}

#[cfg(feature = "message_signer")]
#[derive(Debug, Parser, Clone, PartialEq)]
pub struct SignMessageCommand {
    /// The message to sign
    #[arg(long)]
    pub message: String,

    /// The signature format (e.g., Legacy, Simple, Full)
    #[arg(long, default_value = "simple")]
    pub signature_type: String,

    /// Address to sign
    #[arg(long)]
    pub address: String,

    /// Optional list of specific UTXOs for proof-of-funds (only for `FullWithProofOfFunds`)
    #[arg(long)]
    pub utxos: Option<Vec<OutPoint>>,
}

#[cfg(feature = "message_signer")]
impl AppCommand<AppContext<OfflineOperations<'_>>> for SignMessageCommand {
    type Output = MessageResult;

    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let wallet = &mut ctx.state.wallet;
        let address: Address = parse_address(&self.address)?;
        let signature_format = parse_signature_format(&self.signature_type)?;

        if !wallet.is_mine(address.script_pubkey()) {
            return Err(Error::Generic(format!(
                "Address {} does not belong to this wallet.",
                address
            )));
        }

        let proof = wallet.sign_message(
            self.message.as_str(),
            signature_format,
            &address,
            self.utxos.clone(),
        )?;

        Ok(MessageResult {
            proof: Some(proof.to_base64()),
            ..Default::default()
        })
    }
}

#[cfg(feature = "message_signer")]
#[derive(Debug, Parser, Clone, PartialEq)]
pub struct VerifyMessageCommand {
    /// The signature proof to verify
    #[arg(long)]
    pub proof: String,

    /// The message that was signed
    #[arg(long)]
    pub message: String,

    /// The address associated with the signature
    #[arg(long)]
    pub address: String,
}

#[cfg(feature = "message_signer")]
impl AppCommand<AppContext<OfflineOperations<'_>>> for VerifyMessageCommand {
    type Output = MessageResult;

    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let wallet = &ctx.state.wallet;

        let address: Address = parse_address(&self.address)?;

        let parsed_proof = MessageProof::from_base64(&self.proof)
            .map_err(|e| Error::Generic(format!("Invalid proof format: {e}")))?;

        let is_valid = wallet.verify_message(&parsed_proof, &self.message, &address)?;

        Ok(MessageResult {
            valid: Some(is_valid.valid),
            proven_amount: is_valid.proven_amount.map(|a| a.to_sat()),
            ..Default::default()
        })
    }
}

#[derive(Parser, Debug, Clone, PartialEq)]
pub struct LockUtxoCommand {
    /// Outpoint(s) to lock, format TXID:VOUT.
    #[arg(env = "TXID", long = "utxo", required = true, value_parser = parse_outpoint)]
    pub utxos: Vec<OutPoint>,
}

impl AppCommand<AppContext<OfflineOperations<'_>>> for LockUtxoCommand {
    type Output = ListResult<String>;
    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let wallet = &mut ctx.state.wallet;
        for out_point in &self.utxos {
            if wallet.get_utxo(*out_point).is_none() {
                eprintln!("warning: {out_point} is not a known wallet UTXO; skipping");
                continue;
            }
            wallet.lock_outpoint(*out_point);
        }
        let locked = wallet
            .list_locked_outpoints()
            .map(|o| o.to_string())
            .collect();
        Ok(ListResult::new(locked))
    }
}

#[derive(Parser, Debug, Clone, PartialEq)]
pub struct UnlockUtxoCommand {
    /// Outpoint(s) to unlock, format TXID:VOUT.
    #[arg(env = "TXID", long = "utxo", required = true, value_parser = parse_outpoint)]
    pub utxos: Vec<OutPoint>,
}

impl AppCommand<AppContext<OfflineOperations<'_>>> for UnlockUtxoCommand {
    type Output = ListResult<String>;
    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let wallet = &mut ctx.state.wallet;
        for out_point in &self.utxos {
            wallet.unlock_outpoint(*out_point);
        }
        let locked = wallet
            .list_locked_outpoints()
            .map(|o| o.to_string())
            .collect();
        Ok(ListResult::new(locked))
    }
}

#[derive(Parser, Debug, Clone, PartialEq)]
pub struct LockedUtxosCommand {}

impl AppCommand<AppContext<OfflineOperations<'_>>> for LockedUtxosCommand {
    type Output = ListResult<String>;
    fn execute(&self, ctx: &mut AppContext<OfflineOperations<'_>>) -> Result<Self::Output, Error> {
        let wallet = &mut ctx.state.wallet;
        let locked = wallet
            .list_locked_outpoints()
            .map(|o| o.to_string())
            .collect();
        Ok(ListResult::new(locked))
    }
}