|
| 1 | +use alloy_primitives::{hex, keccak256, Bytes}; |
| 2 | +use jsonrpsee::proc_macros::rpc; |
| 3 | +use jsonrpsee_core::RpcResult; |
| 4 | +use std::sync::Arc; |
| 5 | +use tokio::sync::RwLock; |
| 6 | + |
| 7 | +use super::{ |
| 8 | + args::FlashtestationsArgs, |
| 9 | + attestation::{AttestationConfig, get_attestation_provider}, |
| 10 | + service::load_or_generate_tee_key, |
| 11 | +}; |
| 12 | + |
| 13 | +/// Admin API for op-rbuilder flashtestations |
| 14 | +#[rpc(server, namespace = "admin")] |
| 15 | +pub trait AdminApi { |
| 16 | + /// Get the raw attestation quote (cached after first request) |
| 17 | + #[method(name = "getAttestationQuote")] |
| 18 | + async fn get_attestation_quote(&self) -> RpcResult<Option<String>>; |
| 19 | +} |
| 20 | + |
| 21 | +/// Admin RPC server implementation |
| 22 | +pub struct AdminRpcServer { |
| 23 | + flashtestations_args: FlashtestationsArgs, |
| 24 | + cached_quote: Arc<RwLock<Option<Vec<u8>>>>, |
| 25 | +} |
| 26 | + |
| 27 | +impl AdminRpcServer { |
| 28 | + pub fn new(flashtestations_args: FlashtestationsArgs) -> Self { |
| 29 | + Self { |
| 30 | + flashtestations_args, |
| 31 | + cached_quote: Arc::new(RwLock::new(None)), |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +#[async_trait::async_trait] |
| 37 | +impl AdminApiServer for AdminRpcServer { |
| 38 | + async fn get_attestation_quote(&self) -> RpcResult<Option<String>> { |
| 39 | + // Check if quote is already cached |
| 40 | + { |
| 41 | + let cache = self.cached_quote.read().await; |
| 42 | + if let Some(quote) = cache.as_ref() { |
| 43 | + return Ok(Some(hex::encode(quote))); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + // Load TEE key using same logic as bootstrap |
| 48 | + let tee_service_signer = match load_or_generate_tee_key( |
| 49 | + &self.flashtestations_args.flashtestations_key_path, |
| 50 | + self.flashtestations_args.debug, |
| 51 | + &self.flashtestations_args.debug_tee_key_seed, |
| 52 | + ) { |
| 53 | + Ok(signer) => signer, |
| 54 | + Err(e) => { |
| 55 | + tracing::error!(error = %e, "Failed to load TEE key"); |
| 56 | + return Ok(None); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + // Quote not cached, fetch it |
| 61 | + let attestation_provider = get_attestation_provider(AttestationConfig { |
| 62 | + debug: self.flashtestations_args.debug, |
| 63 | + quote_provider: self.flashtestations_args.quote_provider.clone(), |
| 64 | + }); |
| 65 | + |
| 66 | + // Prepare report data same as in bootstrap |
| 67 | + let mut report_data = [0u8; 64]; |
| 68 | + let tee_address_bytes: [u8; 20] = tee_service_signer.address.into(); |
| 69 | + report_data[0..20].copy_from_slice(&tee_address_bytes); |
| 70 | + |
| 71 | + // Use empty ext_data same as bootstrap |
| 72 | + let ext_data = Bytes::from(b""); |
| 73 | + let ext_data_hash = keccak256(ext_data.as_ref()); |
| 74 | + report_data[20..52].copy_from_slice(ext_data_hash.as_ref()); |
| 75 | + |
| 76 | + // Request attestation |
| 77 | + match attestation_provider.get_attestation(report_data).await { |
| 78 | + Ok(quote) => { |
| 79 | + // Cache the quote for future requests |
| 80 | + let mut cache = self.cached_quote.write().await; |
| 81 | + *cache = Some(quote.clone()); |
| 82 | + Ok(Some(hex::encode(quote))) |
| 83 | + } |
| 84 | + Err(e) => { |
| 85 | + tracing::error!(error = %e, "Failed to get attestation quote"); |
| 86 | + Ok(None) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments