use crate::*;
use solana_sdk::signature::{Signature, Signer};
use std::str::FromStr;
use std::sync::Arc;
#[derive(Default, Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct ServiceWorkerVerifyMessage {
pub service: Pubkey,
pub service_worker: Pubkey,
pub enclave_size: u64,
pub cpu: u64,
}
impl ServiceWorkerVerifyMessage {
fn to_msg_bytes<'a>(&'a self) -> Vec<u8> {
serde_json::to_string(&self).unwrap().as_bytes().to_vec()
}
pub fn sign(&self, worker_authority: &Keypair) -> Signature {
worker_authority.sign_message(&self.to_msg_bytes())
}
pub fn verify(&self, worker_authority: &Pubkey, signature: &str) -> Result<(), SbError> {
let signature = solana_sdk::signature::Signature::from_str(signature).map_err(|err| {
SbError::CustomError {
message: format!("Failed to deserialize signature"),
source: Arc::new(err),
}
})?;
if signature.verify(
worker_authority.as_ref(),
serde_json::to_string(&self).unwrap().as_bytes(),
) {
Ok(())
} else {
Err(SbError::Message("Failed to verify signature"))
}
}
}