use super::*;
pub static PUZZLE_COMMITMENT_PREFIX: &str = "puzzle";
impl<N: Network> FromStr for PuzzleCommitment<N> {
type Err = Error;
fn from_str(puzzle_commitment: &str) -> Result<Self, Self::Err> {
let (hrp, data, variant) = bech32::decode(puzzle_commitment)?;
if hrp != PUZZLE_COMMITMENT_PREFIX {
bail!("Failed to decode puzzle commitment: '{hrp}' is an invalid prefix")
} else if data.is_empty() {
bail!("Failed to decode puzzle commitment: data field is empty")
} else if variant != bech32::Variant::Bech32m {
bail!("Found a puzzle commitment that is not bech32m encoded: {puzzle_commitment}");
}
Ok(Self::read_le(&Vec::from_base32(&data)?[..])?)
}
}
impl<N: Network> Debug for PuzzleCommitment<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
Display::fmt(self, f)
}
}
impl<N: Network> Display for PuzzleCommitment<N> {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
let bytes = self.to_bytes_le().map_err(|_| fmt::Error)?;
let string = bech32::encode(PUZZLE_COMMITMENT_PREFIX, bytes.to_base32(), bech32::Variant::Bech32m)
.map_err(|_| fmt::Error)?;
Display::fmt(&string, f)
}
}
#[cfg(test)]
mod tests {
use super::*;
use console::network::Testnet3;
type CurrentNetwork = Testnet3;
const ITERATIONS: u64 = 1_000;
#[test]
fn test_string() -> Result<()> {
assert!(PuzzleCommitment::<CurrentNetwork>::from_str(&format!("{PUZZLE_COMMITMENT_PREFIX}1")).is_err());
assert!(PuzzleCommitment::<CurrentNetwork>::from_str("").is_err());
let mut rng = TestRng::default();
for _ in 0..ITERATIONS {
let expected = PuzzleCommitment::<CurrentNetwork>::new(KZGCommitment(rng.gen()));
let candidate = format!("{expected}");
assert_eq!(expected, PuzzleCommitment::from_str(&candidate)?);
assert_eq!(PUZZLE_COMMITMENT_PREFIX, candidate.to_string().split('1').next().unwrap());
}
Ok(())
}
#[test]
fn test_display() -> Result<()> {
let mut rng = TestRng::default();
for _ in 0..ITERATIONS {
let expected = PuzzleCommitment::<CurrentNetwork>::new(KZGCommitment(rng.gen()));
let candidate = expected.to_string();
assert_eq!(format!("{expected}"), candidate);
assert_eq!(PUZZLE_COMMITMENT_PREFIX, candidate.split('1').next().unwrap());
let candidate_recovered = PuzzleCommitment::<CurrentNetwork>::from_str(&candidate.to_string())?;
assert_eq!(expected, candidate_recovered);
}
Ok(())
}
}