use super::*;
static PROVING_KEY: &str = "prover";
impl<N: Network> Parser for ProvingKey<N> {
#[inline]
fn parse(string: &str) -> ParserResult<Self> {
let parse_key = recognize(pair(
pair(tag(PROVING_KEY), tag("1")),
many1(terminated(one_of("qpzry9x8gf2tvdw0s3jn54khce6mua7l"), many0(char('_')))),
));
map_res(parse_key, |key: &str| -> Result<_, Error> { Self::from_str(&key.replace('_', "")) })(string)
}
}
impl<N: Network> FromStr for ProvingKey<N> {
type Err = Error;
fn from_str(key: &str) -> Result<Self, Self::Err> {
let (hrp, data, variant) = bech32::decode(key)?;
if hrp != PROVING_KEY {
bail!("Failed to decode proving key: '{hrp}' is an invalid prefix")
} else if data.is_empty() {
bail!("Failed to decode proving key: data field is empty")
} else if variant != bech32::Variant::Bech32m {
bail!("Found a proving key that is not bech32m encoded: {key}");
}
Ok(Self::read_le(&Vec::from_base32(&data)?[..])?)
}
}
impl<N: Network> Debug for ProvingKey<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
impl<N: Network> Display for ProvingKey<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let bytes = self.to_bytes_le().map_err(|_| fmt::Error)?;
let string =
bech32::encode(PROVING_KEY, bytes.to_base32(), bech32::Variant::Bech32m).map_err(|_| fmt::Error)?;
Display::fmt(&string, f)
}
}