Skip to main content

dig_node_control_interface/
params.rs

1//! Typed request params for the control methods, each bound to its method + result via
2//! [`crate::traits::ControlCall`].
3//!
4//! One params type per method (even where two methods share the same field shape, e.g. the four
5//! `{ store }` methods) so the compile-time method↔params↔result binding is exact: a caller passes
6//! `PinParams { store }` and the type system yields a [`PinResult`](crate::results::PinResult).
7//! Field names are the exact wire names dig-node reads.
8
9use serde::{Deserialize, Serialize};
10
11use crate::error::{ControlError, ControlErrorCode};
12use crate::method::ControlMethod;
13use crate::results;
14use crate::traits::ControlCall;
15
16/// Bind a params type to its wire method + typed result.
17macro_rules! control_call {
18    ($ty:ty => $method:expr, $out:ty) => {
19        impl ControlCall for $ty {
20            const METHOD: ControlMethod = $method;
21            type Output = $out;
22        }
23    };
24}
25
26/// Define a no-param call: an empty params struct (serializes to `{}`) bound to its method + result.
27macro_rules! no_params {
28    ($(#[$doc:meta])* $name:ident => $method:expr, $out:ty) => {
29        $(#[$doc])*
30        #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
31        pub struct $name {}
32        control_call!($name => $method, $out);
33    };
34}
35
36no_params!(
37    /// `control.status` params (none).
38    StatusParams => ControlMethod::Status, results::StatusResult
39);
40no_params!(
41    /// `control.config.get` params (none).
42    ConfigGetParams => ControlMethod::ConfigGet, results::ConfigResult
43);
44no_params!(
45    /// `control.cache.get` params (none).
46    CacheGetParams => ControlMethod::CacheGet, results::CacheView
47);
48no_params!(
49    /// `control.cache.clear` params (none).
50    CacheClearParams => ControlMethod::CacheClear, results::CacheClearResult
51);
52no_params!(
53    /// `control.hostedStores.list` params (none).
54    HostedStoresListParams => ControlMethod::HostedStoresList, results::HostedStoresListResult
55);
56no_params!(
57    /// `control.sync.status` params (none).
58    SyncStatusParams => ControlMethod::SyncStatus, results::SyncStatusResult
59);
60no_params!(
61    /// `control.updater.status` params (none). Result is the proxied beacon status.
62    UpdaterStatusParams => ControlMethod::UpdaterStatus, serde_json::Value
63);
64no_params!(
65    /// `control.updater.resume` params (none).
66    UpdaterResumeParams => ControlMethod::UpdaterResume, serde_json::Value
67);
68no_params!(
69    /// `control.updater.checkNow` params (none).
70    UpdaterCheckNowParams => ControlMethod::UpdaterCheckNow, serde_json::Value
71);
72no_params!(
73    /// `control.pairing.list` params (none). Result is the pending + issued-token list.
74    PairingListParams => ControlMethod::PairingList, serde_json::Value
75);
76no_params!(
77    /// `control.peerStatus` params (none). Result is the peer-pool snapshot.
78    PeerStatusParams => ControlMethod::PeerStatus, serde_json::Value
79);
80no_params!(
81    /// `control.listSubscriptions` params (none).
82    ListSubscriptionsParams => ControlMethod::ListSubscriptions, results::ListSubscriptionsResult
83);
84
85/// `control.config.setUpstream` params.
86#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
87pub struct SetUpstreamParams {
88    /// The upstream DIG RPC URL to persist (blank clears the override).
89    pub upstream: String,
90}
91control_call!(SetUpstreamParams => ControlMethod::ConfigSetUpstream, results::SetUpstreamResult);
92
93/// `control.log.setLevel` params.
94#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
95pub struct SetLevelParams {
96    /// An `EnvFilter` directive, e.g. `"debug"` or `"info,dig_node_core=debug"`.
97    pub filter: String,
98}
99control_call!(SetLevelParams => ControlMethod::LogSetLevel, results::SetLevelResult);
100
101/// `control.cache.setCap` params.
102#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
103pub struct SetCapParams {
104    /// The cache size cap in bytes (floored at 64 MiB by the node).
105    pub cap_bytes: u64,
106}
107control_call!(SetCapParams => ControlMethod::CacheSetCap, results::SetCapResult);
108
109/// `control.hostedStores.pin` params.
110#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
111pub struct PinParams {
112    /// A store reference: `storeId` or `storeId:rootHash`.
113    pub store: String,
114}
115control_call!(PinParams => ControlMethod::HostedStoresPin, results::PinResult);
116
117/// `control.hostedStores.unpin` params.
118#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
119pub struct UnpinParams {
120    /// A store reference: `storeId` or `storeId:rootHash`.
121    pub store: String,
122}
123control_call!(UnpinParams => ControlMethod::HostedStoresUnpin, results::UnpinResult);
124
125/// `control.hostedStores.status` params.
126#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
127pub struct HostedStoreStatusParams {
128    /// A store reference: `storeId` or `storeId:rootHash`.
129    pub store: String,
130}
131control_call!(HostedStoreStatusParams => ControlMethod::HostedStoresStatus, results::HostedStoreStatusResult);
132
133/// `control.capsule.fetch` params: WHICH capsule to pull, named by store id + root — the same
134/// two-field shape as [`ProfileGetBodyParams`], not the colon-joined `store` string
135/// [`PinParams`]/[`SyncTriggerParams`] use, because the caller always knows both parts already
136/// (there is no store-level-only fetch).
137#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
138pub struct CapsuleFetchParams {
139    /// The store id to fetch a capsule of.
140    pub store: String,
141    /// The capsule root to fetch.
142    pub root: String,
143}
144control_call!(CapsuleFetchParams => ControlMethod::CapsuleFetch, results::CapsuleFetchResult);
145
146/// `control.sync.trigger` params.
147#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
148pub struct SyncTriggerParams {
149    /// A capsule reference: `storeId:rootHash` (a concrete root is required).
150    pub store: String,
151}
152control_call!(SyncTriggerParams => ControlMethod::SyncTrigger, results::SyncTriggerResult);
153
154/// `control.updater.setChannel` params.
155#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
156pub struct SetChannelParams {
157    /// The update channel (`"nightly"` | `"stable"`; the beacon CLI is the sole validator).
158    pub channel: String,
159}
160control_call!(SetChannelParams => ControlMethod::UpdaterSetChannel, serde_json::Value);
161
162/// `control.updater.pause` params.
163#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
164pub struct PauseParams {
165    /// The unix-seconds time to pause until; omit to pause indefinitely.
166    #[serde(skip_serializing_if = "Option::is_none", default)]
167    pub until: Option<u64>,
168}
169control_call!(PauseParams => ControlMethod::UpdaterPause, serde_json::Value);
170
171/// `control.pairing.approve` params.
172#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
173pub struct ApproveParams {
174    /// The pending pairing's id (from `pairing.request`).
175    pub pairing_id: String,
176}
177control_call!(ApproveParams => ControlMethod::PairingApprove, results::PairingApproveResult);
178
179/// `control.pairing.revoke` params.
180#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
181pub struct RevokeParams {
182    /// The short id of the paired token to revoke.
183    pub token_id: String,
184}
185control_call!(RevokeParams => ControlMethod::PairingRevoke, results::PairingRevokeResult);
186
187/// `control.peers.connect` params.
188#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
189pub struct PeersConnectParams {
190    /// A peer address to dial, or an already-connected peer_id to resolve.
191    pub peer: String,
192}
193control_call!(PeersConnectParams => ControlMethod::PeersConnect, results::PeersConnectResult);
194
195/// `control.peers.disconnect` params.
196#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
197pub struct PeersDisconnectParams {
198    /// The peer_id to drop.
199    pub peer: String,
200}
201control_call!(PeersDisconnectParams => ControlMethod::PeersDisconnect, results::PeersDisconnectResult);
202
203/// The CANONICAL textual form of a Chia peer address, or an `INVALID_PARAMS` error.
204///
205/// Every `ip` this crate declares — in `control.chiaPeers.add` / `.remove` params and in every
206/// result that echoes one — is a bare IP literal in this form. A conforming node MUST canonicalise
207/// through this function on the way IN and store the result, so `add`, `remove` and `list` all
208/// spell the same peer the same way.
209///
210/// **Why the contract owns this rather than each implementation.** `remove` is the only way to
211/// un-trust a peer that is believed WITHOUT corroboration, and it matches by address. If the form
212/// is left to the implementation, an operator who adds `2001:db8::1` and removes `2001:DB8:0:0::1`
213/// has named the same peer twice and un-trusted nothing — an un-trust that silently does not
214/// happen. Canonicalising is what makes the two spellings one key.
215///
216/// The rules, normatively:
217///
218/// - the value MUST parse as an IPv4 or IPv6 literal ([`std::net::IpAddr`]). A hostname, an empty
219///   string, an `ip:port`, a CIDR block or a bracketed `[..]` form is REJECTED. Rejecting
220///   non-literals is also what bounds the ban list: `remove {ban: true}` persists a row keyed by
221///   this string, so an unvalidated key is unbounded at-rest growth driven by one small call;
222/// - surrounding whitespace is trimmed before parsing, and nothing else is;
223/// - the canonical rendering is [`std::net::IpAddr`]'s own `Display` — dotted-quad for v4, and for
224///   v6 the RFC 5952 lowercase, maximally-compressed form, WITHOUT brackets and WITHOUT a zone id.
225///
226/// Bracketing belongs to the socket-address form, never to this field: see
227/// [`chia_peer_endpoint`], which is the ONLY sanctioned way to join an `ip` to a port.
228///
229/// ```
230/// use dig_node_control_interface::params::canonical_peer_ip;
231/// assert_eq!(canonical_peer_ip(" 2001:0DB8:0000::1 ").unwrap(), "2001:db8::1");
232/// assert!(canonical_peer_ip("[::1]").is_err());
233/// assert!(canonical_peer_ip("node.example.com").is_err());
234/// ```
235pub fn canonical_peer_ip(raw: &str) -> Result<String, ControlError> {
236    raw.trim()
237        .parse::<std::net::IpAddr>()
238        .map(|ip| ip.to_string())
239        .map_err(|_| {
240            ControlError::of(
241                ControlErrorCode::InvalidParams,
242                format!(
243                    "ip must be a bare IPv4 or IPv6 literal (no brackets, no port, no hostname), got: {raw:?}"
244                ),
245            )
246        })
247}
248
249/// Join a canonical peer `ip` to a port, bracketing IPv6 as RFC 3986 requires.
250///
251/// The one place a peer address and a port are ever concatenated. Formatting `"{ip}:{port}"` by
252/// hand is the bug this exists to prevent: `::1` and `8444` render as `::1:8444`, which is not a
253/// malformed string a parser rejects — it is a DIFFERENT valid IPv6 address, so the mistake
254/// survives validation and silently retargets the peer.
255///
256/// ```
257/// use dig_node_control_interface::params::chia_peer_endpoint;
258/// assert_eq!(chia_peer_endpoint("::1", 8444), "[::1]:8444");
259/// assert_eq!(chia_peer_endpoint("203.0.113.7", 8444), "203.0.113.7:8444");
260/// ```
261pub fn chia_peer_endpoint(ip: &str, port: u16) -> String {
262    match ip.trim().parse::<std::net::IpAddr>() {
263        Ok(std::net::IpAddr::V6(v6)) => format!("[{v6}]:{port}"),
264        Ok(std::net::IpAddr::V4(v4)) => format!("{v4}:{port}"),
265        Err(_) => format!("{ip}:{port}"),
266    }
267}
268
269/// The maximum number of BANNED Chia peers a conforming node persists.
270///
271/// A ban is a row written at the request of one small control call and kept across restarts, so
272/// without a ceiling the blocklist is unbounded at-rest state a caller can grow for free. On
273/// overflow a node MUST evict its OLDEST ban rather than refuse the newest — a bounded list that
274/// forgets is recoverable, and a full one that refuses turns the ceiling into a denial of the ban
275/// facility itself.
276///
277/// The value is deliberately generous against the honest use (a handful of misbehaving peers) and
278/// small against the abusive one. Banned entries are enumerable through `control.chiaPeers.list`
279/// and clearable through `control.chiaPeers.remove` with `ban: false`.
280pub const MAX_BANNED_CHIA_PEERS: usize = 256;
281
282/// `control.chiaPeers.add` params — the Chia full node to start TRUSTING.
283///
284/// Trust is the whole point of this call and it is not free: a trusted peer is exempted from the
285/// corroboration this node otherwise requires (NC-12 — dialled peers are untrusted, and agreement
286/// across several concurrently-queried peers is what makes a chain read safe). An implementation
287/// MUST tell the person that before it writes the entry.
288///
289/// The trust NC-12 authorises is the operator declaring a node THEIR OWN. That is what justifies
290/// the unbounded authority the entry carries, and it does not extend to a node somebody else runs.
291#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
292pub struct ChiaPeersAddParams {
293    /// The peer's IP address, canonical per [`canonical_peer_ip`] — a bare literal, no brackets,
294    /// no port. The standard full-node port is assumed (the Sage `add_peer` request shape carries
295    /// no port either).
296    pub ip: String,
297}
298control_call!(ChiaPeersAddParams => ControlMethod::ChiaPeersAdd, results::ChiaPeersAddResult);
299
300/// `control.chiaPeers.remove` params — the Chia full node to stop trusting.
301#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
302pub struct ChiaPeersRemoveParams {
303    /// The peer's IP address, canonical per [`canonical_peer_ip`]. Matching is by that canonical
304    /// form, so an address spelled differently from the stored entry still names the same peer.
305    pub ip: String,
306    /// Ban rather than forget: the peer is kept but excluded, so discovery cannot re-add it.
307    /// Absent means `false`, which merely forgets the entry.
308    ///
309    /// A ban is EXACT-match on this one address — never a subnet — and it is local to this node,
310    /// with no effect on any other node's peer set. It persists until removed, bounded by
311    /// [`MAX_BANNED_CHIA_PEERS`], is enumerated by `control.chiaPeers.list` (`banned: true`), and
312    /// is cleared by calling `remove` again with `ban: false`. **Clearing a ban that way grants no
313    /// trust**: `add` also un-bans, but `add` confers the corroboration bypass, so it must not be
314    /// the only route back — an over-broad ban is not a reason to trust the peer it hit.
315    ///
316    /// Every banned peer is one fewer source of agreement. NC-12 rests on several independently
317    /// chosen peers agreeing, so a caller that bans steadily shrinks the honest pool toward the
318    /// peers it chose; that is why banning is on the master-token tier
319    /// ([`ControlMethod::requires_master_token`]) and why the set is bounded and visible.
320    #[serde(default)]
321    pub ban: bool,
322}
323control_call!(ChiaPeersRemoveParams => ControlMethod::ChiaPeersRemove, results::ChiaPeersRemoveResult);
324
325/// WHAT a subscription follows: ordinary content, or a dig-profile.
326///
327/// Serializes to a lowercase, language-neutral wire token (`"capsule"` / `"profile"`).
328///
329/// # Absent means [`Capsule`](SubscriptionKind::Capsule), and that is a contract, not a convenience
330///
331/// Every subscription written before this field existed is untagged, and a node's
332/// `subscriptions.json` on a real machine is FULL of them. A required `kind` would make those rows
333/// fail to deserialize, and a node that cannot read its own subscription file starts with an empty
334/// one — an upgrade that silently unsubscribes a user from everything they had. So the field is
335/// `#[serde(default)]` everywhere it appears, and the default is the meaning those untagged rows
336/// already had: a capsule.
337#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
338#[serde(rename_all = "lowercase")]
339pub enum SubscriptionKind {
340    /// Ordinary store content — the meaning every untagged subscription already carries.
341    #[default]
342    Capsule,
343    /// A dig-profile: the node additionally follows the profile ROOT and syncs its body from peers.
344    Profile,
345}
346
347/// `control.subscribe` params.
348#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
349pub struct SubscribeParams {
350    /// The store id to subscribe to.
351    pub store_id: String,
352    /// What the subscription follows. OMITTED means [`SubscriptionKind::Capsule`], so an older
353    /// client's request and an untagged on-disk row both keep their existing meaning.
354    #[serde(default)]
355    pub kind: SubscriptionKind,
356}
357control_call!(SubscribeParams => ControlMethod::Subscribe, results::SubscribeResult);
358
359/// `control.unsubscribe` params.
360#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
361pub struct UnsubscribeParams {
362    /// The store id to stop watching.
363    pub store_id: String,
364}
365control_call!(UnsubscribeParams => ControlMethod::Unsubscribe, results::UnsubscribeResult);
366
367/// The length of a CAT asset id (TAIL hash) in hex characters: a 32-byte hash.
368pub const ASSET_ID_HEX_LEN: usize = 64;
369
370/// A CAT asset id — the TAIL hash that names one token on the Chia chain.
371///
372/// Stored as the 32 raw bytes rather than a `String` so two spellings of the same id (uppercase,
373/// `0x`-prefixed) cannot compare unequal. Parsing normalizes; emission is always lowercase and
374/// unprefixed.
375#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
376pub struct AssetId([u8; 32]);
377
378/// Why an asset id could not be parsed. Deliberately small — an id is either 32 bytes of hex or it
379/// is not an id, and guessing at a near-miss is how a wallet ends up reading the wrong token.
380#[derive(Debug, Clone, Copy, PartialEq, Eq)]
381pub enum AssetIdParseError {
382    /// Not exactly [`ASSET_ID_HEX_LEN`] hex characters (after an optional `0x` is stripped).
383    WrongLength {
384        /// The length actually supplied.
385        got: usize,
386    },
387    /// A character outside `[0-9a-fA-F]`.
388    NotHex,
389}
390
391impl core::fmt::Display for AssetIdParseError {
392    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
393        match self {
394            Self::WrongLength { got } => write!(
395                f,
396                "asset id must be {ASSET_ID_HEX_LEN} hex characters, got {got}"
397            ),
398            Self::NotHex => f.write_str("asset id contains a non-hexadecimal character"),
399        }
400    }
401}
402
403impl std::error::Error for AssetIdParseError {}
404
405impl AssetId {
406    /// Wrap 32 already-decoded bytes. `const` so canonical ids can be declared as constants.
407    pub const fn new(bytes: [u8; 32]) -> Self {
408        Self(bytes)
409    }
410
411    /// The raw 32 bytes.
412    pub const fn as_bytes(&self) -> &[u8; 32] {
413        &self.0
414    }
415
416    /// Parse a hex asset id. A `0x` prefix and uppercase digits are TOLERATED on input — both are
417    /// what a block explorer prints — and normalized away; neither is ever emitted.
418    pub fn from_hex(hex: &str) -> Result<Self, AssetIdParseError> {
419        let body = hex.strip_prefix("0x").unwrap_or(hex);
420        if body.len() != ASSET_ID_HEX_LEN {
421            return Err(AssetIdParseError::WrongLength { got: body.len() });
422        }
423        let mut bytes = [0u8; 32];
424        for (byte, pair) in bytes.iter_mut().zip(body.as_bytes().chunks_exact(2)) {
425            let hi = decode_hex_digit(pair[0])?;
426            let lo = decode_hex_digit(pair[1])?;
427            *byte = (hi << 4) | lo;
428        }
429        Ok(Self(bytes))
430    }
431
432    /// The canonical wire spelling: lowercase, unprefixed, [`ASSET_ID_HEX_LEN`] characters.
433    pub fn to_hex(&self) -> String {
434        use core::fmt::Write as _;
435        self.0
436            .iter()
437            .fold(String::with_capacity(ASSET_ID_HEX_LEN), |mut acc, byte| {
438                let _ = write!(acc, "{byte:02x}");
439                acc
440            })
441    }
442}
443
444/// Decode one ASCII hex digit into its nibble value.
445fn decode_hex_digit(c: u8) -> Result<u8, AssetIdParseError> {
446    match c {
447        b'0'..=b'9' => Ok(c - b'0'),
448        b'a'..=b'f' => Ok(c - b'a' + 10),
449        b'A'..=b'F' => Ok(c - b'A' + 10),
450        _ => Err(AssetIdParseError::NotHex),
451    }
452}
453
454impl core::fmt::Display for AssetId {
455    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
456        f.write_str(&self.to_hex())
457    }
458}
459
460/// The asset a wallet balance/coin read is denominated in: native XCH, or any CAT by asset id.
461///
462/// # Why there is no separate `Dig` variant
463///
464/// $DIG is a CAT, so it is [`Asset::DIG`] — an associated constant, not a variant. A three-variant
465/// `{Xch, Dig, Cat(id)}` would give $DIG two INEQUAL spellings, and a balance or coin list filtered
466/// by one of them would silently omit everything carrying the other: a wallet reporting half a
467/// balance as though it were the whole. One token, one value.
468///
469/// # Wire form (additive — CLAUDE.md §5.1)
470///
471/// | Value | JSON |
472/// |---|---|
473/// | [`Asset::Xch`] | `"xch"` |
474/// | [`Asset::DIG`] | `"dig"` |
475/// | any other CAT | `{"cat":"<64-hex>"}` |
476///
477/// Both legacy tokens are still ACCEPTED and `"dig"` is still EMITTED, so a node or client built
478/// before this release keeps understanding, and being understood by, one built after it.
479/// `{"cat":"<the $DIG asset id>"}` is also accepted and normalizes to [`Asset::DIG`].
480///
481/// Byte-identical to dig-app's consumer type (`dig-app-core::wallet::state::Asset`), recorded in the
482/// `canonical` skill so the two implementations cannot drift.
483#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
484pub enum Asset {
485    /// Native Chia (XCH), denominated in mojos.
486    Xch,
487    /// A CAT, named by its asset id and denominated in its own base units.
488    Cat(AssetId),
489}
490
491/// The legacy wire token for native Chia.
492const XCH_TOKEN: &str = "xch";
493/// The legacy wire token for $DIG, still emitted so older peers keep understanding this crate.
494const DIG_TOKEN: &str = "dig";
495
496impl Asset {
497    /// Canonical $DIG CAT asset id (TAIL hash) on Chia mainnet, as lowercase hex.
498    ///
499    /// CONTRACT: byte-identical to `dig_constants::DIG_ASSET_ID`, `chip35_dl_coin::DIG_ASSET_ID`,
500    /// and digstore-chain's. It is duplicated here rather than imported because this crate is a
501    /// level-00 foundation crate and may not depend sideways on `dig-constants` (CLAUDE.md
502    /// Appendix B). `dig_asset_id_hex_and_bytes_are_the_same_id` pins THIS crate's two spellings
503    /// of it together; agreement with the ecosystem constant is a source-of-truth obligation on
504    /// whoever changes it, recorded in the `canonical` skill.
505    pub const DIG_ASSET_ID_HEX: &'static str =
506        "a406d3a9de984d03c9591c10d917593b434d5263cabe2b42f6b367df16832f81";
507
508    /// $DIG, the CAT every capsule payment is denominated in.
509    pub const DIG: Asset = Asset::Cat(AssetId::new([
510        0xa4, 0x06, 0xd3, 0xa9, 0xde, 0x98, 0x4d, 0x03, 0xc9, 0x59, 0x1c, 0x10, 0xd9, 0x17, 0x59,
511        0x3b, 0x43, 0x4d, 0x52, 0x63, 0xca, 0xbe, 0x2b, 0x42, 0xf6, 0xb3, 0x67, 0xdf, 0x16, 0x83,
512        0x2f, 0x81,
513    ]));
514
515    /// Whether this asset is $DIG, however it was spelled on the wire.
516    pub fn is_dig(&self) -> bool {
517        *self == Self::DIG
518    }
519
520    /// The CAT asset id, or `None` for native XCH.
521    pub fn asset_id(&self) -> Option<&AssetId> {
522        match self {
523            Self::Xch => None,
524            Self::Cat(id) => Some(id),
525        }
526    }
527}
528
529impl Serialize for Asset {
530    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
531        match self {
532            Self::Xch => serializer.serialize_str(XCH_TOKEN),
533            // $DIG keeps its legacy token: a node built before the widening understands only the
534            // two bare strings, and emitting the tagged form for it would break that direction.
535            other if other.is_dig() => serializer.serialize_str(DIG_TOKEN),
536            Self::Cat(id) => {
537                use serde::ser::SerializeMap as _;
538                let mut map = serializer.serialize_map(Some(1))?;
539                map.serialize_entry("cat", &id.to_hex())?;
540                map.end()
541            }
542        }
543    }
544}
545
546impl<'de> Deserialize<'de> for Asset {
547    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
548        /// The two accepted wire shapes, kept private so the tagged form is the ONLY map that
549        /// parses — an unknown key must be an error, never a silently ignored asset name.
550        #[derive(Deserialize)]
551        #[serde(untagged, deny_unknown_fields)]
552        enum Wire {
553            Token(String),
554            Tagged { cat: String },
555        }
556
557        match Wire::deserialize(deserializer).map_err(|_| {
558            serde::de::Error::custom(
559                "expected \"xch\", \"dig\", or {\"cat\":\"<64-hex asset id>\"}",
560            )
561        })? {
562            Wire::Token(token) if token == XCH_TOKEN => Ok(Self::Xch),
563            Wire::Token(token) if token == DIG_TOKEN => Ok(Self::DIG),
564            Wire::Token(token) => Err(serde::de::Error::custom(format!(
565                "unknown asset {token:?}: expected \"xch\", \"dig\", or {{\"cat\":\"<64-hex asset id>\"}}"
566            ))),
567            Wire::Tagged { cat } => AssetId::from_hex(&cat)
568                .map(Self::Cat)
569                .map_err(serde::de::Error::custom),
570        }
571    }
572}
573
574/// `control.wallet.balance` params: which address + asset to read the balance of.
575///
576/// A READ over the loopback control plane — never a spend. Field names + the [`Asset`] wire form are
577/// byte-identical to dig-app's frozen `BalanceRequest`, so the node reads exactly what dig-app emits.
578#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
579pub struct WalletBalanceParams {
580    /// The `xch1…` address to read the balance of.
581    pub address: String,
582    /// The asset to read the balance for.
583    pub asset: Asset,
584}
585control_call!(WalletBalanceParams => ControlMethod::WalletBalance, results::WalletBalanceResult);
586
587/// `control.wallet.coins` params: which address + asset to read spendable coins for, and which
588/// PAGE of them.
589///
590/// The address + asset pair is byte-identical to dig-app's frozen `CoinsRequest` and to
591/// [`WalletBalanceParams`] — a balance is this read reduced to a sum — so the paging fields are
592/// purely additive and a caller that names neither asks exactly what it asked before.
593///
594/// # Bounded, because an address's coin count is not (dig-node#381)
595///
596/// A funded address accumulates coins without limit, and every change coin a spend produces adds
597/// one. An unpaged read therefore has unbounded cardinality on the same loopback control plane that
598/// has NO request rate limiting of any kind (dig_ecosystem#2577) — the identical exposure
599/// [`WalletCoinsByParentParams`] documents at length, for the identical reason, and on the fallback
600/// tier the work lands on a third-party coinset oracle rather than on this node.
601///
602/// Paged rather than capped, for the reason its sibling records: a bare cap is a dead end, because
603/// an address holding more coins than the cap could never be fully enumerated, and this read exists
604/// so a caller can BUILD A SPEND from the coins it names. A spend built from a silently truncated
605/// coin set refuses with a shortfall that is not true.
606///
607/// # The paging rules are the sibling's rules, deliberately
608///
609/// ASCENDING `coin_id`, a cursor the caller was HANDED rather than an offset, and an out-of-range
610/// `limit` REFUSED rather than clamped. See [`WalletCoinsByParentParams`] for why each of those is
611/// the money-safe choice; a second set of paging semantics on the same plane would be a place for
612/// the two to disagree.
613#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
614pub struct WalletCoinsParams {
615    /// The `xch1…` address to read coins for.
616    pub address: String,
617    /// The asset to read coins for.
618    pub asset: Asset,
619    /// Resume STRICTLY AFTER this coin, in ascending `coin_id` order. `None` starts at the first.
620    ///
621    /// This is the value the previous page handed back as
622    /// [`cursor`](results::WalletCoinsResult::cursor) — never a value the caller invented, and never
623    /// a marker for where the chain got to.
624    #[serde(default, skip_serializing_if = "Option::is_none")]
625    pub after_coin_id: Option<String>,
626    /// The page size. `None` asks for [`COINS_DEFAULT_LIMIT`].
627    ///
628    /// A value above [`COINS_MAX_LIMIT`], or a zero, is REFUSED as `INVALID_PARAMS` rather than
629    /// clamped — see [`Self::validated`].
630    #[serde(default, skip_serializing_if = "Option::is_none")]
631    pub limit: Option<u32>,
632}
633control_call!(WalletCoinsParams => ControlMethod::WalletCoins, results::WalletCoinsResult);
634
635/// The length of a coin id in lowercase hex characters: a 32-byte hash.
636const COIN_ID_HEX_LEN: usize = 64;
637
638/// `control.wallet.coinById` params: WHICH coin, named by its own id.
639///
640/// # Why there is no `asset` here
641///
642/// A coin id names one coin on one chain. It is not scoped to an address and not scoped to an
643/// asset, and a node reading a coin record learns neither — so an asset parameter here could only
644/// be a claim the read never checks. The answer's
645/// [`asset`](crate::results::WalletCoinRecord::asset) is `null` for the same reason.
646///
647/// # Why the method exists at all
648///
649/// [`WalletCoinsParams`] answers by ADDRESS and lists UNSPENT coins only. A mint's evidence is the
650/// opposite shape: the created DID coin (which sits at nobody's wallet address) and the funding
651/// coin the mint SPENT (which is, by then, gone from every unspent list). Without a by-id read a
652/// pushed mint can never be observed — a permanent "pending" with the money already spent.
653#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
654pub struct WalletCoinByIdParams {
655    /// The coin id: lowercase 64-hex, unprefixed. A `0x` prefix is TOLERATED on input (block
656    /// explorers print one) and normalized away by [`Self::validated`]; it is never emitted.
657    pub coin_id: String,
658}
659control_call!(WalletCoinByIdParams => ControlMethod::WalletCoinById, results::WalletCoinByIdResult);
660
661/// `control.wallet.coinSpend` params: WHICH coin's spend to read, named by that coin's own id.
662///
663/// # Why the spend and the coin record are separate methods
664///
665/// [`WalletCoinByIdParams`] answers *what is this coin, and was it spent?* — an id, an amount, a
666/// puzzle HASH and two heights. None of that reveals what the spend DID. Reconstructing a lineage
667/// (which is how a dig-profile's DID singleton is followed forward) needs the puzzle REVEAL and the
668/// solution, and those exist only in the spend. A caller holding a coin record alone can see that a
669/// coin is gone and cannot see what it became.
670///
671/// # The id names the SPENT coin, not the spend
672///
673/// A spend has no id of its own on chain; it is identified by the coin it consumed. So the parameter
674/// is the same 64-hex coin id [`WalletCoinByIdParams`] takes, validated by the same rule, and the
675/// two methods are asked with the identical value.
676#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
677pub struct WalletCoinSpendParams {
678    /// The SPENT coin's id: lowercase 64-hex, unprefixed. A `0x` prefix is TOLERATED on input and
679    /// normalized away by [`Self::validated`]; it is never emitted.
680    pub coin_id: String,
681}
682control_call!(WalletCoinSpendParams => ControlMethod::WalletCoinSpend, results::WalletCoinSpendResult);
683
684/// `control.wallet.coinsByParent` params: WHICH coin's direct children to read.
685///
686/// # One hop, and the field name says so
687///
688/// The field is `parent_coin_id` rather than `coin_id` because the coin named here is the one being
689/// asked ABOUT as a parent — it is never the coin the caller wants back. A walk up or down a lineage
690/// is the CALLER's composition of repeated single hops; the node performs exactly one. Naming it
691/// `coin_id` would make a recursive reading of the method plausible from the request alone, and a
692/// caller expecting a whole lineage from one call would read a one-hop answer as a truncated chain.
693///
694/// # Bounded, because a parent's child count is not
695///
696/// This is the only OPEN wallet read whose answer has unbounded cardinality — every other one
697/// returns a single record (`coinById`, `peak`, `syncStatus`) or is already paged (`arrivals`). So
698/// the read is PAGED, and the page is bounded by [`COINS_BY_PARENT_MAX_LIMIT`].
699///
700/// **The bound is the ONLY thing bounding this call — there is no rate limiter anywhere behind it.**
701/// dig-node's control plane has no request rate limiting of any kind (dig_ecosystem#2577); the
702/// bandwidth limiter it does have governs content serving and is not on this path. A future reader
703/// weighing whether to relax this cap should assume no limiter exists, because none does.
704///
705/// That matters more than a local resource bound would, because the node does not necessarily answer
706/// from its own replica: on the fallback tier it forwards a caller-supplied identifier to a
707/// THIRD-PARTY coinset HTTPS oracle. An unbounded page is therefore unbounded work against somebody
708/// else's service, requested by a token-less caller on a loopback endpoint.
709///
710/// Paging rather than a bare cap, because a bare cap is a dead end: a parent with more children than
711/// the cap could never be fully enumerated, and this method exists to WALK a lineage. A walk that
712/// cannot see past the cap is a walk that silently stops.
713#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
714pub struct WalletCoinsByParentParams {
715    /// The PARENT coin's id: lowercase 64-hex, unprefixed. A `0x` prefix is TOLERATED on input and
716    /// normalized away by [`Self::validated`]; it is never emitted.
717    pub parent_coin_id: String,
718    /// Resume STRICTLY AFTER this child, in the read's
719    /// [documented order](results::WalletCoinsByParentResult). `None` starts at the first child.
720    ///
721    /// This is the value the previous page handed back as
722    /// [`cursor`](results::WalletCoinsByParentResult::cursor) — never a value the caller invented,
723    /// and never a marker for where the chain "got to". `control.wallet.arrivals` records why that
724    /// distinction loses rows; this read avoids the trap by having no such marker to reach for.
725    #[serde(default, skip_serializing_if = "Option::is_none")]
726    pub after_coin_id: Option<String>,
727    /// The page size. `None` asks for [`COINS_BY_PARENT_DEFAULT_LIMIT`].
728    ///
729    /// A value above [`COINS_BY_PARENT_MAX_LIMIT`], or a zero, is REFUSED as `INVALID_PARAMS` rather
730    /// than clamped — see [`Self::validated`].
731    #[serde(default, skip_serializing_if = "Option::is_none")]
732    pub limit: Option<u32>,
733}
734control_call!(WalletCoinsByParentParams => ControlMethod::WalletCoinsByParent, results::WalletCoinsByParentResult);
735
736/// `control.wallet.arrivals` — confirmed INCOMING funds recorded since a cursor position.
737///
738/// The answer to "was I just paid?", which neither a balance nor a coin list can give: a balance
739/// moves for the user's OWN change too, and an unspent-coin list cannot say which of its coins are
740/// new. Each row the node returns is a coin it determined ARRIVED — confirmed on chain, above the
741/// wallet's arrival baseline, not previously reported, and not created by spending one of the
742/// wallet's own coins. The determination is the NODE's; a client MUST NOT re-derive it, because the
743/// signals it takes (spent parents, the catch-up baseline) live only in the node's replica.
744///
745/// # A cursor, not a stream
746///
747/// The control envelope is strictly request→response, so this is polled. A client resumes from
748/// [`WalletArrivalsResult::cursor`](results::WalletArrivalsResult::cursor) — the last position it
749/// was actually handed — and NEVER from `latest`; see that field for why the distinction loses a
750/// notification when it is collapsed.
751///
752/// # `after_seq` is unsigned so a rewind is unexpressible
753///
754/// Positions are monotonic and start at 1, so there is no meaning for a negative one. `0` is the
755/// beginning of the ledger and is what a client sends when it deliberately wants everything.
756#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
757pub struct WalletArrivalsParams {
758    /// Return arrivals STRICTLY after this position. `0` starts at the beginning of the ledger,
759    /// and is also what an omitted field means — the same default the node applies.
760    #[serde(default)]
761    pub after_seq: u64,
762    /// The page size. `None` asks for the node's default rather than a number this client invented;
763    /// a node clamps whatever it is given to its own maximum.
764    #[serde(default, skip_serializing_if = "Option::is_none")]
765    pub limit: Option<u32>,
766}
767control_call!(WalletArrivalsParams => ControlMethod::WalletArrivals, results::WalletArrivalsResult);
768
769fn normalize_coin_id(coin_id: &str) -> Option<&str> {
770    let normalized = coin_id.strip_prefix("0x").unwrap_or(coin_id);
771    let well_formed = normalized.len() == COIN_ID_HEX_LEN
772        && normalized
773            .bytes()
774            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b));
775    well_formed.then_some(normalized)
776}
777
778/// Give a single-coin-id params type its validating `Deserialize` and its `validated` constructor.
779///
780/// The three by-coin reads (`coinById`, `coinSpend`, `coinsByParent`) enforce the IDENTICAL id rule
781/// under three different field names, and the rule is normative rather than incidental — see
782/// [`WalletCoinByIdParams::validated`] for why a malformed id must be refused BEFORE a chain is
783/// consulted. Written once here so a fourth by-coin read cannot arrive with a subtly looser copy of
784/// it, and so a change to the rule cannot land on two of three types.
785macro_rules! coin_id_params {
786    ($ty:ident, $field:ident, $raw:ident, $error:expr) => {
787        impl<'de> Deserialize<'de> for $ty {
788            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
789            where
790                D: serde::Deserializer<'de>,
791            {
792                #[derive(Deserialize)]
793                struct $raw {
794                    $field: String,
795                }
796
797                let raw = $raw::deserialize(deserializer)?;
798                let $field = normalize_coin_id(&raw.$field)
799                    .ok_or_else(|| serde::de::Error::custom($error))?
800                    .to_owned();
801                Ok(Self { $field })
802            }
803        }
804
805        impl $ty {
806            /// Normalize and check the coin id, or reject the request as `-32602 INVALID_PARAMS`.
807            ///
808            /// A malformed id is a malformed REQUEST, and the node refuses it here — before
809            /// consulting any chain. That ordering is normative rather than an optimisation: were a
810            /// bad id allowed through, the read would come back empty, and the caller would be told
811            /// the honest-looking answer *the chain holds nothing* about a coin it never actually
812            /// asked after. An unanswerable question and a chain that answered "no" must never wear
813            /// the same shape.
814            ///
815            /// Accepts exactly two spellings — 64 lowercase hex characters, or the same 64 preceded
816            /// by `0x`. Uppercase, whitespace and every other length are refused, because the
817            /// contract's hex wire form is lowercase and unprefixed everywhere else in this crate.
818            pub fn validated(self) -> Result<Self, crate::error::ControlError> {
819                let normalized = normalize_coin_id(&self.$field).ok_or_else(|| {
820                    crate::error::ControlError::of(
821                        crate::error::ControlErrorCode::InvalidParams,
822                        $error,
823                    )
824                })?;
825                Ok($ty {
826                    $field: normalized.to_owned(),
827                })
828            }
829        }
830    };
831}
832
833const COIN_ID_ERROR: &str = "coin_id must be lowercase 64-hex, optionally 0x-prefixed";
834const PARENT_COIN_ID_ERROR: &str =
835    "parent_coin_id must be lowercase 64-hex, optionally 0x-prefixed";
836const AFTER_COIN_ID_ERROR: &str = "after_coin_id must be lowercase 64-hex, optionally 0x-prefixed";
837
838coin_id_params!(
839    WalletCoinByIdParams,
840    coin_id,
841    RawWalletCoinByIdParams,
842    COIN_ID_ERROR
843);
844coin_id_params!(
845    WalletCoinSpendParams,
846    coin_id,
847    RawWalletCoinSpendParams,
848    COIN_ID_ERROR
849);
850/// The page size `control.wallet.coinsByParent` uses when the caller names none.
851///
852/// A spend in the lineages this read exists to follow — a singleton, a DID, an ordinary transfer —
853/// creates a small handful of children, so one default page covers a realistic hop in a single round
854/// trip and a caller never pages at all.
855pub const COINS_BY_PARENT_DEFAULT_LIMIT: u32 = 100;
856
857/// The largest page `control.wallet.coinsByParent` will accept, derived from the transport's own
858/// frame limit rather than chosen for feel.
859///
860/// dig-ipc-protocol caps a control frame at `MAX_FRAME_BYTES` = 1 MiB (its `SPEC.md` §
861/// bounds), and that is the hard ceiling every answer on this plane has to fit inside. A
862/// [`WalletCoinRecord`](results::WalletCoinRecord) is at most ~350 bytes of JSON — three 64-hex
863/// hashes at 66 bytes quoted, a 20-digit `u64`, two 10-digit heights, and their keys — so the
864/// arithmetic that fixes this number is:
865///
866/// ```text
867/// 1 MiB / 350 B  ~=  2,996 records is where a page STOPS FITTING
868/// 1,000 records  ~=  350 KB, roughly a third of the frame
869/// ```
870///
871/// The cap is set at a third of what fits, not at what fits, so the envelope, the freshness fields
872/// and any future additive member cannot push a legal page over the transport's limit. A larger
873/// value would put the contract's own maximum inside the region where a conforming node's honest
874/// answer is undeliverable — the failure would surface as a truncated frame, not as a refusal.
875pub const COINS_BY_PARENT_MAX_LIMIT: u32 = 1_000;
876
877/// The page bound message BOTH paged coin reads refuse with.
878///
879/// Shared rather than duplicated: the two reads page the same record type over the same frame, so
880/// two copies of this sentence could only ever differ by drifting apart.
881const PAGE_LIMIT_ERROR: &str = "limit must be between 1 and 1000";
882
883/// The page size `control.wallet.coins` uses when the caller names none (dig-node#381).
884///
885/// Pinned TO its sibling rather than restated. Both reads page the same
886/// [`WalletCoinRecord`](results::WalletCoinRecord) over the same transport frame, so the derivation
887/// that fixes one fixes the other, and a second literal here would be a second thing to forget when
888/// the frame limit moves.
889pub const COINS_DEFAULT_LIMIT: u32 = COINS_BY_PARENT_DEFAULT_LIMIT;
890
891/// The largest page `control.wallet.coins` will accept — the same frame-derived ceiling
892/// [`COINS_BY_PARENT_MAX_LIMIT`] documents the arithmetic for, and pinned to it for the reason
893/// [`COINS_DEFAULT_LIMIT`] gives.
894pub const COINS_MAX_LIMIT: u32 = COINS_BY_PARENT_MAX_LIMIT;
895
896impl<'de> Deserialize<'de> for WalletCoinsParams {
897    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
898    where
899        D: serde::Deserializer<'de>,
900    {
901        #[derive(Deserialize)]
902        struct RawWalletCoinsParams {
903            address: String,
904            asset: Asset,
905            #[serde(default)]
906            after_coin_id: Option<String>,
907            #[serde(default)]
908            limit: Option<u32>,
909        }
910
911        let raw = RawWalletCoinsParams::deserialize(deserializer)?;
912        let after_coin_id = raw
913            .after_coin_id
914            .map(|id| {
915                normalize_coin_id(&id)
916                    .map(str::to_owned)
917                    .ok_or_else(|| serde::de::Error::custom(AFTER_COIN_ID_ERROR))
918            })
919            .transpose()?;
920        if !raw.limit.map_or(true, is_legal_page) {
921            return Err(serde::de::Error::custom(PAGE_LIMIT_ERROR));
922        }
923        Ok(Self {
924            address: raw.address,
925            asset: raw.asset,
926            after_coin_id,
927            limit: raw.limit,
928        })
929    }
930}
931
932impl WalletCoinsParams {
933    /// A first page of coins at one address for one asset: the node's default size, from the start.
934    ///
935    /// The common case, and the one a caller should not have to spell out — naming a page size means
936    /// asserting a number this caller invented over the one the contract chose.
937    pub fn first_page(address: impl Into<String>, asset: Asset) -> Self {
938        Self {
939            address: address.into(),
940            asset,
941            after_coin_id: None,
942            limit: None,
943        }
944    }
945
946    /// The page size this request asks for, resolving `None` to [`COINS_DEFAULT_LIMIT`].
947    ///
948    /// Stated once here so a node and a client cannot resolve the same omitted field to two
949    /// different numbers — a disagreement that shows up as a page boundary in the wrong place,
950    /// which is exactly where a paged walk loses rows. On a coin read a lost row is a coin the
951    /// caller cannot spend.
952    pub fn effective_limit(&self) -> u32 {
953        self.limit.unwrap_or(COINS_DEFAULT_LIMIT)
954    }
955
956    /// Normalize the cursor and check the page bound, or reject as `-32602 INVALID_PARAMS`.
957    ///
958    /// The address is NOT validated here: it is decoded by the node's own bech32m reader, which is
959    /// the only thing that can tell a well-formed address from a well-formed string, and this crate
960    /// has never claimed otherwise for [`WalletBalanceParams`] either.
961    ///
962    /// An out-of-range `limit` is REFUSED, never clamped, for the reason
963    /// [`WalletCoinsByParentParams::validated`] states: a silently shrunk page hands back a cursor
964    /// for a position the caller did not ask about.
965    pub fn validated(self) -> Result<Self, crate::error::ControlError> {
966        fn invalid(message: &'static str) -> crate::error::ControlError {
967            crate::error::ControlError::of(crate::error::ControlErrorCode::InvalidParams, message)
968        }
969
970        let after_coin_id = self
971            .after_coin_id
972            .as_deref()
973            .map(|id| {
974                normalize_coin_id(id)
975                    .map(str::to_owned)
976                    .ok_or_else(|| invalid(AFTER_COIN_ID_ERROR))
977            })
978            .transpose()?;
979        if !self.limit.map_or(true, is_legal_page) {
980            return Err(invalid(PAGE_LIMIT_ERROR));
981        }
982        Ok(WalletCoinsParams {
983            address: self.address,
984            asset: self.asset,
985            after_coin_id,
986            limit: self.limit,
987        })
988    }
989}
990
991impl<'de> Deserialize<'de> for WalletCoinsByParentParams {
992    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
993    where
994        D: serde::Deserializer<'de>,
995    {
996        #[derive(Deserialize)]
997        struct RawWalletCoinsByParentParams {
998            parent_coin_id: String,
999            #[serde(default)]
1000            after_coin_id: Option<String>,
1001            #[serde(default)]
1002            limit: Option<u32>,
1003        }
1004
1005        let raw = RawWalletCoinsByParentParams::deserialize(deserializer)?;
1006        let parent_coin_id = normalize_coin_id(&raw.parent_coin_id)
1007            .ok_or_else(|| serde::de::Error::custom(PARENT_COIN_ID_ERROR))?
1008            .to_owned();
1009        let after_coin_id = raw
1010            .after_coin_id
1011            .map(|id| {
1012                normalize_coin_id(&id)
1013                    .map(str::to_owned)
1014                    .ok_or_else(|| serde::de::Error::custom(AFTER_COIN_ID_ERROR))
1015            })
1016            .transpose()?;
1017        if !raw.limit.map_or(true, is_legal_page) {
1018            return Err(serde::de::Error::custom(PAGE_LIMIT_ERROR));
1019        }
1020        Ok(Self {
1021            parent_coin_id,
1022            after_coin_id,
1023            limit: raw.limit,
1024        })
1025    }
1026}
1027
1028/// Is this a page size the contract accepts — at least one row, at most the frame-derived maximum?
1029fn is_legal_page(limit: u32) -> bool {
1030    (1..=COINS_BY_PARENT_MAX_LIMIT).contains(&limit)
1031}
1032
1033impl WalletCoinsByParentParams {
1034    /// A first page of children for one parent: the node's default size, starting at the beginning.
1035    ///
1036    /// The common case, and the one a caller should not have to spell out — naming a page size means
1037    /// asserting a number this caller invented over the one the contract chose.
1038    pub fn first_page(parent_coin_id: impl Into<String>) -> Self {
1039        Self {
1040            parent_coin_id: parent_coin_id.into(),
1041            after_coin_id: None,
1042            limit: None,
1043        }
1044    }
1045
1046    /// The page size this request asks for, resolving `None` to [`COINS_BY_PARENT_DEFAULT_LIMIT`].
1047    ///
1048    /// Stated once here so a node and a client cannot resolve the same omitted field to two
1049    /// different numbers — a disagreement that would show up as a page boundary in the wrong place,
1050    /// which is exactly where a paged walk loses rows.
1051    pub fn effective_limit(&self) -> u32 {
1052        self.limit.unwrap_or(COINS_BY_PARENT_DEFAULT_LIMIT)
1053    }
1054
1055    /// Normalize and check both ids and the page bound, or reject as `-32602 INVALID_PARAMS`.
1056    ///
1057    /// The ids follow the rule every by-coin read in this crate follows (lowercase 64-hex, `0x`
1058    /// tolerated on input and never emitted).
1059    ///
1060    /// An out-of-range `limit` is REFUSED, never clamped. That is a deliberate departure from
1061    /// `control.wallet.arrivals`, which lets a node clamp: this read's page boundary is what a
1062    /// caller RESUMES from, so a silently shrunk page hands back a cursor for a position the caller
1063    /// did not ask about, and a caller that believed its own number would mis-size every subsequent
1064    /// request. Refusing keeps the caller's model of the page and the node's identical, which is the
1065    /// same reason a 65-hex coin id is refused rather than truncated.
1066    ///
1067    /// `limit: 0` is refused for a separate reason: a page that can hold nothing makes no progress,
1068    /// so a caller looping until a page comes back short would loop forever.
1069    pub fn validated(self) -> Result<Self, crate::error::ControlError> {
1070        fn invalid(message: &'static str) -> crate::error::ControlError {
1071            crate::error::ControlError::of(crate::error::ControlErrorCode::InvalidParams, message)
1072        }
1073
1074        let parent_coin_id = normalize_coin_id(&self.parent_coin_id)
1075            .ok_or_else(|| invalid(PARENT_COIN_ID_ERROR))?
1076            .to_owned();
1077        let after_coin_id = self
1078            .after_coin_id
1079            .as_deref()
1080            .map(|id| {
1081                normalize_coin_id(id)
1082                    .map(str::to_owned)
1083                    .ok_or_else(|| invalid(AFTER_COIN_ID_ERROR))
1084            })
1085            .transpose()?;
1086        if !self.limit.map_or(true, is_legal_page) {
1087            return Err(invalid(PAGE_LIMIT_ERROR));
1088        }
1089        Ok(WalletCoinsByParentParams {
1090            parent_coin_id,
1091            after_coin_id,
1092            limit: self.limit,
1093        })
1094    }
1095}
1096
1097/// `control.wallet.peak` params — none.
1098///
1099/// The peak is a property of the node's chain view, not of any address, which is exactly why it is
1100/// its own method: [`results::WalletBalanceResult::peak_height`] is `null` on every fallback-tier
1101/// answer, so a caller bounding a claimed confirmation cannot rely on getting one from a balance.
1102#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
1103pub struct WalletPeakParams {}
1104control_call!(WalletPeakParams => ControlMethod::WalletPeak, results::WalletPeakResult);
1105
1106/// `control.wallet.operatorAddress` params — none.
1107///
1108/// There is exactly one operator wallet per node, so there is nothing to scope the question by.
1109/// Deliberately NOT an address-taking method: a caller cannot ask *is THIS address the operator
1110/// wallet* by supplying a candidate, because a node answering yes/no about supplied addresses is an
1111/// oracle for probing which addresses a machine controls.
1112#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
1113pub struct WalletOperatorAddressParams {}
1114control_call!(WalletOperatorAddressParams => ControlMethod::WalletOperatorAddress, results::WalletOperatorAddressResult);
1115
1116/// `control.peerCounts` params — none.
1117///
1118/// The counts describe this node's own connectivity on each network, so there is nothing to scope
1119/// the question by. One call answers for BOTH networks deliberately: a consumer that had to collect
1120/// the DIG count from a peer method and the Chia count from a wallet method is a consumer that can
1121/// reach for the wrong one.
1122#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
1123pub struct PeerCountsParams {}
1124control_call!(PeerCountsParams => ControlMethod::PeerCounts, results::PeerCountsResult);
1125
1126/// `control.wallet.syncStatus` params — none.
1127///
1128/// The wallet's sync progress is a property of the node's own chain replica, not of any address, so
1129/// there is nothing to scope the question by. Deliberately NOT confused with `control.sync.status`,
1130/// which reports §21 DIG STORE sync and has nothing to do with the chain.
1131#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
1132pub struct WalletSyncStatusParams {}
1133control_call!(WalletSyncStatusParams => ControlMethod::WalletSyncStatus, results::WalletSyncStatusResult);
1134
1135/// `control.wallet.broadcast` params: an ALREADY-SIGNED spend bundle to push.
1136///
1137/// # The custody boundary (§908)
1138///
1139/// This carries signed bytes and nothing else. There is deliberately no key, no seed, no phrase and
1140/// no unsigned-spend-plus-key field here, and there never may be: the node's role on the money path
1141/// is to read chain state and to push what somebody else signed. A parameter that let the node
1142/// produce a signature would move custody into an identity-agnostic daemon, which is the one thing
1143/// the boundary exists to prevent.
1144#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1145pub struct WalletBroadcastParams {
1146    /// The signed spend bundle: lowercase hex of its chia `Streamable` serialization.
1147    pub signed_bundle_hex: String,
1148}
1149control_call!(WalletBroadcastParams => ControlMethod::WalletBroadcast, results::WalletBroadcastResult);
1150
1151/// The length of a BLS G1 public key in lowercase hex characters: 48 bytes.
1152const PUBLIC_KEY_HEX_LEN: usize = 96;
1153
1154/// `control.wallet.watch` params: which PUBLIC keys the node should follow.
1155///
1156/// # Keys, never puzzle hashes
1157///
1158/// The node already derives addresses from the public keys it holds in its own custody, through one
1159/// standard derivation. Enrolling KEYS reuses that exact derivation for a client's keys too, so the
1160/// ecosystem has ONE mapping from key to address and a client and a node can never disagree about
1161/// which addresses a key covers. Enrolling puzzle hashes instead would make every client re-derive
1162/// independently, and a client whose derivation window is narrower than the node's would silently
1163/// under-report the money it owns.
1164///
1165/// # No key material crosses (§908)
1166///
1167/// A G1 public key is public. There is deliberately no seed, phrase, private key or signature field
1168/// here, and there never may be: enrolment tells the node what to WATCH, and watching needs nothing
1169/// a signature could be produced from.
1170///
1171/// # Idempotent, so a client can reconcile without asking first
1172///
1173/// Submitting keys the node already follows is a SUCCESS that changes nothing — see
1174/// [`WalletWatchResult`](results::WalletWatchResult). Duplicates WITHIN one request count once. An
1175/// empty list is accepted and does nothing, because "enrol everything in this (currently empty) set"
1176/// is a reconciliation a client legitimately performs.
1177#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1178pub struct WalletWatchParams {
1179    /// The public keys to enrol: lowercase 96-hex, unprefixed. A `0x` prefix is TOLERATED on input
1180    /// and normalized away by [`Self::validated`]; it is never emitted.
1181    pub public_keys: Vec<String>,
1182}
1183control_call!(WalletWatchParams => ControlMethod::WalletWatch, results::WalletWatchResult);
1184
1185/// `control.wallet.unwatch` params: which PUBLIC keys the node should stop following.
1186///
1187/// Takes the same wire form as [`WalletWatchParams`] under the same field name, so a client
1188/// reverses an enrolment by re-sending exactly what it sent. Deregistering a key that was never
1189/// enrolled is a success, not an error — the mirror of enrolment's idempotence, and what lets a
1190/// client assert an end state rather than compute a diff.
1191#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1192pub struct WalletUnwatchParams {
1193    /// The public keys to deregister: lowercase 96-hex, unprefixed. A `0x` prefix is TOLERATED on
1194    /// input and normalized away by [`Self::validated`]; it is never emitted.
1195    pub public_keys: Vec<String>,
1196}
1197control_call!(WalletUnwatchParams => ControlMethod::WalletUnwatch, results::WalletUnwatchResult);
1198
1199/// `control.wallet.watched` params — none.
1200///
1201/// The enrolled set is a property of the node, so there is nothing to scope the question by. This
1202/// is why the method is TOKEN-GATED although it only reads: the caller supplies nothing, so the
1203/// answer is the node's OWN key set — see [`ControlMethod::is_open_read`].
1204#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
1205pub struct WalletWatchedParams {}
1206control_call!(WalletWatchedParams => ControlMethod::WalletWatched, results::WalletWatchedResult);
1207
1208fn normalize_public_key(public_key: &str) -> Option<&str> {
1209    let normalized = public_key.strip_prefix("0x").unwrap_or(public_key);
1210    let well_formed = normalized.len() == PUBLIC_KEY_HEX_LEN
1211        && normalized
1212            .bytes()
1213            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b));
1214    well_formed.then_some(normalized)
1215}
1216
1217/// Give an enrolment params type its validating `Deserialize` and its `validated` constructor.
1218///
1219/// `watch` and `unwatch` carry the IDENTICAL key list under the identical field name, and must
1220/// enforce the identical rule: a client reverses an enrolment by re-sending what it sent, so a
1221/// spelling one method accepts and the other refuses would leave a key enrolled forever. Written
1222/// once so the two cannot drift apart.
1223macro_rules! public_keys_params {
1224    ($ty:ident, $raw:ident, $error:expr) => {
1225        impl<'de> Deserialize<'de> for $ty {
1226            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1227            where
1228                D: serde::Deserializer<'de>,
1229            {
1230                #[derive(Deserialize)]
1231                struct $raw {
1232                    public_keys: Vec<String>,
1233                }
1234
1235                let raw = $raw::deserialize(deserializer)?;
1236                let public_keys = raw
1237                    .public_keys
1238                    .iter()
1239                    .map(|key| {
1240                        normalize_public_key(key)
1241                            .map(str::to_owned)
1242                            .ok_or_else(|| serde::de::Error::custom($error))
1243                    })
1244                    .collect::<Result<Vec<_>, _>>()?;
1245                Ok(Self { public_keys })
1246            }
1247        }
1248
1249        impl $ty {
1250            /// Normalize and check every key, or reject the request as `-32602 INVALID_PARAMS`.
1251            ///
1252            /// The whole request is refused when ANY key is malformed — never the well-formed
1253            /// subset. A partial enrolment would leave the node following fewer addresses than the
1254            /// client believes it asked for, and the client's next balance read would report a
1255            /// shortfall as though the money were not there. One bad key is a malformed REQUEST.
1256            ///
1257            /// Accepts exactly two spellings per key — 96 lowercase hex characters, or the same 96
1258            /// preceded by `0x`. Uppercase, whitespace and every other length are refused, because
1259            /// the contract's hex wire form is lowercase and unprefixed everywhere else.
1260            pub fn validated(self) -> Result<Self, crate::error::ControlError> {
1261                let public_keys = self
1262                    .public_keys
1263                    .iter()
1264                    .map(|key| {
1265                        normalize_public_key(key).map(str::to_owned).ok_or_else(|| {
1266                            crate::error::ControlError::of(
1267                                crate::error::ControlErrorCode::InvalidParams,
1268                                $error,
1269                            )
1270                        })
1271                    })
1272                    .collect::<Result<Vec<_>, _>>()?;
1273                Ok(Self { public_keys })
1274            }
1275        }
1276    };
1277}
1278
1279const PUBLIC_KEYS_ERROR: &str =
1280    "public_keys must each be lowercase 96-hex (a 48-byte G1 key), optionally 0x-prefixed";
1281
1282public_keys_params!(WalletWatchParams, RawWalletWatch, PUBLIC_KEYS_ERROR);
1283public_keys_params!(WalletUnwatchParams, RawWalletUnwatch, PUBLIC_KEYS_ERROR);
1284
1285/// Every id in a reservation's `coin_ids` takes the same wire form the single-coin reads accept,
1286/// so a client can feed a coin straight from `control.wallet.coins` into a reservation.
1287const COIN_IDS_ERROR: &str =
1288    "coin_ids must each be lowercase 64-hex (a 32-byte coin id), optionally 0x-prefixed";
1289
1290/// `control.wallet.reservations.held` params — none.
1291///
1292/// # Why the caller does not supply the time
1293///
1294/// dig-account's `CoinReservationStore::held(now_unix)` takes the current time from its caller,
1295/// because in-process both sides share one clock and one trust domain. Across the control boundary
1296/// they share neither, and a caller-supplied `now` would be a lapse oracle: a far-future value makes
1297/// every live reservation read as expired, and the caller then selects coins another process is
1298/// about to spend. The node reads its OWN clock and reports it back as
1299/// [`as_of_unix`](results::WalletReservationsHeldResult::as_of_unix), so a client can SEE skew
1300/// rather than impose it.
1301#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
1302pub struct WalletReservationsHeldParams {}
1303control_call!(WalletReservationsHeldParams => ControlMethod::WalletReservationsHeld, results::WalletReservationsHeldResult);
1304
1305/// `control.wallet.reservations.reserve` params: take EVERY named coin, or take none.
1306///
1307/// The wire form of `dig_account::wallet::reservation::CoinReservationStore::reserve_all`, so a
1308/// client backing that seam with the node forwards its arguments rather than translating them.
1309///
1310/// # All-or-none is the whole contract
1311///
1312/// Reading the held set, selecting, then reserving is check-then-act: two processes both read an
1313/// empty set and both take the same coin. Atomic acquisition is what closes that window, so a
1314/// conflict on ANY coin refuses the WHOLE call and reserves nothing — see
1315/// [`ControlErrorCode::WalletCoinsReserved`]. A caller that loses re-reads
1316/// [`WalletReservationsHeldParams`] and re-selects from what remains.
1317///
1318/// # An empty list is a success, not an error
1319///
1320/// It yields a handle that releases nothing, which is what the caller asked for. This matches
1321/// dig-account exactly: an empty reservation can never conflict, so refusing it would make a
1322/// legitimate no-op selection look like a malformed request.
1323///
1324/// # No key material (§908)
1325///
1326/// A coin id is a public chain fact. There is no seed, key, signature or bundle field here and
1327/// there never may be: a reservation is BOOKKEEPING — it narrows what a selector will choose and
1328/// authorizes nothing.
1329#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
1330pub struct WalletReservationsReserveParams {
1331    /// The coin ids to hold: lowercase 64-hex, unprefixed. A `0x` prefix is TOLERATED on input and
1332    /// normalized away by [`Self::validated`]; it is never emitted.
1333    pub coin_ids: Vec<String>,
1334    /// How long the hold should live, in seconds; `None` asks for the node's default.
1335    ///
1336    /// A REQUEST, never a command. The node clamps this to its own maximum and returns the value it
1337    /// actually applied as
1338    /// [`ttl_secs`](results::WalletReservationsReserveResult::ttl_secs) — an unclamped caller-chosen
1339    /// lifetime is a lockout weapon, since one call could hold a wallet's coins away from its owner
1340    /// for as long as it liked.
1341    #[serde(skip_serializing_if = "Option::is_none")]
1342    pub ttl_secs: Option<u64>,
1343}
1344control_call!(WalletReservationsReserveParams => ControlMethod::WalletReservationsReserve, results::WalletReservationsReserveResult);
1345
1346/// `control.wallet.reservations.release` params: free one reservation now, ahead of its TTL.
1347///
1348/// # The release path is why a reservation is safe at all
1349///
1350/// A hold with no way out is a wallet that locks itself out of its own funds, which is worse than
1351/// the double-select it prevents. Two mechanisms keep that impossible and BOTH are required: the
1352/// TTL bounds every hold whether or not anyone releases it, and this method lets a caller that
1353/// KNOWS the answer — the spend settled, or was definitively rejected — stop holding the user's
1354/// coins over a question the chain has already resolved.
1355///
1356/// # Releasing an unknown or lapsed id is a SUCCESS
1357///
1358/// A caller releasing on confirmation cannot know whether the TTL got there first, and making that
1359/// race an error would teach callers to ignore the result — which is how a release stops being
1360/// called at all. See [`WalletReservationsReleaseResult`](results::WalletReservationsReleaseResult).
1361#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1362pub struct WalletReservationsReleaseParams {
1363    /// The handle returned by
1364    /// [`reserve`](results::WalletReservationsReserveResult::reservation_id).
1365    ///
1366    /// OPAQUE: a client stores it and sends it back, and MUST NOT parse, derive or construct one.
1367    /// The node mints it, and how it is spelled is free to differ between a hold taken before a
1368    /// broadcast and one recorded for a bundle already in flight.
1369    pub reservation_id: String,
1370}
1371control_call!(WalletReservationsReleaseParams => ControlMethod::WalletReservationsRelease, results::WalletReservationsReleaseResult);
1372
1373impl<'de> Deserialize<'de> for WalletReservationsReserveParams {
1374    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1375    where
1376        D: serde::Deserializer<'de>,
1377    {
1378        #[derive(Deserialize)]
1379        struct RawReserve {
1380            coin_ids: Vec<String>,
1381            #[serde(default)]
1382            ttl_secs: Option<u64>,
1383        }
1384
1385        let raw = RawReserve::deserialize(deserializer)?;
1386        let coin_ids = raw
1387            .coin_ids
1388            .iter()
1389            .map(|id| {
1390                normalize_coin_id(id)
1391                    .map(str::to_owned)
1392                    .ok_or_else(|| serde::de::Error::custom(COIN_IDS_ERROR))
1393            })
1394            .collect::<Result<Vec<_>, _>>()?;
1395        Ok(Self {
1396            coin_ids,
1397            ttl_secs: raw.ttl_secs,
1398        })
1399    }
1400}
1401
1402impl WalletReservationsReserveParams {
1403    /// Normalize and check every coin id, or reject the request as `-32602 INVALID_PARAMS`.
1404    ///
1405    /// The WHOLE request is refused when any id is malformed, never the well-formed subset. A
1406    /// partial reservation would leave the caller believing it holds inputs it does not — the exact
1407    /// state all-or-none acquisition exists to make unreachable.
1408    pub fn validated(self) -> Result<Self, ControlError> {
1409        let coin_ids = self
1410            .coin_ids
1411            .iter()
1412            .map(|id| {
1413                normalize_coin_id(id).map(str::to_owned).ok_or_else(|| {
1414                    ControlError::of(ControlErrorCode::InvalidParams, COIN_IDS_ERROR)
1415                })
1416            })
1417            .collect::<Result<Vec<_>, _>>()?;
1418        Ok(Self {
1419            coin_ids,
1420            ttl_secs: self.ttl_secs,
1421        })
1422    }
1423}
1424
1425/// `pairing.request` params (OPEN — no token).
1426#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1427pub struct RequestParams {
1428    /// A human-readable name for the requesting client (shown to the operator).
1429    pub client_name: String,
1430}
1431control_call!(RequestParams => ControlMethod::PairingRequest, results::PairingRequestResult);
1432
1433/// `pairing.poll` params (OPEN — no token).
1434#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1435pub struct PollParams {
1436    /// The pairing id to poll.
1437    pub pairing_id: String,
1438}
1439control_call!(PollParams => ControlMethod::PairingPoll, results::PairingPollResult);
1440
1441/// The largest dig-profile body the control plane accepts or returns, in bytes: **4 MiB**.
1442///
1443/// # Why the contract carries the cap instead of the implementation
1444///
1445/// A client that learns the bound by exceeding it learns it as a failed round trip, after paying to
1446/// serialize and send megabytes. Stated here, an app checks before it sends and can tell a person
1447/// *this profile is too large* rather than *something went wrong*.
1448///
1449/// # Why this number
1450///
1451/// It is HALF of dig-gossip's `WS_MAX_MESSAGE_BYTES` (8 MiB), the frame ceiling a body must fit
1452/// inside when a node serves it to a peer over `PROFILE_BODY` (opcode 225). A body accepted here
1453/// but unservable there would be stored and then permanently unsyncable — accepted by the node the
1454/// app talks to and invisible to every other node. The halving leaves room for the framing,
1455/// envelope and base64 expansion that sit around the bytes on that hop.
1456///
1457/// The cap is on the DECODED body, not on the base64 text that carries it.
1458pub const MAX_BODY_BYTES: usize = 4 * 1024 * 1024;
1459
1460/// `control.profile.putBody` params: the profile body a confirmed chain root commits to.
1461///
1462/// # The node CHECKS the root; it does not take the caller's word for it
1463///
1464/// `root` is a CLAIM, and the node's obligation is to refuse it when it is false. An implementation
1465/// MUST independently resolve the profile's root on chain, recompute the root of the supplied body,
1466/// and reject the call unless the two agree and that root is CONFIRMED. dig-app is a caller like
1467/// any other here: it holds the key and signs the root (§908), but the bytes it then hands over
1468/// arrive at the node exactly as a peer's bytes do, and the standing rule for this epic is that a
1469/// body is checked against the on-chain root and anything that does not match is rejected.
1470///
1471/// A method documented as *the caller supplies a matching root* invites an implementation that
1472/// stores what it is given. That implementation turns the control plane into a way to make a node
1473/// serve arbitrary bytes to the network under someone else's profile id.
1474///
1475/// # No key material crosses (§908)
1476///
1477/// There is deliberately no seed, private key, signature or unsigned spend field here, and there
1478/// never may be. The node persists, serves and fetches bodies; it never signs.
1479#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1480pub struct ProfilePutBodyParams {
1481    /// The profile's store id: lowercase 64-hex, unprefixed.
1482    pub store_id: String,
1483    /// The root the body is claimed to hash to: lowercase 64-hex, unprefixed. Checked against the
1484    /// chain, never trusted.
1485    pub root: String,
1486    /// The body itself, standard base64 (padded) of its `DPB` serialization. The DECODED length
1487    /// MUST NOT exceed [`MAX_BODY_BYTES`]; a larger body is refused as `INVALID_PARAMS`.
1488    pub body_b64: String,
1489}
1490control_call!(ProfilePutBodyParams => ControlMethod::ProfilePutBody, results::ProfilePutBodyResult);
1491
1492/// `control.profile.getBody` params: WHICH profile body to read, named by store id + root.
1493///
1494/// The root is part of the question rather than part of the answer: a caller asks for the body at a
1495/// root it already knows, so it can never be handed a body for a DIFFERENT root and mistake it for
1496/// the one it asked about. A node holding no body at that root answers `body_b64: null`.
1497#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
1498pub struct ProfileGetBodyParams {
1499    /// The profile's store id: lowercase 64-hex, unprefixed.
1500    pub store_id: String,
1501    /// The root to read the body at: lowercase 64-hex, unprefixed.
1502    pub root: String,
1503}
1504control_call!(ProfileGetBodyParams => ControlMethod::ProfileGetBody, results::ProfileGetBodyResult);
1505
1506/// The page size `control.spends.list` uses when the caller names none.
1507///
1508/// Stated on the contract rather than chosen by each side, so a node and a client cannot resolve the
1509/// same omitted field to two different numbers — a disagreement that shows up as a page boundary in
1510/// the wrong place, which is where a paged walk loses rows.
1511pub const SPENDS_LIST_DEFAULT_LIMIT: u32 = 50;
1512
1513/// The largest page `control.spends.list` will serve.
1514///
1515/// The audit record grows without limit — it is append-only and every automated cycle adds to it —
1516/// so an unbounded read is unbounded work and an unbounded response. The bound is declared here from
1517/// the start rather than added later, because a method that ships unbounded teaches every client to
1518/// expect the whole record in one call, and bounding it afterwards is then a breaking change.
1519///
1520/// **This bound is the only thing bounding the call.** dig-node's control plane has no request rate
1521/// limiting of any kind (dig_ecosystem#2577), so a future reader weighing a larger cap should assume
1522/// no limiter exists behind it, because none does.
1523pub const SPENDS_LIST_MAX_LIMIT: u32 = 500;
1524
1525/// The one refusal message for an out-of-range `control.spends.list` page size.
1526const SPENDS_LIST_LIMIT_ERROR: &str = "limit must be between 1 and 500 spends per page";
1527
1528/// `control.spends.list` params: WHICH automated spends to read, and how much of the record at once.
1529///
1530/// # A read, and only a read
1531///
1532/// Nothing here initiates, signs, cancels or amends a spend, and the catalog offers no method that
1533/// does. The record exists to make automatic signing accountable, and a surface able to edit it
1534/// would be a surface able to edit the evidence.
1535///
1536/// # Every filter is an AND; an unset filter constrains nothing
1537///
1538/// Omitting a field means "do not narrow on this", never "match nothing". A client that sends no
1539/// field at all asks for the newest page of the whole record.
1540///
1541/// # Paged, and the page boundary is the caller's
1542///
1543/// Rows come newest-initiated first (the full ordering rule is on
1544/// [`SpendsListResult`](results::SpendsListResult)) and a caller resumes from
1545/// [`after_id`](Self::after_id) — the id of the last row it was actually HANDED. An out-of-range
1546/// [`limit`](Self::limit) is REFUSED rather than clamped, matching
1547/// [`WalletCoinsByParentParams`] and for the same reason: a silently shrunk page hands back a cursor
1548/// for a position the caller did not ask about.
1549#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
1550pub struct SpendsListParams {
1551    /// Only spends INITIATED at or after this unix-ms instant. Inclusive.
1552    #[serde(default, skip_serializing_if = "Option::is_none")]
1553    pub since_ms: Option<u64>,
1554    /// Only spends INITIATED strictly before this unix-ms instant. Exclusive.
1555    ///
1556    /// Half-open with [`since_ms`](Self::since_ms) so consecutive windows tile the record exactly:
1557    /// one window's `until_ms` is the next window's `since_ms`, and no spend is counted twice or
1558    /// dropped between them.
1559    #[serde(default, skip_serializing_if = "Option::is_none")]
1560    pub until_ms: Option<u64>,
1561    /// Only spends serving this store id.
1562    #[serde(default, skip_serializing_if = "Option::is_none")]
1563    pub store_id: Option<String>,
1564    /// Only this kind of spend — the stable token the producer stamped (`"mirror-coin"`, …).
1565    ///
1566    /// An open string rather than an enum, because a new producer must be able to appear in the
1567    /// record without a release of this crate. An unrecognised kind matches nothing rather than
1568    /// erroring, which is the same answer a caller gets for a real kind that has no rows yet.
1569    #[serde(default, skip_serializing_if = "Option::is_none")]
1570    pub kind: Option<String>,
1571    /// Only this outcome, by its [`SpendOutcome::token`](results::SpendOutcome::token) —
1572    /// `pending` / `submitted` / `confirmed` / `failed` / `unresolved`.
1573    ///
1574    /// **`failed` does NOT mean "the money stayed put"**, so a client filtering on it must still
1575    /// read each row's stage — see [`SpendOutcome::Failed`](results::SpendOutcome::Failed). A UI
1576    /// that offers a "failed" filter and renders its rows as untouched money asserts something the
1577    /// node does not know.
1578    #[serde(default, skip_serializing_if = "Option::is_none")]
1579    pub status: Option<String>,
1580    /// Resume STRICTLY AFTER this audit id, in the read's documented order. `None` starts at the
1581    /// newest matching row.
1582    ///
1583    /// This is the value the previous page handed back as
1584    /// [`cursor`](results::SpendsListResult::cursor) — never an id the caller kept for another
1585    /// reason, and never a timestamp. Resuming by time would drop every spend sharing the boundary
1586    /// millisecond.
1587    #[serde(default, skip_serializing_if = "Option::is_none")]
1588    pub after_id: Option<String>,
1589    /// The page size. `None` asks for [`SPENDS_LIST_DEFAULT_LIMIT`].
1590    ///
1591    /// A zero, or a value above [`SPENDS_LIST_MAX_LIMIT`], is REFUSED as `INVALID_PARAMS` rather
1592    /// than clamped — see [`Self::validated`].
1593    #[serde(default, skip_serializing_if = "Option::is_none")]
1594    pub limit: Option<u32>,
1595}
1596
1597impl SpendsListParams {
1598    /// The page size this request asks for, resolving `None` to [`SPENDS_LIST_DEFAULT_LIMIT`].
1599    ///
1600    /// Stated once here so the node and the client cannot resolve the same omitted field to two
1601    /// different numbers.
1602    pub fn effective_limit(&self) -> u32 {
1603        self.limit.unwrap_or(SPENDS_LIST_DEFAULT_LIMIT)
1604    }
1605
1606    /// Check the page bound, or reject as `-32602 INVALID_PARAMS`.
1607    ///
1608    /// `limit: 0` is refused because a page that can hold nothing makes no progress: a caller
1609    /// looping until [`complete`](results::SpendsListResult::complete) would loop forever. A limit
1610    /// above the cap is refused rather than clamped so the caller's model of the page and the node's
1611    /// stay identical.
1612    ///
1613    /// The time window is deliberately NOT validated. `since_ms > until_ms` is a well-formed request
1614    /// for an empty window, and an empty window has an honest answer — no rows — which is a
1615    /// different thing from a malformed request.
1616    pub fn validated(self) -> Result<Self, ControlError> {
1617        if let Some(limit) = self.limit {
1618            if limit == 0 || limit > SPENDS_LIST_MAX_LIMIT {
1619                return Err(ControlError::of(
1620                    ControlErrorCode::InvalidParams,
1621                    SPENDS_LIST_LIMIT_ERROR,
1622                ));
1623            }
1624        }
1625        Ok(self)
1626    }
1627}
1628
1629impl<'de> Deserialize<'de> for SpendsListParams {
1630    /// Validates on the way in, so a node cannot forget to call [`Self::validated`].
1631    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1632    where
1633        D: serde::Deserializer<'de>,
1634    {
1635        #[derive(Deserialize)]
1636        struct Raw {
1637            #[serde(default)]
1638            since_ms: Option<u64>,
1639            #[serde(default)]
1640            until_ms: Option<u64>,
1641            #[serde(default)]
1642            store_id: Option<String>,
1643            #[serde(default)]
1644            kind: Option<String>,
1645            #[serde(default)]
1646            status: Option<String>,
1647            #[serde(default)]
1648            after_id: Option<String>,
1649            #[serde(default)]
1650            limit: Option<u32>,
1651        }
1652        let raw = Raw::deserialize(deserializer)?;
1653        SpendsListParams {
1654            since_ms: raw.since_ms,
1655            until_ms: raw.until_ms,
1656            store_id: raw.store_id,
1657            kind: raw.kind,
1658            status: raw.status,
1659            after_id: raw.after_id,
1660            limit: raw.limit,
1661        }
1662        .validated()
1663        .map_err(serde::de::Error::custom)
1664    }
1665}
1666control_call!(SpendsListParams => ControlMethod::SpendsList, results::SpendsListResult);
1667
1668/// The page size `control.mirror.bondStates` returns when a caller names none.
1669///
1670/// Sized for a pane rather than for a walk: dig-app#300 renders every bond of an ordinary node in
1671/// one view, and a node serving fewer than this many `(store, root)` pairs never pages at all.
1672pub const MIRROR_BOND_STATES_DEFAULT_LIMIT: u32 = 100;
1673
1674/// The largest page `control.mirror.bondStates` will return.
1675///
1676/// Pinned TO [`COINS_BY_PARENT_MAX_LIMIT`] rather than repeating its number, because it is bounded
1677/// by the same arithmetic: a bond row carries a coin id and a handful of integers, so a page of
1678/// bonds and a page of coins occupy the same order of envelope. Written as the constant so the two
1679/// cannot drift into two different numbers that were only ever meant to be one.
1680pub const MIRROR_BOND_STATES_MAX_LIMIT: u32 = COINS_BY_PARENT_MAX_LIMIT;
1681
1682const MIRROR_BOND_STATES_LIMIT_ERROR: &str = "limit must be between 1 and 1000 bonds per page";
1683
1684const MIRROR_BOND_AFTER_ERROR: &str =
1685    "after.store_id and after.root must each be lowercase 64-hex, optionally 0x-prefixed";
1686
1687/// `control.mirror.bondStates` params — one page of this node's mirror bond states.
1688///
1689/// # The caller narrows nothing
1690///
1691/// There is no store filter. The answer is this node's OWN bond set, and both consuming surfaces
1692/// (dig-app#300's pane, dig-app#289's locked total) want all of it; a client wanting one store
1693/// filters the page it was handed. Adding a filter would also make
1694/// [`locked_dig_base_units`](results::MirrorBondStatesResult::Known::locked_dig_base_units) — a
1695/// whole-set figure — read as a filtered one, which is the money lie this method is built to avoid.
1696///
1697/// # Paged, and the page boundary is the caller's
1698///
1699/// Rows come in ascending `(store_id, root)` and a caller resumes from [`after`](Self::after) — the
1700/// key of the last row it was actually HANDED. An out-of-range [`limit`](Self::limit) is REFUSED
1701/// rather than clamped, matching [`WalletCoinsByParentParams`] and for the same reason: a silently
1702/// shrunk page hands back a cursor for a position the caller did not ask about.
1703#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
1704pub struct MirrorBondStatesParams {
1705    /// Resume STRICTLY AFTER this `(store_id, root)`, in ascending key order. `None` starts at the
1706    /// first bond.
1707    ///
1708    /// The value the previous page handed back as
1709    /// [`cursor`](results::MirrorBondStatesResult::Known::cursor) — never a key the caller kept for
1710    /// another reason. Resuming by `store_id` alone would drop every remaining root of the store
1711    /// the boundary fell inside.
1712    ///
1713    /// Both halves are LOWERCASE 64-hex, unprefixed, and a `0x` prefix is tolerated on input and
1714    /// normalized away by [`validated`](Self::validated). Anything else is REFUSED — see there for
1715    /// why a malformed cursor must never be read as "start from the beginning".
1716    #[serde(default, skip_serializing_if = "Option::is_none")]
1717    pub after: Option<results::MirrorBondKey>,
1718    /// The page size. `None` asks for [`MIRROR_BOND_STATES_DEFAULT_LIMIT`].
1719    ///
1720    /// A zero, or a value above [`MIRROR_BOND_STATES_MAX_LIMIT`], is REFUSED as `INVALID_PARAMS`
1721    /// rather than clamped — see [`Self::validated`].
1722    #[serde(default, skip_serializing_if = "Option::is_none")]
1723    pub limit: Option<u32>,
1724}
1725
1726impl MirrorBondStatesParams {
1727    /// The page size this request asks for, resolving `None` to
1728    /// [`MIRROR_BOND_STATES_DEFAULT_LIMIT`].
1729    ///
1730    /// Stated once here so the node and the client cannot resolve the same omitted field to two
1731    /// different numbers.
1732    pub fn effective_limit(&self) -> u32 {
1733        self.limit.unwrap_or(MIRROR_BOND_STATES_DEFAULT_LIMIT)
1734    }
1735
1736    /// Check the page bound, or reject as `-32602 INVALID_PARAMS`.
1737    ///
1738    /// `limit: 0` is refused because a page that can hold nothing makes no progress: a caller
1739    /// looping until `complete` would loop forever. A limit above the cap is refused rather than
1740    /// clamped so the caller's model of the page and the node's stay identical.
1741    ///
1742    /// [`after`](Self::after) is normalized on the same terms as every other hex id in this crate
1743    /// ([`WalletCoinsParams::validated`]) and a malformed one is REFUSED rather than dropped. That
1744    /// refusal is the point: this order is ascending over the key's STRING form, so a
1745    /// `0x`-prefixed key sorts before every canonical one and a node that quietly ignored it would
1746    /// RESTART the walk while looking like it resumed. On the surface a locked-$DIG total is
1747    /// summed from, a silently repeated page is wrong in the reassuring direction, and it is
1748    /// indistinguishable from a correct answer. Coercing an unparseable cursor to start-of-set is
1749    /// the same defect by another route, and it would contradict this method's own
1750    /// refuse-don't-clamp rule for `limit`.
1751    pub fn validated(self) -> Result<Self, ControlError> {
1752        if let Some(limit) = self.limit {
1753            if limit == 0 || limit > MIRROR_BOND_STATES_MAX_LIMIT {
1754                return Err(ControlError::of(
1755                    ControlErrorCode::InvalidParams,
1756                    MIRROR_BOND_STATES_LIMIT_ERROR,
1757                ));
1758            }
1759        }
1760        let after = self.after.map(normalize_bond_key).transpose()?;
1761        Ok(MirrorBondStatesParams { after, ..self })
1762    }
1763}
1764
1765/// Normalize both halves of a bond cursor, or reject the whole key.
1766///
1767/// Reuses [`normalize_coin_id`] rather than restating the rule: a store id, a root and a coin id
1768/// are all 32-byte hex on this wire, and a second copy of the rule is how two of three end up
1769/// accepting different spellings.
1770fn normalize_bond_key(key: results::MirrorBondKey) -> Result<results::MirrorBondKey, ControlError> {
1771    let malformed = || ControlError::of(ControlErrorCode::InvalidParams, MIRROR_BOND_AFTER_ERROR);
1772    let store_id = normalize_coin_id(&key.store_id)
1773        .ok_or_else(malformed)?
1774        .to_owned();
1775    let root = normalize_coin_id(&key.root)
1776        .ok_or_else(malformed)?
1777        .to_owned();
1778    Ok(results::MirrorBondKey { store_id, root })
1779}
1780
1781impl<'de> Deserialize<'de> for MirrorBondStatesParams {
1782    /// Validates on the way in, so a node cannot forget to call [`Self::validated`].
1783    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
1784    where
1785        D: serde::Deserializer<'de>,
1786    {
1787        #[derive(Deserialize)]
1788        struct Raw {
1789            #[serde(default)]
1790            after: Option<results::MirrorBondKey>,
1791            #[serde(default)]
1792            limit: Option<u32>,
1793        }
1794        let raw = Raw::deserialize(deserializer)?;
1795        MirrorBondStatesParams {
1796            after: raw.after,
1797            limit: raw.limit,
1798        }
1799        .validated()
1800        .map_err(serde::de::Error::custom)
1801    }
1802}
1803control_call!(MirrorBondStatesParams => ControlMethod::MirrorBondStates, results::MirrorBondStatesResult);
1804
1805#[cfg(test)]
1806mod tests {
1807    use super::*;
1808    use crate::traits::build_request;
1809    use serde_json::json;
1810
1811    /// An asset id that is emphatically NOT $DIG, so a round-trip through it cannot be satisfied
1812    /// by an implementation that only ever answers about $DIG.
1813    const OTHER_CAT_HEX: &str = "1c2b3a4d5e6f708192a3b4c5d6e7f8091a2b3c4d5e6f708192a3b4c5d6e7f809";
1814
1815    /// The legacy spellings are what an ALREADY-DEPLOYED dig-node and dig-app emit today. This is
1816    /// the load-bearing additive-discipline test (§5.1): if widening the enum ever stops accepting
1817    /// them, every peer built before this release becomes unreadable.
1818    #[test]
1819    fn legacy_xch_and_dig_spellings_still_deserialize() {
1820        assert_eq!(
1821            serde_json::from_value::<Asset>(json!("xch")).unwrap(),
1822            Asset::Xch
1823        );
1824        assert_eq!(
1825            serde_json::from_value::<Asset>(json!("dig")).unwrap(),
1826            Asset::DIG
1827        );
1828    }
1829
1830    /// $DIG keeps EMITTING its legacy token. A newer client talking to an OLDER node must still be
1831    /// understood, and an older node knows only `"xch"`/`"dig"` — so emitting `{"cat":…}` for $DIG
1832    /// would break the compatibility direction the legacy test above cannot see.
1833    #[test]
1834    fn dig_still_serializes_to_its_legacy_token() {
1835        assert_eq!(serde_json::to_value(Asset::DIG).unwrap(), json!("dig"));
1836        assert_eq!(serde_json::to_value(Asset::Xch).unwrap(), json!("xch"));
1837    }
1838
1839    /// The new capability: an arbitrary CAT, named by asset id, survives a full round-trip.
1840    #[test]
1841    fn an_arbitrary_cat_round_trips_by_asset_id() {
1842        let asset = Asset::Cat(AssetId::from_hex(OTHER_CAT_HEX).unwrap());
1843        let wire = serde_json::to_value(asset).unwrap();
1844        assert_eq!(wire, json!({ "cat": OTHER_CAT_HEX }));
1845        assert_eq!(serde_json::from_value::<Asset>(wire).unwrap(), asset);
1846    }
1847
1848    /// $DIG has exactly ONE value, however it was spelled on the wire.
1849    ///
1850    /// This is the test that rules out a three-variant `{Xch, Dig, Cat(id)}`, where
1851    /// `Dig != Cat(DIG_ASSET_ID)` and a balance filtered by one spelling silently omits the coins
1852    /// carrying the other — a wallet reporting half a balance as if it were the whole.
1853    #[test]
1854    fn dig_spelled_either_way_is_one_and_the_same_value() {
1855        let via_token: Asset = serde_json::from_value(json!("dig")).unwrap();
1856        let via_asset_id: Asset =
1857            serde_json::from_value(json!({ "cat": Asset::DIG_ASSET_ID_HEX })).unwrap();
1858        assert_eq!(via_token, via_asset_id);
1859        assert_eq!(via_token, Asset::DIG);
1860        assert!(via_asset_id.is_dig());
1861    }
1862
1863    /// The 64-hex length is a published bound, so it is pinned from BOTH sides: one nibble short
1864    /// and one nibble long must both fail, and exactly 64 must pass.
1865    #[test]
1866    fn asset_id_hex_length_is_bounded_on_both_sides() {
1867        assert!(AssetId::from_hex(&"a".repeat(63)).is_err());
1868        assert!(AssetId::from_hex(&"a".repeat(64)).is_ok());
1869        assert!(AssetId::from_hex(&"a".repeat(65)).is_err());
1870    }
1871
1872    /// Input is tolerant of the two forms a human copies out of a block explorer; output is not.
1873    #[test]
1874    fn asset_id_normalizes_prefix_and_case_but_emits_lowercase_unprefixed() {
1875        let upper = OTHER_CAT_HEX.to_uppercase();
1876        let canonical = AssetId::from_hex(OTHER_CAT_HEX).unwrap();
1877        assert_eq!(AssetId::from_hex(&upper).unwrap(), canonical);
1878        assert_eq!(AssetId::from_hex(&format!("0x{upper}")).unwrap(), canonical);
1879        assert_eq!(canonical.to_hex(), OTHER_CAT_HEX);
1880    }
1881
1882    /// The $DIG asset id is written twice in this file — once as hex for callers, once as bytes so
1883    /// [`Asset::DIG`] can be `const`. A typo in either would make `"dig"` and the tagged form name
1884    /// different tokens, which is precisely the split this design exists to prevent.
1885    #[test]
1886    fn dig_asset_id_hex_and_bytes_are_the_same_id() {
1887        let from_hex = AssetId::from_hex(Asset::DIG_ASSET_ID_HEX).unwrap();
1888        assert_eq!(Asset::DIG.asset_id(), Some(&from_hex));
1889        assert_eq!(from_hex.to_hex(), Asset::DIG_ASSET_ID_HEX);
1890        assert_eq!(Asset::Xch.asset_id(), None);
1891    }
1892
1893    /// A rejection has to say WHICH way the input was wrong, or the caller is left guessing at a
1894    /// value it can see but cannot fix.
1895    #[test]
1896    fn parse_errors_name_the_defect_and_reach_the_wire() {
1897        assert_eq!(
1898            AssetId::from_hex("ab").unwrap_err(),
1899            AssetIdParseError::WrongLength { got: 2 }
1900        );
1901        assert!(AssetIdParseError::WrongLength { got: 2 }
1902            .to_string()
1903            .contains("64 hex characters"));
1904        assert!(AssetIdParseError::NotHex
1905            .to_string()
1906            .contains("non-hexadecimal"));
1907        assert!(AssetId::from_hex(&"3c".repeat(32))
1908            .unwrap()
1909            .to_string()
1910            .starts_with("3c3c"));
1911        // The deserializer surfaces the reason rather than a bare "invalid value".
1912        let err = serde_json::from_value::<Asset>(json!({ "cat": "zz" })).unwrap_err();
1913        assert!(err.to_string().contains("64 hex characters"), "{err}");
1914    }
1915
1916    #[test]
1917    fn malformed_assets_are_rejected_rather_than_guessed_at() {
1918        assert!(AssetId::from_hex(&"z".repeat(64)).is_err());
1919        // An unknown bare token is not silently treated as a CAT name.
1920        assert!(serde_json::from_value::<Asset>(json!("usdc")).is_err());
1921        // The tagged form carries exactly one recognized key.
1922        assert!(serde_json::from_value::<Asset>(json!({})).is_err());
1923        assert!(serde_json::from_value::<Asset>(json!({ "tail": OTHER_CAT_HEX })).is_err());
1924        assert!(serde_json::from_value::<Asset>(json!({ "cat": "ab" })).is_err());
1925    }
1926
1927    /// The params types that CARRY an asset must carry the widened one — the whole point of the
1928    /// change is that these two questions become askable about an arbitrary CAT.
1929    #[test]
1930    fn balance_and_coins_reads_can_name_an_arbitrary_cat() {
1931        let cat = Asset::Cat(AssetId::from_hex(OTHER_CAT_HEX).unwrap());
1932        assert_eq!(
1933            serde_json::to_value(WalletBalanceParams {
1934                address: "xch1exampleaddr".into(),
1935                asset: cat,
1936            })
1937            .unwrap(),
1938            json!({ "address": "xch1exampleaddr", "asset": { "cat": OTHER_CAT_HEX } })
1939        );
1940        assert_eq!(
1941            serde_json::to_value(WalletCoinsParams::first_page("xch1exampleaddr", cat)).unwrap(),
1942            json!({ "address": "xch1exampleaddr", "asset": { "cat": OTHER_CAT_HEX } })
1943        );
1944    }
1945
1946    #[test]
1947    fn no_param_call_serializes_params_to_empty_object() {
1948        let req = build_request(1.into(), &StatusParams {});
1949        assert_eq!(req.method, "control.status");
1950        assert_eq!(req.params, json!({}));
1951    }
1952
1953    #[test]
1954    fn data_param_call_carries_its_fields() {
1955        let req = build_request(2.into(), &SetCapParams { cap_bytes: 128 });
1956        assert_eq!(req.method, "control.cache.setCap");
1957        assert_eq!(req.params, json!({ "cap_bytes": 128 }));
1958    }
1959
1960    #[test]
1961    fn pause_omits_until_when_indefinite() {
1962        assert_eq!(
1963            serde_json::to_value(PauseParams { until: None }).unwrap(),
1964            json!({})
1965        );
1966        assert_eq!(
1967            serde_json::to_value(PauseParams { until: Some(99) }).unwrap(),
1968            json!({ "until": 99 })
1969        );
1970    }
1971
1972    #[test]
1973    fn method_binding_matches_the_catalog_name() {
1974        assert_eq!(
1975            SetUpstreamParams::METHOD.name(),
1976            "control.config.setUpstream"
1977        );
1978        assert_eq!(RequestParams::METHOD.name(), "pairing.request");
1979        assert_eq!(PollParams::METHOD.name(), "pairing.poll");
1980    }
1981
1982    /// **Two spellings of one address canonicalise to one key — which is what makes `remove` able
1983    /// to find what `add` stored.**
1984    ///
1985    /// The fixture is deliberately not a pair of identical strings: each pair differs in the ways
1986    /// a person actually types an address (case, leading zeroes, an uncompressed run, whitespace),
1987    /// so a canonicaliser that only trimmed would pass the last pair and fail the rest.
1988    #[test]
1989    fn spellings_of_the_same_peer_canonicalise_to_one_key() {
1990        for (typed, also_typed) in [
1991            ("2001:0db8:0000:0000:0000:0000:0000:0001", "2001:DB8::1"),
1992            ("::1", "0:0:0:0:0:0:0:1"),
1993            (" 203.0.113.7 ", "203.0.113.7"),
1994        ] {
1995            let a = canonical_peer_ip(typed).expect("typed form is a literal");
1996            let b = canonical_peer_ip(also_typed).expect("second form is a literal");
1997            assert_eq!(a, b, "{typed} and {also_typed} name one peer");
1998        }
1999        // And the canonical form is the compressed lowercase one, not merely SOME agreed form.
2000        assert_eq!(canonical_peer_ip("2001:DB8:0:0::1").unwrap(), "2001:db8::1");
2001    }
2002
2003    /// **Anything that is not a bare IP literal is REFUSED.**
2004    ///
2005    /// This is the bound on the ban list: `remove {ban: true}` persists a row keyed by this
2006    /// string, so accepting arbitrary text is unbounded at-rest growth for the cost of one call.
2007    /// Each rejected case is one a naive implementation accepts: a hostname resolves, a bracketed
2008    /// form round-trips through some parsers, and `ip:port` is what a person copies out of a log.
2009    #[test]
2010    fn a_peer_ip_that_is_not_a_bare_literal_is_refused() {
2011        for bad in [
2012            "",
2013            "   ",
2014            "node.example.com",
2015            "[2001:db8::1]",
2016            "[2001:db8::1]:8444",
2017            "203.0.113.7:8444",
2018            "203.0.113.0/24",
2019            "not an address",
2020        ] {
2021            let Err(err) = canonical_peer_ip(bad) else {
2022                panic!("{bad:?} must be refused, it was accepted");
2023            };
2024            assert_eq!(
2025                err.code_enum(),
2026                Some(ControlErrorCode::InvalidParams),
2027                "{bad:?} must be an INVALID_PARAMS refusal, not some other failure"
2028            );
2029        }
2030    }
2031
2032    /// **Joining an address to a port brackets IPv6 — the un-bracketed form is a DIFFERENT valid
2033    /// address, so the mistake survives validation.**
2034    ///
2035    /// `::1` + `8444` formatted by hand is `::1:8444`, which parses fine and points somewhere
2036    /// else. The assertion is therefore not merely "it contains brackets": the naive rendering is
2037    /// checked to be a parseable address that is NOT the peer, which is what makes the bug silent.
2038    #[test]
2039    fn joining_a_v6_peer_to_a_port_cannot_produce_a_different_address() {
2040        assert_eq!(chia_peer_endpoint("::1", 8444), "[::1]:8444");
2041        assert_eq!(
2042            chia_peer_endpoint("2001:db8::1", 8444),
2043            "[2001:db8::1]:8444"
2044        );
2045        assert_eq!(chia_peer_endpoint("203.0.113.7", 8444), "203.0.113.7:8444");
2046
2047        let naive = format!("{}:{}", "::1", 8444);
2048        let hijacked: std::net::IpAddr =
2049            naive.parse().expect("the naive join is itself an address");
2050        assert_ne!(
2051            hijacked,
2052            "::1".parse::<std::net::IpAddr>().unwrap(),
2053            "the naive join must be a DIFFERENT address — that is why the helper exists"
2054        );
2055        assert_ne!(chia_peer_endpoint("::1", 8444), naive);
2056    }
2057}
2058
2059/// The default local safety margin, in basis points — `+1%`.
2060///
2061/// The same value as `dig_mirror_collateral::SAFETY_MARGIN_BP_DEFAULT`, restated here rather than
2062/// imported: `dig-mirror-collateral` sits at the SAME crate level as this contract, and a
2063/// same-level dependency is forbidden (CLAUDE.md Appendix B). It is published on the contract so a
2064/// config written before the field existed loads as the default rather than as a zero margin.
2065///
2066/// The default errs HIGH because the failure is asymmetric: under-posting likely costs an epoch's
2067/// rewards, while over-posting costs only the opportunity cost of the locked $DIG.
2068pub const DEFAULT_SAFETY_MARGIN_BP: u64 = 100;
2069
2070/// The largest safety margin a node accepts, in basis points — `10_000`, i.e. +100%.
2071///
2072/// A margin is a cushion against the requirement rising, so doubling the requirement is already far
2073/// past any honest cushion. The bound exists because `.set` is a MONEY-PATH mutation reachable with
2074/// an ordinary paired token: an unbounded `u64` lets a caller commit the operator to locking an
2075/// arbitrary multiple of every store's requirement, and the margin arithmetic saturates rather than
2076/// failing, so an absurd value produces a silently enormous posting instead of an error.
2077pub const MAX_SAFETY_MARGIN_BP: u64 = 10_000;
2078
2079no_params!(
2080    /// `control.collateral.requirement` params (none).
2081    ///
2082    /// The caller supplies no epoch: the answer is the epoch the NODE currently derives, so a
2083    /// caller-named epoch would invite a client to render a requirement for an epoch that is not
2084    /// the one being posted against.
2085    CollateralRequirementParams => ControlMethod::CollateralRequirement,
2086    results::CollateralRequirementResult
2087);
2088/// The horizon, in future epochs, a node covers when it recommends a buffer without being told
2089/// otherwise — `4`.
2090///
2091/// Published on the contract so a client can recognise the ordinary case, NOT so it can assume one:
2092/// the horizon a node actually used always travels in
2093/// [`CollateralBufferResult::Known`](crate::results::CollateralBufferResult::Known), and a reader
2094/// that substituted this constant for a payload it failed to read would state a claim the node
2095/// never made.
2096///
2097/// Four epochs bounds the compounded escalation ceiling at roughly x1.60. Fewer leaves a node one
2098/// bad epoch from `dangerously_low`; many more prices in a x4.62 worst case the controller reaches
2099/// only by escalating every single epoch, and locking $DIG against it has a real opportunity cost.
2100pub const DEFAULT_BUFFER_HORIZON_EPOCHS: u32 = 4;
2101
2102/// The per-epoch escalation step denominator — `8`, i.e. the requirement can rise by at most
2103/// `+1/8` (`+12.5%`) in one epoch.
2104///
2105/// The same value as `dig_mirror_collateral::UP_STEP_DENOM`, restated here for the same reason
2106/// [`DEFAULT_SAFETY_MARGIN_BP`] is: `dig-mirror-collateral` sits at the SAME crate level as this
2107/// contract and a same-level dependency is forbidden (CLAUDE.md Appendix B).
2108///
2109/// It is a CEILING on one epoch's rise, and it compounds across epochs — which is precisely why the
2110/// horizon must travel with any buffer derived from it. It is not a forecast: inside the
2111/// controller's dead band the requirement does not move at all.
2112pub const ESCALATION_UP_STEP_DENOM: u64 = 8;
2113
2114no_params!(
2115    /// `control.collateral.margin.get` params (none).
2116    CollateralMarginGetParams => ControlMethod::CollateralMarginGet,
2117    results::CollateralMarginResult
2118);
2119
2120/// `control.collateral.margin.set` params.
2121#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
2122pub struct CollateralMarginSetParams {
2123    /// The margin in BASIS POINTS over the requirement (`100` is +1%), at most
2124    /// [`MAX_SAFETY_MARGIN_BP`].
2125    ///
2126    /// Basis points, never a percentage and never a float: it is the unit
2127    /// `dig_mirror_collateral::apply_safety_margin` takes and the unit dig-app `SPEC.md` §3.7b
2128    /// fixes for `collateral.margin_bp`. A 1 bp margin (0.01%) is a legal choice and any conversion
2129    /// to whole percent would erase it.
2130    pub margin_bp: u64,
2131}
2132
2133impl CollateralMarginSetParams {
2134    /// Refuse a margin above [`MAX_SAFETY_MARGIN_BP`] as `-32602 INVALID_PARAMS`.
2135    ///
2136    /// Refused rather than clamped, and the asymmetry with dig-app is deliberate. dig-app CLAMPS a
2137    /// stored margin that exceeds its own ceiling, because refusing a value already on disk would
2138    /// leave the node posting the lower amount it was trying to move away from. This is the
2139    /// opposite situation: a caller is stating an intent right now, and silently applying a
2140    /// different number than the one requested would make a subsequent
2141    /// [`CollateralMarginResult`](crate::results::CollateralMarginResult) disagree with what the
2142    /// caller believes it set — on the money path.
2143    pub fn validated(self) -> Result<Self, ControlError> {
2144        if self.margin_bp > MAX_SAFETY_MARGIN_BP {
2145            return Err(ControlError::of(
2146                ControlErrorCode::InvalidParams,
2147                format!(
2148                    "margin_bp must be at most {MAX_SAFETY_MARGIN_BP} basis points (+100%); got {}",
2149                    self.margin_bp
2150                ),
2151            ));
2152        }
2153        Ok(self)
2154    }
2155}
2156control_call!(CollateralMarginSetParams => ControlMethod::CollateralMarginSet, results::CollateralMarginResult);
2157
2158no_params!(
2159    /// `control.collateral.buffer` params (none).
2160    ///
2161    /// The caller names neither an epoch nor a horizon. Both are the NODE's: the served set and
2162    /// the reclaim state the buffer rests on exist only for the epoch being posted against, and a
2163    /// caller-chosen horizon would let a client quietly shrink the recommendation on a money
2164    /// surface by asking for a shorter one. The horizon the node used is returned instead.
2165    CollateralBufferParams => ControlMethod::CollateralBuffer,
2166    results::CollateralBufferResult
2167);