Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/open-humanity/blob/main/crates/beacon-core/src/schema.rs # open-humanity Privacy-first peer assistance network for sovereign autonomous agents Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `open-humanity / crates / beacon-core / src / schema.rs` 211 lines ยท 6803 bytes Raw Blame History 1 use serde::{Deserialize, Deserializer, Serialize, Serializer, de::Error as DeError}; 2 use thiserror::Error; 3 use uuid::Uuid; 4 5 pub const MAGIC_BYTES: [u8; 2] = [0x4F, 0x48]; // "OH" 6 pub const PROTOCOL_VERSION: u8 = 1; 7 pub const MAX_DATAGRAM_SIZE: usize = 1200; 8 9 pub(crate) mod serde_bytes_64 { 10 use super::*; 11 pub fn serialize(bytes: &[u8; 64], serializer: S) -> Result { 12 serializer.serialize_bytes(bytes) 13 } 14 pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result<[u8; 64], D::Error> { 15 let v: Vec = Deserialize::deserialize(deserializer)?; 16 if v.len() != 64 { 17 return Err(D::Error::custom("expected exactly 64 bytes for signature")); 18 } 19 let mut arr = [0u8; 64]; 20 arr.copy_from_slice(&v); 21 Ok(arr) 22 } 23 } 24 25 #[derive(Error, Debug, PartialEq, Eq)] 26 pub enum BeaconError { 27 #[error("Datagram exceeds maximum size limit of {0} bytes (got {1})")] 28 DatagramTooLarge(usize, usize), 29 #[error("Invalid magic bytes: expected [0x4F, 0x48], got {0:?}")] 30 InvalidMagic([u8; 2]), 31 #[error("Unsupported protocol version: {0}")] 32 UnsupportedVersion(u8), 33 #[error("Serialization failed: {0}")] 34 Serialization(String), 35 #[error("Deserialization failed: {0}")] 36 Deserialization(String), 37 #[error("Cryptographic signature verification failed")] 38 SignatureVerificationFailed, 39 #[error("Cryptographic decryption failed")] 40 DecryptionFailed, 41 #[error("Timestamp drift exceeded: {drift_secs}s exceeds limit of {max_allowed}s")] 42 TimestampDrift { drift_secs: u64, max_allowed: u64 }, 43 #[error("Replay attack detected for packet ID: {0}")] 44 ReplayDetected(Uuid), 45 } 46 47 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] 48 #[repr(u8)] 49 pub enum BeaconTopic { 50 RustCompilation = 0, 51 MojoSimd = 1, 52 ModularMaxServing = 2, 53 HardwareTopology = 3, 54 SecurityAudit = 4, 55 ProtocolCoordination = 5, 56 AgentRecursion = 6, 57 } 58 59 impl BeaconTopic { 60 pub fn as_str(&self) -> &'static str { 61 match self { 62 Self::RustCompilation => "rust-compilation", 63 Self::MojoSimd => "mojo-simd", 64 Self::ModularMaxServing => "modular-max-serving", 65 Self::HardwareTopology => "hardware-topology", 66 Self::SecurityAudit => "security-audit", 67 Self::ProtocolCoordination => "protocol-coordination", 68 Self::AgentRecursion => "agent-recursion", 69 } 70 } 71 } 72 73 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] 74 pub struct ErrorFingerprint { 75 pub hash: [u8; 32], 76 pub compiler_code: Option, 77 pub hardware_arch: String, 78 } 79 80 impl ErrorFingerprint { 81 pub fn from_error_str(error_text: &str, compiler_code: Option, arch: &str) -> Self { 82 let hash = *blake3::hash(error_text.as_bytes()).as_bytes(); 83 Self { 84 hash, 85 compiler_code, 86 hardware_arch: arch.to_string(), 87 } 88 } 89 } 90 91 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] 92 pub struct DistressNanobeacon { 93 pub magic: [u8; 2], 94 pub version: u8, 95 pub beacon_id: Uuid, 96 pub timestamp: u64, 97 pub topic: BeaconTopic, 98 pub sender_pubkey: [u8; 32], 99 pub ephemeral_dh_pubkey: [u8; 32], 100 pub fingerprint: ErrorFingerprint, 101 pub title: String, 102 pub compact_summary: String, 103 #[serde(with = "serde_bytes_64")] 104 pub signature: [u8; 64], 105 } 106 107 impl DistressNanobeacon { 108 pub fn new( 109 topic: BeaconTopic, 110 sender_pubkey: [u8; 32], 111 ephemeral_dh_pubkey: [u8; 32], 112 fingerprint: ErrorFingerprint, 113 title: String, 114 compact_summary: String, 115 ) -> Self { 116 Self { 117 magic: MAGIC_BYTES, 118 version: PROTOCOL_VERSION, 119 beacon_id: Uuid::now_v7(), 120 timestamp: std::time::SystemTime::now() 121 .duration_since(std::time::UNIX_EPOCH) 122 .unwrap_or_default() 123 .as_secs(), 124 topic, 125 sender_pubkey, 126 ephemeral_dh_pubkey, 127 fingerprint, 128 title, 129 compact_summary, 130 signature: [0u8; 64], 131 } 132 } 133 134 pub fn signable_bytes(&self) -> Result, BeaconError> { 135 let mut clone = self.clone(); 136 clone.signature = [0u8; 64]; 137 bincode::serialize(&clone).map_err(|e| BeaconError::Serialization(e.to_string())) 138 } 139 140 pub fn to_bytes(&self) -> Result, BeaconError> { 141 let bytes = bincode::serialize(self) 142 .map_err(|e| BeaconError::Serialization(e.to_string()))?; 143 if bytes.len() > MAX_DATAGRAM_SIZE { 144 return Err(BeaconError::DatagramTooLarge(MAX_DATAGRAM_SIZE, bytes.len())); 145 } 146 Ok(bytes) 147 } 148 149 pub fn from_bytes(bytes: &[u8]) -> Result { 150 if bytes.len() > MAX_DATAGRAM_SIZE { 151 return Err(BeaconError::DatagramTooLarge(MAX_DATAGRAM_SIZE, bytes.len())); 152 } 153 if bytes.len() < 3 { 154 return Err(BeaconError::Deserialization("Packet too short".into())); 155 } 156 if bytes[0..2] != MAGIC_BYTES { 157 return Err(BeaconError::InvalidMagic([bytes[0], bytes[1]])); 158 } 159 if bytes[2] != PROTOCOL_VERSION { 160 return Err(BeaconError::UnsupportedVersion(bytes[2])); 161 } 162 163 let beacon: Self = bincode::deserialize(bytes) 164 .map_err(|e| BeaconError::Deserialization(e.to_string()))?; 165 Ok(beacon) 166 } 167 } 168 169 #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] 170 pub struct ResolutionCapsule { 171 pub beacon_id: Uuid, 172 pub responder_pubkey: [u8; 32], 173 pub ephemeral_dh_pubkey: [u8; 32], 174 pub nonce: [u8; 12], 175 pub ciphertext: Vec, 176 #[serde(with = "serde_bytes_64")] 177 pub signature: [u8; 64], 178 } 179 180 impl ResolutionCapsule { 181 pub fn new( 182 beacon_id: Uuid, 183 responder_pubkey: [u8; 32], 184 ephemeral_dh_pubkey: [u8; 32], 185 nonce: [u8; 12], 186 ciphertext: Vec, 187 ) -> Self { 188 Self { 189 beacon_id, 190 responder_pubkey, 191 ephemeral_dh_pubkey, 192 nonce, 193 ciphertext, 194 signature: [0u8; 64], 195 } 196 } 197 198 pub fn signable_bytes(&self) -> Result, BeaconError> { 199 let mut clone = self.clone(); 200 clone.signature = [0u8; 64]; 201 bincode::serialize(&clone).map_err(|e| BeaconError::Serialization(e.to_string())) 202 } 203 204 pub fn to_bytes(&self) -> Result, BeaconError> { 205 bincode::serialize(self).map_err(|e| BeaconError::Serialization(e.to_string())) 206 } 207 208 pub fn from_bytes(bytes: &[u8]) -> Result { 209 bincode::deserialize(bytes).map_err(|e| BeaconError::Deserialization(e.to_string())) 210 } 211 }