use crate::imp::core::ErrorCode;
use alloc::vec;
use alloc::vec::Vec;
#[link(wasm_import_module = "dig_host")]
extern "C" {
pub fn host_get_public_key() -> i32;
pub fn host_create_attestation(challenge_ptr: i32) -> i32;
pub fn host_establish_session(challenge_ptr: i32) -> i32;
pub fn host_verify_session() -> i32;
pub fn jwks_fetch(url_ptr: i32, url_len: i32) -> i32;
pub fn host_get_current_time() -> i64;
pub fn host_random_bytes(count: i32) -> i32;
pub fn host_read_return_buffer(dest_ptr: i32) -> i32;
}
#[cfg(target_arch = "wasm32")]
#[inline(never)]
pub fn retain_dig_host_imports() -> i32 {
if core::hint::black_box(false) {
let mut acc: i64 = 0;
unsafe {
acc ^= host_get_public_key() as i64;
acc ^= host_create_attestation(0) as i64;
acc ^= host_establish_session(0) as i64;
acc ^= host_verify_session() as i64;
acc ^= jwks_fetch(0, 0) as i64;
acc ^= host_get_current_time();
acc ^= host_random_bytes(0) as i64;
acc ^= host_read_return_buffer(0) as i64;
}
return acc as i32;
}
0
}
pub fn read_result(code: i32) -> Result<Vec<u8>, ErrorCode> {
if code < 0 {
return Err(map_error(code));
}
let len = code as usize;
let mut buf = vec![0u8; len];
unsafe {
let written = host_read_return_buffer(buf.as_mut_ptr() as i32);
if written < 0 {
return Err(map_error(written));
}
buf.truncate(written as usize);
}
Ok(buf)
}
pub fn map_error(code: i32) -> ErrorCode {
match code {
-100 => ErrorCode::NoSession,
-101 => ErrorCode::SessionExpired,
-102 => ErrorCode::AttestationFailed,
-200 => ErrorCode::NetworkError,
-203 => ErrorCode::Timeout,
-300 => ErrorCode::NotFound,
-301 => ErrorCode::ValidationFailed,
-2 => ErrorCode::InvalidParameter,
-3 => ErrorCode::BufferTooSmall,
_ => ErrorCode::GeneralError,
}
}