1
//! Code for a replay log for Proof-of-Work [`Nonce`]s.
2

            
3
use std::{borrow::Cow, ffi::OsStr};
4

            
5
use tor_hscrypto::pow::v1::{Nonce, Seed};
6

            
7
use super::{ReplayLogType, MAGIC_LEN, OUTPUT_LEN, REPLAY_LOG_SUFFIX};
8

            
9
/// A [`ReplayLogType`] to indicate using [`Nonce`] messages with [`Seed`] names.
10
pub(crate) struct PowNonceReplayLogType;
11

            
12
impl ReplayLogType for PowNonceReplayLogType {
13
    type Name = Seed;
14
    type Message = Nonce;
15

            
16
    const MAGIC: &'static [u8; MAGIC_LEN] = b"<tor hss pow replay Kangaroo12>\n";
17

            
18
    fn format_filename(name: &Seed) -> String {
19
        format!("{name}{REPLAY_LOG_SUFFIX}")
20
    }
21

            
22
    fn transform_message(message: &Nonce) -> [u8; OUTPUT_LEN] {
23
        *message.as_ref()
24
    }
25

            
26
    fn parse_log_leafname(leaf: &OsStr) -> Result<Seed, Cow<'static, str>> {
27
        let leaf = leaf.to_str().ok_or("not proper unicode")?;
28
        let seed = leaf.strip_suffix(REPLAY_LOG_SUFFIX).ok_or("not *.bin")?;
29
        seed.parse().or(Err("invalid seed".into()))
30
    }
31
}