dig-account 0.26.2

The DIG Network user Account: the fat, strictly-logical crate for everything an account does — the Account+Profile object model, unlock policy + keystore crypto, the in-process identity+money signer, per-profile key/DEK derivation, DID+dig-store mint/sign, and all wallet ops. Headless (no UI). Consumed by dig-app.
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
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
//! The $DIG (CAT) transfer spend BUILDER: a $DIG payment from a hot-wallet profile to somebody else.
//!
//! Like [`transfer`](crate::wallet::transfer), this module builds spends and stops — it does not
//! gate, does not sign and does not push.
//!
//! # A CAT send is a different builder, not a parameter on the XCH one
//!
//! The two differ in the one place a wallet cannot afford to be approximate: **where the money
//! lives**. A CAT coin is NOT locked by its owner's bare p2 puzzle hash. It is locked by the CAT
//! puzzle curried around the asset id and that p2 hash —
//! [`CatArgs::curry_tree_hash`](chia_puzzle_types::cat::CatArgs::curry_tree_hash). So:
//!
//! - Selecting against the bare p2 hash finds no $DIG at all, and reports an empty wallet to a user
//!   who holds thousands.
//! - PAYING against the bare p2 hash sends the CAT to a puzzle hash the recipient does not control
//!   as a CAT. The value conserves, the bundle confirms, and the money is gone.
//!
//! That is a lose-the-funds difference, which is why the CAT destination is derived here from
//! [`DIG_ASSET_ID`] and never taken from a caller, and why [`dig_curried_puzzle_hash`] is pinned
//! against a known-good value in the tests.
//!
//! # A CAT coin cannot be spent from a coin record alone
//!
//! The CAT puzzle demands a lineage proof — proof that this coin descends from a genuine coin of
//! the same asset. That proof is only obtainable by reading the coin's PARENT SPEND and parsing it.
//! Every selected input therefore costs a second chain read
//! ([`ChainSource::parent_spend`](dig_chainsource_interface::ChainSource::parent_spend)), and an
//! input whose lineage cannot be established is not "skipped" — it is
//! [`CatTransferError::LineageUnavailable`], because a coin we cannot prove is a coin we must not
//! spend.
//!
//! # The fee is paid in XCH, so a $DIG send needs BOTH assets
//!
//! Chia charges fees in native mojos. A CAT bundle cannot pay its own fee out of the CAT, so a $DIG
//! send with a non-zero fee also selects an XCH coin. A wallet holding $DIG and no XCH is a real and
//! ordinary state, and it gets its own named error ([`CatTransferError::NoXchForFee`]) telling the
//! user exactly what to do — never a generic build failure, and never "insufficient funds", which
//! would read as a $DIG shortfall.
//!
//! # Every input is confirmed by name first
//!
//! Both the CAT inputs and the XCH fee coin go through
//! [`confirm_all_spendable_by_name`](crate::chain_confirm::confirm_all_spendable_by_name), all-or-
//! nothing, for the reasons in [`crate::chain_confirm`].

use std::collections::HashSet;

use chia_protocol::{Bytes32, Coin, CoinSpend};
use chia_puzzle_types::cat::CatArgs;
use chia_puzzle_types::Memos;
use chia_wallet_sdk::driver::{
    Cat, CatSpend, Puzzle, SpendContext, SpendWithConditions, StandardLayer,
};
use chia_wallet_sdk::prelude::TreeHash;
use chia_wallet_sdk::types::Conditions;
use clvmr::serde::node_from_bytes;
use clvmr::Allocator;
use dig_chainsource_interface::ChainSource;
use dig_constants::DIG_ASSET_ID;

use crate::chain_confirm::{confirm_all_spendable_by_name, UnconfirmedInput};
use crate::keys::wallet_key::WalletKey;
use crate::wallet::authorizer::WalletOps;
use crate::wallet::policy::CustodyPolicy;
use crate::wallet::reservation::{select_and_reserve, CoinReservations, ReservationId};
use crate::wallet::transfer::{
    PayableDestination, MAX_RESERVATION_ATTEMPTS, MAX_TRANSFER_INPUT_COINS,
};

/// Base units in one $DIG.
///
/// $DIG is a **3-decimal** CAT, the Chia CAT convention, so one $DIG is 1,000 base units — the same
/// factor `dig-tips` denominates its tip amounts in and `dig-app` decodes CAT amounts with.
///
/// # Why this is a named constant and not an inline `1_000`
///
/// XCH is 12 decimals and $DIG is 3. A display path that divided a $DIG amount by the XCH factor
/// would show `0.000000001` for 1,000 $DIG; one that divided by nothing would show 1,000,000. Either
/// is a 1,000x lie about somebody's money, told by an anonymous integer literal. Naming the factor
/// puts it somewhere a test can pin it, and [`amount_in_dig`] is the only division this module
/// offers.
pub const DIG_BASE_UNITS_PER_TOKEN: u64 = 1_000;

/// The most $DIG coins one transfer will consume — the same cap, for the same reasons, as
/// [`MAX_TRANSFER_INPUT_COINS`].
pub const MAX_CAT_TRANSFER_INPUT_COINS: usize = MAX_TRANSFER_INPUT_COINS;

/// The message the lead CAT announces and the XCH fee coin asserts.
const FEE_BINDING_MESSAGE: &[u8] = b"dig-account:cat-transfer";

/// A $DIG transfer result.
pub type CatTransferResult<T> = std::result::Result<T, CatTransferError>;

/// Why a $DIG transfer could not be built.
///
/// Kept separate from [`TransferError`](crate::wallet::transfer::TransferError) rather than folded
/// into it, because the two builders are short of DIFFERENT things and a surface must not conflate
/// them: "you need more $DIG" and "you need some XCH to pay the fee" are opposite instructions, and
/// telling a user the wrong one sends them to the wrong place to fix it.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum CatTransferError {
    /// The wallet's confirmed, unspent $DIG does not cover the amount.
    ///
    /// Both figures are $DIG BASE UNITS, never mojos — see [`DIG_BASE_UNITS_PER_TOKEN`].
    #[error(
        "insufficient $DIG: the transfer needs {required} base units, the wallet holds {available}"
    )]
    InsufficientDig {
        /// The amount, in $DIG base units.
        required: u64,
        /// Every confirmed, unspent $DIG the wallet holds, in base units.
        available: u64,
    },

    /// Every coin that could fund this send — $DIG or the XCH fee — is already committed to an
    /// in-flight spend.
    ///
    /// Deliberately NOT [`InsufficientDig`](Self::InsufficientDig) or
    /// [`NoXchForFee`](Self::NoXchForFee): `available` is what the user really holds, reserved coins
    /// included, because a reservation narrows what may be selected and never what they own. The
    /// remedy is to wait, never to deposit.
    #[error("every coin that could fund this send is reserved by an in-flight spend: it needs {required}, the wallet holds {available} but only {selectable} is free right now")]
    CoinsReserved {
        /// What this leg needs — $DIG base units for the asset leg, mojos for the fee leg.
        required: u64,
        /// Everything the wallet holds on that leg, reserved coins included.
        available: u64,
        /// The part of it that is not currently reserved.
        selectable: u64,
    },

    /// The reservation store could not be consulted, so what is already in flight is UNKNOWN.
    ///
    /// The build REFUSES. Proceeding over an unreadable guard is precisely the double-select the
    /// reservation exists to prevent.
    #[error("{0}")]
    ReservationUnusable(String),

    /// The wallet holds enough $DIG, spread across more coins than one transfer may consume.
    #[error(
        "this transfer would need {needed} $DIG coins, above the {cap}-coin limit; consolidate \
         some coins first"
    )]
    TooManyInputCoins {
        /// How many coins covering the amount would take.
        needed: usize,
        /// The limit ([`MAX_CAT_TRANSFER_INPUT_COINS`]).
        cap: usize,
    },

    /// The wallet holds the $DIG but no XCH to pay the fee with.
    ///
    /// # Why this is its own error and its message is an instruction
    ///
    /// Chia charges fees in native mojos, and a CAT cannot pay its own. A $DIG-only wallet is
    /// therefore a perfectly ordinary state that CANNOT send at a non-zero fee, and the user has two
    /// real remedies: acquire a little XCH, or send at a zero fee and wait longer. Reporting this as
    /// a $DIG shortfall would send them to buy the one token they already have enough of.
    #[error(
        "this wallet holds $DIG but only {available} mojos of XCH, and a {required}-mojo fee is \
         paid in XCH, not in $DIG; add a little XCH, or send with a zero fee and wait longer for \
         it to confirm"
    )]
    NoXchForFee {
        /// The fee, in mojos.
        required: u64,
        /// The wallet's whole spendable XCH balance, in mojos.
        available: u64,
    },

    /// The recipient is this very wallet.
    #[error(
        "the recipient is this wallet's own address; a self-payment moves nothing and costs a fee"
    )]
    SelfPayment,

    /// The amount is zero.
    #[error("a transfer must move a positive number of $DIG base units")]
    ZeroAmount,

    /// The profile is on the VAULT custody tier, which cannot pay a third party at all.
    #[error(
        "this profile's funds are in the vault tier, which cannot send directly; move funds vault \
         -> hot wallet through the clawback window first, then send from the hot wallet"
    )]
    VaultTransferUnsupported,

    /// The wallet's $DIG coins sum to more than a `u64` can hold, so no shortfall check is meaningful.
    #[error("the wallet's $DIG coins total more than u64 can represent, so the balance cannot be judged")]
    BalanceUnjudgeable,

    /// A selected input's lineage proof could not be established from its parent spend.
    ///
    /// The CAT puzzle will not run without one, and a coin whose descent from the asset cannot be
    /// proven is a coin this builder must not spend. Never silently skipped: skipping would change
    /// the plan, and a source that cannot answer for one coin is not evidence about the others.
    #[error(
        "the lineage of $DIG coin {coin_id} could not be established from its parent spend ({reason}), \
         so it cannot be proven to be a genuine $DIG coin"
    )]
    LineageUnavailable {
        /// The coin whose lineage could not be proven.
        coin_id: Bytes32,
        /// Why.
        reason: String,
    },

    /// The chain could NOT be reached or could not answer. The outcome is UNKNOWN, never "no".
    #[error("chain unreachable: {0}")]
    ChainUnreachable(String),

    /// Building the unsigned spend failed inside the SDK drivers.
    #[error("could not build the $DIG transfer spend: {0}")]
    Build(String),
}

impl From<UnconfirmedInput> for CatTransferError {
    fn from(error: UnconfirmedInput) -> Self {
        CatTransferError::ChainUnreachable(error.to_string())
    }
}

/// A $DIG payment to build: who, how much, and what XCH fee to attach.
#[derive(Debug, Clone, Copy)]
pub struct CatTransferRequest {
    recipient: PayableDestination,
    /// The amount, in $DIG BASE UNITS ([`DIG_BASE_UNITS_PER_TOKEN`] of them per $DIG).
    amount_base_units: u64,
    /// The fee, in XCH MOJOS. A CAT cannot pay its own fee.
    fee_mojos: u64,
}

impl CatTransferRequest {
    /// A $DIG payment of `amount_base_units` to `recipient`, with no fee.
    ///
    /// `recipient` is the recipient's ordinary `xch` destination: a CAT is addressed by the owner's
    /// p2 puzzle hash, which this builder wraps in the $DIG CAT puzzle itself. There is deliberately
    /// no way to hand this builder a pre-wrapped CAT puzzle hash — see the module docs.
    pub fn new(recipient: PayableDestination, amount_base_units: u64) -> Self {
        Self {
            recipient,
            amount_base_units,
            fee_mojos: 0,
        }
    }

    /// The same payment with an XCH fee attached.
    pub fn with_fee_mojos(mut self, fee_mojos: u64) -> Self {
        self.fee_mojos = fee_mojos;
        self
    }

    /// The recipient's p2 puzzle hash.
    pub fn recipient(&self) -> Bytes32 {
        self.recipient.puzzle_hash()
    }

    /// The amount, in $DIG base units.
    pub fn amount_base_units(&self) -> u64 {
        self.amount_base_units
    }

    /// The fee, in XCH mojos.
    pub fn fee_mojos(&self) -> u64 {
        self.fee_mojos
    }
}

/// The unsigned spends of a $DIG transfer, plus what they will do.
///
/// Inert, exactly like [`TransferPlan`](crate::wallet::transfer::TransferPlan): not authorized, not
/// signed, not broadcast.
#[derive(Debug, Clone)]
pub struct CatTransferPlan {
    coin_spends: Vec<CoinSpend>,
    dig_source_coin_ids: Vec<Bytes32>,
    xch_source_coin_ids: Vec<Bytes32>,
    recipient: Bytes32,
    amount_base_units: u64,
    fee_mojos: u64,
    change_base_units: u64,
    reservation: Option<ReservationId>,
}

impl CatTransferPlan {
    /// The reservation holding BOTH legs of this plan — the $DIG coins and the XCH fee coin.
    ///
    /// One reservation rather than two on purpose: a build that reserved its $DIG and then failed to
    /// fund its fee would strand the $DIG for the whole TTL over a spend that never existed.
    ///
    /// Release it as soon as the send is known settled or known dead.
    pub fn reservation(&self) -> Option<ReservationId> {
        self.reservation
    }

    /// The unsigned spends, to hand to the authorizing gate.
    pub fn coin_spends(&self) -> &[CoinSpend] {
        &self.coin_spends
    }

    /// The $DIG coins this bundle spends.
    pub fn dig_source_coin_ids(&self) -> &[Bytes32] {
        &self.dig_source_coin_ids
    }

    /// The XCH coins this bundle spends, to pay the fee. Empty when the fee is zero.
    pub fn xch_source_coin_ids(&self) -> &[Bytes32] {
        &self.xch_source_coin_ids
    }

    /// The recipient's p2 puzzle hash.
    pub fn recipient(&self) -> Bytes32 {
        self.recipient
    }

    /// The amount paid, in $DIG base units.
    pub fn amount_base_units(&self) -> u64 {
        self.amount_base_units
    }

    /// The XCH fee, in mojos.
    pub fn fee_mojos(&self) -> u64 {
        self.fee_mojos
    }

    /// The $DIG returning to this wallet, in base units.
    pub fn change_base_units(&self) -> u64 {
        self.change_base_units
    }
}

/// The outer puzzle hash CAT coins of `asset_id` belonging to `p2_puzzle_hash` are locked by.
///
/// This — NOT `p2_puzzle_hash` — is where a wallet's CAT balance lives, and the address a CAT
/// payment must be built against. See the module docs for what happens if the two are confused.
pub fn cat_curried_puzzle_hash(asset_id: Bytes32, p2_puzzle_hash: Bytes32) -> Bytes32 {
    CatArgs::curry_tree_hash(asset_id, TreeHash::new(p2_puzzle_hash.into())).into()
}

/// The outer puzzle hash $DIG coins belonging to `p2_puzzle_hash` are locked by.
///
/// [`cat_curried_puzzle_hash`] pinned to [`DIG_ASSET_ID`], so no caller can accidentally supply a
/// different asset id and be handed a puzzle hash the wallet's $DIG is not at.
pub fn dig_curried_puzzle_hash(p2_puzzle_hash: Bytes32) -> Bytes32 {
    cat_curried_puzzle_hash(DIG_ASSET_ID, p2_puzzle_hash)
}

/// Render an amount of $DIG base units as whole $DIG plus the remaining thousandths.
///
/// The ONLY conversion this module offers, so a display path cannot invent its own divisor. Returns
/// `(whole_dig, base_units_remainder)` rather than a float: $DIG amounts are exact integers and a
/// binary float cannot represent thousandths exactly, so formatting through one would round somebody's
/// balance.
pub fn amount_in_dig(base_units: u64) -> (u64, u64) {
    (
        base_units / DIG_BASE_UNITS_PER_TOKEN,
        base_units % DIG_BASE_UNITS_PER_TOKEN,
    )
}

impl WalletOps {
    /// Build the unsigned coin spends for a $DIG payment out of this profile's wallet.
    ///
    /// The result is inert. Hand [`CatTransferPlan::coin_spends`] to
    /// [`PolicyAuthorizer::authorize_op`](crate::wallet::enforcer::PolicyAuthorizer::authorize_op),
    /// which will always route a CAT spend to the human confirmation ceremony (SPEC §6.4) — no
    /// mojo-denominated auto-send limit can bound a $DIG amount.
    ///
    /// # Errors
    ///
    /// See [`CatTransferError`]. A $DIG shortfall, a missing XCH fee coin, an unprovable lineage and
    /// an unreachable chain are four different things to tell a user about their money.
    pub fn build_dig_transfer<C>(
        &self,
        chain: &C,
        custody: &CustodyPolicy,
        request: &CatTransferRequest,
        reservations: &CoinReservations<'_>,
    ) -> CatTransferResult<CatTransferPlan>
    where
        C: ChainSource + ?Sized,
    {
        self.build_cat_transfer(chain, custody, DIG_ASSET_ID, request, reservations)
    }

    /// Build the unsigned coin spends for a payment of ANY CAT out of this profile's wallet.
    ///
    /// [`build_dig_transfer`](Self::build_dig_transfer) is this method pinned to [`DIG_ASSET_ID`],
    /// and is what product code should call: it removes the one parameter a caller can get wrong.
    ///
    /// The general form is public because the mechanism genuinely is general — the CAT ring, the
    /// lineage walk and the XCH fee leg are identical for every asset — and because the in-process
    /// consensus simulator cannot issue mainnet's $DIG TAIL, so the end-to-end consensus proof has
    /// to run against an asset the simulator CAN issue. A seam that exists only for tests would be
    /// worse: it would be untested itself, and it would still be reachable.
    ///
    /// The lose-the-funds guard is unaffected. The destination is ALWAYS derived through
    /// [`cat_curried_puzzle_hash`] from `asset_id`; there is no way to hand this builder a raw
    /// pre-wrapped puzzle hash, whichever asset it is spending.
    ///
    /// # Errors
    ///
    /// See [`CatTransferError`]. Its messages name $DIG because that is the asset the product moves;
    /// they are accurate for any CAT read as "this asset".
    pub fn build_cat_transfer<C>(
        &self,
        chain: &C,
        custody: &CustodyPolicy,
        asset_id: Bytes32,
        request: &CatTransferRequest,
        reservations: &CoinReservations<'_>,
    ) -> CatTransferResult<CatTransferPlan>
    where
        C: ChainSource + ?Sized,
    {
        if let CustodyPolicy::Vault(_) = custody {
            return Err(CatTransferError::VaultTransferUnsupported);
        }
        if request.amount_base_units == 0 {
            return Err(CatTransferError::ZeroAmount);
        }

        let wallet = self.wallet_key();
        if request.recipient() == wallet.puzzle_hash() {
            return Err(CatTransferError::SelfPayment);
        }

        // BOTH legs are chosen and reserved in ONE atomic step. Reserving the $DIG and then
        // discovering the fee coin is gone would leave the user's $DIG held for the full TTL by a
        // spend that was never built — a reservation that locks the wallet out of its own funds is a
        // worse failure than the double-select it was added to prevent.
        let ((cat_coins, fee_coins), held) = select_and_reserve(
            reservations,
            MAX_RESERVATION_ATTEMPTS,
            |excluded| {
                let cat_coins = select_cat_coins(
                    chain,
                    asset_id,
                    wallet.puzzle_hash(),
                    request.amount_base_units,
                    excluded,
                )?;
                let fee_coins =
                    select_fee_coins(chain, wallet.puzzle_hash(), request.fee_mojos, excluded)?;
                let ids = cat_coins
                    .iter()
                    .chain(fee_coins.iter())
                    .map(Coin::coin_id)
                    .collect();
                Ok(((cat_coins, fee_coins), ids))
            },
            |e| CatTransferError::ReservationUnusable(e.to_string()),
        )?;
        // `held` stays alive across BOTH remaining fallible steps, and the first of them matters
        // most: `resolve_lineage` is a REMOTE read, answered by a peer this crate does not trust.
        // A source that answers the listings truthfully and then fails the parent-spend read would
        // otherwise orphan the reserved coins on every attempt — and because selection correctly
        // excludes what is held, each retry orphans a DIFFERENT coin, walking the whole wallet shut.
        let cats = resolve_lineage(chain, asset_id, &cat_coins)?;
        let mut plan = build_dig_transfer_spends(&wallet, &cats, &fee_coins, request, None)?;
        plan.reservation = Some(held.commit());
        Ok(plan)
    }
}

/// Pick the CAT coins to spend, fewest first, and confirm every one of them by name.
///
/// The selection rule is [`select_input_coins`](crate::wallet::transfer)'s, for the same reasons:
/// the smallest single coin that covers the amount, else largest-first accumulation. A dust attack
/// is as available against a CAT balance as against an XCH one.
fn select_cat_coins<C>(
    chain: &C,
    asset_id: Bytes32,
    p2_puzzle_hash: Bytes32,
    required: u64,
    reserved: &HashSet<Bytes32>,
) -> CatTransferResult<Vec<Coin>>
where
    C: ChainSource + ?Sized,
{
    let cat_puzzle_hash = cat_curried_puzzle_hash(asset_id, p2_puzzle_hash);
    let records = chain
        .coin_records_by_puzzle_hash(cat_puzzle_hash, false)
        .map_err(|e| CatTransferError::ChainUnreachable(e.to_string()))?;

    // Deduplicated by coin id BEFORE anything is totalled, for the reason spelled out in
    // `transfer::select_input_coins`: an aggregating source answering with one coin twice would
    // otherwise inflate the balance AND put two spends of one coin in the bundle.
    let mut seen: HashSet<Bytes32> = HashSet::with_capacity(records.len());
    let mut spendable: Vec<Coin> = Vec::with_capacity(records.len());
    for record in &records {
        if record.is_spent() || record.confirmed_height.is_none() {
            continue;
        }
        // A record at any other puzzle hash is not this wallet's $DIG. A hint-indexing source
        // returns coins an attacker chose, so this is exclusion rather than refusal — a stranger
        // must not be able to stop the wallet spending by hinting one coin at it.
        if record.coin.puzzle_hash != cat_puzzle_hash {
            continue;
        }
        if !seen.insert(record.coin.coin_id()) {
            continue;
        }
        spendable.push(record.coin);
    }
    spendable.sort_by_cached_key(|coin| (coin.amount, coin.coin_id()));

    let available = spendable
        .iter()
        .try_fold(0u64, |sum, coin| sum.checked_add(coin.amount))
        .ok_or(CatTransferError::BalanceUnjudgeable)?;
    if available < required {
        return Err(CatTransferError::InsufficientDig {
            required,
            available,
        });
    }

    // After the balance, never before it — the same ordering property `transfer::select_input_coins`
    // documents. A reserved CAT coin is still the user's $DIG.
    spendable.retain(|coin| !reserved.contains(&coin.coin_id()));
    let selectable = spendable
        .iter()
        .try_fold(0u64, |sum, coin| sum.checked_add(coin.amount))
        .ok_or(CatTransferError::BalanceUnjudgeable)?;
    if selectable < required {
        return Err(CatTransferError::CoinsReserved {
            required,
            available,
            selectable,
        });
    }

    let selected = if let Some(single) = spendable.iter().find(|coin| coin.amount >= required) {
        vec![*single]
    } else {
        let mut selected = Vec::new();
        let mut total = 0u64;
        for coin in spendable.iter().rev() {
            total = total.saturating_add(coin.amount);
            selected.push(*coin);
            if total >= required {
                break;
            }
        }
        if selected.len() > MAX_CAT_TRANSFER_INPUT_COINS {
            return Err(CatTransferError::TooManyInputCoins {
                needed: selected.len(),
                cap: MAX_CAT_TRANSFER_INPUT_COINS,
            });
        }
        selected
    };

    confirm_all_spendable_by_name(chain, &selected)?;
    Ok(selected)
}

/// Pick the XCH coins that will pay the fee, and confirm them by name.
///
/// A zero fee needs no XCH at all, and asking for one would refuse a send a $DIG-only wallet can
/// legitimately make.
fn select_fee_coins<C>(
    chain: &C,
    p2_puzzle_hash: Bytes32,
    fee_mojos: u64,
    reserved: &HashSet<Bytes32>,
) -> CatTransferResult<Vec<Coin>>
where
    C: ChainSource + ?Sized,
{
    if fee_mojos == 0 {
        return Ok(Vec::new());
    }

    let records = chain
        .coin_records_by_puzzle_hash(p2_puzzle_hash, false)
        .map_err(|e| CatTransferError::ChainUnreachable(e.to_string()))?;

    let mut seen: HashSet<Bytes32> = HashSet::with_capacity(records.len());
    let mut spendable: Vec<Coin> = Vec::new();
    for record in &records {
        if record.is_spent()
            || record.confirmed_height.is_none()
            || record.coin.puzzle_hash != p2_puzzle_hash
            || !seen.insert(record.coin.coin_id())
        {
            continue;
        }
        spendable.push(record.coin);
    }

    let available = spendable
        .iter()
        .try_fold(0u64, |sum, coin| sum.checked_add(coin.amount))
        .ok_or(CatTransferError::BalanceUnjudgeable)?;

    // ONE coin, the smallest that covers the fee. A fee is small and a multi-coin fee leg would add
    // inputs, signatures and bytes to a bundle that is already carrying a CAT ring.
    spendable.sort_by_cached_key(|coin| (coin.amount, coin.coin_id()));

    // Whether the wallet OWNS a coin big enough is answered before reservations are applied, and
    // the two answers are reported as different errors. "You have no XCH for the fee" and "your XCH
    // is busy for the next few minutes" are different things to tell a person, and only one of them
    // is fixed by depositing.
    let owns_a_covering_coin = spendable.iter().any(|coin| coin.amount >= fee_mojos);

    // The fee leg draws from the SAME XCH puzzle hash as an ordinary transfer, so an unreserved fee
    // coin here is selectable by `transfer::select_input_coins` at the same moment. That is why both
    // paths consult one reservation set rather than each keeping its own view.
    spendable.retain(|coin| !reserved.contains(&coin.coin_id()));
    let selectable = spendable
        .iter()
        .try_fold(0u64, |sum, coin| sum.checked_add(coin.amount))
        .ok_or(CatTransferError::BalanceUnjudgeable)?;

    let chosen = match spendable
        .iter()
        .find(|coin| coin.amount >= fee_mojos)
        .copied()
    {
        Some(coin) => coin,
        None if owns_a_covering_coin => {
            return Err(CatTransferError::CoinsReserved {
                required: fee_mojos,
                available,
                selectable,
            })
        }
        None => {
            return Err(CatTransferError::NoXchForFee {
                required: fee_mojos,
                available,
            })
        }
    };

    confirm_all_spendable_by_name(chain, std::slice::from_ref(&chosen))?;
    Ok(vec![chosen])
}

/// Turn each selected $DIG coin into a spendable [`Cat`], by reading and parsing its parent spend.
///
/// The lineage proof the CAT puzzle demands is not in a coin record; it is only derivable from the
/// spend that CREATED the coin. So each input costs one `parent_spend` read, and a parent that
/// cannot be read, cannot be parsed as a CAT, or does not actually create this coin is a refusal —
/// never a skip.
fn resolve_lineage<C>(chain: &C, asset_id: Bytes32, coins: &[Coin]) -> CatTransferResult<Vec<Cat>>
where
    C: ChainSource + ?Sized,
{
    let mut allocator = Allocator::new();
    let mut cats = Vec::with_capacity(coins.len());

    for coin in coins {
        let coin_id = coin.coin_id();
        let unprovable = |reason: String| CatTransferError::LineageUnavailable { coin_id, reason };

        let parent = chain
            .parent_spend(coin_id)
            .map_err(|e| CatTransferError::ChainUnreachable(e.to_string()))?
            .ok_or_else(|| unprovable("its parent spend is unknown to this source".into()))?;

        let puzzle_ptr = node_from_bytes(&mut allocator, &parent.puzzle_reveal)
            .map_err(|e| unprovable(format!("the parent puzzle reveal does not decode: {e}")))?;
        let solution_ptr = node_from_bytes(&mut allocator, &parent.solution)
            .map_err(|e| unprovable(format!("the parent solution does not decode: {e}")))?;
        let puzzle = Puzzle::parse(&allocator, puzzle_ptr);

        let children = Cat::parse_children(&mut allocator, parent.coin, puzzle, solution_ptr)
            .map_err(|e| unprovable(format!("the parent spend could not be parsed: {e}")))?
            .ok_or_else(|| unprovable("the parent coin is not a CAT".into()))?;

        // The child is matched BY COIN ID, never by position or by amount. The parent may create
        // several CAT children — a payment and change — and taking the wrong one would build a spend
        // of a coin the wallet does not hold, around a lineage proof that happens to verify.
        let cat = children
            .into_iter()
            .find(|child| child.coin.coin_id() == coin_id)
            .ok_or_else(|| {
                unprovable("the parent spend does not create this coin as a CAT child".into())
            })?;

        if cat.info.asset_id != asset_id {
            return Err(unprovable(format!(
                "it is a CAT of asset {} rather than the {} being spent",
                hex::encode(cat.info.asset_id),
                hex::encode(asset_id)
            )));
        }
        cats.push(cat);
    }

    Ok(cats)
}

/// Build the unsigned spends: the $DIG ring, plus an XCH coin carrying the fee.
///
/// # The CAT ring
///
/// CAT coins of one asset spent together form an announcement ring whose value must net to zero
/// across the whole ring. [`Cat::spend_all`] computes that ring — the subtotals and the neighbour
/// proofs — so this function only decides what each input's INNER (p2) spend says: the LEAD creates
/// the payment and the change, and every secondary creates nothing and contributes its value to the
/// ring.
///
/// # Change is computed exactly
///
/// A CAT ring that does not net to zero is rejected outright by the CAT puzzle, so a short change
/// figure here fails loudly rather than silently donating value — the opposite of the XCH case,
/// where the difference would quietly become a fee. It is still a checked subtraction over the very
/// coins selected, and the change coin is omitted when it is zero.
///
/// # The fee coin is bound to the ring
///
/// The XCH coin asserts a coin announcement made by the lead CAT, so it is not includable as a
/// free-standing donation of the user's XCH. As in the XCH builder, the aggregate signature is what
/// actually stops a stranger splitting the bundle; the announcement is defence in depth for shapes
/// where the aggregate is not the protection.
fn build_dig_transfer_spends(
    wallet: &WalletKey,
    cats: &[Cat],
    fee_coins: &[Coin],
    request: &CatTransferRequest,
    reservation: Option<ReservationId>,
) -> CatTransferResult<CatTransferPlan> {
    if cats.is_empty() {
        return Err(CatTransferError::Build(
            "no $DIG coins were selected".into(),
        ));
    }

    let total = cats
        .iter()
        .try_fold(0u64, |sum, cat| sum.checked_add(cat.coin.amount))
        .ok_or_else(|| CatTransferError::Build("the selected $DIG coins overflow u64".into()))?;
    let change = total
        .checked_sub(request.amount_base_units)
        .ok_or_else(|| {
            CatTransferError::Build("the selected $DIG coins do not cover the transfer".into())
        })?;

    let mut ctx = SpendContext::new();
    let layer = StandardLayer::new(wallet.public_key());

    // The recipient's hint is what makes the paid CAT discoverable by its owner's wallet. Without
    // it the coin exists and is theirs, and no wallet in the ecosystem will ever show it to them.
    let recipient_hint = ctx
        .hint(request.recipient())
        .map_err(|e| CatTransferError::Build(format!("recipient hint: {e}")))?;
    let change_hint = ctx
        .hint(wallet.puzzle_hash())
        .map_err(|e| CatTransferError::Build(format!("change hint: {e}")))?;

    let mut lead_conditions = Conditions::new().create_coin(
        request.recipient(),
        request.amount_base_units,
        recipient_hint,
    );
    if change > 0 {
        lead_conditions = lead_conditions.create_coin(wallet.puzzle_hash(), change, change_hint);
    }
    if !fee_coins.is_empty() {
        lead_conditions =
            lead_conditions.create_coin_announcement(FEE_BINDING_MESSAGE.to_vec().into());
    }

    let mut cat_spends = Vec::with_capacity(cats.len());
    for (index, cat) in cats.iter().enumerate() {
        let is_lead = index == cats.len() - 1;
        let conditions = if is_lead {
            lead_conditions.clone()
        } else {
            // A secondary creates nothing: its whole value flows into the ring, which the lead's
            // outputs consume. `Cat::spend_all` is what makes that net to zero.
            Conditions::new()
        };
        let spend = layer
            .spend_with_conditions(&mut ctx, conditions)
            .map_err(|e| CatTransferError::Build(format!("$DIG input {index}: {e}")))?;
        cat_spends.push(CatSpend::new(*cat, spend));
    }

    Cat::spend_all(&mut ctx, &cat_spends)
        .map_err(|e| CatTransferError::Build(format!("the $DIG ring: {e}")))?;

    let lead_coin_id = cats[cats.len() - 1].coin.coin_id();
    let binding = chia_wallet_sdk::types::announcement_id(lead_coin_id, FEE_BINDING_MESSAGE);
    let fee_total = fee_coins
        .iter()
        .try_fold(0u64, |sum, coin| sum.checked_add(coin.amount))
        .ok_or_else(|| CatTransferError::Build("the fee coins overflow u64".into()))?;
    if !fee_coins.is_empty() {
        let fee_change = fee_total
            .checked_sub(request.fee_mojos)
            .ok_or_else(|| CatTransferError::Build("the fee coins do not cover the fee".into()))?;
        for (index, coin) in fee_coins.iter().enumerate() {
            let mut conditions = Conditions::new().assert_coin_announcement(binding);
            // Only ONE fee coin carries the fee reserve and the change, so the two are never
            // double-counted across a multi-coin fee leg.
            if index == 0 {
                if request.fee_mojos > 0 {
                    conditions = conditions.reserve_fee(request.fee_mojos);
                }
                if fee_change > 0 {
                    conditions =
                        conditions.create_coin(wallet.puzzle_hash(), fee_change, Memos::None);
                }
            }
            layer
                .spend(&mut ctx, *coin, conditions)
                .map_err(|e| CatTransferError::Build(format!("fee input {index}: {e}")))?;
        }
    }

    Ok(CatTransferPlan {
        coin_spends: ctx.take(),
        dig_source_coin_ids: cats.iter().map(|cat| cat.coin.coin_id()).collect(),
        xch_source_coin_ids: fee_coins.iter().map(Coin::coin_id).collect(),
        recipient: request.recipient(),
        amount_base_units: request.amount_base_units,
        fee_mojos: request.fee_mojos,
        change_base_units: change,
        reservation,
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use dig_chainsource_interface::CoinRecord;

    /// A chain source over a fixed record set. Records are supplied whole, not as bare coins,
    /// because confirmed / unconfirmed / spent are the distinctions these selectors are made of.
    #[derive(Debug)]
    struct FixedChain {
        records: Vec<CoinRecord>,
    }

    impl ChainSource for FixedChain {
        type Error = String;

        fn coin_record(&self, coin_id: Bytes32) -> std::result::Result<Option<CoinRecord>, String> {
            Ok(self
                .records
                .iter()
                .find(|record| record.coin.coin_id() == coin_id)
                .cloned())
        }

        fn coin_records_by_puzzle_hash(
            &self,
            puzzle_hash: Bytes32,
            _include_spent: bool,
        ) -> std::result::Result<Vec<CoinRecord>, String> {
            Ok(self
                .records
                .iter()
                .filter(|record| record.coin.puzzle_hash == puzzle_hash)
                .cloned()
                .collect())
        }

        fn coin_records_by_parent(
            &self,
            _parent: Bytes32,
        ) -> std::result::Result<Vec<CoinRecord>, String> {
            Ok(Vec::new())
        }

        fn coin_spend(&self, _coin_id: Bytes32) -> std::result::Result<Option<CoinSpend>, String> {
            Ok(None)
        }

        fn resolve_singleton_lineage(
            &self,
            _launcher_id: Bytes32,
        ) -> std::result::Result<Option<dig_chainsource_interface::SingletonLineage>, String>
        {
            Err("not supported by this test double".to_string())
        }

        fn peak_height(&self) -> std::result::Result<Option<u32>, String> {
            Ok(Some(4_200_000))
        }

        fn block_timestamp(&self, _height: u32) -> std::result::Result<Option<u64>, String> {
            Ok(None)
        }
    }

    fn confirmed(coin: Coin) -> CoinRecord {
        CoinRecord {
            coin,
            confirmed_height: Some(4_100_000),
            spent_height: None,
            timestamp: None,
            coinbase: false,
        }
    }

    const P2: Bytes32 = Bytes32::new([0x11; 32]);

    fn xch(tag: u8, amount: u64) -> Coin {
        Coin::new(Bytes32::new([tag; 32]), P2, amount)
    }

    /// **The $DIG fee leg and an ordinary XCH transfer draw from the SAME coins, so they must share
    /// one reservation set.**
    ///
    /// This is the rival-implementation property. Two selection paths over one puzzle hash, each
    /// keeping its own idea of what is in flight, double-select by construction — which is exactly
    /// the defect this family exists to close, reappearing between two paths instead of between two
    /// calls of one path.
    ///
    /// The fixture keeps a second, larger coin as the truthful control: the fee leg must still
    /// SUCCEED on it, so a blanket refusal cannot pass this test.
    #[test]
    fn the_fee_leg_will_not_take_an_xch_coin_an_ordinary_transfer_reserved() {
        let taken = xch(1, 1_000);
        let spare = xch(2, 2_000);
        let chain = FixedChain {
            records: vec![confirmed(taken), confirmed(spare)],
        };

        let control = select_fee_coins(&chain, P2, 500, &HashSet::new())
            .expect("the control: unreserved, the smallest sufficient coin pays the fee");
        assert_eq!(control[0].coin_id(), taken.coin_id());

        let reserved: HashSet<Bytes32> = [taken.coin_id()].into_iter().collect();
        let chosen = select_fee_coins(&chain, P2, 500, &reserved)
            .expect("a $DIG send can still pay its fee from the coin that is free");
        assert_eq!(
            chosen[0].coin_id(),
            spare.coin_id(),
            "an XCH coin an in-flight transfer holds must not also pay a CAT fee"
        );
    }

    /// **A busy fee coin is reported as busy, never as an absence of XCH.**
    ///
    /// `NoXchForFee` tells a user to acquire XCH. Saying that to somebody who HAS the XCH — it is
    /// simply committed for the next few minutes — sends them to an exchange to solve a wait.
    #[test]
    fn a_reserved_fee_coin_is_not_reported_as_missing_xch() {
        let only = xch(1, 1_000);
        let chain = FixedChain {
            records: vec![confirmed(only)],
        };
        let reserved: HashSet<Bytes32> = [only.coin_id()].into_iter().collect();

        let error = select_fee_coins(&chain, P2, 500, &reserved)
            .expect_err("the only XCH coin is committed elsewhere");

        match error {
            CatTransferError::CoinsReserved {
                available,
                selectable,
                ..
            } => {
                assert_eq!(available, 1_000, "the XCH is still the user's money");
                assert_eq!(selectable, 0);
            }
            other => panic!("a busy fee coin is not missing XCH: {other:?}"),
        }
    }

    /// **A zero fee reserves nothing.** A $DIG-only wallet can legitimately send with no fee, and a
    /// fee leg that reserved a coin it was never going to spend would hold funds for no reason.
    #[test]
    fn a_zero_fee_selects_and_reserves_no_xch_at_all() {
        let chain = FixedChain {
            records: vec![confirmed(xch(1, 1_000))],
        };
        assert!(select_fee_coins(&chain, P2, 0, &HashSet::new())
            .expect("a zero fee needs no XCH")
            .is_empty());
    }
}