miden_node_store/allowlist/
invitation.rs1use std::fmt;
2
3use sha2::{Digest, Sha256};
4use thiserror::Error;
5
6#[derive(Clone, PartialEq, Eq)]
12pub struct InvitationCode([u8; 32]);
13
14impl InvitationCode {
15 pub fn new(code: &str) -> Result<Self, InvalidInvitationCode> {
17 if code.is_empty() {
18 return Err(InvalidInvitationCode);
19 }
20 Ok(Self(Sha256::digest(code.as_bytes()).into()))
21 }
22
23 pub fn from_digest(digest: [u8; 32]) -> Self {
26 Self(digest)
27 }
28
29 pub fn from_hex_digest(value: &str) -> Result<Self, hex::FromHexError> {
32 let mut digest = [0; 32];
33 hex::decode_to_slice(value, &mut digest)?;
34 Ok(Self::from_digest(digest))
35 }
36
37 pub fn to_hex_digest(&self) -> String {
39 hex::encode(self.0)
40 }
41
42 pub(crate) fn digest(&self) -> &[u8] {
43 &self.0
44 }
45}
46
47impl fmt::Debug for InvitationCode {
48 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49 f.write_str("InvitationCode([REDACTED])")
50 }
51}
52
53#[derive(Debug, Error, PartialEq, Eq)]
55#[error("invitation code must not be empty")]
56pub struct InvalidInvitationCode;