mirror of
https://gitlab.com/veilid/veilid.git
synced 2025-01-13 08:31:55 -06:00
(wasm) Better TS types for serialized structs, RoutingContext++, more crypto fns
This commit is contained in:
parent
797e34f965
commit
c2c607efac
@ -1,7 +1,6 @@
|
||||
use super::*;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
#[cfg_attr(target_arch = "wasm32", derive(Tsify), tsify(into_wasm_abi))]
|
||||
pub struct CryptoTyped<K>
|
||||
where
|
||||
K: Clone
|
||||
|
@ -1,9 +1,7 @@
|
||||
use super::*;
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize, PartialOrd, Ord, PartialEq, Eq, Hash, Default)]
|
||||
#[cfg_attr(target_arch = "wasm32", derive(Tsify))]
|
||||
#[serde(from = "Vec<CryptoTyped<K>>", into = "Vec<CryptoTyped<K>>")]
|
||||
// TODO: figure out hot to TS type this as `string`, since it's converted to string via the JSON API.
|
||||
pub struct CryptoTypedGroup<K = PublicKey>
|
||||
where
|
||||
K: Clone
|
||||
|
@ -7,9 +7,7 @@ use super::*;
|
||||
tsify(from_wasm_abi, into_wasm_abi)
|
||||
)]
|
||||
pub struct KeyPair {
|
||||
#[cfg_attr(target_arch = "wasm32", tsify(type = "string"))]
|
||||
pub key: PublicKey,
|
||||
#[cfg_attr(target_arch = "wasm32", tsify(type = "string"))]
|
||||
pub secret: SecretKey,
|
||||
}
|
||||
from_impl_to_jsvalue!(KeyPair);
|
||||
|
@ -10,16 +10,14 @@ use super::*;
|
||||
pub struct DHTRecordDescriptor {
|
||||
/// DHT Key = Hash(ownerKeyKind) of: [ ownerKeyValue, schema ]
|
||||
#[schemars(with = "String")]
|
||||
#[cfg_attr(target_arch = "wasm32", tsify(type = "string"))]
|
||||
key: TypedKey,
|
||||
/// The public key of the owner
|
||||
#[schemars(with = "String")]
|
||||
#[cfg_attr(target_arch = "wasm32", tsify(type = "string"))]
|
||||
owner: PublicKey,
|
||||
/// If this key is being created: Some(the secret key of the owner)
|
||||
/// If this key is just being opened: None
|
||||
#[schemars(with = "Option<String>")]
|
||||
#[cfg_attr(target_arch = "wasm32", tsify(optional, type = "string"))]
|
||||
#[cfg_attr(target_arch = "wasm32", tsify(optional))]
|
||||
owner_secret: Option<SecretKey>,
|
||||
/// The schema in use associated with the key
|
||||
schema: DHTSchema,
|
||||
|
@ -6,7 +6,6 @@ use super::*;
|
||||
pub struct DHTSchemaSMPLMember {
|
||||
/// Member key
|
||||
#[schemars(with = "String")]
|
||||
#[cfg_attr(target_arch = "wasm32", tsify(type = "string"))]
|
||||
pub m_key: PublicKey,
|
||||
/// Member subkey count
|
||||
pub m_cnt: u16,
|
||||
|
@ -15,7 +15,6 @@ pub struct ValueData {
|
||||
|
||||
/// The public identity key of the writer of the data
|
||||
#[schemars(with = "String")]
|
||||
#[cfg_attr(target_arch = "wasm32", tsify(type = "string"))]
|
||||
writer: PublicKey,
|
||||
}
|
||||
from_impl_to_jsvalue!(ValueData);
|
||||
|
@ -4,7 +4,6 @@ use super::*;
|
||||
#[derive(
|
||||
Copy, Default, Clone, Hash, PartialOrd, Ord, PartialEq, Eq, Serialize, Deserialize, JsonSchema,
|
||||
)]
|
||||
#[cfg_attr(target_arch = "wasm32", derive(Tsify))]
|
||||
#[serde(try_from = "String")]
|
||||
#[serde(into = "String")]
|
||||
pub struct FourCC(pub [u8; 4]);
|
||||
|
@ -83,10 +83,8 @@ pub struct VeilidStateNetwork {
|
||||
#[cfg_attr(target_arch = "wasm32", derive(Tsify))]
|
||||
pub struct VeilidRouteChange {
|
||||
#[schemars(with = "Vec<String>")]
|
||||
#[cfg_attr(target_arch = "wasm32", tsify(type = "string"))]
|
||||
pub dead_routes: Vec<RouteId>,
|
||||
#[schemars(with = "Vec<String>")]
|
||||
#[cfg_attr(target_arch = "wasm32", tsify(type = "string"))]
|
||||
pub dead_remote_routes: Vec<RouteId>,
|
||||
}
|
||||
|
||||
@ -100,7 +98,6 @@ pub struct VeilidStateConfig {
|
||||
#[cfg_attr(target_arch = "wasm32", derive(Tsify))]
|
||||
pub struct VeilidValueChange {
|
||||
#[schemars(with = "String")]
|
||||
#[cfg_attr(target_arch = "wasm32", tsify(type = "string"))]
|
||||
pub key: TypedKey,
|
||||
pub subkeys: Vec<ValueSubkey>,
|
||||
pub count: u32,
|
||||
|
@ -246,11 +246,6 @@ pub fn change_log_level(layer: String, log_level: String) {
|
||||
}
|
||||
}
|
||||
|
||||
#[wasm_bindgen(typescript_custom_section)]
|
||||
const IUPDATE_VEILID_FUNCTION: &'static str = r#"
|
||||
type UpdateVeilidFunction = (event: VeilidUpdate) => void;
|
||||
"#;
|
||||
|
||||
#[wasm_bindgen()]
|
||||
pub fn startup_veilid_core(update_callback_js: Function, json_config: String) -> Promise {
|
||||
let update_callback_js = SendWrapper::new(update_callback_js);
|
||||
|
@ -3,7 +3,16 @@ use super::*;
|
||||
|
||||
#[wasm_bindgen(typescript_custom_section)]
|
||||
const IUPDATE_VEILID_FUNCTION: &'static str = r#"
|
||||
type UpdateVeilidFunction = (event: VeilidUpdate) => void;
|
||||
export type UpdateVeilidFunction = (event: VeilidUpdate) => void;
|
||||
|
||||
// Type overrides for structs that always get serialized by serde.
|
||||
export type CryptoKey = string;
|
||||
export type Nonce = string;
|
||||
export type Signature = string;
|
||||
export type KeyPair = `${PublicKey}:${SecretKey}`;
|
||||
export type FourCC = "NONE" | "VLD0" | string;
|
||||
export type CryptoTyped<TCryptoKey extends string> = `${FourCC}:${TCryptoKey}`;
|
||||
export type CryptoTypedGroup<TCryptoKey extends string> = Array<CryptoTyped<TCryptoKey>>;
|
||||
"#;
|
||||
|
||||
#[wasm_bindgen]
|
||||
|
@ -1,12 +1,6 @@
|
||||
#![allow(non_snake_case)]
|
||||
use super::*;
|
||||
|
||||
#[wasm_bindgen]
|
||||
extern "C" {
|
||||
#[wasm_bindgen(typescript_type = "string[]")]
|
||||
pub type ValidCryptoKinds;
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_name = veilidCrypto)]
|
||||
pub struct VeilidCrypto {}
|
||||
|
||||
@ -196,7 +190,59 @@ impl VeilidCrypto {
|
||||
APIResult::Ok(out.to_string())
|
||||
}
|
||||
|
||||
pub fn generateKeyPair(kind: String) -> APIResult<KeyPair> {
|
||||
pub fn verifySignatures(
|
||||
node_ids: StringArray,
|
||||
data: String,
|
||||
signatures: StringArray,
|
||||
) -> VeilidAPIResult<StringArray> {
|
||||
let node_ids = into_unchecked_string_vec(node_ids);
|
||||
let node_ids: Vec<veilid_core::TypedKey> = node_ids
|
||||
.iter()
|
||||
.map(|k| veilid_core::TypedKey::from_str(k).unwrap())
|
||||
.collect();
|
||||
|
||||
let data: Vec<u8> = data_encoding::BASE64URL_NOPAD
|
||||
.decode(data.as_bytes())
|
||||
.unwrap();
|
||||
|
||||
let typed_signatures = into_unchecked_string_vec(signatures);
|
||||
let typed_signatures: Vec<veilid_core::TypedSignature> = typed_signatures
|
||||
.iter()
|
||||
.map(|k| veilid_core::TypedSignature::from_str(k).unwrap())
|
||||
.collect();
|
||||
|
||||
let veilid_api = get_veilid_api()?;
|
||||
let crypto = veilid_api.crypto()?;
|
||||
let out = crypto.verify_signatures(&node_ids, &data, &typed_signatures)?;
|
||||
let out = out
|
||||
.iter()
|
||||
.map(|item| item.to_string())
|
||||
.collect::<Vec<String>>();
|
||||
let out = into_unchecked_string_array(out);
|
||||
APIResult::Ok(out)
|
||||
}
|
||||
|
||||
pub fn generateSignatures(data: String, key_pairs: StringArray) -> APIResult<StringArray> {
|
||||
let data: Vec<u8> = data_encoding::BASE64URL_NOPAD
|
||||
.decode(data.as_bytes())
|
||||
.unwrap();
|
||||
|
||||
let key_pairs = into_unchecked_string_vec(key_pairs);
|
||||
let key_pairs: Vec<veilid_core::TypedKeyPair> = key_pairs
|
||||
.iter()
|
||||
.map(|k| veilid_core::TypedKeyPair::from_str(k).unwrap())
|
||||
.collect();
|
||||
|
||||
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).to_string()
|
||||
})?;
|
||||
let out = into_unchecked_string_array(out);
|
||||
APIResult::Ok(out)
|
||||
}
|
||||
|
||||
pub fn generateKeyPair(kind: String) -> APIResult<String> {
|
||||
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
|
||||
|
||||
let veilid_api = get_veilid_api()?;
|
||||
@ -209,6 +255,7 @@ impl VeilidCrypto {
|
||||
)
|
||||
})?;
|
||||
let out = crypto_system.generate_keypair();
|
||||
let out = out.encode();
|
||||
APIResult::Ok(out)
|
||||
}
|
||||
|
||||
|
@ -3,70 +3,23 @@ use super::*;
|
||||
|
||||
#[wasm_bindgen()]
|
||||
pub struct VeilidRoutingContext {
|
||||
inner_routing_context: Option<RoutingContext>,
|
||||
inner_routing_context: RoutingContext,
|
||||
}
|
||||
|
||||
#[wasm_bindgen()]
|
||||
impl VeilidRoutingContext {
|
||||
/// Don't use this constructor directly.
|
||||
/// Use one of the `VeilidRoutingContext.create___()` factory methods instead.
|
||||
/// @deprecated
|
||||
/// Create a new VeilidRoutingContext, without any privacy or sequencing settings.
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
inner_routing_context: None,
|
||||
}
|
||||
}
|
||||
|
||||
// --------------------------------
|
||||
// Constructor factories
|
||||
// --------------------------------
|
||||
|
||||
/// Get a new RoutingContext object to use to send messages over the Veilid network.
|
||||
pub fn createWithoutPrivacy() -> APIResult<VeilidRoutingContext> {
|
||||
pub fn new() -> APIResult<VeilidRoutingContext> {
|
||||
let veilid_api = get_veilid_api()?;
|
||||
let routing_context = veilid_api.routing_context();
|
||||
Ok(VeilidRoutingContext {
|
||||
inner_routing_context: Some(routing_context),
|
||||
APIResult::Ok(VeilidRoutingContext {
|
||||
inner_routing_context: veilid_api.routing_context(),
|
||||
})
|
||||
}
|
||||
|
||||
/// Turn on sender privacy, enabling the use of safety routes.
|
||||
///
|
||||
/// Default values for hop count, stability and sequencing preferences are used.
|
||||
///
|
||||
/// Hop count default is dependent on config, but is set to 1 extra hop.
|
||||
/// Stability default is to choose 'low latency' routes, preferring them over long-term reliability.
|
||||
/// Sequencing default is to have no preference for ordered vs unordered message delivery
|
||||
/// To modify these defaults, use `VeilidRoutingContext.createWithCustomPrivacy()`.
|
||||
pub fn createWithPrivacy() -> APIResult<VeilidRoutingContext> {
|
||||
let veilid_api = get_veilid_api()?;
|
||||
let routing_context = veilid_api.routing_context().with_privacy()?;
|
||||
Ok(VeilidRoutingContext {
|
||||
inner_routing_context: Some(routing_context),
|
||||
})
|
||||
}
|
||||
|
||||
/// Turn on privacy using a custom `SafetySelection`
|
||||
pub fn createWithCustomPrivacy(
|
||||
safety_selection: SafetySelection,
|
||||
) -> APIResult<VeilidRoutingContext> {
|
||||
let veilid_api = get_veilid_api()?;
|
||||
let routing_context = veilid_api
|
||||
.routing_context()
|
||||
.with_custom_privacy(safety_selection)?;
|
||||
Ok(VeilidRoutingContext {
|
||||
inner_routing_context: Some(routing_context),
|
||||
})
|
||||
}
|
||||
|
||||
/// Use a specified `Sequencing` preference, with or without privacy.
|
||||
pub fn createWithSequencing(sequencing: Sequencing) -> APIResult<VeilidRoutingContext> {
|
||||
let veilid_api = get_veilid_api()?;
|
||||
let routing_context = veilid_api.routing_context().with_sequencing(sequencing);
|
||||
Ok(VeilidRoutingContext {
|
||||
inner_routing_context: Some(routing_context),
|
||||
})
|
||||
/// Same as `new VeilidRoutingContext()` except easier to chain.
|
||||
pub fn create() -> APIResult<VeilidRoutingContext> {
|
||||
VeilidRoutingContext::new()
|
||||
}
|
||||
|
||||
// --------------------------------
|
||||
@ -87,6 +40,18 @@ impl VeilidRoutingContext {
|
||||
APIResult::Ok(route_blob)
|
||||
}
|
||||
|
||||
/// Import a private route blob as a remote private route.
|
||||
///
|
||||
/// Returns a route id that can be used to send private messages to the node creating this route.
|
||||
pub fn importRemotePrivateRoute(&self, blob: String) -> APIResult<RouteId> {
|
||||
let blob: Vec<u8> = data_encoding::BASE64URL_NOPAD
|
||||
.decode(blob.as_bytes())
|
||||
.unwrap();
|
||||
let veilid_api = get_veilid_api()?;
|
||||
let route_id = veilid_api.import_remote_private_route(blob)?;
|
||||
APIResult::Ok(route_id)
|
||||
}
|
||||
|
||||
/// Allocate a new private route and specify a specific cryptosystem, stability and sequencing preference.
|
||||
/// Returns a route id and a publishable 'blob' with the route encrypted with each crypto kind.
|
||||
/// Those nodes importing the blob will have their choice of which crypto kind to use.
|
||||
@ -110,7 +75,7 @@ impl VeilidRoutingContext {
|
||||
///
|
||||
/// This will deactivate the route and free its resources and it can no longer be sent to or received from.
|
||||
pub fn releasePrivateRoute(route_id: String) -> APIResult<()> {
|
||||
let route_id: veilid_core::RouteId = veilid_core::deserialize_json(&route_id).unwrap();
|
||||
let route_id: veilid_core::RouteId = RouteId::from_str(&route_id)?;
|
||||
let veilid_api = get_veilid_api()?;
|
||||
veilid_api.release_private_route(route_id)?;
|
||||
APIRESULT_UNDEFINED
|
||||
@ -139,10 +104,43 @@ impl VeilidRoutingContext {
|
||||
// Instance methods
|
||||
// --------------------------------
|
||||
fn getRoutingContext(&self) -> APIResult<RoutingContext> {
|
||||
let Some(routing_context) = &self.inner_routing_context else {
|
||||
return APIResult::Err(veilid_core::VeilidAPIError::generic("Unable to getRoutingContext instance. inner_routing_context is None."));
|
||||
};
|
||||
APIResult::Ok(routing_context.clone())
|
||||
APIResult::Ok(self.inner_routing_context.clone())
|
||||
}
|
||||
|
||||
/// Turn on sender privacy, enabling the use of safety routes.
|
||||
/// Returns a new instance of VeilidRoutingContext - does not mutate.
|
||||
///
|
||||
/// Default values for hop count, stability and sequencing preferences are used.
|
||||
///
|
||||
/// Hop count default is dependent on config, but is set to 1 extra hop.
|
||||
/// Stability default is to choose 'low latency' routes, preferring them over long-term reliability.
|
||||
/// Sequencing default is to have no preference for ordered vs unordered message delivery
|
||||
pub fn withPrivacy(&self) -> APIResult<VeilidRoutingContext> {
|
||||
let routing_context = self.getRoutingContext()?;
|
||||
APIResult::Ok(VeilidRoutingContext {
|
||||
inner_routing_context: routing_context.with_privacy()?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Turn on privacy using a custom `SafetySelection`.
|
||||
/// Returns a new instance of VeilidRoutingContext - does not mutate.
|
||||
pub fn withCustomPrivacy(
|
||||
&self,
|
||||
safety_selection: SafetySelection,
|
||||
) -> APIResult<VeilidRoutingContext> {
|
||||
let routing_context = self.getRoutingContext()?;
|
||||
APIResult::Ok(VeilidRoutingContext {
|
||||
inner_routing_context: routing_context.with_custom_privacy(safety_selection)?,
|
||||
})
|
||||
}
|
||||
|
||||
/// Use a specified `Sequencing` preference.
|
||||
/// Returns a new instance of VeilidRoutingContext - does not mutate.
|
||||
pub fn withSequencing(&self, sequencing: Sequencing) -> APIResult<VeilidRoutingContext> {
|
||||
let routing_context = self.getRoutingContext()?;
|
||||
APIResult::Ok(VeilidRoutingContext {
|
||||
inner_routing_context: routing_context.with_sequencing(sequencing),
|
||||
})
|
||||
}
|
||||
|
||||
/// App-level unidirectional message that does not expect any value to be returned.
|
||||
|
@ -36,3 +36,13 @@ pub(crate) fn into_unchecked_string_array(items: Vec<String>) -> StringArray {
|
||||
.collect::<js_sys::Array>()
|
||||
.unchecked_into::<StringArray>() // TODO: can I do this a better way?
|
||||
}
|
||||
|
||||
/// Convert a StringArray (`js_sys::Array` with the type of `string[]`) into `Vec<String>`
|
||||
pub(crate) fn into_unchecked_string_vec(items: StringArray) -> Vec<String> {
|
||||
items
|
||||
.unchecked_into::<js_sys::Array>()
|
||||
.to_vec()
|
||||
.into_iter()
|
||||
.map(|i| serde_wasm_bindgen::from_value(i).unwrap())
|
||||
.collect::<Vec<String>>()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user