use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct EpochKeyBinding {
pub epoch: u64,
pub verifier_id: String,
pub key_hash: [u8; 32],
pub governance_root: [u8; 32],
}
impl EpochKeyBinding {
#[must_use]
pub fn is_valid_for(&self, epoch: u64, governance_root: &[u8; 32]) -> bool {
self.epoch == epoch && self.governance_root == *governance_root
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn epoch_key_binding_serialize() {
let binding = EpochKeyBinding {
epoch: 5,
verifier_id: "v1".into(),
key_hash: [0x11; 32],
governance_root: [0x22; 32],
};
let mut bytes = alloc::vec::Vec::new();
ciborium::into_writer(&binding, &mut bytes).unwrap();
let decoded: EpochKeyBinding = ciborium::from_reader(bytes.as_slice()).unwrap();
assert_eq!(binding, decoded);
}
#[test]
fn is_valid_for_correct() {
let binding = EpochKeyBinding {
epoch: 5,
verifier_id: "v1".into(),
key_hash: [0x11; 32],
governance_root: [0x22; 32],
};
assert!(binding.is_valid_for(5, &[0x22; 32]));
assert!(!binding.is_valid_for(4, &[0x22; 32]));
assert!(!binding.is_valid_for(5, &[0x33; 32]));
}
}