1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
use crate::prelude::*;
pub type MrEnclave = [u8; 32];
#[zero_copy(unsafe)]
#[repr(packed)]
#[derive(Debug, PartialEq)]
pub struct Quote {
/// The address of the signer generated within an enclave.
pub enclave_signer: Pubkey,
/// The quotes MRENCLAVE measurement dictating the contents of the secure enclave.
pub mr_enclave: [u8; 32],
/// The VerificationStatus of the quote.
pub verification_status: u8,
/// The unix timestamp when the quote was last verified.
pub verification_timestamp: i64,
/// The unix timestamp when the quotes verification status expires.
pub valid_until: i64,
/// The off-chain registry where the verifiers quote can be located.
pub quote_registry: [u8; 32],
/// Key to lookup the buffer data on IPFS or an alternative decentralized storage solution.
pub registry_key: [u8; 64],
/// The address of the secp256k1 signer.
pub secp256k1_signer: [u8; 64],
pub last_ed25519_signer: Pubkey,
pub last_secp256k1_signer: [u8; 64],
pub last_rotate_slot: u64,
/// Reserved.
pub _ebuf: [u8; 88],
}
impl Default for Quote {
fn default() -> Self {
unsafe { std::mem::zeroed() }
}
}
impl Quote {
pub fn reset_verification(&mut self) -> anchor_lang::Result<()> {
if self.verification_status != VerificationStatus::VerificationOverride as u8 {
self.verification_status = VerificationStatus::None.into();
}
self.enclave_signer = Pubkey::default();
self.verification_timestamp = 0;
self.valid_until = 0;
Ok(())
}
pub fn is_verified(&self, clock: &Clock) -> bool {
match self.verification_status.into() {
VerificationStatus::VerificationOverride => true,
VerificationStatus::VerificationSuccess => self.valid_until > clock.unix_timestamp,
_ => false,
}
}
}
#[derive(Copy, Clone, Debug, AnchorSerialize, AnchorDeserialize, PartialEq)]
pub struct BorshQuote {
/// The address of the signer generated within an enclave.
pub enclave_signer: Pubkey,
/// The [`VerifierAccountData`] which completed the verification.
pub verifier: Pubkey,
/// The quotes MRENCLAVE measurement dictating the contents of the secure enclave.
pub mr_enclave: [u8; 32],
/// The off-chain registry where the verifiers quote can be located.
pub quote_registry: [u8; 32],
/// Key to lookup the buffer data on IPFS or an alternative decentralized storage solution.
pub registry_key: [u8; 64],
pub verification_status: u8,
/// The unix timestamp when verification was requested.
pub request_timestamp: i64,
/// The unix timestamp when the quote was last verified.
pub verification_timestamp: i64,
/// The unix timestamp when the quotes verification status expires.
pub valid_until: i64,
pub _ebuf: [u8; 32],
}
impl Default for BorshQuote {
fn default() -> Self {
unsafe { std::mem::zeroed() }
}
}
pub trait ToU8 {
fn to_u8(&self) -> u8;
}
impl ToU8 for bool {
fn to_u8(&self) -> u8 {
if *self {
1
} else {
0
}
}
}
impl ToU8 for &bool {
fn to_u8(&self) -> u8 {
if **self {
1
} else {
0
}
}
}
pub trait ToBool {
fn to_bool(&self) -> bool;
}
impl ToBool for u8 {
fn to_bool(&self) -> bool {
!matches!(*self, 0)
}
}
impl ToBool for &u8 {
fn to_bool(&self) -> bool {
!matches!(**self, 0)
}
}
/// An enum representing a boolean flag which can be locked.
/// Byte #0: 0 = Disabled, 1 = Enabled
/// Byte #1: 0 = Unlocked, 1 = Locked
#[repr(u8)]
#[derive(
Copy, Clone, Default, Debug, Eq, PartialEq, AnchorSerialize, AnchorDeserialize, InitSpace,
)]
pub enum BoolWithLock {
#[default]
FalseUnlocked, // 0 : 00000000
TrueUnlocked, // 1 : 00000001 : 1 << 0
FalseLocked, // 2 : 00000010 : 1 << 1
TrueLocked, // 3 : 00000011
}
impl BoolWithLock {
pub fn to_bool(&self) -> bool {
let byte: u8 = (*self).into();
byte & (1 << 0) != 0
}
pub fn is_locked(&self) -> bool {
let byte: u8 = (*self).into();
byte & (1 << 1) != 0
}
/// Converts boolean flags into a bitfield enum value.
///
/// # Arguments
///
/// * `b` - A boolean flag indicating if the feature is enabled.
/// * `is_locked` - A boolean flag indicating if the feature is locked.
///
/// # Returns
///
/// A bitfield enum value representing the input flags.
fn from_flags(b: bool, is_locked: Option<bool>) -> Self {
let mut value: u8 = 0;
if b {
value |= 1 << 0; // Set the 0th bit if enabled
}
if is_locked.unwrap_or_default() {
value |= 1 << 1; // Set the 1st bit if locked
}
value.into()
}
/// Asserts that the configuration parameter is unlocked.
pub fn assert_unlocked(&self) -> anchor_lang::Result<()> {
if self.is_locked() {
return Err(error!(SwitchboardError::ConfigParameterLocked));
}
Ok(())
}
/// Updates the value of the enum with a new value.
///
/// # Arguments
///
/// * `new_value` - A reference to a `BoolWithLock` struct containing the new value.
///
/// # Errors
///
/// Returns an error if the enum is locked and an update attempt is made.
///
/// # Returns
///
/// Returns `Ok(())` if the update is successful.
pub fn update(&mut self, b: bool, is_locked: Option<bool>) -> anchor_lang::Result<()> {
self.assert_unlocked()?;
let new_value = Self::from_flags(b, is_locked);
*self = new_value;
Ok(())
}
/// Locks the enum value for further updates. No action taken if the enum is already locked.
pub fn lock(&mut self) -> anchor_lang::Result<()> {
if self.is_locked() {
return Ok(());
}
let mut val: u8 = (*self).into();
val |= 1 << 1;
*self = val.into();
Ok(())
}
}
impl From<BoolWithLock> for u8 {
fn from(value: BoolWithLock) -> Self {
match value {
BoolWithLock::FalseUnlocked => 0,
BoolWithLock::TrueUnlocked => 1,
BoolWithLock::FalseLocked => 2,
BoolWithLock::TrueLocked => 3,
}
}
}
impl From<u8> for BoolWithLock {
fn from(value: u8) -> Self {
match value {
1 => BoolWithLock::TrueUnlocked,
2 => BoolWithLock::FalseLocked,
3 => BoolWithLock::TrueLocked,
_ => BoolWithLock::default(),
}
}
}
/// An enum representing a heirarchy of resources that can modify a field.
#[repr(u8)]
#[derive(
Copy, Clone, Default, Debug, Eq, PartialEq, AnchorSerialize, AnchorDeserialize, InitSpace,
)]
pub enum ResourceLevel {
#[default]
None = 0, // 0
/// The resource's authority has set this value.
Authority,
/// The resource function's authority has set this value.
Function,
/// The resource queue's authority has set this value.
Queue,
}
impl PartialOrd for ResourceLevel {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl Ord for ResourceLevel {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
(*self as u8).cmp(&(*other as u8))
}
}
impl From<ResourceLevel> for u8 {
fn from(value: ResourceLevel) -> Self {
match value {
ResourceLevel::Authority => 1,
ResourceLevel::Function => 2,
ResourceLevel::Queue => 3,
_ => 0,
}
}
}
impl From<u8> for ResourceLevel {
fn from(value: u8) -> Self {
match value {
1 => ResourceLevel::Authority,
2 => ResourceLevel::Function,
3 => ResourceLevel::Queue,
_ => ResourceLevel::default(),
}
}
}
impl From<ResourceLevel> for bool {
fn from(value: ResourceLevel) -> Self {
!matches!(value, ResourceLevel::None)
}
}
impl ResourceLevel {
pub fn update(
&mut self,
access_level: &ResourceLevel,
reset: Option<bool>,
) -> anchor_lang::Result<()> {
let target_value = if reset.unwrap_or_default() {
ResourceLevel::None
} else {
*access_level
};
// No action needed
if self == &target_value {
return Ok(());
}
// If insufficient access to change the value
if self > &mut access_level.clone() {
msg!(
"ResourceLevel: curr ({:?}), target ({:?}), access_level ({:?})",
self,
target_value,
access_level
);
return Err(error!(SwitchboardError::IllegalExecuteAttempt));
}
*self = target_value;
Ok(())
}
}