use super::runtime::sha256_hex;
pub fn store_wasm_binary(
catalog: &crate::control::security::catalog::types::SystemCatalog,
wasm_bytes: &[u8],
max_size: usize,
) -> crate::Result<String> {
if wasm_bytes.is_empty() {
return Err(crate::Error::BadRequest {
detail: "WASM binary is empty".into(),
});
}
if wasm_bytes.len() > max_size {
return Err(crate::Error::BadRequest {
detail: format!(
"WASM binary exceeds maximum size ({} bytes > {max_size} bytes)",
wasm_bytes.len()
),
});
}
if wasm_bytes.len() < 4 || &wasm_bytes[..4] != b"\0asm" {
return Err(crate::Error::BadRequest {
detail: "invalid WASM binary: missing \\0asm magic header".into(),
});
}
let hash = sha256_hex(wasm_bytes);
let key = format!("wasm_module:{hash}");
catalog
.put_raw(key.as_bytes(), wasm_bytes)
.map_err(|e| crate::Error::Internal {
detail: format!("failed to store WASM binary: {e}"),
})?;
Ok(hash)
}
pub fn load_wasm_binary(
catalog: &crate::control::security::catalog::types::SystemCatalog,
hash: &str,
) -> crate::Result<Vec<u8>> {
let key = format!("wasm_module:{hash}");
catalog
.get_raw(key.as_bytes())
.map_err(|e| crate::Error::Internal {
detail: format!("failed to load WASM binary: {e}"),
})?
.ok_or_else(|| crate::Error::BadRequest {
detail: format!("WASM module with hash '{hash}' not found"),
})
}
pub fn delete_wasm_binary(
catalog: &crate::control::security::catalog::types::SystemCatalog,
hash: &str,
) -> crate::Result<()> {
let key = format!("wasm_module:{hash}");
catalog
.delete_raw(key.as_bytes())
.map_err(|e| crate::Error::Internal {
detail: format!("failed to delete WASM binary: {e}"),
})
}