use rand::{Rng, rng};
#[cfg(not(target_arch = "wasm32"))]
use std::time::{SystemTime, UNIX_EPOCH};
use zeroize::Zeroizing;
pub(crate) fn generate_seed<const N: usize>() -> Zeroizing<[u8; N]> {
let mut entropy = Zeroizing::new([0u8; N]);
let mut rng = rng();
rng.fill_bytes(&mut *entropy);
entropy
}
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn now_secs() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs()
}
pub(crate) fn verify_timestamps(
envelope_timestamp: Option<prost_types::Timestamp>,
timestamp: Option<prost_types::Timestamp>,
) -> Result<(), crate::Error> {
match (envelope_timestamp, timestamp) {
(Some(envelope_ts), Some(inner_ts)) if envelope_ts == inner_ts => Ok(()),
(None, None) => {
#[cfg(feature = "logging")]
tracing::warn!(
"timestamp invariant violated: both envelope and inner timestamp absent"
);
Err(crate::Error::Invariant(
"Envelope and request/response timestamps are both absent; absence cannot be used to satisfy the binding check",
))
}
_ => {
#[cfg(feature = "logging")]
tracing::warn!("timestamp invariant violated");
Err(crate::Error::Invariant(
"Envelope timestamp does not match request/response timestamp",
))
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use prost_types::Timestamp;
fn ts(seconds: i64) -> Timestamp {
Timestamp { seconds, nanos: 0 }
}
#[test]
fn accepts_matching_some_timestamps() {
assert!(verify_timestamps(Some(ts(1_700_000_000)), Some(ts(1_700_000_000))).is_ok());
}
#[test]
fn rejects_mismatched_some_timestamps() {
let res = verify_timestamps(Some(ts(1_700_000_000)), Some(ts(1_700_000_001)));
match res {
Err(crate::Error::Invariant(msg)) => {
assert!(msg.contains("does not match"), "msg: {msg}")
}
other => panic!("expected Invariant mismatch, got {other:?}"),
}
}
#[test]
fn rejects_both_none() {
let res = verify_timestamps(None, None);
match res {
Err(crate::Error::Invariant(msg)) => {
assert!(
msg.contains("absent"),
"expected absence-specific message, got: {msg}"
)
}
other => panic!("expected Invariant for (None, None), got {other:?}"),
}
}
#[test]
fn rejects_envelope_none() {
let res = verify_timestamps(None, Some(ts(1_700_000_000)));
assert!(matches!(res, Err(crate::Error::Invariant(_))));
}
#[test]
fn rejects_inner_none() {
let res = verify_timestamps(Some(ts(1_700_000_000)), None);
assert!(matches!(res, Err(crate::Error::Invariant(_))));
}
}