snarkvm_console_program/data/record/
parse_ciphertext.rs1use super::*;
17
18static RECORD_CIPHERTEXT_PREFIX: &str = "record";
19
20impl<N: Network> Parser for Record<N, Ciphertext<N>> {
21 #[inline]
23 fn parse(string: &str) -> ParserResult<Self> {
24 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 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 fn from_str(ciphertext: &str) -> Result<Self, Self::Err> {
42 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 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 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
65 let bytes = self.to_bytes_le().map_err(|_| fmt::Error)?;
67 let string = bech32::encode(RECORD_CIPHERTEXT_PREFIX, bytes.to_base32(), bech32::Variant::Bech32m)
69 .map_err(|_| fmt::Error)?;
70 Display::fmt(&string, f)
72 }
73}