use miden_protocol::{Felt, Word};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ProcedureName {
UpdateSigners,
UpdateProcedureThreshold,
AuthTx,
UpdateGuardian,
VerifyGuardian,
SendAsset,
ReceiveAsset,
}
impl ProcedureName {
pub fn root(&self) -> Word {
match self {
ProcedureName::UpdateSigners => {
word_from_hex("cb3364ddaa023b48707a5b5cc48c74079b83b000fe198db9e4d0ce6327d7ae0b")
}
ProcedureName::UpdateProcedureThreshold => {
word_from_hex("d772d8edee882f6b6d7a78aff6e3041c5782294b6bdc5c8d94b23a0a12f9a1cd")
}
ProcedureName::AuthTx => {
word_from_hex("a9dc7f8f5a1d53a5555c24b308e59e8ffe91f80e6fcb4288d91a6370d5bc1a61")
}
ProcedureName::UpdateGuardian => {
word_from_hex("5bf5d8a2d44c6825ba867f6028bcbc2b8b9ba054dc94000eae24bce3e68c4935")
}
ProcedureName::VerifyGuardian => {
word_from_hex("d1dfb9694996bf59bf7e1454ef660a3e9dbaed441462d81d541a9fd8e9901b2f")
}
ProcedureName::SendAsset => {
word_from_hex("d6c130dba13c67ac4733915f24bea9d19f517f51a65c74ded7bcd27e066b400e")
}
ProcedureName::ReceiveAsset => {
word_from_hex("016ab79593165e5b849776919e0c0298fb9dac880d593d93edd7134bdcdb4b6f")
}
}
}
pub fn all() -> &'static [ProcedureName] {
&[
ProcedureName::UpdateSigners,
ProcedureName::UpdateProcedureThreshold,
ProcedureName::AuthTx,
ProcedureName::UpdateGuardian,
ProcedureName::VerifyGuardian,
ProcedureName::SendAsset,
ProcedureName::ReceiveAsset,
]
}
}
#[derive(Debug, Clone, Copy)]
pub struct ProcedureThreshold {
pub procedure: ProcedureName,
pub threshold: u32,
}
impl ProcedureThreshold {
pub fn new(procedure: ProcedureName, threshold: u32) -> Self {
Self {
procedure,
threshold,
}
}
pub fn procedure_root(&self) -> Word {
self.procedure.root()
}
}
impl std::fmt::Display for ProcedureName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ProcedureName::UpdateSigners => write!(f, "update_signers"),
ProcedureName::UpdateProcedureThreshold => write!(f, "update_procedure_threshold"),
ProcedureName::AuthTx => write!(f, "auth_tx"),
ProcedureName::UpdateGuardian => write!(f, "update_guardian"),
ProcedureName::VerifyGuardian => write!(f, "verify_guardian"),
ProcedureName::SendAsset => write!(f, "send_asset"),
ProcedureName::ReceiveAsset => write!(f, "receive_asset"),
}
}
}
impl std::str::FromStr for ProcedureName {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"update_signers" => Ok(ProcedureName::UpdateSigners),
"update_procedure_threshold" => Ok(ProcedureName::UpdateProcedureThreshold),
"auth_tx" => Ok(ProcedureName::AuthTx),
"update_guardian" => Ok(ProcedureName::UpdateGuardian),
"verify_guardian" => Ok(ProcedureName::VerifyGuardian),
"send_asset" => Ok(ProcedureName::SendAsset),
"receive_asset" => Ok(ProcedureName::ReceiveAsset),
_ => Err(format!("unknown procedure name: {}", s)),
}
}
}
fn word_from_hex(hex_str: &str) -> Word {
let bytes = hex::decode(hex_str).expect("invalid hex in procedure root constant");
assert_eq!(bytes.len(), 32, "procedure root must be 32 bytes");
let e3 = u64::from_be_bytes(bytes[0..8].try_into().unwrap());
let e2 = u64::from_be_bytes(bytes[8..16].try_into().unwrap());
let e1 = u64::from_be_bytes(bytes[16..24].try_into().unwrap());
let e0 = u64::from_be_bytes(bytes[24..32].try_into().unwrap());
Word::from([Felt::new(e0), Felt::new(e1), Felt::new(e2), Felt::new(e3)])
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn procedure_threshold_new_creates_correctly() {
let threshold = ProcedureThreshold::new(ProcedureName::ReceiveAsset, 1);
assert_eq!(threshold.procedure, ProcedureName::ReceiveAsset);
assert_eq!(threshold.threshold, 1);
}
#[test]
fn procedure_threshold_procedure_root_returns_correct_root() {
let threshold = ProcedureThreshold::new(ProcedureName::SendAsset, 2);
assert_eq!(threshold.procedure_root(), ProcedureName::SendAsset.root());
}
#[test]
fn procedure_name_round_trip() {
for name in ProcedureName::all() {
let s = name.to_string();
let parsed: ProcedureName = s.parse().unwrap();
assert_eq!(*name, parsed);
}
}
#[test]
fn procedure_roots_are_valid() {
for name in ProcedureName::all() {
let _root = name.root();
}
}
#[test]
fn parse_unknown_returns_error() {
let result: Result<ProcedureName, _> = "unknown_proc".parse();
assert!(result.is_err());
}
}