snarkvm_ledger_narwhal_transmission_id/
string.rs

1// Copyright (c) 2019-2025 Provable Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use super::*;
17
18impl<N: Network> FromStr for TransmissionID<N> {
19    type Err = Error;
20
21    /// Initializes the transmission ID from a string.
22    fn from_str(input: &str) -> Result<Self, Self::Err> {
23        // Split the id and checksum.
24        let (id, checksum) = input.split_once('.').ok_or_else(|| anyhow!("Invalid transmission ID: {input}"))?;
25        // Parse the string.
26        if id.starts_with(SOLUTION_ID_PREFIX) {
27            Ok(Self::Solution(
28                SolutionID::from_str(id)?,
29                N::TransmissionChecksum::from_str(checksum)
30                    .map_err(|_| anyhow!("Failed to parse checksum: {checksum}"))?,
31            ))
32        } else if id.starts_with(TRANSACTION_PREFIX) {
33            Ok(Self::Transaction(
34                N::TransactionID::from_str(id).map_err(|_| anyhow!("Failed to parse transaction ID: {id}"))?,
35                N::TransmissionChecksum::from_str(checksum)
36                    .map_err(|_| anyhow!("Failed to parse checksum: {checksum}"))?,
37            ))
38        } else {
39            bail!("Invalid transmission ID: {input}")
40        }
41    }
42}
43
44impl<N: Network> Debug for TransmissionID<N> {
45    /// Prints the transmission ID as a string.
46    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
47        Display::fmt(self, f)
48    }
49}
50
51impl<N: Network> Display for TransmissionID<N> {
52    /// Prints the transmission ID as a string.
53    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
54        match self {
55            Self::Ratification => write!(f, "ratification"),
56            Self::Solution(id, checksum) => write!(f, "{}.{}", id, checksum),
57            Self::Transaction(id, checksum) => write!(f, "{}.{}", id, checksum),
58        }
59    }
60}
61
62#[cfg(test)]
63mod tests {
64    use super::*;
65    use console::network::MainnetV0;
66
67    type CurrentNetwork = MainnetV0;
68
69    #[test]
70    fn test_string() {
71        let rng = &mut TestRng::default();
72
73        for expected in crate::test_helpers::sample_transmission_ids(rng) {
74            // Check the string representation.
75            let candidate = format!("{expected}");
76            assert_eq!(expected, TransmissionID::from_str(&candidate).unwrap());
77            assert!(TransmissionID::<CurrentNetwork>::from_str(&candidate[1..]).is_err());
78        }
79    }
80}