use std::fmt::Debug; use std::str::FromStr; use std::sync::{Mutex, MutexGuard}; pub use aes_gcm; pub use env_logger; pub use log; pub use sp_client; pub use serde_json; pub use serde_wasm_bindgen; pub mod crypto; pub mod device; pub mod error; pub mod network; pub mod pcd; pub mod prd; pub mod process; pub mod secrets; pub mod serialization; pub mod signature; pub mod silentpayments; pub const MAX_PRD_PAYLOAD_SIZE: usize = u16::MAX as usize; // 64KiB sounds reasonable for now const DEMIURGE: &str = "demiurge"; const PAIRING: &str = "pairing"; const APOPHIS: &str = "apophis"; const MEMBERPUBLICNAME: &str = "memberPublicName"; const PAIREDADDRESSES: &str = "pairedAddresses"; #[derive(Debug, PartialEq, Eq)] pub enum SpecialRoles { Demiurge, // Only valid for the first state of a process Pairing, // Special validation rules for pairing process Apophis, // Users in this role have the power to destroy the process } impl std::fmt::Display for SpecialRoles { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!(f, "{}", <&Self as Into<&str>>::into(self)) } } impl From<&SpecialRoles> for &str { fn from(value: &SpecialRoles) -> Self { match value { SpecialRoles::Demiurge => DEMIURGE, SpecialRoles::Pairing => PAIRING, SpecialRoles::Apophis => APOPHIS, } } } impl FromStr for SpecialRoles { type Err = String; fn from_str(s: &str) -> Result { match s { DEMIURGE => Ok(SpecialRoles::Demiurge), PAIRING => Ok(SpecialRoles::Pairing), APOPHIS => Ok(SpecialRoles::Apophis), _ => Err(format!("Invalid special role: {}", s)), } } } #[derive(Debug, PartialEq, Eq)] pub enum ReservedFields { PairedAddresses, MemberPublicName, } impl From<&ReservedFields> for &str { fn from(value: &ReservedFields) -> Self { match value { ReservedFields::MemberPublicName => MEMBERPUBLICNAME, ReservedFields::PairedAddresses => PAIREDADDRESSES, } } } impl FromStr for ReservedFields { type Err = String; fn from_str(s: &str) -> Result { match s { MEMBERPUBLICNAME => Ok(ReservedFields::MemberPublicName), PAIREDADDRESSES => Ok(ReservedFields::PairedAddresses), _ => Err(format!("Invalid field name: {}", s)), } } } pub trait MutexExt { fn lock_anyhow(&self) -> Result, anyhow::Error>; } impl MutexExt for Mutex { fn lock_anyhow(&self) -> Result, anyhow::Error> { match self.lock() { Ok(guard) => Ok(guard), Err(poison_error) => { let data = poison_error.into_inner(); log::debug!( "Failed to lock Mutex (poisoned). Data was: {:?}", data ); Err(anyhow::anyhow!("Failed to lock Mutex (poisoned)")) } } } }