use crate::imp::core::ErrorCode;
use alloc::vec::Vec;
pub type HostResult = Result<Vec<u8>, ErrorCode>;
pub trait DigHost {
fn get_public_key(&self) -> HostResult;
fn create_attestation(&self, challenge: &[u8]) -> HostResult;
fn establish_session(&self, challenge: &[u8]) -> HostResult;
fn verify_session(&self) -> bool;
fn jwks_fetch(&self, url: &[u8]) -> HostResult;
fn current_time(&self) -> u64;
fn random_bytes(&self, count: u32) -> HostResult;
}
#[cfg(target_arch = "wasm32")]
pub struct WasmHost;
#[cfg(target_arch = "wasm32")]
impl DigHost for WasmHost {
fn get_public_key(&self) -> HostResult {
crate::imp::guest::imports::read_result(unsafe {
crate::imp::guest::imports::host_get_public_key()
})
}
fn create_attestation(&self, challenge: &[u8]) -> HostResult {
crate::imp::guest::imports::read_result(unsafe {
crate::imp::guest::imports::host_create_attestation(challenge.as_ptr() as i32)
})
}
fn establish_session(&self, challenge: &[u8]) -> HostResult {
crate::imp::guest::imports::read_result(unsafe {
crate::imp::guest::imports::host_establish_session(challenge.as_ptr() as i32)
})
}
fn verify_session(&self) -> bool {
unsafe { crate::imp::guest::imports::host_verify_session() == 1 }
}
fn jwks_fetch(&self, url: &[u8]) -> HostResult {
crate::imp::guest::imports::read_result(unsafe {
crate::imp::guest::imports::jwks_fetch(url.as_ptr() as i32, url.len() as i32)
})
}
fn current_time(&self) -> u64 {
unsafe { crate::imp::guest::imports::host_get_current_time() as u64 }
}
fn random_bytes(&self, count: u32) -> HostResult {
crate::imp::guest::imports::read_result(unsafe {
crate::imp::guest::imports::host_random_bytes(count as i32)
})
}
}