monero/
network.rs

1// Rust Monero Library
2// Written in 2019-2023 by
3//   Monero Rust Contributors
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15
16//! Monero networks definition and related error types.
17//!
18//! This module defines the existing Monero networks and their associated magic bytes.
19//!
20
21use crate::util::address::AddressType;
22use thiserror::Error;
23
24/// Potential errors encountered while manipulating Monero networks.
25#[derive(Error, Debug, PartialEq, Eq)]
26pub enum Error {
27    /// Invalid magic network byte.
28    #[error("Invalid magic network byte")]
29    InvalidMagicByte,
30}
31
32/// The list of the existing Monero networks.
33///
34/// Network implements [`Default`] and returns [`Network::Mainnet`].
35///
36#[derive(Default, Debug, PartialEq, Eq, Hash, Clone, Copy)]
37pub enum Network {
38    /// Mainnet is the "production" network and blockchain.
39    #[default]
40    Mainnet,
41    /// Stagenet is technically equivalent to mainnet, both in terms of features and consensus
42    /// rules.
43    Stagenet,
44    /// Testnet is the "experimental" network and blockchain where things get released long before
45    /// mainnet.
46    Testnet,
47}
48
49impl Network {
50    /// Get the associated magic byte given an address type.
51    ///
52    /// **Source:** [`monero/src/cryptonote_config.h`](https://github.com/monero-project/monero/blob/159c78758af0a0af9df9a4f9ab81888f9322e9be/src/cryptonote_config.h#L190-L239)
53    pub fn as_u8(self, addr_type: &AddressType) -> u8 {
54        use AddressType::*;
55        use Network::*;
56        match self {
57            Mainnet => match addr_type {
58                Standard => 18,
59                Integrated(_) => 19,
60                SubAddress => 42,
61            },
62            Testnet => match addr_type {
63                Standard => 53,
64                Integrated(_) => 54,
65                SubAddress => 63,
66            },
67            Stagenet => match addr_type {
68                Standard => 24,
69                Integrated(_) => 25,
70                SubAddress => 36,
71            },
72        }
73    }
74
75    /// Recover the network type given an address magic byte.
76    ///
77    /// **Source:** [`monero/src/cryptonote_config.h`](https://github.com/monero-project/monero/blob/159c78758af0a0af9df9a4f9ab81888f9322e9be/src/cryptonote_config.h#L190-L239)
78    pub fn from_u8(byte: u8) -> Result<Network, Error> {
79        use Network::*;
80        match byte {
81            18 | 19 | 42 => Ok(Mainnet),
82            53 | 54 | 63 => Ok(Testnet),
83            24 | 25 | 36 => Ok(Stagenet),
84            _ => Err(Error::InvalidMagicByte),
85        }
86    }
87}