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
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
#[cfg(feature = "electrum")]
mod test_online {
    use crate::common::BdkCli;
    use assert_cmd::Command;
    use bdk_testenv::{TestEnv, bitcoincore_rpc::RpcApi};
    use bdk_wallet::bitcoin::{Address, Amount, Network, Txid};
    use serde_json::Value;
    use std::str::FromStr;
    use std::time::Duration;
    use tempfile::TempDir;

    static WALLET_NAME: &str = "test_online_wallet";
    static RECIPIENT: &str = "tb1p4tp4l6glyr2gs94neqcpr5gha7344nfyznfkc8szkreflscsdkgqsdent4";

    /// Spins up the envt, a Regtest node + electrs, and saves the wallet config.
    fn setup_online_wallet() -> (BdkCli, Command, TestEnv) {
        let env = TestEnv::new().expect("Failed to start bdk_testenv");
        let server_url = env.electrsd.electrum_url.as_str();

        let temp_dir = TempDir::new().unwrap();
        let cli = BdkCli::new("regtest", Some(temp_dir.path().to_path_buf()));

        let desc = cli
            .cmd("descriptor", &["--type", "tr"])
            .output()
            .expect("Command to generate descriptors failed");
        let desc_values: Value =
            serde_json::from_slice(&desc.stdout).expect("Invalid JSON from output descriptor");
        let priv_desc = &desc_values["private_descriptors"];
        let ext_desc = priv_desc["external"].as_str().unwrap();
        let int_desc = priv_desc["internal"].as_str().unwrap();

        let mut cmd_init = cli.build_base_cmd();
        cmd_init
            .arg("wallet")
            .arg("--wallet")
            .arg(WALLET_NAME)
            .arg("config")
            .arg("--ext-descriptor")
            .arg(ext_desc)
            .arg("--int-descriptor")
            .arg(int_desc)
            .arg("--client-type")
            .arg("electrum")
            .arg("--database-type")
            .arg("sqlite")
            .arg("--url")
            .arg(server_url);

        (cli, cmd_init, env)
    }

    /// Mines blocks, funds wallet with 0.5 BTC, confirms it, and
    /// runs `full_scan` so the wallet's persisted state reflects the funding.
    /// Asserts the resulting confirmed balance is exactly 50,000,000 sats.
    fn fund_and_sync_wallet(cli: &BdkCli, env: &TestEnv) {
        let address = cli_new_address(cli);

        let node_address = env
            .rpc_client()
            .get_new_address(None, None)
            .unwrap()
            .assume_checked();

        let mined_blocks = env
            .mine_blocks(101, Some(node_address))
            .expect("Failed to mine initial blocks");
        assert_eq!(mined_blocks.len(), 101, "expected exactly 101 blocks mined");
        env.wait_until_electrum_sees_block(Duration::from_secs(10))
            .expect("Electrum did not catch up to initial blocks");

        let txid = env
            .send(&address, Amount::from_btc(0.5).unwrap())
            .expect("Failed to fund wallet address");
        env.wait_until_electrum_sees_txid(txid, Duration::from_secs(10))
            .expect("Electrum did not see funding tx");

        env.mine_blocks(3, None)
            .expect("Failed to confirm funding tx");
        env.wait_until_electrum_sees_block(Duration::from_secs(10))
            .expect("Electrum did not catch up to confirmation blocks");

        cli_full_scan(cli);
        assert_eq!(
            cli_balance(cli),
            50_000_000,
            "wallet should show funded balance after full_scan"
        );
    }

    /// Runs `wallet --wallet <WALLET_NAME> <args>`, asserts the process exited
    /// successfully, and parses stdout as JSON.
    fn run_wallet_json(cli: &BdkCli, args: &[&str]) -> Value {
        let mut full_args = vec!["--wallet", WALLET_NAME];
        full_args.extend_from_slice(args);

        let output = cli
            .wallet_cmd(&full_args)
            .output()
            .unwrap_or_else(|e| panic!("failed to spawn `{}`: {e}", args.join(" ")));

        assert!(
            output.status.success(),
            "`{}` failed: {}",
            args.join(" "),
            String::from_utf8_lossy(&output.stderr)
        );

        serde_json::from_slice(&output.stdout).unwrap_or_else(|e| {
            panic!(
                "`{}` returned non-JSON stdout ({e}): {}",
                args.join(" "),
                String::from_utf8_lossy(&output.stdout)
            )
        })
    }

    fn cli_new_address(cli: &BdkCli) -> Address {
        let json = run_wallet_json(cli, &["new_address"]);
        let addr = json["address"]
            .as_str()
            .expect("new_address: missing 'address' field");
        Address::from_str(addr)
            .expect("new_address: invalid address string")
            .require_network(Network::Regtest)
            .expect("new_address: wrong network")
    }

    fn cli_full_scan(cli: &BdkCli) {
        run_wallet_json(cli, &["full_scan"]);
    }

    fn cli_sync(cli: &BdkCli) {
        run_wallet_json(cli, &["sync"]);
    }

    fn cli_balance(cli: &BdkCli) -> u64 {
        let json = run_wallet_json(cli, &["balance"]);
        json["confirmed"]
            .as_u64()
            .expect("balance: missing 'confirmed' field")
    }

    fn cli_create_tx(cli: &BdkCli, to: &str) -> String {
        let json = run_wallet_json(cli, &["create_tx", "--to", to]);
        json["psbt"]
            .as_str()
            .expect("create_tx: missing 'psbt' field")
            .to_string()
    }

    /// Returns (signed_psbt_base64, is_finalized).
    fn cli_sign(cli: &BdkCli, psbt: &str) -> (String, bool) {
        let json = run_wallet_json(cli, &["sign", psbt]);
        let signed = json["psbt"]
            .as_str()
            .expect("sign: missing 'psbt' field")
            .to_string();
        let finalized = json["is_finalized"]
            .as_bool()
            .expect("sign: missing 'is_finalized' field");
        (signed, finalized)
    }

    fn cli_extract_psbt(cli: &BdkCli, psbt: &str) -> String {
        let json = run_wallet_json(cli, &["extract_psbt", psbt]);
        json["raw_tx"]
            .as_str()
            .expect("extract_psbt: missing 'raw_tx' field")
            .to_string()
    }

    fn cli_broadcast(cli: &BdkCli, raw_tx: &str) -> Txid {
        let json = run_wallet_json(cli, &["broadcast", "--tx", raw_tx]);
        let txid = json["txid"]
            .as_str()
            .expect("broadcast: missing 'txid' field");
        Txid::from_str(txid).expect("broadcast: invalid txid")
    }

    #[test]
    fn test_funded_wallet_unspent_and_transactions() {
        let (cli, mut cmd_init, env) = setup_online_wallet();
        cmd_init.assert().success();
        fund_and_sync_wallet(&cli, &env);

        //  unspent: exactly one confirmed, unspent 50,000,000 sat UTXO
        let unspent = run_wallet_json(&cli, &["unspent"]);
        assert_eq!(
            unspent["count"].as_u64(),
            Some(1),
            "funded wallet should report exactly one UTXO, got: {unspent}"
        );

        let utxo = &unspent["items"][0];
        assert_eq!(
            utxo["txout"]["value"].as_u64(),
            Some(50_000_000),
            "the single UTXO should equal the 0.5 BTC funding amount"
        );
        assert_eq!(
            utxo["is_spent"].as_bool(),
            Some(false),
            "the funding UTXO must be unspent"
        );
        assert!(
            utxo["outpoint"].as_str().is_some_and(|o| o.contains(':')),
            "UTXO outpoint should be a `txid:vout` string, got: {}",
            utxo["outpoint"]
        );
        assert!(
            utxo["derivation_index"].is_number(),
            "UTXO should expose a numeric derivation_index"
        );

        // transactions: exactly one relevant tx, funding our address
        let txs = run_wallet_json(&cli, &["transactions"]);
        assert_eq!(
            txs["count"].as_u64(),
            Some(1),
            "funded wallet should report exactly one transaction, got: {txs}"
        );

        let tx = &txs["items"][0];
        assert_eq!(
            tx["is_coinbase"].as_bool(),
            Some(false),
            "the funding transaction is a normal send, not a coinbase"
        );
        assert!(
            tx["txid"].as_str().is_some_and(|t| t.len() == 64),
            "transaction should expose a 64-char hex txid, got: {}",
            tx["txid"]
        );
        // The funding tx must contain the output that paid us 0.5 BTC.
        let outputs = tx["outputs"]
            .as_array()
            .expect("transaction outputs should be a JSON array");
        assert!(
            outputs
                .iter()
                .any(|o| o["value"].as_u64() == Some(50_000_000)),
            "funding tx should contain a 50,000,000 sat output to the wallet"
        );
    }

    #[test]
    fn test_create_tx_send_all_drains_wallet() {
        let (cli, mut cmd_init, env) = setup_online_wallet();
        cmd_init.assert().success();
        fund_and_sync_wallet(&cli, &env);

        let unsigned_psbt = run_wallet_json(
            &cli,
            &["create_tx", "--send_all", "--to", &format!("{RECIPIENT}:0")],
        )["psbt"]
            .as_str()
            .expect("create_tx: missing 'psbt' field")
            .to_string();

        let (signed_psbt, finalized) = cli_sign(&cli, &unsigned_psbt);
        assert!(finalized, "drain PSBT should be finalized after signing");

        let raw_tx = cli_extract_psbt(&cli, &signed_psbt);
        let drain_txid = cli_broadcast(&cli, &raw_tx);
        env.rpc_client()
            .get_mempool_entry(&drain_txid)
            .expect("drain tx not found in node mempool");

        env.mine_blocks(1, None)
            .expect("Failed to mine drain confirmation block");
        env.wait_until_electrum_sees_block(Duration::from_secs(10))
            .expect("Electrum did not catch up to drain confirmation");

        cli_sync(&cli);
        assert_eq!(
            cli_balance(&cli),
            0,
            "send_all should leave a zero confirmed balance"
        );
    }

    #[test]
    fn test_full_online_transaction_lifecycle() {
        let (cli, mut cmd_init, env) = setup_online_wallet();
        cmd_init.assert().success();

        fund_and_sync_wallet(&cli, &env);

        // Build, sign, extract, and broadcast a spend
        let unsigned_psbt = cli_create_tx(&cli, &format!("{RECIPIENT}:20000"));

        let (signed_psbt, finalized) = cli_sign(&cli, &unsigned_psbt);
        assert!(finalized, "PSBT should be finalized after signing");

        let raw_tx = cli_extract_psbt(&cli, &signed_psbt);

        let spend_txid = cli_broadcast(&cli, &raw_tx);
        env.rpc_client()
            .get_mempool_entry(&spend_txid)
            .expect("broadcast tx not found in node mempool");

        // Confirm the spend and verify the final balance
        env.mine_blocks(1, None)
            .expect("Failed to mine spend confirmation block");
        env.wait_until_electrum_sees_block(Duration::from_secs(10))
            .expect("Electrum did not catch up to spend confirmation");

        cli_sync(&cli);
        let final_balance = cli_balance(&cli);
        assert!(
            (49_900_000..50_000_000).contains(&final_balance),
            "unexpected post-spend confirmed balance: {final_balance}"
        );
    }

    #[test]
    fn test_finalize_psbt_on_signed_tx() {
        let (cli, mut cmd_init, env) = setup_online_wallet();
        cmd_init.assert().success();
        fund_and_sync_wallet(&cli, &env);

        let unsigned_psbt = cli_create_tx(&cli, &format!("{RECIPIENT}:15000"));
        let (signed_psbt, finalized) = cli_sign(&cli, &unsigned_psbt);
        assert!(finalized, "PSBT should be finalized after signing");

        // finalize_psbt should be idempotent: finalizing an already-finalized
        // PSBT should succeed and still report is_finalized = true.
        let finalize_json = run_wallet_json(&cli, &["finalize_psbt", &signed_psbt]);
        assert_eq!(
            finalize_json["is_finalized"].as_bool(),
            Some(true),
            "finalize_psbt should report the PSBT as finalized"
        );
    }

    #[test]
    fn test_combine_psbt_merges_signatures() {
        let (cli, mut cmd_init, env) = setup_online_wallet();
        cmd_init.assert().success();
        fund_and_sync_wallet(&cli, &env);

        let unsigned_psbt = cli_create_tx(&cli, &format!("{RECIPIENT}:15000"));
        let (signed_psbt, finalized) = cli_sign(&cli, &unsigned_psbt);
        assert!(finalized, "PSBT should be finalized after signing");

        // Combine the unsigned and signed versions of the same transaction
        let combined_json = run_wallet_json(&cli, &["combine_psbt", &unsigned_psbt, &signed_psbt]);
        let combined_psbt = combined_json["psbt"]
            .as_str()
            .expect("combine_psbt: missing 'psbt' field");

        // If signature data survived the combine, this should extract cleanly.
        let raw_tx = cli_extract_psbt(&cli, combined_psbt);
        assert!(
            !raw_tx.is_empty(),
            "combined PSBT did not extract to a valid raw transaction"
        );
    }

    #[test]
    fn test_bump_fee_replaces_unconfirmed_tx() {
        let (cli, mut cmd_init, env) = setup_online_wallet();
        cmd_init.assert().success();
        fund_and_sync_wallet(&cli, &env);

        // Create and broadcast a low-fee-rate, RBF-enabled transaction.
        let unsigned_psbt = run_wallet_json(
            &cli,
            &[
                "create_tx",
                "--to",
                &format!("{RECIPIENT}:15000"),
                "--fee_rate",
                "1.0",
            ],
        )["psbt"]
            .as_str()
            .expect("create_tx: missing 'psbt' field")
            .to_string();

        let (signed_psbt, finalized) = cli_sign(&cli, &unsigned_psbt);
        assert!(finalized);
        let raw_tx = cli_extract_psbt(&cli, &signed_psbt);
        let original_txid = cli_broadcast(&cli, &raw_tx);

        env.rpc_client()
            .get_mempool_entry(&original_txid)
            .expect("original tx should be in the mempool before bumping");

        env.wait_until_electrum_sees_txid(original_txid, Duration::from_secs(10))
            .expect("electrs did not see the broadcast tx");
        cli_sync(&cli);

        // Bump to a much higher fee rate; this should replace the original tx.
        let bumped_json = run_wallet_json(
            &cli,
            &[
                "bump_fee",
                "--txid",
                &original_txid.to_string(),
                "--fee_rate",
                "10.0",
            ],
        );
        let bumped_psbt = bumped_json["psbt"]
            .as_str()
            .expect("bump_fee: missing 'psbt' field");

        let (signed_bumped, bumped_finalized) = cli_sign(&cli, bumped_psbt);
        assert!(
            bumped_finalized,
            "bumped PSBT should be finalized after signing"
        );

        let bumped_raw_tx = cli_extract_psbt(&cli, &signed_bumped);
        let bumped_txid = cli_broadcast(&cli, &bumped_raw_tx);

        assert_ne!(
            original_txid, bumped_txid,
            "fee-bumped transaction should have a different txid than the original"
        );

        env.mine_blocks(1, None)
            .expect("Failed to confirm bumped tx");
        env.wait_until_electrum_sees_block(Duration::from_secs(10))
            .expect("Electrum did not catch up after bump confirmation");
        cli_sync(&cli);

        let final_balance = cli_balance(&cli);
        assert!(
            final_balance < 50_000_000,
            "balance should reflect exactly one confirmed spend after RBF, got {final_balance}"
        );
    }

    #[test]
    fn test_create_tx_multiple_recipients() {
        use bdk_wallet::bitcoin::Psbt;

        let (cli, mut cmd_init, env) = setup_online_wallet();
        cmd_init.assert().success();
        fund_and_sync_wallet(&cli, &env);

        let second = cli_new_address(&cli);

        let psbt_b64 = run_wallet_json(
            &cli,
            &[
                "create_tx",
                "--to",
                &format!("{RECIPIENT}:20000"),
                "--to",
                &format!("{second}:30000"),
            ],
        )["psbt"]
            .as_str()
            .expect("create_tx: missing 'psbt' field")
            .to_string();

        let psbt: Psbt = psbt_b64
            .parse()
            .expect("create_tx returned an invalid PSBT");
        let values: Vec<u64> = psbt
            .unsigned_tx
            .output
            .iter()
            .map(|o| o.value.to_sat())
            .collect();

        assert!(
            values.contains(&20_000),
            "first recipient (20_000) output missing: {values:?}"
        );
        assert!(
            values.contains(&30_000),
            "second recipient (30_000) output missing: {values:?}"
        );
        assert!(
            psbt.unsigned_tx.output.len() >= 2,
            "expected at least the two recipient outputs, got {}",
            psbt.unsigned_tx.output.len()
        );
    }

    // `create_tx --add_string` embeds an OP_RETURN (nulldata) output carrying
    // the given text, at zero value.
    #[test]
    fn test_create_tx_op_return() {
        use bdk_wallet::bitcoin::Psbt;
        let (cli, mut cmd_init, env) = setup_online_wallet();
        cmd_init.assert().success();
        fund_and_sync_wallet(&cli, &env);

        let msg = "hello bdk cli";
        let psbt_b64 = run_wallet_json(
            &cli,
            &[
                "create_tx",
                "--to",
                &format!("{RECIPIENT}:20000"),
                "--add_string",
                msg,
            ],
        )["psbt"]
            .as_str()
            .unwrap()
            .to_string();

        let psbt: Psbt = psbt_b64.parse().expect("invalid PSBT");
        let op_return = psbt
            .unsigned_tx
            .output
            .iter()
            .find(|o| o.script_pubkey.is_op_return())
            .expect("expected an OP_RETURN output");
        assert_eq!(
            op_return.value.to_sat(),
            0,
            "OP_RETURN output must carry 0 value"
        );
        assert!(
            op_return
                .script_pubkey
                .as_bytes()
                .windows(msg.len())
                .any(|w| w == msg.as_bytes()),
            "OP_RETURN should embed the message bytes"
        );
    }

    // `create_tx --utxos` forces a specific UTXO to be spent;
    // `--unspendable`excludes one. Funds two UTXOs so the selection is actually observable.
    #[test]
    fn test_create_tx_utxo_selection() {
        use bdk_wallet::bitcoin::Psbt;
        let (cli, mut cmd_init, env) = setup_online_wallet();
        cmd_init.assert().success();

        let node_addr = env
            .rpc_client()
            .get_new_address(None, None)
            .unwrap()
            .assume_checked();
        env.mine_blocks(101, Some(node_addr)).unwrap();
        env.wait_until_electrum_sees_block(Duration::from_secs(10))
            .unwrap();
        let a1 = cli_new_address(&cli);
        let t1 = env.send(&a1, Amount::from_btc(0.3).unwrap()).unwrap();
        env.wait_until_electrum_sees_txid(t1, Duration::from_secs(10))
            .unwrap();
        let a2 = cli_new_address(&cli);
        let t2 = env.send(&a2, Amount::from_btc(0.2).unwrap()).unwrap();
        env.wait_until_electrum_sees_txid(t2, Duration::from_secs(10))
            .unwrap();
        env.mine_blocks(3, None).unwrap();
        env.wait_until_electrum_sees_block(Duration::from_secs(10))
            .unwrap();
        cli_full_scan(&cli);

        let unspent = run_wallet_json(&cli, &["unspent"]);
        let items = unspent["items"].as_array().unwrap();
        assert_eq!(items.len(), 2, "expected two UTXOs, got {}", items.len());
        let op0 = items[0]["outpoint"].as_str().unwrap().to_string();

        // --utxos forces op0 to be an input.
        let psbt_a: Psbt = run_wallet_json(
            &cli,
            &[
                "create_tx",
                "--to",
                &format!("{RECIPIENT}:10000"),
                "--utxos",
                &op0,
            ],
        )["psbt"]
            .as_str()
            .unwrap()
            .parse()
            .unwrap();
        let inputs_a: Vec<String> = psbt_a
            .unsigned_tx
            .input
            .iter()
            .map(|i| i.previous_output.to_string())
            .collect();
        assert!(
            inputs_a.contains(&op0),
            "--utxos should force {op0}: {inputs_a:?}"
        );

        // --unspendable excludes op0 (the other UTXO covers the amount).
        let psbt_b: Psbt = run_wallet_json(
            &cli,
            &[
                "create_tx",
                "--to",
                &format!("{RECIPIENT}:10000"),
                "--unspendable",
                &op0,
            ],
        )["psbt"]
            .as_str()
            .unwrap()
            .parse()
            .unwrap();
        let inputs_b: Vec<String> = psbt_b
            .unsigned_tx
            .input
            .iter()
            .map(|i| i.previous_output.to_string())
            .collect();
        assert!(
            !inputs_b.contains(&op0),
            "--unspendable should exclude {op0}: {inputs_b:?}"
        );
    }

    #[cfg(feature = "rpc")]
    mod test_rpc {
        use super::*;

        static WALLET_NAME: &str = "test_rpc_wallet";

        #[test]
        fn test_rpc_full_scan_reflects_funding() {
            let env = TestEnv::new().expect("start testenv");
            let rpc_url = env.bitcoind.rpc_url();
            let cookie = env
                .bitcoind
                .params
                .cookie_file
                .to_str()
                .unwrap()
                .to_string();

            let temp_dir = TempDir::new().unwrap();
            let cli = BdkCli::new("regtest", Some(temp_dir.path().to_path_buf()));

            // descriptors
            let desc = cli.cmd("descriptor", &["--type", "tr"]).output().unwrap();
            let dv: Value = serde_json::from_slice(&desc.stdout).unwrap();
            let ext = dv["private_descriptors"]["external"].as_str().unwrap();
            let int = dv["private_descriptors"]["internal"].as_str().unwrap();

            // config: rpc client-type, --url flag, cookie as a POSITIONAL arg
            cli.build_base_cmd()
                .args(["wallet", "--wallet", WALLET_NAME, "config"])
                .args(["--ext-descriptor", ext, "--int-descriptor", int])
                .args(["--client-type", "rpc", "--database-type", "sqlite"])
                .args(["--url", &rpc_url])
                .arg(&cookie)
                .assert()
                .success();

            // wallet address to fund
            let out = cli
                .wallet_cmd(&["--wallet", WALLET_NAME, "new_address"])
                .output()
                .unwrap();
            let addr_json: Value = serde_json::from_slice(&out.stdout).unwrap();
            let address = Address::from_str(addr_json["address"].as_str().unwrap())
                .unwrap()
                .require_network(Network::Regtest)
                .unwrap();

            // fund + confirm
            let node_addr = env
                .rpc_client()
                .get_new_address(None, None)
                .unwrap()
                .assume_checked();
            env.mine_blocks(101, Some(node_addr)).unwrap();
            env.send(&address, Amount::from_btc(0.5).unwrap()).unwrap();
            env.mine_blocks(3, None).unwrap();

            // full_scan + balance via rpc
            let scan = cli
                .wallet_cmd(&["--wallet", WALLET_NAME, "full_scan"])
                .output()
                .unwrap();
            assert!(
                scan.status.success(),
                "rpc full_scan failed: {}",
                String::from_utf8_lossy(&scan.stderr)
            );

            let bal = cli
                .wallet_cmd(&["--wallet", WALLET_NAME, "balance"])
                .output()
                .unwrap();
            let bj: Value = serde_json::from_slice(&bal.stdout).unwrap();
            assert_eq!(
                bj["confirmed"].as_u64(),
                Some(50_000_000),
                "rpc-synced confirmed balance mismatch: {bj}"
            );
        }
    }

    #[cfg(feature = "silent-payments")]
    mod test_sp {
        use super::*;
        use bdk_wallet::bitcoin::{Transaction, consensus::encode::deserialize_hex};

        const SCAN: &str = "0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798";
        const SPEND: &str = "02c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5";
        static WALLET: &str = "sp_wallet";

        #[test]
        fn test_create_sp_tx_produces_taproot_output() {
            let env = TestEnv::new().unwrap();
            let url = env.electrsd.electrum_url.as_str();
            let tmp = TempDir::new().unwrap();
            let cli = BdkCli::new("regtest", Some(tmp.path().to_path_buf()));

            let desc = cli.cmd("descriptor", &["--type", "tr"]).output().unwrap();
            let dv: Value = serde_json::from_slice(&desc.stdout).unwrap();
            let ext_desc = dv["private_descriptors"]["external"].as_str().unwrap();
            let int_desc = dv["private_descriptors"]["internal"].as_str().unwrap();
            cli.build_base_cmd()
                .args([
                    "wallet",
                    "--wallet",
                    WALLET,
                    "config",
                    "--ext-descriptor",
                    ext_desc,
                    "--int-descriptor",
                    int_desc,
                    "--client-type",
                    "electrum",
                    "--database-type",
                    "sqlite",
                    "--url",
                    url,
                ])
                .assert()
                .success();

            let new_address = cli
                .wallet_cmd(&["--wallet", WALLET, "new_address"])
                .output()
                .unwrap();
            let addr = Address::from_str(
                serde_json::from_slice::<Value>(&new_address.stdout).unwrap()["address"]
                    .as_str()
                    .unwrap(),
            )
            .unwrap()
            .require_network(Network::Regtest)
            .unwrap();
            let node_address = env
                .rpc_client()
                .get_new_address(None, None)
                .unwrap()
                .assume_checked();
            env.mine_blocks(101, Some(node_address)).unwrap();
            env.wait_until_electrum_sees_block(Duration::from_secs(10))
                .unwrap();
            let txid = env.send(&addr, Amount::from_btc(0.5).unwrap()).unwrap();
            env.wait_until_electrum_sees_txid(txid, Duration::from_secs(10))
                .unwrap();
            env.mine_blocks(3, None).unwrap();
            env.wait_until_electrum_sees_block(Duration::from_secs(10))
                .unwrap();
            cli.wallet_cmd(&["--wallet", WALLET, "full_scan"])
                .assert()
                .success();

            let silent_payment_output = cli
                .cmd(
                    "silent_payment_code",
                    &["--scan_key", SCAN, "--spend_key", SPEND],
                )
                .output()
                .unwrap();
            let sp_code =
                serde_json::from_slice::<Value>(&silent_payment_output.stdout).unwrap()["message"]
                    .as_str()
                    .unwrap()
                    .to_string();

            let out = cli
                .wallet_cmd(&[
                    "--wallet",
                    WALLET,
                    "create_sp_tx",
                    "--to-sp",
                    &format!("{sp_code}:20000"),
                ])
                .output()
                .unwrap();
            assert!(
                out.status.success(),
                "create_sp_tx failed: {}",
                String::from_utf8_lossy(&out.stderr)
            );
            let rawtx = serde_json::from_slice::<Value>(&out.stdout).unwrap()["raw_tx"]
                .as_str()
                .unwrap()
                .to_string();

            let tx: Transaction = deserialize_hex(&rawtx).expect("invalid raw tx");
            let sp_out = tx
                .output
                .iter()
                .find(|o| o.value.to_sat() == 20_000)
                .expect("no 20_000 output");
            assert!(
                sp_out.script_pubkey.is_p2tr(),
                "SP recipient output should be P2TR"
            );
        }
    }

    #[cfg(feature = "message_signer")]
    #[test]
    fn test_verify_message_proof_of_funds_uses_persisted_utxos() {
        let env = TestEnv::new().expect("Failed to start bdk_testenv");
        let server_url = env.electrsd.electrum_url.as_str();
        let temp_dir = TempDir::new().unwrap();
        let cli = BdkCli::new("regtest", Some(temp_dir.path().to_path_buf()));

        // A wpkh wallet
        let desc = cli.cmd("descriptor", &["--type", "wpkh"]).output().unwrap();
        let desc_val: Value = serde_json::from_slice(&desc.stdout).unwrap();
        let ext_desc = desc_val["private_descriptors"]["external"]
            .as_str()
            .unwrap();
        let int_desc = desc_val["private_descriptors"]["internal"]
            .as_str()
            .unwrap();
        cli.build_base_cmd()
            .arg("wallet")
            .arg("--wallet")
            .arg(WALLET_NAME)
            .arg("config")
            .arg("--ext-descriptor")
            .arg(ext_desc)
            .arg("--int-descriptor")
            .arg(int_desc)
            .arg("--client-type")
            .arg("electrum")
            .arg("--database-type")
            .arg("sqlite")
            .arg("--url")
            .arg(server_url)
            .assert()
            .success();

        // Fund a single small UTXO
        let address = cli_new_address(&cli);
        let node_addr = env
            .rpc_client()
            .get_new_address(None, None)
            .unwrap()
            .assume_checked();
        env.mine_blocks(101, Some(node_addr)).unwrap();
        env.wait_until_electrum_sees_block(Duration::from_secs(10))
            .unwrap();
        let txid = env.send(&address, Amount::from_sat(5_000)).unwrap();
        env.wait_until_electrum_sees_txid(txid, Duration::from_secs(10))
            .unwrap();
        env.mine_blocks(3, None).unwrap();
        env.wait_until_electrum_sees_block(Duration::from_secs(10))
            .unwrap();
        cli_full_scan(&cli);

        // The funded outpoint to prove control of.
        let unspent = run_wallet_json(&cli, &["unspent"]);
        let outpoint = unspent["items"][0]["outpoint"]
            .as_str()
            .expect("funded wallet should have one UTXO")
            .to_string();
        let addr = address.to_string();

        // Produce a proof-of-funds over that UTXO.
        let proof = run_wallet_json(
            &cli,
            &[
                "sign_message",
                "--message",
                "proof-of-funds",
                "--address",
                &addr,
                "--signature-type",
                "fullproofoffunds",
                "--utxos",
                &outpoint,
            ],
        )["proof"]
            .as_str()
            .expect("sign_message should return a proof")
            .to_string();

        let result = run_wallet_json(
            &cli,
            &[
                "verify_message",
                "--proof",
                &proof,
                "--message",
                "proof-of-funds",
                "--address",
                &addr,
            ],
        );
        assert_eq!(
            result["valid"].as_bool(),
            Some(true),
            "proof-of-funds should verify against the persisted wallet: {result}"
        );
        assert_eq!(
            result["proven_amount"].as_u64(),
            Some(5_000),
            "proven_amount should equal the funded UTXO value: {result}"
        );
    }

    // `create_dns_tx` with a plain `--to` recipient
    #[cfg(feature = "dns_payment")]
    #[test]
    fn test_create_dns_tx_plain_recipient() {
        use bdk_wallet::bitcoin::Psbt;
        let (cli, mut cmd_init, env) = setup_online_wallet();
        cmd_init.assert().success();
        fund_and_sync_wallet(&cli, &env);

        let psbt_b64 = run_wallet_json(
            &cli,
            &["create_dns_tx", "--to", &format!("{RECIPIENT}:20000")],
        )["psbt"]
            .as_str()
            .expect("create_dns_tx: missing 'psbt'")
            .to_string();

        let psbt: Psbt = psbt_b64
            .parse()
            .expect("create_dns_tx returned an invalid PSBT");
        assert!(
            psbt.unsigned_tx
                .output
                .iter()
                .any(|o| o.value.to_sat() == 20_000),
            "expected the 20_000 sat recipient output"
        );
    }

    // With neither `--to` nor `--to_dns`, the command errors.
    #[cfg(feature = "dns_payment")]
    #[test]
    fn test_create_dns_tx_requires_recipient() {
        let (cli, mut cmd_init, _env) = setup_online_wallet();
        cmd_init.assert().success();

        let out = cli
            .wallet_cmd(&["--wallet", WALLET_NAME, "create_dns_tx"])
            .output()
            .unwrap();
        assert!(!out.status.success(), "should fail with no recipients");
        assert!(
            String::from_utf8_lossy(&out.stderr).contains("Either --to or --to_dns"),
            "expected recipient-required error, got: {}",
            String::from_utf8_lossy(&out.stderr)
        );
    }

    // lock → list → unlock cycle.
    #[test]
    fn test_lock_unlock_and_list_utxos() {
        let (cli, mut cmd_init, env) = setup_online_wallet();
        cmd_init.assert().success();
        fund_and_sync_wallet(&cli, &env);

        let unspent = run_wallet_json(&cli, &["unspent"]);
        let outpoint = unspent["items"][0]["outpoint"]
            .as_str()
            .expect("one utxo")
            .to_string();

        let locked = run_wallet_json(&cli, &["lock_utxo", "--utxo", &outpoint]);
        assert_eq!(locked["count"].as_u64(), Some(1), "lock output: {locked}");

        // separate process — must still be locked (persistence)
        let listed = run_wallet_json(&cli, &["locked_utxos"]);
        assert_eq!(
            listed["count"].as_u64(),
            Some(1),
            "lock must persist across processes: {listed}"
        );
        assert!(
            listed["items"]
                .as_array()
                .unwrap()
                .iter()
                .any(|i| i.as_str() == Some(outpoint.as_str())),
            "locked_utxos should list {outpoint}: {listed}"
        );

        let after = run_wallet_json(&cli, &["unlock_utxo", "--utxo", &outpoint]);
        assert_eq!(
            after["count"].as_u64(),
            Some(0),
            "unlock should clear it: {after}"
        );
        let final_list = run_wallet_json(&cli, &["locked_utxos"]);
        assert_eq!(
            final_list["count"].as_u64(),
            Some(0),
            "empty after unlock: {final_list}"
        );
    }

    // a locked UTXO is excluded from coin selection.
    #[test]
    fn test_locked_utxo_excluded_from_coin_selection() {
        let (cli, mut cmd_init, env) = setup_online_wallet();
        cmd_init.assert().success();
        fund_and_sync_wallet(&cli, &env);

        let unspent = run_wallet_json(&cli, &["unspent"]);
        let outpoint = unspent["items"][0]["outpoint"]
            .as_str()
            .unwrap()
            .to_string();

        run_wallet_json(&cli, &["lock_utxo", "--utxo", &outpoint]);

        // only UTXO is locked → create_tx must fail
        let out = cli
            .wallet_cmd(&[
                "--wallet",
                WALLET_NAME,
                "create_tx",
                "--to",
                &format!("{RECIPIENT}:20000"),
            ])
            .output()
            .unwrap();
        assert!(
            !out.status.success(),
            "create_tx must fail when the only UTXO is locked"
        );

        // unlock → create_tx succeeds
        run_wallet_json(&cli, &["unlock_utxo", "--utxo", &outpoint]);
        let psbt = run_wallet_json(&cli, &["create_tx", "--to", &format!("{RECIPIENT}:20000")]);
        assert!(
            psbt["psbt"].as_str().is_some(),
            "create_tx should succeed after unlock: {psbt}"
        );
    }
}

#[cfg(feature = "esplora")]
mod test_esplora {
    use crate::common::BdkCli;
    use bdk_testenv::{TestEnv, bitcoincore_rpc::RpcApi};
    use bdk_wallet::bitcoin::{Address, Amount, Network};
    use serde_json::Value;
    use std::str::FromStr;
    use std::time::Duration;
    use tempfile::TempDir;

    static WALLET_NAME: &str = "test_esplora_wallet";

    // config an esplora wallet, fund it, `full_scan`, and confirm the synced balance.
    #[test]
    fn test_esplora_full_scan_reflects_funding() {
        let env = TestEnv::new().expect("start testenv");
        let esplora = env
            .electrsd
            .esplora_url
            .clone()
            .expect("esplora http endpoint (TestEnv sets http_enabled)");
        // esplora_url is a bind addr ("0.0.0.0:PORT"); connect via loopback.
        let url = format!("http://{}", esplora.replace("0.0.0.0", "127.0.0.1"));

        let temp_dir = TempDir::new().unwrap();
        let cli = BdkCli::new("regtest", Some(temp_dir.path().to_path_buf()));

        let desc = cli.cmd("descriptor", &["--type", "tr"]).output().unwrap();
        let desc_value: Value = serde_json::from_slice(&desc.stdout).unwrap();
        let ext = desc_value["private_descriptors"]["external"]
            .as_str()
            .unwrap();
        let int = desc_value["private_descriptors"]["internal"]
            .as_str()
            .unwrap();
        cli.build_base_cmd()
            .args(["wallet", "--wallet", WALLET_NAME, "config"])
            .args(["--ext-descriptor", ext, "--int-descriptor", int])
            .args(["--client-type", "esplora", "--database-type", "sqlite"])
            .args(["--url", &url])
            .assert()
            .success();

        let out = cli
            .wallet_cmd(&["--wallet", WALLET_NAME, "new_address"])
            .output()
            .unwrap();
        let address = Address::from_str(
            serde_json::from_slice::<Value>(&out.stdout).unwrap()["address"]
                .as_str()
                .unwrap(),
        )
        .unwrap()
        .require_network(Network::Regtest)
        .unwrap();

        let node_addr = env
            .rpc_client()
            .get_new_address(None, None)
            .unwrap()
            .assume_checked();
        env.mine_blocks(101, Some(node_addr)).unwrap();
        env.wait_until_electrum_sees_block(Duration::from_secs(10))
            .unwrap();
        let txid = env.send(&address, Amount::from_btc(0.5).unwrap()).unwrap();
        env.wait_until_electrum_sees_txid(txid, Duration::from_secs(10))
            .unwrap();
        env.mine_blocks(3, None).unwrap();
        env.wait_until_electrum_sees_block(Duration::from_secs(10))
            .unwrap();

        let scan = cli
            .wallet_cmd(&["--wallet", WALLET_NAME, "full_scan"])
            .output()
            .unwrap();
        assert!(
            scan.status.success(),
            "esplora full_scan failed: {}",
            String::from_utf8_lossy(&scan.stderr)
        );

        let bal = cli
            .wallet_cmd(&["--wallet", WALLET_NAME, "balance"])
            .output()
            .unwrap();
        let bj: Value = serde_json::from_slice(&bal.stdout).unwrap();
        assert_eq!(
            bj["confirmed"].as_u64(),
            Some(50_000_000),
            "esplora-synced confirmed balance mismatch: {bj}"
        );
    }
}