dig_constants/lib.rs
1//! DIG Network Constants
2//!
3//! Defines network parameters for the DIG L2 blockchain. This crate exists
4//! separately so that any DIG crate can import network constants without
5//! pulling in the full CLVM engine or other heavy dependencies.
6//!
7//! The core type is [`NetworkConstants`], which wraps `chia-consensus`'s
8//! `ConsensusConstants` with DIG-specific values (genesis challenge,
9//! AGG_SIG additional data, cost limits, etc.).
10//!
11//! # Chia L1 vs DIG L2 (do not mix)
12//!
13//! [`DIG_MAINNET`] / [`DIG_TESTNET`] describe the DIG **L2** network. Separately,
14//! [`CHIA_L1_MAINNET_AGG_SIG_ME`] / [`CHIA_L1_TESTNET11_AGG_SIG_ME`] hold the
15//! **Chia L1 (foreign chain)** genesis challenge that DIG wallet code needs as
16//! AGG_SIG_ME additional data when signing L1 spends. They live here as the
17//! ecosystem's single source of truth, but are DELIBERATELY distinct from the DIG
18//! L2 genesis — signing an L1 spend with the DIG L2 genesis produces an invalid
19//! signature. The `CHIA_L1_` prefix is the anti-mixup guard.
20//!
21//! # Usage
22//!
23//! ```rust,ignore
24//! use dig_constants::DIG_MAINNET;
25//!
26//! let genesis = DIG_MAINNET.genesis_challenge();
27//! let consensus = DIG_MAINNET.consensus();
28//! ```
29
30use chia_consensus::consensus_constants::ConsensusConstants;
31use chia_protocol::Bytes32;
32use hex_literal::hex;
33
34/// DIG network constants.
35///
36/// Wraps `chia-consensus::ConsensusConstants` with accessors for the fields
37/// that DIG validators and wallet code commonly need. The underlying
38/// `ConsensusConstants` is available via [`consensus()`](Self::consensus)
39/// for direct use with `chia-consensus` functions like `run_spendbundle()`.
40#[derive(Debug, Clone)]
41pub struct NetworkConstants {
42 inner: ConsensusConstants,
43}
44
45impl NetworkConstants {
46 /// The underlying `chia-consensus` constants, for passing directly to
47 /// `run_spendbundle()`, `validate_clvm_and_signature()`, etc.
48 pub fn consensus(&self) -> &ConsensusConstants {
49 &self.inner
50 }
51
52 /// DIG genesis challenge.
53 pub fn genesis_challenge(&self) -> Bytes32 {
54 self.inner.genesis_challenge
55 }
56
57 /// AGG_SIG_ME additional data (== genesis_challenge on Chia L1).
58 pub fn agg_sig_me_additional_data(&self) -> Bytes32 {
59 self.inner.agg_sig_me_additional_data
60 }
61
62 /// Maximum CLVM cost per block.
63 pub fn max_block_cost_clvm(&self) -> u64 {
64 self.inner.max_block_cost_clvm
65 }
66
67 /// Cost per byte of generator program.
68 pub fn cost_per_byte(&self) -> u64 {
69 self.inner.cost_per_byte
70 }
71
72 /// Maximum coin amount (u64::MAX).
73 pub fn max_coin_amount(&self) -> u64 {
74 self.inner.max_coin_amount
75 }
76}
77
78// =============================================================================
79// AGG_SIG additional data derivation
80//
81// On Chia L1, each AGG_SIG_* variant's additional_data is:
82// sha256(genesis_challenge || opcode_byte)
83// except AGG_SIG_ME which uses genesis_challenge directly.
84//
85// See: condition_tools.py:58-71
86// https://github.com/Chia-Network/chia-blockchain/blob/main/chia/consensus/condition_tools.py#L58
87// =============================================================================
88
89// ---------------------------------------------------------------------------
90// DIG Mainnet
91//
92// The genesis challenge is the 32-byte consensus anchor for the DIG L2 network.
93// It doubles as the gossip `network_id` gate: `dig-gossip` REJECTS an all-zero
94// network_id, so this value MUST be non-zero for the node's gossip pool / DHT /
95// PEX to start.
96//
97// DIG_MAINNET L2 genesis = the Chia mainnet header hash @ height 9,021,277
98// (0af981...1abf), pinned 2026-07-17 — anchors the DIG L2 genesis to a real,
99// verifiable Chia block (captured via coinset.org get_blockchain_state).
100//
101// DIG_MAINNET_GENESIS_CHALLENGE
102// = 0af981862a4df51f51ec59c312315d959931d917c375730b89b9e2b0854d1abf
103//
104// This is the PRE-LAUNCH canonical DIG mainnet genesis. Per CLAUDE.md §3.7 the
105// ecosystem is pre-release with no live users, so this value is revisable at
106// true mainnet launch — re-anchor to the launch-time Chia header hash and
107// recompute every derived value below if it is ever changed.
108//
109// All `agg_sig_*_additional_data` values are derived from this genesis as
110// `sha256(genesis_challenge || opcode_byte)` (AGG_SIG_ME = genesis directly),
111// so they were all recomputed for this genesis.
112// ---------------------------------------------------------------------------
113
114/// Canonical DIG mainnet genesis challenge.
115///
116/// The Chia mainnet header hash at block height 9,021,277 (`0af981…1abf`),
117/// pinned 2026-07-17 — a real, verifiable, fixed 32-byte value anchoring the
118/// DIG L2 genesis to a real Chia block. This is the pre-launch canonical value;
119/// per §3.7 it is revisable at true mainnet launch. All
120/// `agg_sig_*_additional_data` fields are derived from this.
121const DIG_MAINNET_GENESIS_CHALLENGE: [u8; 32] =
122 hex!("0af981862a4df51f51ec59c312315d959931d917c375730b89b9e2b0854d1abf");
123
124/// DIG mainnet constants.
125///
126/// Uses DIG's own genesis challenge and AGG_SIG domain separation.
127/// Proof-of-space and VDF fields are set to neutral values since DIG L2
128/// does not use Chia's proof-of-space consensus.
129pub const DIG_MAINNET: NetworkConstants = NetworkConstants {
130 inner: ConsensusConstants {
131 // -- DIG-specific values --
132 genesis_challenge: Bytes32::new(DIG_MAINNET_GENESIS_CHALLENGE),
133
134 // AGG_SIG additional data: derived from genesis_challenge.
135 // AGG_SIG_ME = genesis_challenge directly.
136 // Others = sha256(genesis_challenge || opcode_byte).
137 // Derivation: condition_tools.py:58-71
138 // https://github.com/Chia-Network/chia-blockchain/blob/main/chia/consensus/condition_tools.py#L58
139 // Opcode bytes: AGG_SIG_PARENT=43, PUZZLE=44, AMOUNT=45,
140 // PUZZLE_AMOUNT=46, PARENT_AMOUNT=47, PARENT_PUZZLE=48
141 // NOTE: Recompute ALL values when genesis_challenge is finalized.
142 agg_sig_me_additional_data: Bytes32::new(DIG_MAINNET_GENESIS_CHALLENGE),
143 agg_sig_parent_additional_data: Bytes32::new(hex!(
144 "196d63b6dfbd4440656f9c1eadc686cacfaae771c565762a8cd6e51c892a0077"
145 )),
146 agg_sig_puzzle_additional_data: Bytes32::new(hex!(
147 "9ca719659b5e2355a91ff330c8612cb58c74f1063eaff99e507602d450b1f71f"
148 )),
149 agg_sig_amount_additional_data: Bytes32::new(hex!(
150 "d13767da4a8bd9520dbd9e039e68b3eb4b16fdcbb7e7755b5064840eaeb553ce"
151 )),
152 agg_sig_puzzle_amount_additional_data: Bytes32::new(hex!(
153 "73eea3473bd0daa28793d4bcd218ade462b634b53af97f9a01a91f3059ac75df"
154 )),
155 agg_sig_parent_amount_additional_data: Bytes32::new(hex!(
156 "eb7302224e77c0f269d0c8b105d4cc786775ae012ed2db49751c33c244c3f647"
157 )),
158 agg_sig_parent_puzzle_additional_data: Bytes32::new(hex!(
159 "ccac5983685257d50ee7b439bbb502128ddb262813dde4e4a11ac6cdfc66fa8e"
160 )),
161
162 // DIG L2 cost limits
163 max_block_cost_clvm: 11_000_000_000, // per-spend limit, same as Chia L1
164 cost_per_byte: 12_000,
165 max_coin_amount: u64::MAX,
166
167 // Block generator limits
168 max_generator_size: 1_000_000,
169 max_generator_ref_list_size: 512,
170
171 // Hard fork heights — set to 0 to always use latest consensus rules.
172 // DIG L2 starts with all features enabled from block 0.
173 hard_fork_height: 0,
174 hard_fork2_height: 0,
175
176 // Pre-farm puzzle hashes — not used by DIG L2, set to zero.
177 genesis_pre_farm_pool_puzzle_hash: Bytes32::new([0u8; 32]),
178 genesis_pre_farm_farmer_puzzle_hash: Bytes32::new([0u8; 32]),
179
180 // -- Proof-of-space / VDF fields (not used by DIG L2) --
181 // These must be valid values since ConsensusConstants is passed to
182 // chia-consensus functions, but DIG does not use PoS consensus.
183 slot_blocks_target: 32,
184 min_blocks_per_challenge_block: 16,
185 max_sub_slot_blocks: 128,
186 num_sps_sub_slot: 64,
187 sub_slot_iters_starting: 1 << 27,
188 difficulty_constant_factor: 1 << 67,
189 difficulty_starting: 7,
190 difficulty_change_max_factor: 3,
191 sub_epoch_blocks: 384,
192 epoch_blocks: 4608,
193 significant_bits: 8,
194 discriminant_size_bits: 1024,
195 number_zero_bits_plot_filter_v1: 9,
196 number_zero_bits_plot_filter_v2: 9,
197 min_plot_size_v1: 32,
198 max_plot_size_v1: 50,
199 min_plot_size_v2: 28,
200 max_plot_size_v2: 32,
201 sub_slot_time_target: 600,
202 num_sp_intervals_extra: 3,
203 max_future_time2: 120,
204 number_of_timestamps: 11,
205 max_vdf_witness_size: 64,
206 mempool_block_buffer: 10,
207 weight_proof_threshold: 2,
208 blocks_cache_size: 4608 + (128 * 4),
209 weight_proof_recent_blocks: 1000,
210 max_block_count_per_requests: 32,
211 pool_sub_slot_iters: 37_600_000_000,
212 plot_filter_128_height: 0xffff_ffff,
213 plot_filter_64_height: 0xffff_ffff,
214 plot_filter_32_height: 0xffff_ffff,
215 plot_difficulty_initial: 2,
216 plot_difficulty_4_height: 0xffff_ffff,
217 plot_difficulty_5_height: 0xffff_ffff,
218 plot_difficulty_6_height: 0xffff_ffff,
219 plot_difficulty_7_height: 0xffff_ffff,
220 plot_difficulty_8_height: 0xffff_ffff,
221 },
222};
223
224// =============================================================================
225// NAT-traversal relay endpoint
226//
227// A DIG Node behind NAT cannot accept inbound dials, so it holds a constant
228// reservation with a publicly-reachable relay to stay discoverable. The
229// canonical public relay is `relay.dig.net`, serving the `RelayMessage`
230// WebSocket wire (RLY-001..RLY-007) on port 9450.
231//
232// This constant is the single source of truth for that endpoint so consumers
233// (`dig-node`, `dig-gossip`) don't each hardcode it. It MUST stay byte-identical
234// to `dig-node`'s `relay::DEFAULT_RELAY_URL` (the string a node actually dials
235// when `DIG_RELAY_URL` is unset) and to the `dig-relay` server's documented
236// client endpoint.
237//
238// Port 443: the live `relay.dig.net` NLB exposes its public TLS listener on the
239// standard HTTPS port 443 (the earlier :9450 listener is closed). Using 443 also
240// maximizes reachability from restrictive networks that only allow outbound 443.
241// =============================================================================
242
243/// Canonical DIG NAT-traversal relay endpoint.
244///
245/// This is the WebSocket URL a DIG Node dials by default to obtain a relay
246/// reservation (so NAT'd peers stay reachable). It is the value used unless an
247/// operator overrides it via the `DIG_RELAY_URL` environment variable (or
248/// disables the reservation with `DIG_RELAY_URL=off`).
249///
250/// Format: `wss://<host>:<port>` — the relay protocol (`RelayMessage`,
251/// RLY-001..RLY-007) is JSON over a secure WebSocket. Mainnet uses the canonical
252/// public deployment `relay.dig.net` on port 443 (the live NLB public TLS
253/// listener; the earlier :9450 listener is closed).
254///
255/// Kept byte-identical to `dig-node`'s `relay::DEFAULT_RELAY_URL` and the
256/// `dig-relay` server's documented client endpoint.
257pub const DIG_RELAY_URL: &str = "wss://relay.dig.net:443";
258
259// =============================================================================
260// DIG Node localhost endpoint
261//
262// A client connecting to a local DIG node (§5.3 client→node connection order)
263// resolves `dig.local` or `localhost` to reach the node via localhost TCP on
264// port 9778. This constant is the single source of truth for that port so
265// consumers (dig-node, dig-dns, dig-installer, SDK, CLI) don't each hardcode it.
266// =============================================================================
267
268/// The default localhost port a client uses to reach the local DIG node.
269///
270/// This is used to implement §5.3 client→node connection order: when a client
271/// needs to connect to a DIG node, it tries `dig.local` and `localhost` on this
272/// port before falling back to the public `rpc.dig.net` gateway. This constant
273/// ensures all consumers (dig-node, dig-dns, dig-installer, dig-sdk, digstore CLI)
274/// use an identical port, preventing port-mismatch bugs. It MUST stay byte-identical
275/// to `dig-node`'s documented localhost serve port and the installer's registered
276/// `dig.local` address.
277pub const DIG_NODE_PORT: u16 = 9778;
278
279/// The mDNS/local hostname the installed DIG node registers.
280///
281/// This is the FIRST tier of the §5.3 client→node connection order: a client
282/// tries `dig.local` (on [`DIG_NODE_PORT`]) before falling back to `localhost`
283/// and finally the public [`RPC_DIG_NET_URL`] gateway. This constant ensures
284/// all consumers (dig-node, dig-dns, dig-installer, dig-sdk, digstore CLI) use
285/// an identical hostname, preventing drift between the address the installer
286/// registers and the address clients probe.
287pub const DIG_LOCAL_HOST: &str = "dig.local";
288
289/// The public DIG read gateway.
290///
291/// This is the FINAL-FALLBACK tier of the §5.3 client→node connection order:
292/// a client falls through to this plain-HTTPS public read tier only when
293/// neither `dig.local` nor `localhost` (both on [`DIG_NODE_PORT`]) responds.
294/// This constant ensures all consumers (dig-download, digstore CLI, dig-sdk,
295/// dig-node) reference an identical gateway URL instead of each hardcoding
296/// their own copy of `rpc.dig.net`.
297pub const RPC_DIG_NET_URL: &str = "https://rpc.dig.net";
298
299// =============================================================================
300// DIG CAT asset id ($DIG token)
301//
302// $DIG is a Chia CAT (CHIP-0004); its asset id is the TAIL program's hash,
303// fixed for the token's lifetime. This is the single canonical home for that
304// value — `chip35_dl_coin`, `dig-cat-decoder`, and any DIG-aware wallet/
305// balance/spend code import it from HERE rather than each hardcoding a copy.
306// =============================================================================
307
308/// Canonical $DIG CAT asset id (TAIL hash) on Chia mainnet.
309///
310/// The single token every capsule (commit) payment is denominated in
311/// (`chip35_dl_coin::build_dig_store_payment`) and the value a wallet/decoder
312/// checks a CAT coin's `asset_id` against to recognize $DIG.
313///
314/// CONTRACT: byte-identical to `chip35_dl_coin::DIG_ASSET_ID`, digstore-chain's
315/// `DIG_ASSET_ID`, and DataLayer-Driver's. Do not change without changing every
316/// consumer in lockstep (SYSTEM.md → Shared contracts → DIG CAT payment).
317pub const DIG_ASSET_ID: Bytes32 = Bytes32::new(hex!(
318 "a406d3a9de984d03c9591c10d917593b434d5263cabe2b42f6b367df16832f81"
319));
320
321// =============================================================================
322// DIG treasury recipient (destination of $DIG payments + dev-tips)
323//
324// Every $DIG capsule/commit payment and dev-tip is created-coin'd to the DIG
325// treasury. This section is the single canonical home for that recipient in two
326// equivalent forms: the on-chain inner (standard) puzzle hash and its bech32m
327// address. A WRONG value here silently MISDIRECTS funds to an attacker/void —
328// a custody break — so both forms are pinned byte-for-byte by tests, and a KAT
329// proves the address decodes to the puzzle hash (they cannot drift apart).
330//
331// CONTRACT: dig-constants is the intended canonical LOWEST-level home for this
332// value. The existing higher-level copies (`digstore_chain::dig`,
333// `chip35_dl_coin`, `dighub-core`) SHOULD later converge to re-export from HERE.
334// That convergence is a SEPARATE follow-up — this change only introduces the
335// canonical constants; it does not touch those crates. Until convergence, this
336// value stays byte-identical to `digstore_chain::dig` (the current source of
337// truth: `TREASURY_ADDRESS` at `crates/digstore-chain/src/dig.rs:41`, from which
338// it derives `treasury_inner_puzzle_hash()`, pinned by its test at dig.rs:206-209).
339// =============================================================================
340
341/// Canonical DIG treasury inner (standard) puzzle hash.
342///
343/// The on-chain recipient every $DIG capsule/commit payment and dev-tip is
344/// created-coin'd to. A wrong value silently misdirects treasury funds (a
345/// custody break), so it is pinned byte-for-byte by a test.
346///
347/// CONTRACT: byte-identical to what `digstore_chain::dig::treasury_inner_puzzle_hash()`
348/// decodes to (pinned by that crate's test at `crates/digstore-chain/src/dig.rs:206-209`).
349/// dig-constants is the intended canonical lowest-level home; higher copies
350/// (`digstore_chain::dig`, `chip35_dl_coin`, `dighub-core`) should later
351/// re-export from here (a separate follow-up — see the section note above).
352pub const DIG_TREASURY_INNER_PUZZLE_HASH: Bytes32 = Bytes32::new(hex!(
353 "ec7c304708c7d59c078d5ae098d0dea004decf47fa1cafebb266c10ad6466ce8"
354));
355
356/// Canonical DIG treasury address (bech32m form of [`DIG_TREASURY_INNER_PUZZLE_HASH`]).
357///
358/// The human-readable `xch1…` form of the same treasury recipient — the
359/// destination of $DIG payments and dev-tips. A wrong value misdirects funds
360/// (a custody break), so it is pinned by a test AND a KAT proves it decodes to
361/// [`DIG_TREASURY_INNER_PUZZLE_HASH`] (the two forms cannot silently drift).
362///
363/// CONTRACT: digstore-chain's source-of-truth form (`digstore_chain::dig::TREASURY_ADDRESS`,
364/// `crates/digstore-chain/src/dig.rs:41`), from which it derives the puzzle hash
365/// at runtime. dig-constants is the intended canonical lowest-level home; higher
366/// copies should later re-export from here (a separate follow-up).
367pub const DIG_TREASURY_ADDRESS: &str =
368 "xch1a37rq3cgcl2ecpudttsf35x75qzdan68lgw2l6ajvmqs44jxdn5qv6pk3y";
369
370// =============================================================================
371// Chia L1 (foreign chain) AGG_SIG_ME additional data
372//
373// The DIG wallet signs and validates spends on the Chia L1 chain. On Chia L1 the
374// AGG_SIG_ME additional data IS the network genesis challenge, so every L1 spend
375// signature is bound to it. This is a FOREIGN chain's value — completely distinct
376// from the DIG L2 genesis (`DIG_MAINNET_GENESIS_CHALLENGE`, 0af98186…).
377//
378// Both the wallet's signer seam AND the engine's message-binding seam MUST read
379// the SAME 32 bytes from here, or a spend the engine builds is signed with a
380// different domain than it binds — a custody break (invalid, unspendable
381// signatures on mainnet). This crate is the single source of truth for those
382// bytes; the `[u8; 32]` shape matches the signer field directly (the engine wraps
383// it once via `Bytes32::new(...)`).
384//
385// The value is invariant-forced: it is exactly Chia's well-known mainnet genesis
386// (ccd5bb71…) / testnet11 genesis (37a90eb5…), the same values
387// `chia-wallet-sdk`'s `MAINNET_CONSTANTS` / `TESTNET11_CONSTANTS` carry (asserted
388// by an anti-drift dev-dependency test).
389// =============================================================================
390
391/// Chia **L1 mainnet** genesis challenge, used as AGG_SIG_ME additional data.
392///
393/// The 32-byte domain every Chia L1 mainnet spend signature is bound to. This is
394/// the foreign-chain (Chia) value — DISTINCT from the DIG L2 genesis
395/// ([`DIG_MAINNET`]); signing an L1 spend with the DIG L2 genesis yields an
396/// invalid signature.
397///
398/// CONTRACT: DIG wallet consumers (the client signer AND the engine's
399/// message-binding path) MUST both use this constant so signer == engine,
400/// producing byte-identical, valid signatures. Equals Chia's canonical mainnet
401/// genesis `ccd5bb71…` (== `chia_sdk_types::MAINNET_CONSTANTS.agg_sig_me_additional_data`).
402pub const CHIA_L1_MAINNET_AGG_SIG_ME: [u8; 32] =
403 hex!("ccd5bb71183532bff220ba46c268991a3ff07eb358e8255a65c30a2dce0e5fbb");
404
405/// Chia **L1 testnet11** genesis challenge, used as AGG_SIG_ME additional data.
406///
407/// The 32-byte domain every Chia L1 testnet11 spend signature is bound to. As
408/// with [`CHIA_L1_MAINNET_AGG_SIG_ME`], this is the foreign-chain (Chia) value,
409/// DISTINCT from the DIG L2 genesis ([`DIG_TESTNET`]).
410///
411/// CONTRACT: DIG wallet consumers (signer AND engine) MUST both use this constant
412/// so signer == engine on testnet11. Equals Chia's canonical testnet11 genesis
413/// `37a90eb5…` (== `chia_sdk_types::TESTNET11_CONSTANTS.agg_sig_me_additional_data`).
414pub const CHIA_L1_TESTNET11_AGG_SIG_ME: [u8; 32] =
415 hex!("37a90eb5185a9c4439a91ddc98bbadce7b4feba060d50116a067de66bf236615");
416
417// ---------------------------------------------------------------------------
418// DIG Testnet
419// ---------------------------------------------------------------------------
420
421/// Canonical DIG testnet genesis challenge.
422///
423/// Deterministically derived as `sha256(b"DIG_TESTNET:genesis:v1")` — distinct
424/// from mainnet so the two networks never share a `network_id`. Non-zero so the
425/// gossip network_id gate accepts it. Pre-launch canonical value (§3.7),
426/// revisable at true launch; all derived agg_sig data below follows it.
427/// = 088c18d6b7859d885dc2f03166e862c958f74b63b6353c3df71d103b9b806c3b
428const DIG_TESTNET_GENESIS_CHALLENGE: [u8; 32] =
429 hex!("088c18d6b7859d885dc2f03166e862c958f74b63b6353c3df71d103b9b806c3b");
430
431/// DIG testnet constants.
432///
433/// Same structure as mainnet but with a different genesis challenge.
434/// Useful for testing without risking mainnet state.
435pub const DIG_TESTNET: NetworkConstants = NetworkConstants {
436 inner: ConsensusConstants {
437 genesis_challenge: Bytes32::new(DIG_TESTNET_GENESIS_CHALLENGE),
438 // AGG_SIG_ME = genesis_challenge. Others = sha256(genesis || opcode_byte).
439 agg_sig_me_additional_data: Bytes32::new(DIG_TESTNET_GENESIS_CHALLENGE),
440 agg_sig_parent_additional_data: Bytes32::new(hex!(
441 "85b3963bdeb9848af970a9bbd1d36809ae41491ffd67aee7f27e8883936d495c"
442 )),
443 agg_sig_puzzle_additional_data: Bytes32::new(hex!(
444 "66aba1939e128e1465d58fde414325630e891747c1428d76ebce193cbe966301"
445 )),
446 agg_sig_amount_additional_data: Bytes32::new(hex!(
447 "eccab86920a6d982a68898b2dcb7c150383529fcd532fe84c693fb4592c38ae3"
448 )),
449 agg_sig_puzzle_amount_additional_data: Bytes32::new(hex!(
450 "eb088fad0d4caba66e29130fb07407e60a7545d035d19a188fef0855c874084e"
451 )),
452 agg_sig_parent_amount_additional_data: Bytes32::new(hex!(
453 "232aec0a351ba4936b04920e074aebcc621a458f6b1461c4b28c658552f2f35d"
454 )),
455 agg_sig_parent_puzzle_additional_data: Bytes32::new(hex!(
456 "96263ac395703ab9b3b0f0587e79185f4a9898574a28b4491015ddcf9d321873"
457 )),
458 // All other fields same as mainnet
459 max_block_cost_clvm: 11_000_000_000,
460 cost_per_byte: 12_000,
461 max_coin_amount: u64::MAX,
462 max_generator_size: 1_000_000,
463 max_generator_ref_list_size: 512,
464 hard_fork_height: 0,
465 hard_fork2_height: 0,
466 genesis_pre_farm_pool_puzzle_hash: Bytes32::new([0u8; 32]),
467 genesis_pre_farm_farmer_puzzle_hash: Bytes32::new([0u8; 32]),
468 slot_blocks_target: 32,
469 min_blocks_per_challenge_block: 16,
470 max_sub_slot_blocks: 128,
471 num_sps_sub_slot: 64,
472 sub_slot_iters_starting: 1 << 27,
473 difficulty_constant_factor: 1 << 67,
474 difficulty_starting: 7,
475 difficulty_change_max_factor: 3,
476 sub_epoch_blocks: 384,
477 epoch_blocks: 4608,
478 significant_bits: 8,
479 discriminant_size_bits: 1024,
480 number_zero_bits_plot_filter_v1: 9,
481 number_zero_bits_plot_filter_v2: 9,
482 min_plot_size_v1: 32,
483 max_plot_size_v1: 50,
484 min_plot_size_v2: 28,
485 max_plot_size_v2: 32,
486 sub_slot_time_target: 600,
487 num_sp_intervals_extra: 3,
488 max_future_time2: 120,
489 number_of_timestamps: 11,
490 max_vdf_witness_size: 64,
491 mempool_block_buffer: 10,
492 weight_proof_threshold: 2,
493 blocks_cache_size: 4608 + (128 * 4),
494 weight_proof_recent_blocks: 1000,
495 max_block_count_per_requests: 32,
496 pool_sub_slot_iters: 37_600_000_000,
497 plot_filter_128_height: 0xffff_ffff,
498 plot_filter_64_height: 0xffff_ffff,
499 plot_filter_32_height: 0xffff_ffff,
500 plot_difficulty_initial: 2,
501 plot_difficulty_4_height: 0xffff_ffff,
502 plot_difficulty_5_height: 0xffff_ffff,
503 plot_difficulty_6_height: 0xffff_ffff,
504 plot_difficulty_7_height: 0xffff_ffff,
505 plot_difficulty_8_height: 0xffff_ffff,
506 },
507};
508
509// =============================================================================
510// Profile DEK at-rest byte contract
511//
512// A DIG user profile's data-encryption-key (DEK) is derived, never stored, from
513// the user's identity scalar via HKDF-SHA256:
514//
515// HKDF-SHA256(salt = DEK_SALT,
516// ikm = IDENTITY_IKM_VERSION || identity_scalar_32,
517// info = PROFILE_DEK_LABEL)
518// -> SYMMETRIC_KEY_LEN bytes
519//
520// These four values are a PERMANENT at-rest byte-identical contract (§4.1/§5.1/
521// NC-5): every sealed profile on disk was encrypted with a DEK derived from
522// EXACTLY these bytes. Changing any one of them re-derives a different key and
523// makes every already-sealed profile permanently unreadable — there is no
524// migration path for a derived (never-stored) key. Treat this section as
525// frozen; only ever ADD a new version-scoped label/version alongside it.
526//
527// Consumers (this crate is their single source of truth — do not duplicate the
528// literals locally):
529// - dig-app: crates/dig-app-core/src/keystore/secrets.rs
530// - dig-session: src/unlocked.rs (derive_symmetric_key)
531// =============================================================================
532
533/// HKDF salt for the per-profile DEK derivation.
534///
535/// Part of the frozen [profile DEK byte contract](self#profile-dek-at-rest-byte-contract)
536/// — see the section comment above. Consumed by dig-app's
537/// `keystore/secrets.rs` and dig-session's `derive_symmetric_key`.
538pub const DEK_SALT: &[u8] = b"dig-app:dek-salt:v1";
539
540/// Version byte prefixed to the 32-byte identity scalar to form the DEK's HKDF
541/// input key material (`IDENTITY_IKM_VERSION || identity_scalar_32`).
542///
543/// Part of the frozen [profile DEK byte contract](self#profile-dek-at-rest-byte-contract).
544/// Consumed by dig-app's `keystore/secrets.rs` and dig-session's
545/// `derive_symmetric_key`.
546pub const IDENTITY_IKM_VERSION: u8 = 2;
547
548/// HKDF info/label for the per-profile DEK derivation.
549///
550/// Part of the frozen [profile DEK byte contract](self#profile-dek-at-rest-byte-contract).
551/// Consumed by dig-app's `keystore/secrets.rs` and dig-session's
552/// `derive_symmetric_key`.
553pub const PROFILE_DEK_LABEL: &[u8] = b"dig-app:profile-dek:v2";
554
555/// Output length, in bytes, of the derived per-profile DEK (HKDF-SHA256's
556/// natural output for a symmetric AEAD key).
557///
558/// Part of the frozen [profile DEK byte contract](self#profile-dek-at-rest-byte-contract).
559/// Consumed by dig-app's `keystore/secrets.rs` and dig-session's
560/// `derive_symmetric_key`.
561pub const SYMMETRIC_KEY_LEN: usize = 32;
562
563// =============================================================================
564// Profile sealing X25519 byte contract
565//
566// A DIG user profile's per-profile X25519 *sealing* keypair (used by the DIG
567// App to seal/unseal `DIGCHAT1` messages for dig-chat, §NC-1 end-to-end
568// encryption) is derived, never stored, from the same identity scalar as the
569// DEK — but under a DISTINCT HKDF `info` label, which is the sole thing that
570// domain-separates the sealing key from the at-rest DEK:
571//
572// HKDF-SHA256(salt = DEK_SALT,
573// ikm = IDENTITY_IKM_VERSION || identity_scalar_32,
574// info = PROFILE_SEALING_X25519_LABEL)
575// -> SYMMETRIC_KEY_LEN (32) bytes, then CLAMPED to an X25519 scalar
576//
577// This label reuses the already-frozen DEK_SALT + IDENTITY_IKM_VERSION on
578// purpose; only the `info` label differs. The 32-byte HKDF output is clamped
579// to a valid X25519 secret scalar by the CONSUMER (dig-account) — this crate
580// owns ONLY the frozen label bytes, not the clamp/derivation.
581//
582// The label is a PERMANENT byte-identical contract (§4.1/§5.1/NC-1): every
583// message already sealed on the network was encrypted under a keypair derived
584// from EXACTLY these bytes. Changing it re-derives a different keypair and
585// makes every already-sealed message permanently unopenable — there is no
586// migration path for a derived (never-stored) key. Treat it as frozen; only
587// ever ADD a new version-scoped label (`…:v2`) alongside it, never mutate it.
588//
589// Consumers (this crate is their single source of truth — do not duplicate the
590// literal locally):
591// - dig-account: derives the sealing keypair via
592// `seed.profile_derive_symmetric_key(ix, PROFILE_SEALING_X25519_LABEL)`
593// then clamps the output to an X25519 scalar.
594// =============================================================================
595
596/// HKDF info/label for deriving a profile's per-profile X25519 **sealing**
597/// keypair — the key the DIG App uses to seal/unseal `DIGCHAT1` messages.
598///
599/// Part of the frozen [profile sealing X25519 byte
600/// contract](self#profile-sealing-x25519-byte-contract) — see the section
601/// comment above. Reuses [`DEK_SALT`] + [`IDENTITY_IKM_VERSION`]; this distinct
602/// `info` label is what domain-separates the sealing key from [`PROFILE_DEK_LABEL`].
603/// The 32-byte HKDF output is clamped to an X25519 scalar by the consumer
604/// (dig-account), not here.
605pub const PROFILE_SEALING_X25519_LABEL: &[u8] = b"dig-app:profile-sealing-x25519:v1";
606
607#[cfg(test)]
608mod tests {
609 use super::*;
610
611 /// The canonical relay endpoint must equal exactly what a DIG Node dials by
612 /// default. This pins the value byte-for-byte against `dig-node`'s
613 /// `relay::DEFAULT_RELAY_URL` (`wss://relay.dig.net:9450`) and the
614 /// `dig-relay` server's documented client endpoint. If either side ever
615 /// changes the scheme, host, or port, this guard fails so the shared
616 /// contract can't silently drift.
617 #[test]
618 fn dig_relay_url_is_canonical_endpoint() {
619 assert_eq!(DIG_RELAY_URL, "wss://relay.dig.net:443");
620 }
621
622 /// The relay endpoint is a secure-WebSocket URL pointing at the canonical
623 /// public host on the relay protocol port.
624 #[test]
625 fn dig_relay_url_is_well_formed() {
626 assert!(
627 DIG_RELAY_URL.starts_with("wss://"),
628 "relay must use secure WebSocket"
629 );
630 assert!(
631 DIG_RELAY_URL.contains("relay.dig.net"),
632 "relay must point at the canonical host"
633 );
634 assert!(
635 DIG_RELAY_URL.ends_with(":443"),
636 "relay must use the live NLB public TLS port 443"
637 );
638 }
639
640 /// The DIG node localhost port must equal the expected default.
641 ///
642 /// This guards against accidental mutations and ensures all consumers
643 /// (dig-node, dig-dns, dig-installer, dig-sdk, digstore) use a consistent
644 /// port when connecting to the local node on `dig.local` or `localhost`.
645 #[test]
646 fn dig_node_port_is_canonical() {
647 assert_eq!(DIG_NODE_PORT, 9778);
648 }
649
650 /// The local-node hostname must equal the expected default.
651 ///
652 /// This guards the first tier of the §5.3 client→node connection order —
653 /// a drift here would desync the installer's registered address from what
654 /// clients probe.
655 #[test]
656 fn dig_local_host_is_canonical() {
657 assert_eq!(DIG_LOCAL_HOST, "dig.local");
658 }
659
660 /// The public read gateway must equal the expected default.
661 ///
662 /// This guards the final-fallback tier of the §5.3 client→node connection
663 /// order — the gateway every consumer falls through to when no local node
664 /// responds.
665 #[test]
666 fn rpc_dig_net_url_is_canonical() {
667 assert_eq!(RPC_DIG_NET_URL, "https://rpc.dig.net");
668 }
669
670 /// The public read gateway is a plain-HTTPS URL (the public read tier,
671 /// distinct from the mTLS transport node-class clients use, §5.3).
672 #[test]
673 fn rpc_dig_net_url_is_well_formed() {
674 assert!(
675 RPC_DIG_NET_URL.starts_with("https://"),
676 "the public read gateway must use HTTPS"
677 );
678 }
679
680 // -- Genesis challenge canonical-value guards --------------------------
681 //
682 // These pin the pre-launch canonical genesis challenges byte-for-byte AND
683 // prove they are reproducible from their documented preimages, so the
684 // values can never silently drift (a drift changes every derived signature
685 // domain + the gossip network_id — a cross-repo breaking event).
686
687 use sha2::{Digest, Sha256};
688
689 /// AGG_SIG opcode bytes, per §4.2 of `SPEC.md` (Chia L1 `condition_tools`).
690 const AGG_SIG_OPCODES: [u8; 6] = [43, 44, 45, 46, 47, 48];
691
692 fn sha256(bytes: &[u8]) -> [u8; 32] {
693 let mut hasher = Sha256::new();
694 hasher.update(bytes);
695 hasher.finalize().into()
696 }
697
698 /// The genesis MUST be non-zero: `dig-gossip` rejects an all-zero
699 /// `network_id`, so a zero genesis would stop the node's gossip pool / DHT /
700 /// PEX from ever starting. This is the connect-enabler invariant.
701 #[test]
702 fn genesis_challenges_are_non_zero() {
703 assert_ne!(DIG_MAINNET.genesis_challenge(), Bytes32::new([0u8; 32]));
704 assert_ne!(DIG_TESTNET.genesis_challenge(), Bytes32::new([0u8; 32]));
705 }
706
707 /// The mainnet genesis is pinned to the Chia mainnet header hash @ height
708 /// 9,021,277 (a real anchored value), and the testnet genesis is the
709 /// reproducible `sha256` of its documented preimage. These pin both values
710 /// byte-for-byte so neither can silently drift.
711 #[test]
712 fn genesis_challenges_are_the_pinned_values() {
713 assert_eq!(
714 DIG_MAINNET_GENESIS_CHALLENGE,
715 hex_literal::hex!("0af981862a4df51f51ec59c312315d959931d917c375730b89b9e2b0854d1abf"),
716 );
717 assert_eq!(
718 DIG_TESTNET_GENESIS_CHALLENGE,
719 sha256(b"DIG_TESTNET:genesis:v1"),
720 );
721 }
722
723 /// Mainnet and testnet MUST NOT share a genesis (no cross-network replay).
724 #[test]
725 fn mainnet_and_testnet_genesis_differ() {
726 assert_ne!(
727 DIG_MAINNET.genesis_challenge(),
728 DIG_TESTNET.genesis_challenge(),
729 );
730 }
731
732 /// Pins the $DIG CAT asset id byte-for-byte against the value shipped in
733 /// `chip35_dl_coin::DIG_ASSET_ID` — a drift here silently breaks $DIG
734 /// recognition across every consumer (wallets, decoders, payment builders).
735 #[test]
736 fn dig_asset_id_is_canonical() {
737 assert_eq!(
738 DIG_ASSET_ID,
739 Bytes32::new(hex_literal::hex!(
740 "a406d3a9de984d03c9591c10d917593b434d5263cabe2b42f6b367df16832f81"
741 )),
742 );
743 }
744
745 // -- Chia L1 AGG_SIG_ME anti-drift guards ------------------------------
746
747 /// Literal pin: the Chia L1 AGG_SIG_ME constants equal Chia's well-known
748 /// mainnet / testnet11 genesis challenges byte-for-byte. This catches any
749 /// accidental mutation independently of any external crate.
750 #[test]
751 fn chia_l1_agg_sig_me_constants_are_the_pinned_values() {
752 assert_eq!(
753 CHIA_L1_MAINNET_AGG_SIG_ME,
754 hex_literal::hex!("ccd5bb71183532bff220ba46c268991a3ff07eb358e8255a65c30a2dce0e5fbb"),
755 );
756 assert_eq!(
757 CHIA_L1_TESTNET11_AGG_SIG_ME,
758 hex_literal::hex!("37a90eb5185a9c4439a91ddc98bbadce7b4feba060d50116a067de66bf236615"),
759 );
760 }
761
762 /// Source KAT: the Chia L1 constants MUST equal the values `chia-wallet-sdk`
763 /// (via `chia-sdk-types`) uses in its `MAINNET_CONSTANTS` / `TESTNET11_CONSTANTS`.
764 /// This is the primary anti-drift guard — the wallet engine binds spends with
765 /// those SDK constants, so if a future SDK version ever changed the value, this
766 /// fails and forces a deliberate re-pin instead of a silent custody break.
767 #[test]
768 fn chia_l1_agg_sig_me_matches_chia_sdk_types() {
769 use chia_sdk_types::{MAINNET_CONSTANTS, TESTNET11_CONSTANTS};
770 assert_eq!(
771 CHIA_L1_MAINNET_AGG_SIG_ME.as_slice(),
772 MAINNET_CONSTANTS.agg_sig_me_additional_data.as_ref(),
773 );
774 assert_eq!(
775 CHIA_L1_TESTNET11_AGG_SIG_ME.as_slice(),
776 TESTNET11_CONSTANTS.agg_sig_me_additional_data.as_ref(),
777 );
778 }
779
780 /// The Chia L1 (foreign chain) AGG_SIG_ME MUST NOT equal the DIG L2 genesis —
781 /// this is the whole reason the constants exist. Signing an L1 spend with the
782 /// DIG L2 genesis would be a custody break.
783 #[test]
784 fn chia_l1_agg_sig_me_differs_from_dig_l2_genesis() {
785 assert_ne!(
786 Bytes32::new(CHIA_L1_MAINNET_AGG_SIG_ME),
787 DIG_MAINNET.genesis_challenge(),
788 );
789 assert_ne!(
790 Bytes32::new(CHIA_L1_TESTNET11_AGG_SIG_ME),
791 DIG_TESTNET.genesis_challenge(),
792 );
793 }
794
795 // -- DIG treasury recipient anti-drift guards --------------------------
796
797 /// Literal pin: the treasury inner puzzle hash equals the value
798 /// `digstore_chain::dig::treasury_inner_puzzle_hash()` decodes to
799 /// (byte-identical, pinned by that crate's own test at
800 /// `crates/digstore-chain/src/dig.rs:206-209`). A drift here silently
801 /// MISDIRECTS every $DIG capsule/commit payment and dev-tip to the wrong
802 /// on-chain recipient — a custody break.
803 #[test]
804 fn dig_treasury_inner_puzzle_hash_is_canonical() {
805 assert_eq!(
806 DIG_TREASURY_INNER_PUZZLE_HASH,
807 Bytes32::new(hex_literal::hex!(
808 "ec7c304708c7d59c078d5ae098d0dea004decf47fa1cafebb266c10ad6466ce8"
809 )),
810 );
811 }
812
813 /// Literal pin: the treasury address equals digstore-chain's
814 /// source-of-truth bech32m form (`digstore_chain::dig::TREASURY_ADDRESS`,
815 /// `crates/digstore-chain/src/dig.rs:41`). A drift misdirects funds.
816 #[test]
817 fn dig_treasury_address_is_canonical() {
818 assert_eq!(
819 DIG_TREASURY_ADDRESS,
820 "xch1a37rq3cgcl2ecpudttsf35x75qzdan68lgw2l6ajvmqs44jxdn5qv6pk3y",
821 );
822 }
823
824 /// KAT: the bech32m address and the inner puzzle hash cannot silently drift
825 /// apart. Decodes `DIG_TREASURY_ADDRESS` (HRP `xch`, bech32m) and asserts
826 /// the 32 decoded bytes equal `DIG_TREASURY_INNER_PUZZLE_HASH`, proving the
827 /// two constants encode the SAME on-chain recipient.
828 #[test]
829 fn dig_treasury_address_decodes_to_inner_puzzle_hash() {
830 use bech32::Hrp;
831 let (hrp, data) = bech32::decode(DIG_TREASURY_ADDRESS).expect("valid bech32m");
832 assert_eq!(hrp, Hrp::parse("xch").unwrap(), "HRP must be xch");
833 assert_eq!(
834 data.as_slice(),
835 DIG_TREASURY_INNER_PUZZLE_HASH.to_bytes(),
836 "address must decode to the pinned inner puzzle hash",
837 );
838 }
839
840 // -- Profile DEK at-rest byte-contract guards ---------------------------
841 //
842 // These pin every DEK-derivation constant literally so a future edit can't
843 // silently drift the contract (which would make every already-sealed
844 // profile permanently unreadable, §5.1).
845
846 #[test]
847 fn dek_salt_is_the_pinned_value() {
848 assert_eq!(DEK_SALT, b"dig-app:dek-salt:v1");
849 }
850
851 #[test]
852 fn identity_ikm_version_is_the_pinned_value() {
853 assert_eq!(IDENTITY_IKM_VERSION, 2);
854 }
855
856 #[test]
857 fn profile_dek_label_is_the_pinned_value() {
858 assert_eq!(PROFILE_DEK_LABEL, b"dig-app:profile-dek:v2");
859 }
860
861 #[test]
862 fn symmetric_key_len_is_the_pinned_value() {
863 assert_eq!(SYMMETRIC_KEY_LEN, 32);
864 }
865
866 /// The per-profile X25519 sealing label is a PERMANENT crypto byte contract
867 /// (§5.1): every `DIGCHAT1` message a DIG user has ever sealed was encrypted
868 /// under a sealing key derived from EXACTLY these bytes. A drift here would
869 /// re-derive a different keypair and make every already-sealed message
870 /// permanently unopenable. This pins the label literally so no future edit
871 /// can silently change it.
872 #[test]
873 fn profile_sealing_x25519_label_is_the_pinned_value() {
874 assert_eq!(
875 PROFILE_SEALING_X25519_LABEL,
876 b"dig-app:profile-sealing-x25519:v1"
877 );
878 }
879
880 /// The sealing label MUST be distinct from the DEK label — a shared `info`
881 /// would derive the same 32 bytes for both the at-rest DEK and the X25519
882 /// sealing key, collapsing the domain separation the two labels exist to
883 /// provide. This guards that domain separation directly.
884 #[test]
885 fn profile_sealing_label_is_domain_separated_from_dek_label() {
886 assert_ne!(PROFILE_SEALING_X25519_LABEL, PROFILE_DEK_LABEL);
887 }
888
889 /// Every baked-in AGG_SIG additional-data value MUST equal the §4.1 rule
890 /// applied to the network's genesis: AGG_SIG_ME == genesis, and each other
891 /// variant == `sha256(genesis || opcode_byte)`. This regenerates the values
892 /// independently and asserts the constants match — so a genesis change that
893 /// forgets to recompute a derived value is caught.
894 #[test]
895 fn agg_sig_additional_data_matches_derivation_rule() {
896 for net in [&DIG_MAINNET, &DIG_TESTNET] {
897 let genesis = net.genesis_challenge();
898 assert_eq!(net.agg_sig_me_additional_data(), genesis);
899
900 let c = net.consensus();
901 let derived: Vec<Bytes32> = AGG_SIG_OPCODES
902 .iter()
903 .map(|&op| {
904 let mut preimage = genesis.as_ref().to_vec();
905 preimage.push(op);
906 Bytes32::new(sha256(&preimage))
907 })
908 .collect();
909 assert_eq!(c.agg_sig_parent_additional_data, derived[0]);
910 assert_eq!(c.agg_sig_puzzle_additional_data, derived[1]);
911 assert_eq!(c.agg_sig_amount_additional_data, derived[2]);
912 assert_eq!(c.agg_sig_puzzle_amount_additional_data, derived[3]);
913 assert_eq!(c.agg_sig_parent_amount_additional_data, derived[4]);
914 assert_eq!(c.agg_sig_parent_puzzle_additional_data, derived[5]);
915 }
916 }
917}