veilid/veilid-wasm/src/lib.rs

1473 lines
47 KiB
Rust
Raw Normal View History

2022-03-15 13:33:34 +00:00
// wasm-bindgen and clippy don't play well together yet
#![allow(clippy::all)]
2022-02-07 02:18:42 +00:00
#![cfg(target_arch = "wasm32")]
#![no_std]
extern crate alloc;
2022-03-15 13:33:34 +00:00
use alloc::string::String;
use alloc::sync::Arc;
2022-06-16 03:29:45 +00:00
use alloc::*;
2022-03-15 13:33:34 +00:00
use core::cell::RefCell;
2022-12-03 03:52:03 +00:00
use core::fmt::Debug;
2023-07-02 21:23:04 +00:00
use core::sync::atomic::{AtomicBool, Ordering};
2022-03-15 13:33:34 +00:00
use futures_util::FutureExt;
2022-10-05 23:12:10 +00:00
use gloo_utils::format::JsValueSerdeExt;
2022-03-15 13:33:34 +00:00
use js_sys::*;
use lazy_static::*;
use send_wrapper::*;
use serde::*;
2022-06-16 03:29:45 +00:00
use tracing_subscriber::prelude::*;
use tracing_subscriber::*;
2022-06-16 01:51:38 +00:00
use tracing_wasm::{WASMLayerConfigBuilder, *};
2022-12-01 22:38:43 +00:00
use veilid_core::tools::*;
2022-03-15 13:33:34 +00:00
use veilid_core::*;
2022-12-03 03:52:03 +00:00
use wasm_bindgen::prelude::*;
2022-03-15 13:33:34 +00:00
use wasm_bindgen_futures::*;
2022-02-07 02:18:42 +00:00
2022-03-15 13:33:34 +00:00
// Allocator
extern crate wee_alloc;
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
2022-06-16 03:29:45 +00:00
// API Singleton
2022-03-15 13:33:34 +00:00
lazy_static! {
static ref VEILID_API: SendWrapper<RefCell<Option<veilid_core::VeilidAPI>>> =
SendWrapper::new(RefCell::new(None));
2022-07-01 16:13:52 +00:00
static ref FILTERS: SendWrapper<RefCell<BTreeMap<&'static str, veilid_core::VeilidLayerFilter>>> =
SendWrapper::new(RefCell::new(BTreeMap::new()));
2022-11-26 19:16:02 +00:00
static ref ROUTING_CONTEXTS: SendWrapper<RefCell<BTreeMap<u32, veilid_core::RoutingContext>>> =
SendWrapper::new(RefCell::new(BTreeMap::new()));
2022-12-29 03:53:58 +00:00
static ref TABLE_DBS: SendWrapper<RefCell<BTreeMap<u32, veilid_core::TableDB>>> =
SendWrapper::new(RefCell::new(BTreeMap::new()));
static ref TABLE_DB_TRANSACTIONS: SendWrapper<RefCell<BTreeMap<u32, veilid_core::TableDBTransaction>>> =
SendWrapper::new(RefCell::new(BTreeMap::new()));
2022-03-15 13:33:34 +00:00
}
fn get_veilid_api() -> Result<veilid_core::VeilidAPI, veilid_core::VeilidAPIError> {
(*VEILID_API)
.borrow()
.clone()
.ok_or(veilid_core::VeilidAPIError::NotInitialized)
}
fn take_veilid_api() -> Result<veilid_core::VeilidAPI, veilid_core::VeilidAPIError> {
(**VEILID_API)
.take()
.ok_or(veilid_core::VeilidAPIError::NotInitialized)
}
2022-11-26 19:16:02 +00:00
// JSON Helpers for WASM
2022-12-03 03:52:03 +00:00
pub fn to_json<T: Serialize + Debug>(val: T) -> JsValue {
2022-03-15 13:33:34 +00:00
JsValue::from_str(&serialize_json(val))
}
2023-01-04 19:20:41 +00:00
pub fn to_jsvalue<T>(val: T) -> JsValue
where
JsValue: From<T>,
{
JsValue::from(val)
}
2022-12-03 03:52:03 +00:00
pub fn from_json<T: de::DeserializeOwned + Debug>(
val: JsValue,
) -> Result<T, veilid_core::VeilidAPIError> {
2022-03-15 13:33:34 +00:00
let s = val
.as_string()
.ok_or_else(|| veilid_core::VeilidAPIError::ParseError {
message: "Value is not String".to_owned(),
value: String::new(),
})?;
deserialize_json(&s)
}
// Utility types for async API results
type APIResult<T> = Result<T, veilid_core::VeilidAPIError>;
const APIRESULT_UNDEFINED: APIResult<()> = APIResult::Ok(());
2023-01-04 19:20:41 +00:00
pub fn wrap_api_future_json<F, T>(future: F) -> Promise
2022-03-15 13:33:34 +00:00
where
F: Future<Output = APIResult<T>> + 'static,
2022-12-03 03:52:03 +00:00
T: Serialize + Debug + 'static,
2022-03-15 13:33:34 +00:00
{
2023-01-04 19:20:41 +00:00
future_to_promise(future.map(|res| res.map(|v| to_json(v)).map_err(|e| to_json(e))))
}
pub fn wrap_api_future_plain<F, T>(future: F) -> Promise
where
F: Future<Output = APIResult<T>> + 'static,
JsValue: From<T>,
T: 'static,
{
future_to_promise(future.map(|res| res.map(|v| to_jsvalue(v)).map_err(|e| to_json(e))))
}
pub fn wrap_api_future_void<F>(future: F) -> Promise
where
F: Future<Output = APIResult<()>> + 'static,
{
future_to_promise(future.map(|res| res.map(|_| JsValue::UNDEFINED).map_err(|e| to_json(e))))
2022-03-15 13:33:34 +00:00
}
2022-06-16 01:51:38 +00:00
/////////////////////////////////////////
2022-11-26 19:16:02 +00:00
// WASM-specific
2022-06-16 01:51:38 +00:00
#[derive(Debug, Deserialize, Serialize)]
pub struct VeilidWASMConfigLoggingPerformance {
pub enabled: bool,
2022-07-01 20:20:43 +00:00
pub level: veilid_core::VeilidConfigLogLevel,
2022-06-16 01:51:38 +00:00
pub logs_in_timings: bool,
pub logs_in_console: bool,
}
2022-07-01 16:13:52 +00:00
#[derive(Debug, Deserialize, Serialize)]
pub struct VeilidWASMConfigLoggingAPI {
pub enabled: bool,
2022-07-01 20:20:43 +00:00
pub level: veilid_core::VeilidConfigLogLevel,
2022-07-01 16:13:52 +00:00
}
2022-06-16 01:51:38 +00:00
#[derive(Debug, Deserialize, Serialize)]
pub struct VeilidWASMConfigLogging {
pub performance: VeilidWASMConfigLoggingPerformance,
2022-07-01 16:13:52 +00:00
pub api: VeilidWASMConfigLoggingAPI,
2022-06-16 01:51:38 +00:00
}
#[derive(Debug, Deserialize, Serialize)]
pub struct VeilidWASMConfig {
pub logging: VeilidWASMConfigLogging,
}
2022-11-26 19:16:02 +00:00
#[derive(Debug, Deserialize, Serialize)]
2023-03-12 16:24:21 +00:00
pub struct VeilidRouteBlob {
pub route_id: veilid_core::RouteId,
#[serde(with = "veilid_core::as_human_base64")]
2022-11-26 19:16:02 +00:00
pub blob: Vec<u8>,
}
2022-03-15 13:33:34 +00:00
// WASM Bindings
2022-03-16 03:02:24 +00:00
#[wasm_bindgen()]
2022-03-15 13:33:34 +00:00
pub fn initialize_veilid_wasm() {
console_error_panic_hook::set_once();
}
2023-07-02 21:23:04 +00:00
static INITIALIZED: AtomicBool = AtomicBool::new(false);
2022-06-16 01:51:38 +00:00
#[wasm_bindgen()]
2022-07-01 20:20:43 +00:00
pub fn initialize_veilid_core(platform_config: String) {
2023-07-02 21:23:04 +00:00
if INITIALIZED.swap(true, Ordering::Relaxed) {
return;
}
let platform_config: VeilidWASMConfig = veilid_core::deserialize_json(&platform_config)
.expect("failed to deserialize platform config json");
// Set up subscriber and layers
let subscriber = Registry::default();
let mut layers = Vec::new();
let mut filters = (*FILTERS).borrow_mut();
// Performance logger
if platform_config.logging.performance.enabled {
let filter =
veilid_core::VeilidLayerFilter::new(platform_config.logging.performance.level, None);
let layer = WASMLayer::new(
WASMLayerConfigBuilder::new()
.set_report_logs_in_timings(platform_config.logging.performance.logs_in_timings)
.set_console_config(if platform_config.logging.performance.logs_in_console {
ConsoleConfig::ReportWithConsoleColor
} else {
ConsoleConfig::NoReporting
})
.build(),
)
.with_filter(filter.clone());
filters.insert("performance", filter);
layers.push(layer.boxed());
};
// API logger
if platform_config.logging.api.enabled {
let filter = veilid_core::VeilidLayerFilter::new(platform_config.logging.api.level, None);
let layer = veilid_core::ApiTracingLayer::get().with_filter(filter.clone());
filters.insert("api", filter);
layers.push(layer.boxed());
}
2022-12-08 15:24:33 +00:00
2023-07-02 21:23:04 +00:00
let subscriber = subscriber.with(layers);
subscriber
.try_init()
.map_err(|e| format!("failed to initialize logging: {}", e))
.expect("failed to initalize WASM platform");
2022-06-16 01:51:38 +00:00
}
2022-07-01 16:13:52 +00:00
#[wasm_bindgen()]
pub fn change_log_level(layer: String, log_level: String) {
let layer = if layer == "all" { "".to_owned() } else { layer };
let log_level: veilid_core::VeilidConfigLogLevel = deserialize_json(&log_level).unwrap();
let filters = (*FILTERS).borrow();
if layer.is_empty() {
// Change all layers
for f in filters.values() {
f.set_max_level(log_level);
}
} else {
// Change a specific layer
let f = filters.get(layer.as_str()).unwrap();
f.set_max_level(log_level);
}
}
2022-03-16 03:02:24 +00:00
#[wasm_bindgen()]
2022-08-25 00:59:09 +00:00
pub fn startup_veilid_core(update_callback_js: Function, json_config: String) -> Promise {
let update_callback_js = SendWrapper::new(update_callback_js);
2023-01-04 19:20:41 +00:00
wrap_api_future_void(async move {
2022-03-15 13:33:34 +00:00
let update_callback = Arc::new(move |update: VeilidUpdate| {
let _ret =
2022-08-25 00:59:09 +00:00
match Function::call1(&update_callback_js, &JsValue::UNDEFINED, &to_json(update)) {
2022-03-15 13:33:34 +00:00
Ok(v) => v,
Err(e) => {
2022-12-17 01:07:28 +00:00
console_log(&format!("calling update callback failed: {:?}", e));
2022-03-15 13:33:34 +00:00
return;
}
};
});
if VEILID_API.borrow().is_some() {
return Err(veilid_core::VeilidAPIError::AlreadyInitialized);
}
let veilid_api = veilid_core::api_startup_json(update_callback, json_config).await?;
VEILID_API.replace(Some(veilid_api));
APIRESULT_UNDEFINED
})
}
2022-03-16 03:02:24 +00:00
#[wasm_bindgen()]
2022-03-15 13:33:34 +00:00
pub fn get_veilid_state() -> Promise {
2023-01-04 19:20:41 +00:00
wrap_api_future_json(async move {
2022-03-15 13:33:34 +00:00
let veilid_api = get_veilid_api()?;
let core_state = veilid_api.get_state().await?;
2023-01-04 19:20:41 +00:00
APIResult::Ok(core_state)
2022-03-15 13:33:34 +00:00
})
}
2022-09-06 22:59:41 +00:00
#[wasm_bindgen()]
pub fn attach() -> Promise {
2023-01-04 19:20:41 +00:00
wrap_api_future_void(async move {
2022-09-06 22:59:41 +00:00
let veilid_api = get_veilid_api()?;
veilid_api.attach().await?;
APIRESULT_UNDEFINED
})
}
#[wasm_bindgen()]
pub fn detach() -> Promise {
2023-01-04 19:20:41 +00:00
wrap_api_future_void(async move {
2022-09-06 22:59:41 +00:00
let veilid_api = get_veilid_api()?;
veilid_api.detach().await?;
APIRESULT_UNDEFINED
})
}
2022-03-16 03:02:24 +00:00
#[wasm_bindgen()]
2022-03-15 13:33:34 +00:00
pub fn shutdown_veilid_core() -> Promise {
2023-01-04 19:20:41 +00:00
wrap_api_future_void(async move {
2022-03-15 13:33:34 +00:00
let veilid_api = take_veilid_api()?;
veilid_api.shutdown().await;
APIRESULT_UNDEFINED
})
}
2022-12-03 03:52:03 +00:00
fn add_routing_context(routing_context: veilid_core::RoutingContext) -> u32 {
let mut next_id: u32 = 1;
let mut rc = (*ROUTING_CONTEXTS).borrow_mut();
while rc.contains_key(&next_id) {
next_id += 1;
}
rc.insert(next_id, routing_context);
next_id
}
2022-03-16 03:02:24 +00:00
#[wasm_bindgen()]
2022-12-03 03:52:03 +00:00
pub fn routing_context() -> Promise {
2023-01-04 19:20:41 +00:00
wrap_api_future_plain(async move {
2022-03-15 13:33:34 +00:00
let veilid_api = get_veilid_api()?;
2022-12-03 03:52:03 +00:00
let routing_context = veilid_api.routing_context();
let new_id = add_routing_context(routing_context);
APIResult::Ok(new_id)
})
}
#[wasm_bindgen()]
pub fn release_routing_context(id: u32) -> i32 {
let mut rc = (*ROUTING_CONTEXTS).borrow_mut();
if rc.remove(&id).is_none() {
return 0;
}
return 1;
}
#[wasm_bindgen()]
pub fn routing_context_with_privacy(id: u32) -> u32 {
let rc = (*ROUTING_CONTEXTS).borrow();
let Some(routing_context) = rc.get(&id) else {
return 0;
};
let Ok(routing_context) = routing_context.clone().with_privacy() else {
return 0;
};
let new_id = add_routing_context(routing_context);
new_id
}
#[wasm_bindgen()]
2023-07-02 21:23:04 +00:00
pub fn routing_context_with_custom_privacy(id: u32, safety_selection: String) -> u32 {
let safety_selection: veilid_core::SafetySelection =
veilid_core::deserialize_json(&safety_selection).unwrap();
2022-12-03 03:52:03 +00:00
let rc = (*ROUTING_CONTEXTS).borrow();
let Some(routing_context) = rc.get(&id) else {
return 0;
};
2023-07-02 21:23:04 +00:00
let Ok(routing_context) = routing_context.clone().with_custom_privacy(safety_selection) else {
2022-12-03 03:52:03 +00:00
return 0;
};
let new_id = add_routing_context(routing_context);
new_id
}
#[wasm_bindgen()]
pub fn routing_context_with_sequencing(id: u32, sequencing: String) -> u32 {
let sequencing: veilid_core::Sequencing = veilid_core::deserialize_json(&sequencing).unwrap();
let rc = (*ROUTING_CONTEXTS).borrow();
let Some(routing_context) = rc.get(&id) else {
return 0;
};
let routing_context = routing_context.clone().with_sequencing(sequencing);
let new_id = add_routing_context(routing_context);
new_id
}
#[wasm_bindgen()]
2023-08-29 20:52:47 +00:00
pub fn routing_context_app_call(id: u32, target_string: String, request: String) -> Promise {
2022-12-03 03:52:03 +00:00
let request: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(request.as_bytes())
.unwrap();
2023-01-04 19:20:41 +00:00
wrap_api_future_plain(async move {
2022-12-03 03:52:03 +00:00
let routing_context = {
let rc = (*ROUTING_CONTEXTS).borrow();
let Some(routing_context) = rc.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("routing_context_app_call", "id", id));
};
routing_context.clone()
};
2023-08-29 20:52:47 +00:00
let veilid_api = get_veilid_api()?;
let target = veilid_api.parse_as_target(target_string).await?;
2022-12-03 03:52:03 +00:00
let answer = routing_context.app_call(target, request).await?;
let answer = data_encoding::BASE64URL_NOPAD.encode(&answer);
APIResult::Ok(answer)
})
}
#[wasm_bindgen()]
2023-08-29 20:52:47 +00:00
pub fn routing_context_app_message(id: u32, target_string: String, message: String) -> Promise {
2022-12-03 03:52:03 +00:00
let message: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(message.as_bytes())
.unwrap();
2023-01-04 19:20:41 +00:00
wrap_api_future_void(async move {
2022-12-03 03:52:03 +00:00
let routing_context = {
let rc = (*ROUTING_CONTEXTS).borrow();
let Some(routing_context) = rc.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("routing_context_app_call", "id", id));
};
routing_context.clone()
};
2023-08-29 20:52:47 +00:00
let veilid_api = get_veilid_api()?;
let target = veilid_api.parse_as_target(target_string).await?;
2022-12-03 03:52:03 +00:00
routing_context.app_message(target, message).await?;
APIRESULT_UNDEFINED
})
}
2023-05-29 19:24:57 +00:00
#[wasm_bindgen()]
2023-07-09 02:50:44 +00:00
pub fn routing_context_create_dht_record(id: u32, schema: String, kind: u32) -> Promise {
let crypto_kind = if kind == 0 {
None
} else {
Some(veilid_core::FourCC::from(kind))
};
2023-05-29 19:24:57 +00:00
let schema: veilid_core::DHTSchema = veilid_core::deserialize_json(&schema).unwrap();
wrap_api_future_json(async move {
let routing_context = {
let rc = (*ROUTING_CONTEXTS).borrow();
let Some(routing_context) = rc.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("routing_context_create_dht_record", "id", id));
};
routing_context.clone()
};
let dht_record_descriptor = routing_context
.create_dht_record(schema, crypto_kind)
2023-05-29 19:24:57 +00:00
.await?;
APIResult::Ok(dht_record_descriptor)
})
}
#[wasm_bindgen()]
pub fn routing_context_open_dht_record(id: u32, key: String, writer: Option<String>) -> Promise {
let key: veilid_core::TypedKey = veilid_core::deserialize_json(&key).unwrap();
let writer: Option<veilid_core::KeyPair> =
writer.map(|s| veilid_core::deserialize_json(&s).unwrap());
wrap_api_future_json(async move {
let routing_context = {
let rc = (*ROUTING_CONTEXTS).borrow();
let Some(routing_context) = rc.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("routing_context_open_dht_record", "id", id));
};
routing_context.clone()
};
let dht_record_descriptor = routing_context.open_dht_record(key, writer).await?;
APIResult::Ok(dht_record_descriptor)
})
}
#[wasm_bindgen()]
pub fn routing_context_close_dht_record(id: u32, key: String) -> Promise {
let key: veilid_core::TypedKey = veilid_core::deserialize_json(&key).unwrap();
wrap_api_future_void(async move {
let routing_context = {
let rc = (*ROUTING_CONTEXTS).borrow();
let Some(routing_context) = rc.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("routing_context_close_dht_record", "id", id));
};
routing_context.clone()
};
routing_context.close_dht_record(key).await?;
APIRESULT_UNDEFINED
})
}
#[wasm_bindgen()]
pub fn routing_context_delete_dht_record(id: u32, key: String) -> Promise {
let key: veilid_core::TypedKey = veilid_core::deserialize_json(&key).unwrap();
wrap_api_future_void(async move {
let routing_context = {
let rc = (*ROUTING_CONTEXTS).borrow();
let Some(routing_context) = rc.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("routing_context_delete_dht_record", "id", id));
};
routing_context.clone()
};
routing_context.delete_dht_record(key).await?;
APIRESULT_UNDEFINED
})
}
#[wasm_bindgen()]
pub fn routing_context_get_dht_value(
id: u32,
key: String,
subkey: u32,
force_refresh: bool,
) -> Promise {
let key: veilid_core::TypedKey = veilid_core::deserialize_json(&key).unwrap();
2023-07-25 05:04:22 +00:00
wrap_api_future_json(async move {
2023-05-29 19:24:57 +00:00
let routing_context = {
let rc = (*ROUTING_CONTEXTS).borrow();
let Some(routing_context) = rc.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("routing_context_get_dht_value", "id", id));
};
routing_context.clone()
};
let res = routing_context
.get_dht_value(key, subkey, force_refresh)
.await?;
APIResult::Ok(res)
})
}
#[wasm_bindgen()]
pub fn routing_context_set_dht_value(id: u32, key: String, subkey: u32, data: String) -> Promise {
let key: veilid_core::TypedKey = veilid_core::deserialize_json(&key).unwrap();
let data: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(&data.as_bytes())
.unwrap();
2023-07-25 05:04:22 +00:00
wrap_api_future_json(async move {
2023-05-29 19:24:57 +00:00
let routing_context = {
let rc = (*ROUTING_CONTEXTS).borrow();
let Some(routing_context) = rc.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("routing_context_set_dht_value", "id", id));
};
routing_context.clone()
};
let res = routing_context.set_dht_value(key, subkey, data).await?;
APIResult::Ok(res)
})
}
#[wasm_bindgen()]
pub fn routing_context_watch_dht_values(
id: u32,
key: String,
subkeys: String,
expiration: String,
count: u32,
) -> Promise {
let key: veilid_core::TypedKey = veilid_core::deserialize_json(&key).unwrap();
let subkeys: veilid_core::ValueSubkeyRangeSet =
veilid_core::deserialize_json(&subkeys).unwrap();
let expiration = veilid_core::Timestamp::from_str(&expiration).unwrap();
wrap_api_future_plain(async move {
let routing_context = {
let rc = (*ROUTING_CONTEXTS).borrow();
let Some(routing_context) = rc.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("routing_context_watch_dht_values", "id", id));
};
routing_context.clone()
};
let res = routing_context
.watch_dht_values(key, subkeys, expiration, count)
.await?;
APIResult::Ok(res.to_string())
})
}
#[wasm_bindgen()]
pub fn routing_context_cancel_dht_watch(id: u32, key: String, subkeys: String) -> Promise {
let key: veilid_core::TypedKey = veilid_core::deserialize_json(&key).unwrap();
let subkeys: veilid_core::ValueSubkeyRangeSet =
veilid_core::deserialize_json(&subkeys).unwrap();
wrap_api_future_plain(async move {
let routing_context = {
let rc = (*ROUTING_CONTEXTS).borrow();
let Some(routing_context) = rc.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("routing_context_cancel_dht_watch", "id", id));
};
routing_context.clone()
};
let res = routing_context.cancel_dht_watch(key, subkeys).await?;
APIResult::Ok(res)
})
}
2022-12-03 03:52:03 +00:00
#[wasm_bindgen()]
pub fn new_private_route() -> Promise {
2023-01-04 19:20:41 +00:00
wrap_api_future_json(async move {
2022-12-03 03:52:03 +00:00
let veilid_api = get_veilid_api()?;
2023-03-12 16:24:21 +00:00
let (route_id, blob) = veilid_api.new_private_route().await?;
2022-12-03 03:52:03 +00:00
2023-03-12 16:24:21 +00:00
let route_blob = VeilidRouteBlob { route_id, blob };
2022-12-03 03:52:03 +00:00
2023-03-12 16:24:21 +00:00
APIResult::Ok(route_blob)
2022-12-03 03:52:03 +00:00
})
}
#[wasm_bindgen()]
pub fn new_custom_private_route(stability: String, sequencing: String) -> Promise {
let stability: veilid_core::Stability = veilid_core::deserialize_json(&stability).unwrap();
let sequencing: veilid_core::Sequencing = veilid_core::deserialize_json(&sequencing).unwrap();
2023-01-04 19:20:41 +00:00
wrap_api_future_json(async move {
2022-12-03 03:52:03 +00:00
let veilid_api = get_veilid_api()?;
2023-03-12 16:24:21 +00:00
let (route_id, blob) = veilid_api
.new_custom_private_route(&veilid_core::VALID_CRYPTO_KINDS, stability, sequencing)
2022-12-03 03:52:03 +00:00
.await?;
2023-03-12 16:24:21 +00:00
let route_blob = VeilidRouteBlob { route_id, blob };
2022-12-03 03:52:03 +00:00
2023-03-12 16:24:21 +00:00
APIResult::Ok(route_blob)
2022-12-03 03:52:03 +00:00
})
}
#[wasm_bindgen()]
pub fn import_remote_private_route(blob: String) -> Promise {
let blob: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(blob.as_bytes())
.unwrap();
2023-01-04 19:20:41 +00:00
wrap_api_future_plain(async move {
2022-12-03 03:52:03 +00:00
let veilid_api = get_veilid_api()?;
let key = veilid_api.import_remote_private_route(blob)?;
APIResult::Ok(key.encode())
})
}
#[wasm_bindgen()]
2023-03-12 16:24:21 +00:00
pub fn release_private_route(route_id: String) -> Promise {
let route_id: veilid_core::RouteId = veilid_core::deserialize_json(&route_id).unwrap();
2023-01-04 19:20:41 +00:00
wrap_api_future_void(async move {
2022-12-03 03:52:03 +00:00
let veilid_api = get_veilid_api()?;
2023-03-12 16:24:21 +00:00
veilid_api.release_private_route(route_id)?;
2022-12-03 03:52:03 +00:00
APIRESULT_UNDEFINED
2022-03-15 13:33:34 +00:00
})
}
2022-10-01 02:37:55 +00:00
#[wasm_bindgen()]
2023-06-16 15:57:55 +00:00
pub fn app_call_reply(call_id: String, message: String) -> Promise {
2022-12-03 03:52:03 +00:00
let message: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(message.as_bytes())
.unwrap();
2023-01-04 19:20:41 +00:00
wrap_api_future_void(async move {
2023-06-16 15:57:55 +00:00
let call_id = match call_id.parse() {
2022-10-01 02:37:55 +00:00
Ok(v) => v,
Err(e) => {
2023-06-16 15:57:55 +00:00
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument(
e, "call_id", call_id,
))
2022-10-01 02:37:55 +00:00
}
};
let veilid_api = get_veilid_api()?;
2023-06-16 15:57:55 +00:00
veilid_api.app_call_reply(call_id, message).await?;
2023-01-04 19:20:41 +00:00
APIRESULT_UNDEFINED
2022-10-01 02:37:55 +00:00
})
}
2022-12-29 03:53:58 +00:00
fn add_table_db(table_db: veilid_core::TableDB) -> u32 {
let mut next_id: u32 = 1;
let mut tdbs = (*TABLE_DBS).borrow_mut();
while tdbs.contains_key(&next_id) {
next_id += 1;
}
tdbs.insert(next_id, table_db);
next_id
}
#[wasm_bindgen()]
pub fn open_table_db(name: String, column_count: u32) -> Promise {
2023-01-04 19:20:41 +00:00
wrap_api_future_plain(async move {
2022-12-29 03:53:58 +00:00
let veilid_api = get_veilid_api()?;
let tstore = veilid_api.table_store()?;
let table_db = tstore
.open(&name, column_count)
.await
.map_err(veilid_core::VeilidAPIError::generic)?;
let new_id = add_table_db(table_db);
2023-01-04 19:20:41 +00:00
APIResult::Ok(new_id)
2022-12-29 03:53:58 +00:00
})
}
#[wasm_bindgen()]
pub fn release_table_db(id: u32) -> i32 {
let mut tdbs = (*TABLE_DBS).borrow_mut();
if tdbs.remove(&id).is_none() {
return 0;
}
return 1;
}
#[wasm_bindgen()]
pub fn delete_table_db(name: String) -> Promise {
2023-01-04 19:20:41 +00:00
wrap_api_future_plain(async move {
2022-12-29 03:53:58 +00:00
let veilid_api = get_veilid_api()?;
let tstore = veilid_api.table_store()?;
let deleted = tstore
.delete(&name)
.await
.map_err(veilid_core::VeilidAPIError::generic)?;
2023-01-04 19:20:41 +00:00
APIResult::Ok(deleted)
2022-12-29 03:53:58 +00:00
})
}
#[wasm_bindgen()]
pub fn table_db_get_column_count(id: u32) -> u32 {
let table_dbs = (*TABLE_DBS).borrow();
let Some(table_db) = table_dbs.get(&id) else {
return 0;
};
let Ok(cc) = table_db.clone().get_column_count() else {
return 0;
};
return cc;
}
#[wasm_bindgen()]
2023-05-29 19:24:57 +00:00
pub fn table_db_get_keys(id: u32, col: u32) -> Promise {
wrap_api_future_json(async move {
let table_db = {
let table_dbs = (*TABLE_DBS).borrow();
let Some(table_db) = table_dbs.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("table_db_store", "id", id));
};
table_db.clone()
};
let keys = table_db.clone().get_keys(col).await?;
let out: Vec<String> = keys
.into_iter()
.map(|k| data_encoding::BASE64URL_NOPAD.encode(&k))
.collect();
APIResult::Ok(out)
})
2022-12-29 03:53:58 +00:00
}
fn add_table_db_transaction(tdbt: veilid_core::TableDBTransaction) -> u32 {
let mut next_id: u32 = 1;
let mut tdbts = (*TABLE_DB_TRANSACTIONS).borrow_mut();
while tdbts.contains_key(&next_id) {
next_id += 1;
}
tdbts.insert(next_id, tdbt);
next_id
}
#[wasm_bindgen()]
pub fn table_db_transact(id: u32) -> u32 {
let table_dbs = (*TABLE_DBS).borrow();
let Some(table_db) = table_dbs.get(&id) else {
return 0;
};
let tdbt = table_db.clone().transact();
let tdbtid = add_table_db_transaction(tdbt);
return tdbtid;
}
#[wasm_bindgen()]
pub fn release_table_db_transaction(id: u32) -> i32 {
let mut tdbts = (*TABLE_DB_TRANSACTIONS).borrow_mut();
if tdbts.remove(&id).is_none() {
return 0;
}
return 1;
}
#[wasm_bindgen()]
pub fn table_db_transaction_commit(id: u32) -> Promise {
2023-01-04 19:20:41 +00:00
wrap_api_future_void(async move {
2022-12-29 03:53:58 +00:00
let tdbt = {
let tdbts = (*TABLE_DB_TRANSACTIONS).borrow();
let Some(tdbt) = tdbts.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("table_db_transaction_commit", "id", id));
};
tdbt.clone()
};
2023-05-29 19:24:57 +00:00
tdbt.commit().await?;
2022-12-29 03:53:58 +00:00
APIRESULT_UNDEFINED
})
}
#[wasm_bindgen()]
pub fn table_db_transaction_rollback(id: u32) -> Promise {
2023-01-04 19:20:41 +00:00
wrap_api_future_void(async move {
2022-12-29 03:53:58 +00:00
let tdbt = {
let tdbts = (*TABLE_DB_TRANSACTIONS).borrow();
let Some(tdbt) = tdbts.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("table_db_transaction_rollback", "id", id));
};
tdbt.clone()
};
tdbt.rollback();
APIRESULT_UNDEFINED
})
}
#[wasm_bindgen()]
pub fn table_db_transaction_store(id: u32, col: u32, key: String, value: String) -> Promise {
let key: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(key.as_bytes())
.unwrap();
let value: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(value.as_bytes())
.unwrap();
2023-01-04 19:20:41 +00:00
wrap_api_future_void(async move {
2022-12-29 03:53:58 +00:00
let tdbt = {
let tdbts = (*TABLE_DB_TRANSACTIONS).borrow();
let Some(tdbt) = tdbts.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("table_db_transaction_store", "id", id));
};
tdbt.clone()
};
2023-06-18 22:47:39 +00:00
tdbt.store(col, &key, &value)?;
2022-12-29 03:53:58 +00:00
APIRESULT_UNDEFINED
})
}
#[wasm_bindgen()]
pub fn table_db_transaction_delete(id: u32, col: u32, key: String) -> Promise {
let key: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(key.as_bytes())
.unwrap();
2023-01-04 19:20:41 +00:00
wrap_api_future_void(async move {
2022-12-29 03:53:58 +00:00
let tdbt = {
let tdbts = (*TABLE_DB_TRANSACTIONS).borrow();
let Some(tdbt) = tdbts.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("table_db_transaction_delete", "id", id));
};
tdbt.clone()
};
2023-06-18 22:47:39 +00:00
tdbt.delete(col, &key)?;
2023-01-04 19:20:41 +00:00
APIRESULT_UNDEFINED
2022-12-29 03:53:58 +00:00
})
}
#[wasm_bindgen()]
pub fn table_db_store(id: u32, col: u32, key: String, value: String) -> Promise {
let key: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(key.as_bytes())
.unwrap();
let value: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(value.as_bytes())
.unwrap();
2023-01-04 19:20:41 +00:00
wrap_api_future_void(async move {
2022-12-29 03:53:58 +00:00
let table_db = {
let table_dbs = (*TABLE_DBS).borrow();
let Some(table_db) = table_dbs.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("table_db_store", "id", id));
};
table_db.clone()
};
2023-05-29 19:24:57 +00:00
table_db.store(col, &key, &value).await?;
2022-12-29 03:53:58 +00:00
APIRESULT_UNDEFINED
})
}
#[wasm_bindgen()]
pub fn table_db_load(id: u32, col: u32, key: String) -> Promise {
let key: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(key.as_bytes())
.unwrap();
2023-01-04 19:20:41 +00:00
wrap_api_future_plain(async move {
2022-12-29 03:53:58 +00:00
let table_db = {
let table_dbs = (*TABLE_DBS).borrow();
let Some(table_db) = table_dbs.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("table_db_load", "id", id));
};
table_db.clone()
};
2023-05-29 19:24:57 +00:00
let out = table_db.load(col, &key).await?;
2022-12-29 03:53:58 +00:00
let out = out.map(|x| data_encoding::BASE64URL_NOPAD.encode(&x));
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn table_db_delete(id: u32, col: u32, key: String) -> Promise {
let key: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(key.as_bytes())
.unwrap();
2023-01-04 19:20:41 +00:00
wrap_api_future_plain(async move {
2022-12-29 03:53:58 +00:00
let table_db = {
let table_dbs = (*TABLE_DBS).borrow();
let Some(table_db) = table_dbs.get(&id) else {
return APIResult::Err(veilid_core::VeilidAPIError::invalid_argument("table_db_delete", "id", id));
};
table_db.clone()
};
2023-05-29 19:24:57 +00:00
let out = table_db.delete(col, &key).await?;
let out = out.map(|x| data_encoding::BASE64URL_NOPAD.encode(&x));
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn valid_crypto_kinds() -> String {
veilid_core::serialize_json(
veilid_core::VALID_CRYPTO_KINDS
.iter()
.map(|k| (*k).into())
.collect::<Vec<u32>>(),
)
}
#[wasm_bindgen()]
pub fn best_crypto_kind() -> u32 {
veilid_core::best_crypto_kind().into()
}
#[wasm_bindgen()]
pub fn verify_signatures(node_ids: String, data: String, signatures: String) -> Promise {
let node_ids: Vec<veilid_core::TypedKey> = veilid_core::deserialize_json(&node_ids).unwrap();
let data: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(data.as_bytes())
.unwrap();
let typed_signatures: Vec<veilid_core::TypedSignature> =
veilid_core::deserialize_json(&signatures).unwrap();
wrap_api_future_json(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let out = crypto.verify_signatures(&node_ids, &data, &typed_signatures)?;
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn generate_signatures(data: String, key_pairs: String) -> Promise {
let data: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(data.as_bytes())
.unwrap();
let key_pairs: Vec<veilid_core::TypedKeyPair> =
veilid_core::deserialize_json(&key_pairs).unwrap();
wrap_api_future_json(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let out = crypto.generate_signatures(&data, &key_pairs, |k, s| {
veilid_core::TypedSignature::new(k.kind, s)
})?;
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn generate_key_pair(kind: u32) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
wrap_api_future_json(async move {
let out = veilid_core::Crypto::generate_keypair(kind)?;
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_cached_dh(kind: u32, key: String, secret: String) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
let key: veilid_core::PublicKey = veilid_core::deserialize_json(&key).unwrap();
let secret: veilid_core::SecretKey = veilid_core::deserialize_json(&secret).unwrap();
wrap_api_future_json(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_cached_dh",
"kind",
kind.to_string(),
)
})?;
let out = csv.cached_dh(&key, &secret)?;
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_compute_dh(kind: u32, key: String, secret: String) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
let key: veilid_core::PublicKey = veilid_core::deserialize_json(&key).unwrap();
let secret: veilid_core::SecretKey = veilid_core::deserialize_json(&secret).unwrap();
wrap_api_future_json(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_compute_dh",
"kind",
kind.to_string(),
)
})?;
let out = csv.compute_dh(&key, &secret)?;
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_random_bytes(kind: u32, len: u32) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
wrap_api_future_plain(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_random_bytes",
"kind",
kind.to_string(),
)
})?;
let out = csv.random_bytes(len);
let out = data_encoding::BASE64URL_NOPAD.encode(&out);
2022-12-29 03:53:58 +00:00
APIResult::Ok(out)
})
}
2023-05-29 19:24:57 +00:00
#[wasm_bindgen()]
pub fn crypto_default_salt_length(kind: u32) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
wrap_api_future_plain(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_default_salt_length",
"kind",
kind.to_string(),
)
})?;
let out = csv.default_salt_length();
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_hash_password(kind: u32, password: String, salt: String) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
let password: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(password.as_bytes())
.unwrap();
let salt: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(salt.as_bytes())
.unwrap();
wrap_api_future_plain(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_hash_password",
"kind",
kind.to_string(),
)
})?;
let out = csv.hash_password(&password, &salt)?;
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_verify_password(kind: u32, password: String, password_hash: String) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
let password: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(password.as_bytes())
.unwrap();
wrap_api_future_plain(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_verify_password",
"kind",
kind.to_string(),
)
})?;
let out = csv.verify_password(&password, &password_hash)?;
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_derive_shared_secret(kind: u32, password: String, salt: String) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
let password: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(password.as_bytes())
.unwrap();
let salt: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(salt.as_bytes())
.unwrap();
wrap_api_future_json(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_derive_shared_secret",
"kind",
kind.to_string(),
)
})?;
let out = csv.derive_shared_secret(&password, &salt)?;
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_random_nonce(kind: u32) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
wrap_api_future_json(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_random_nonce",
"kind",
kind.to_string(),
)
})?;
let out = csv.random_nonce();
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_random_shared_secret(kind: u32) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
wrap_api_future_json(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_random_shared_secret",
"kind",
kind.to_string(),
)
})?;
let out = csv.random_shared_secret();
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_generate_key_pair(kind: u32) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
wrap_api_future_json(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_generate_key_pair",
"kind",
kind.to_string(),
)
})?;
let out = csv.generate_keypair();
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_generate_hash(kind: u32, data: String) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
let data: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(data.as_bytes())
.unwrap();
wrap_api_future_json(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_generate_hash",
"kind",
kind.to_string(),
)
})?;
let out = csv.generate_hash(&data);
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_validate_key_pair(kind: u32, key: String, secret: String) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
let key: veilid_core::PublicKey = veilid_core::deserialize_json(&key).unwrap();
let secret: veilid_core::SecretKey = veilid_core::deserialize_json(&secret).unwrap();
wrap_api_future_plain(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_validate_key_pair",
"kind",
kind.to_string(),
)
})?;
let out = csv.validate_keypair(&key, &secret);
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_validate_hash(kind: u32, data: String, hash: String) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
let data: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(data.as_bytes())
.unwrap();
let hash: veilid_core::HashDigest = veilid_core::deserialize_json(&hash).unwrap();
wrap_api_future_plain(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_validate_hash",
"kind",
kind.to_string(),
)
})?;
let out = csv.validate_hash(&data, &hash);
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_distance(kind: u32, key1: String, key2: String) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
let key1: veilid_core::CryptoKey = veilid_core::deserialize_json(&key1).unwrap();
let key2: veilid_core::CryptoKey = veilid_core::deserialize_json(&key2).unwrap();
wrap_api_future_json(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_distance",
"kind",
kind.to_string(),
)
})?;
let out = csv.distance(&key1, &key2);
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_sign(kind: u32, key: String, secret: String, data: String) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
let key: veilid_core::CryptoKey = veilid_core::deserialize_json(&key).unwrap();
let secret: veilid_core::CryptoKey = veilid_core::deserialize_json(&secret).unwrap();
let data: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(data.as_bytes())
.unwrap();
wrap_api_future_json(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument("crypto_sign", "kind", kind.to_string())
})?;
let out = csv.sign(&key, &secret, &data)?;
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_verify(kind: u32, key: String, data: String, signature: String) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
let key: veilid_core::CryptoKey = veilid_core::deserialize_json(&key).unwrap();
let data: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(data.as_bytes())
.unwrap();
let signature: veilid_core::Signature = veilid_core::deserialize_json(&signature).unwrap();
wrap_api_future_void(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument("crypto_verify", "kind", kind.to_string())
})?;
csv.verify(&key, &data, &signature)?;
APIRESULT_UNDEFINED
})
}
#[wasm_bindgen()]
pub fn crypto_aead_overhead(kind: u32) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
wrap_api_future_plain(async move {
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_aead_overhead",
"kind",
kind.to_string(),
)
})?;
let out = csv.aead_overhead();
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_decrypt_aead(
kind: u32,
body: String,
nonce: String,
shared_secret: String,
associated_data: Option<String>,
) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
let body: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(body.as_bytes())
.unwrap();
let nonce: veilid_core::Nonce = veilid_core::deserialize_json(&nonce).unwrap();
let shared_secret: veilid_core::SharedSecret =
veilid_core::deserialize_json(&shared_secret).unwrap();
let associated_data: Option<Vec<u8>> = associated_data.map(|ad| {
data_encoding::BASE64URL_NOPAD
.decode(ad.as_bytes())
.unwrap()
});
wrap_api_future_plain(async move {
2023-05-29 19:24:57 +00:00
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_decrypt_aead",
"kind",
kind.to_string(),
)
})?;
let out = csv.decrypt_aead(
&body,
&nonce,
&shared_secret,
match &associated_data {
Some(ad) => Some(ad.as_slice()),
None => None,
},
)?;
2023-07-25 05:04:22 +00:00
let out = data_encoding::BASE64URL_NOPAD.encode(&out);
2023-05-29 19:24:57 +00:00
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_encrypt_aead(
kind: u32,
body: String,
nonce: String,
shared_secret: String,
associated_data: Option<String>,
) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
let body: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(body.as_bytes())
.unwrap();
let nonce: veilid_core::Nonce = veilid_core::deserialize_json(&nonce).unwrap();
let shared_secret: veilid_core::SharedSecret =
veilid_core::deserialize_json(&shared_secret).unwrap();
let associated_data: Option<Vec<u8>> = associated_data.map(|ad| {
data_encoding::BASE64URL_NOPAD
.decode(ad.as_bytes())
.unwrap()
});
wrap_api_future_plain(async move {
2023-05-29 19:24:57 +00:00
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_encrypt_aead",
"kind",
kind.to_string(),
)
})?;
let out = csv.encrypt_aead(
&body,
&nonce,
&shared_secret,
match &associated_data {
Some(ad) => Some(ad.as_slice()),
None => None,
},
)?;
2023-07-25 05:04:22 +00:00
let out = data_encoding::BASE64URL_NOPAD.encode(&out);
2023-05-29 19:24:57 +00:00
APIResult::Ok(out)
})
}
#[wasm_bindgen()]
pub fn crypto_crypt_no_auth(
kind: u32,
body: String,
nonce: String,
shared_secret: String,
) -> Promise {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind);
let mut body: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(body.as_bytes())
.unwrap();
let nonce: veilid_core::Nonce = veilid_core::deserialize_json(&nonce).unwrap();
let shared_secret: veilid_core::SharedSecret =
veilid_core::deserialize_json(&shared_secret).unwrap();
wrap_api_future_plain(async move {
2023-05-29 19:24:57 +00:00
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let csv = crypto.get(kind).ok_or_else(|| {
veilid_core::VeilidAPIError::invalid_argument(
"crypto_crypt_no_auth",
"kind",
kind.to_string(),
)
})?;
csv.crypt_in_place_no_auth(&mut body, &nonce, &shared_secret);
let out = data_encoding::BASE64URL_NOPAD.encode(&body);
APIResult::Ok(out)
2023-05-29 19:24:57 +00:00
})
}
#[wasm_bindgen()]
pub fn now() -> u64 {
veilid_core::get_aligned_timestamp().as_u64()
}
2022-12-03 03:52:03 +00:00
#[wasm_bindgen()]
pub fn debug(command: String) -> Promise {
2023-01-04 19:20:41 +00:00
wrap_api_future_plain(async move {
2022-12-03 03:52:03 +00:00
let veilid_api = get_veilid_api()?;
let out = veilid_api.debug(command).await?;
2023-01-04 19:20:41 +00:00
APIResult::Ok(out)
2022-12-03 03:52:03 +00:00
})
}
2022-03-16 03:02:24 +00:00
#[wasm_bindgen()]
2022-03-15 13:33:34 +00:00
pub fn veilid_version_string() -> String {
veilid_core::veilid_version_string()
}
#[derive(Serialize)]
pub struct VeilidVersion {
pub major: u32,
pub minor: u32,
pub patch: u32,
}
2022-03-16 03:02:24 +00:00
#[wasm_bindgen()]
2022-03-15 13:33:34 +00:00
pub fn veilid_version() -> JsValue {
let (major, minor, patch) = veilid_core::veilid_version();
let vv = VeilidVersion {
major,
minor,
patch,
};
2022-10-05 23:12:10 +00:00
<JsValue as JsValueSerdeExt>::from_serde(&vv).unwrap()
2022-03-15 13:33:34 +00:00
}