//! Implement parsing for the `pow-params v1` scheme use crate::doc::hsdesc::inner::HsInnerKwd; use crate::parse::tokenize::Item; use crate::types::misc::{B64, Iso8601TimeNoSp}; use crate::{KeywordEncodable, NetdocErrorKind, Result}; use std::time::SystemTime; use tor_checkable::timed::TimeRangeBound; use tor_hscrypto::pow::v1::{Effort, Seed}; /// The contents of a `pow-params v1` line /// /// These parameters are defined in the specifications for the `v1` proof of work scheme: /// /// /// In addition to the scheme identifier itself, this type of /// `pow-params` line includes a 32-byte seed with its own expiration /// timestamp, and a suggested effort value that clients may use for /// their initial request. #[derive(Debug, Clone, derive_more::Constructor, amplify::Getters)] pub struct PowParamsV1 { /// Time limited [`Seed`] #[getter(as_ref)] seed: TimeRangeBound, /// Last known suggested [`Effort`] /// /// This can be [`Effort::zero()`] if the puzzle is available but the /// service doesn't recommend using it for an initial connection attempt. #[getter(as_copy)] suggested_effort: Effort, } impl PowParamsV1 { /// Parse a single `pow-params v1` line from an `Item` pub(super) fn from_item(item: &Item<'_, HsInnerKwd>) -> Result { if item.has_obj() { return Err(NetdocErrorKind::UnexpectedObject .with_msg(item.kwd().to_str()) .at_pos(item.pos())); } let seed = item.required_arg(1)?.parse::()?.into_array()?.into(); let suggested_effort = item.required_arg(2)?.parse::()?.into(); let expires: SystemTime = item.required_arg(3)?.parse::()?.into(); Ok(Self { seed: TimeRangeBound::new(seed, ..expires), suggested_effort, }) } }