snarkvm_console_program/data/record/
parse_ciphertext.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
18static RECORD_CIPHERTEXT_PREFIX: &str = "record";
19
20impl<N: Network> Parser for Record<N, Ciphertext<N>> {
21    /// Parses a string into an ciphertext.
22    #[inline]
23    fn parse(string: &str) -> ParserResult<Self> {
24        // Prepare a parser for the record ciphertext.
25        let parse_record_ciphertext = recognize(pair(
26            pair(tag(RECORD_CIPHERTEXT_PREFIX), tag("1")),
27            many1(terminated(one_of("qpzry9x8gf2tvdw0s3jn54khce6mua7l"), many0(char('_')))),
28        ));
29
30        // Parse the record ciphertext from the string.
31        map_res(parse_record_ciphertext, |record_ciphertext: &str| -> Result<_, Error> {
32            Self::from_str(&record_ciphertext.replace('_', ""))
33        })(string)
34    }
35}
36
37impl<N: Network> FromStr for Record<N, Ciphertext<N>> {
38    type Err = Error;
39
40    /// Reads in the ciphertext string.
41    fn from_str(ciphertext: &str) -> Result<Self, Self::Err> {
42        // Decode the ciphertext string from bech32m.
43        let (hrp, data, variant) = bech32::decode(ciphertext)?;
44        if hrp != RECORD_CIPHERTEXT_PREFIX {
45            bail!("Failed to decode record ciphertext: '{hrp}' is an invalid prefix")
46        } else if data.is_empty() {
47            bail!("Failed to decode record ciphertext: data field is empty")
48        } else if variant != bech32::Variant::Bech32m {
49            bail!("Found a record ciphertext that is not bech32m encoded: {ciphertext}");
50        }
51        // Decode the record ciphertext data from u5 to u8, and into the record ciphertext.
52        Ok(Self::read_le(&Vec::from_base32(&data)?[..])?)
53    }
54}
55
56impl<N: Network> Debug for Record<N, Ciphertext<N>> {
57    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
58        Display::fmt(self, f)
59    }
60}
61
62impl<N: Network> Display for Record<N, Ciphertext<N>> {
63    /// Writes the record ciphertext as a bech32m string.
64    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
65        // Convert the ciphertext to bytes.
66        let bytes = self.to_bytes_le().map_err(|_| fmt::Error)?;
67        // Encode the bytes into bech32m.
68        let string = bech32::encode(RECORD_CIPHERTEXT_PREFIX, bytes.to_base32(), bech32::Variant::Bech32m)
69            .map_err(|_| fmt::Error)?;
70        // Output the string.
71        Display::fmt(&string, f)
72    }
73}