cml_core/
network.rs

1use crate::{error::DeserializeError, serialization::Deserialize};
2use cbor_event::de::Deserializer;
3use cbor_event::se::Serializer;
4use schemars::JsonSchema;
5use std::io::{BufRead, Write};
6
7pub static BYRON_MAINNET_NETWORK_MAGIC: u32 = 764824073;
8pub static BYRON_TESTNET_NETWORK_MAGIC: u32 = 1097911063;
9pub static SANCHO_TESTNET_NETWORK_MAGIC: u32 = 4;
10pub static PREPROD_NETWORK_MAGIC: u32 = 1;
11pub static PREVIEW_NETWORK_MAGIC: u32 = 2;
12
13impl std::fmt::Display for ProtocolMagic {
14    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
15        write!(f, "{}", self.0)
16    }
17}
18
19#[derive(
20    Clone,
21    Copy,
22    Debug,
23    Eq,
24    Ord,
25    PartialEq,
26    PartialOrd,
27    Hash,
28    serde::Serialize,
29    serde::Deserialize,
30    JsonSchema,
31)]
32pub struct ProtocolMagic(u32);
33
34impl From<ProtocolMagic> for u32 {
35    fn from(val: ProtocolMagic) -> Self {
36        val.0
37    }
38}
39
40impl From<u32> for ProtocolMagic {
41    fn from(inner: u32) -> Self {
42        ProtocolMagic(inner)
43    }
44}
45
46impl ::std::ops::Deref for ProtocolMagic {
47    type Target = u32;
48    fn deref(&self) -> &Self::Target {
49        &self.0
50    }
51}
52
53impl Default for ProtocolMagic {
54    fn default() -> Self {
55        Self(764824073)
56    }
57}
58
59impl cbor_event::se::Serialize for ProtocolMagic {
60    fn serialize<'se, W: Write>(
61        &self,
62        serializer: &'se mut Serializer<W>,
63    ) -> cbor_event::Result<&'se mut Serializer<W>> {
64        serializer.write_unsigned_integer(self.0 as u64)
65    }
66}
67
68impl Deserialize for ProtocolMagic {
69    fn deserialize<R: BufRead>(raw: &mut Deserializer<R>) -> Result<Self, DeserializeError> {
70        Ok(Self(raw.unsigned_integer()? as u32))
71    }
72}