dig_peer_protocol/lib.rs
1//! # dig-peer-protocol
2//!
3//! DIG Network L2 protocol types — a superset of `chia-protocol`.
4//!
5//! This crate re-exports the entire Chia protocol ecosystem (`chia-protocol`,
6//! `chia-sdk-client`, `chia-ssl`, `chia-traits`) plus DIG-specific extensions
7//! (the `200..=219` consensus opcodes plus [`DIG_MESSAGE`] = 220, the directed
8//! dig-message envelope opcode). Consumers depend on `dig-peer-protocol` alone instead
9//! of importing multiple `chia-*` crates individually.
10//!
11//! ## What's included
12//!
13//! | Source crate | What's re-exported |
14//! |-------------|-------------------|
15//! | `chia-protocol` | All wire types: `Message`, `Handshake`, `ProtocolMessageTypes`, `NodeType`, etc. |
16//! | `chia-sdk-client` | `Peer`, `Client`, `ClientError`, `ClientState`, `Network`, `PeerOptions`, rate limiting, TLS connectors |
17//! | `chia-ssl` | `ChiaCertificate` |
18//! | `chia-traits` | `Streamable` trait |
19//! | `chia_streamable_macro` | `#[streamable]` proc macro |
20//! | **DIG extensions** | `DigMessage`, `DigMessageType`, `RegisterPeer`, `RegisterAck`, introducer wire types |
21//!
22//! ## Feature flags
23//!
24//! | Flag | Forwards to | Effect |
25//! |------|-------------|--------|
26//! | `native-tls` | `chia-sdk-client/native-tls` | OS-native TLS; enables `Client`, `ClientState`, `Connector`, `create_native_tls_connector` |
27//! | `rustls` | `chia-sdk-client/rustls` | Pure-Rust TLS; enables `Client`, `ClientState`, `Connector`, `create_rustls_connector` |
28//!
29//! Neither feature is enabled by default. The crate builds without either but TLS-dependent
30//! re-exports (`Client`, `ClientState`, `Connector`) become unavailable.
31
32// ============================================================================
33// Re-export: chia-protocol (all wire types)
34// ============================================================================
35pub use chia_protocol::*;
36
37// ============================================================================
38// Re-export: chia-sdk-client (peer IO, TLS, rate limiting)
39// ============================================================================
40// Backend-agnostic types — always available.
41pub use chia_sdk_client::{
42 load_ssl_cert, ClientError, Network, Peer, PeerOptions, RateLimit, RateLimiter, RateLimits,
43 V2_RATE_LIMITS,
44};
45
46// `Client`, `ClientState`, and `Connector` require a TLS backend in `chia-sdk-client`.
47// Enable either the `native-tls` or `rustls` feature to use them.
48#[cfg(any(feature = "native-tls", feature = "rustls"))]
49pub use chia_sdk_client::{Client, ClientState, Connector};
50
51#[cfg(feature = "native-tls")]
52pub use chia_sdk_client::create_native_tls_connector;
53
54#[cfg(feature = "rustls")]
55pub use chia_sdk_client::create_rustls_connector;
56
57// ============================================================================
58// Re-export: chia-ssl (certificate types)
59// ============================================================================
60pub use chia_ssl::ChiaCertificate;
61
62// ============================================================================
63// Re-export: chia-traits (serialization)
64// ============================================================================
65pub use chia_traits::Streamable;
66
67// ============================================================================
68// Re-export: chia_streamable_macro (proc macro for wire structs)
69// ============================================================================
70pub use chia_streamable_macro::streamable;
71
72// ============================================================================
73// DIG extensions
74// ============================================================================
75mod dig_message;
76mod dig_message_type;
77mod introducer_wire;
78
79pub use dig_message::DigMessage;
80pub use dig_message_type::{DigMessageType, UnknownDigMessageType};
81pub use introducer_wire::{
82 RegisterAck, RegisterPeer, RequestPeersIntroducer, RespondPeersIntroducer,
83};
84
85/// Wire opcode for a directed **dig-message** envelope (WU6, epic #796).
86///
87/// The `200..=219` band is the DIG L2 **consensus** band ([`DigMessageType`]); `220..=255`
88/// is the **free** band for directed application protocols. Opcode **220** carries a
89/// `dig-message` directed envelope as OPAQUE bytes in [`DigMessage::data`] — the transport
90/// (dig-gossip) never seals, opens, or parses it; end-to-end sealing to the recipient's DID
91/// key is `dig-message`'s job.
92///
93/// This is a cross-repo **canonical** constant — it MUST NOT drift. `dig-gossip` mirrors it
94/// as `dig_gossip::DIG_MESSAGE` (and `ProtocolMessageTypes::DigMessage`) for its transport.
95pub const DIG_MESSAGE: u8 = 220;
96
97#[cfg(test)]
98mod dig_message_opcode_tests {
99 use super::{DigMessage, DigMessageType, DIG_MESSAGE};
100
101 /// The opcode frames a real [`DigMessage`] and survives a wire round-trip with its
102 /// `msg_type` intact — the canonical value (220) exercised through the actual encoder.
103 #[test]
104 fn dig_message_opcode_frames_and_round_trips() {
105 let msg = DigMessage::new(DIG_MESSAGE, Some(9), vec![1, 2, 3].into());
106 let back = DigMessage::from_bytes(&msg.to_bytes()).expect("round-trip");
107 assert_eq!(back.msg_type, 220);
108 assert_eq!(back.msg_type, DIG_MESSAGE);
109 assert_eq!(back.data.as_ref(), &[1, 2, 3]);
110 }
111
112 /// 220 is in the free band: it is NOT a consensus `DigMessageType` discriminant, so a
113 /// consensus-band decode of the opcode fails — the two bands can never collide.
114 #[test]
115 fn dig_message_opcode_is_not_a_consensus_type() {
116 assert!(DigMessageType::try_from(DIG_MESSAGE).is_err());
117 }
118}