diff --git a/veilid-core/src/crypto/crypto_system.rs b/veilid-core/src/crypto/crypto_system.rs index 671ac28b..7b3b8d8b 100644 --- a/veilid-core/src/crypto/crypto_system.rs +++ b/veilid-core/src/crypto/crypto_system.rs @@ -26,11 +26,13 @@ pub trait CryptoSystem { domain: &[u8], ) -> VeilidAPIResult { let dh = self.compute_dh(key, secret)?; - Ok(self.generate_hash(&[&dh.bytes, domain, VEILID_DOMAIN_API].concat())) + Ok(SharedSecret::from(self.generate_hash( + &[&dh.bytes, domain, VEILID_DOMAIN_API].concat(), + ))) } fn generate_keypair(&self) -> KeyPair; fn generate_hash(&self, data: &[u8]) -> HashDigest; - fn generate_hash_reader(&self, reader: &mut dyn std::io::Read) -> VeilidAPIResult; + fn generate_hash_reader(&self, reader: &mut dyn std::io::Read) -> VeilidAPIResult; // Validation fn validate_keypair(&self, key: &PublicKey, secret: &SecretKey) -> bool; @@ -42,7 +44,7 @@ pub trait CryptoSystem { ) -> VeilidAPIResult; // Distance Metric - fn distance(&self, key1: &CryptoKey, key2: &CryptoKey) -> CryptoKeyDistance; + fn distance(&self, hash1: &HashDigest, hash2: &HashDigest) -> HashDistance; // Authentication fn sign(&self, key: &PublicKey, secret: &SecretKey, data: &[u8]) -> VeilidAPIResult; @@ -80,29 +82,24 @@ pub trait CryptoSystem { ) -> VeilidAPIResult>; // NoAuth Encrypt/Decrypt - fn crypt_in_place_no_auth( - &self, - body: &mut [u8], - nonce: &[u8; NONCE_LENGTH], - shared_secret: &SharedSecret, - ); + fn crypt_in_place_no_auth(&self, body: &mut [u8], nonce: &Nonce, shared_secret: &SharedSecret); fn crypt_b2b_no_auth( &self, in_buf: &[u8], out_buf: &mut [u8], - nonce: &[u8; NONCE_LENGTH], + nonce: &Nonce, shared_secret: &SharedSecret, ); fn crypt_no_auth_aligned_8( &self, body: &[u8], - nonce: &[u8; NONCE_LENGTH], + nonce: &Nonce, shared_secret: &SharedSecret, ) -> Vec; fn crypt_no_auth_unaligned( &self, body: &[u8], - nonce: &[u8; NONCE_LENGTH], + nonce: &Nonce, shared_secret: &SharedSecret, ) -> Vec; } diff --git a/veilid-core/src/crypto/envelope.rs b/veilid-core/src/crypto/envelope.rs index fcd04686..d557741d 100644 --- a/veilid-core/src/crypto/envelope.rs +++ b/veilid-core/src/crypto/envelope.rs @@ -211,11 +211,8 @@ impl Envelope { } } // Decrypt message without authentication - let body = vcrypto.crypt_no_auth_aligned_8( - &data[0x6A..data.len() - 64], - &self.nonce.bytes, - &dh_secret, - ); + let body = + vcrypto.crypt_no_auth_aligned_8(&data[0x6A..data.len() - 64], &self.nonce, &dh_secret); // Decompress body let body = decompress_size_prepended(&body, Some(MAX_ENVELOPE_SIZE))?; @@ -294,7 +291,7 @@ impl Envelope { } // Encrypt message - let encrypted_body = vcrypto.crypt_no_auth_unaligned(&body, &self.nonce.bytes, &dh_secret); + let encrypted_body = vcrypto.crypt_no_auth_unaligned(&body, &self.nonce, &dh_secret); // Write body if !encrypted_body.is_empty() { @@ -335,15 +332,15 @@ impl Envelope { self.sender_id } - pub fn get_sender_typed_id(&self) -> TypedKey { - TypedKey::new(self.crypto_kind, self.sender_id) + pub fn get_sender_typed_id(&self) -> TypedPublicKey { + TypedPublicKey::new(self.crypto_kind, self.sender_id) } pub fn get_recipient_id(&self) -> PublicKey { self.recipient_id } - pub fn get_recipient_typed_id(&self) -> TypedKey { - TypedKey::new(self.crypto_kind, self.recipient_id) + pub fn get_recipient_typed_id(&self) -> TypedPublicKey { + TypedPublicKey::new(self.crypto_kind, self.recipient_id) } } diff --git a/veilid-core/src/crypto/guard.rs b/veilid-core/src/crypto/guard.rs index 5b970a2f..7be57c03 100644 --- a/veilid-core/src/crypto/guard.rs +++ b/veilid-core/src/crypto/guard.rs @@ -103,9 +103,10 @@ impl AsyncCryptoSystemGuard<'_> { domain: &[u8], ) -> VeilidAPIResult { let dh = self.compute_dh(key, secret).await?; - Ok(self - .generate_hash(&[&dh.bytes, domain, VEILID_DOMAIN_API].concat()) - .await) + Ok(SharedSecret::from( + self.generate_hash(&[&dh.bytes, domain, VEILID_DOMAIN_API].concat()) + .await, + )) } pub async fn generate_keypair(&self) -> KeyPair { @@ -119,7 +120,7 @@ impl AsyncCryptoSystemGuard<'_> { pub async fn generate_hash_reader( &self, reader: &mut dyn std::io::Read, - ) -> VeilidAPIResult { + ) -> VeilidAPIResult { yielding(|| self.guard.generate_hash_reader(reader)).await } @@ -141,7 +142,7 @@ impl AsyncCryptoSystemGuard<'_> { } // Distance Metric - pub async fn distance(&self, key1: &CryptoKey, key2: &CryptoKey) -> CryptoKeyDistance { + pub async fn distance(&self, key1: &HashDigest, key2: &HashDigest) -> HashDistance { yielding(|| self.guard.distance(key1, key2)).await } @@ -229,7 +230,7 @@ impl AsyncCryptoSystemGuard<'_> { pub async fn crypt_in_place_no_auth( &self, body: &mut [u8], - nonce: &[u8; NONCE_LENGTH], + nonce: &Nonce, shared_secret: &SharedSecret, ) { yielding(|| { @@ -243,7 +244,7 @@ impl AsyncCryptoSystemGuard<'_> { &self, in_buf: &[u8], out_buf: &mut [u8], - nonce: &[u8; NONCE_LENGTH], + nonce: &Nonce, shared_secret: &SharedSecret, ) { yielding(|| { @@ -256,7 +257,7 @@ impl AsyncCryptoSystemGuard<'_> { pub async fn crypt_no_auth_aligned_8( &self, body: &[u8], - nonce: &[u8; NONCE_LENGTH], + nonce: &Nonce, shared_secret: &SharedSecret, ) -> Vec { yielding(|| { @@ -269,7 +270,7 @@ impl AsyncCryptoSystemGuard<'_> { pub async fn crypt_no_auth_unaligned( &self, body: &[u8], - nonce: &[u8; NONCE_LENGTH], + nonce: &Nonce, shared_secret: &SharedSecret, ) -> Vec { yielding(|| { diff --git a/veilid-core/src/crypto/mod.rs b/veilid-core/src/crypto/mod.rs index 207cae0d..b0ef4ac7 100644 --- a/veilid-core/src/crypto/mod.rs +++ b/veilid-core/src/crypto/mod.rs @@ -259,11 +259,11 @@ impl Crypto { /// Returns None if any cryptokinds are supported and do not validate pub fn verify_signatures( &self, - public_keys: &[TypedKey], + public_keys: &[TypedPublicKey], data: &[u8], typed_signatures: &[TypedSignature], - ) -> VeilidAPIResult> { - let mut out = TypedKeyGroup::with_capacity(public_keys.len()); + ) -> VeilidAPIResult> { + let mut out = TypedPublicKeyGroup::with_capacity(public_keys.len()); for sig in typed_signatures { for nid in public_keys { if nid.kind == sig.kind { @@ -352,7 +352,7 @@ impl Crypto { &self, vcrypto: AsyncCryptoSystemGuard<'_>, table_store: &TableStore, - ) -> VeilidAPIResult<(TypedKey, TypedSecret)> { + ) -> VeilidAPIResult<(TypedPublicKey, TypedSecretKey)> { let config = self.config(); let ck = vcrypto.kind(); let (mut node_id, mut node_id_secret) = config.with(|c| { @@ -371,7 +371,7 @@ impl Crypto { if node_id.is_none() { veilid_log!(self debug "pulling {} from storage", table_key_node_id); if let Ok(Some(stored_node_id)) = config_table - .load_json::(0, table_key_node_id.as_bytes()) + .load_json::(0, table_key_node_id.as_bytes()) .await { veilid_log!(self debug "{} found in storage", table_key_node_id); @@ -385,7 +385,7 @@ impl Crypto { if node_id_secret.is_none() { veilid_log!(self debug "pulling {} from storage", table_key_node_id_secret); if let Ok(Some(stored_node_id_secret)) = config_table - .load_json::(0, table_key_node_id_secret.as_bytes()) + .load_json::(0, table_key_node_id_secret.as_bytes()) .await { veilid_log!(self debug "{} found in storage", table_key_node_id_secret); @@ -413,7 +413,10 @@ impl Crypto { // If we still don't have a valid node id, generate one veilid_log!(self debug "generating new node_id_{}", ck); let kp = vcrypto.generate_keypair().await; - (TypedKey::new(ck, kp.key), TypedSecret::new(ck, kp.secret)) + ( + TypedPublicKey::new(ck, kp.key), + TypedSecretKey::new(ck, kp.secret), + ) }; veilid_log!(self info "Node Id: {}", node_id); @@ -432,8 +435,8 @@ impl Crypto { /// Must be done -after- protected store is initialized, during table store init #[cfg_attr(test, allow(unused_variables))] async fn setup_node_ids(&self, table_store: &TableStore) -> VeilidAPIResult<()> { - let mut out_node_id = TypedKeyGroup::new(); - let mut out_node_id_secret = TypedSecretGroup::new(); + let mut out_node_id = TypedPublicKeyGroup::new(); + let mut out_node_id_secret = TypedSecretKeyGroup::new(); for ck in VALID_CRYPTO_KINDS { let vcrypto = self @@ -443,7 +446,10 @@ impl Crypto { #[cfg(test)] let (node_id, node_id_secret) = { let kp = vcrypto.generate_keypair().await; - (TypedKey::new(ck, kp.key), TypedSecret::new(ck, kp.secret)) + ( + TypedPublicKey::new(ck, kp.key), + TypedSecretKey::new(ck, kp.secret), + ) }; #[cfg(not(test))] let (node_id, node_id_secret) = self.setup_node_id(vcrypto, table_store).await?; diff --git a/veilid-core/src/crypto/none/mod.rs b/veilid-core/src/crypto/none/mod.rs index 97f35d10..30e74d82 100644 --- a/veilid-core/src/crypto/none/mod.rs +++ b/veilid-core/src/crypto/none/mod.rs @@ -165,14 +165,14 @@ impl CryptoSystem for CryptoSystemNONE { Ok(bytes == dht_key.bytes) } // Distance Metric - fn distance(&self, key1: &PublicKey, key2: &PublicKey) -> CryptoKeyDistance { + fn distance(&self, key1: &PublicKey, key2: &PublicKey) -> HashDistance { let mut bytes = [0u8; PUBLIC_KEY_LENGTH]; for (n, byte) in bytes.iter_mut().enumerate() { *byte = key1.bytes[n] ^ key2.bytes[n]; } - CryptoKeyDistance::new(bytes) + HashDistance::new(bytes) } // Authentication diff --git a/veilid-core/src/crypto/receipt.rs b/veilid-core/src/crypto/receipt.rs index dda0ca34..10e00327 100644 --- a/veilid-core/src/crypto/receipt.rs +++ b/veilid-core/src/crypto/receipt.rs @@ -212,8 +212,8 @@ impl Receipt { self.sender_id } - pub fn get_sender_typed_id(&self) -> TypedKey { - TypedKey::new(self.crypto_kind, self.sender_id) + pub fn get_sender_typed_id(&self) -> TypedPublicKey { + TypedPublicKey::new(self.crypto_kind, self.sender_id) } #[must_use] diff --git a/veilid-core/src/crypto/tests/test_types.rs b/veilid-core/src/crypto/tests/test_types.rs index 004f585e..04cedb55 100644 --- a/veilid-core/src/crypto/tests/test_types.rs +++ b/veilid-core/src/crypto/tests/test_types.rs @@ -258,7 +258,7 @@ pub fn test_typed_convert(vcrypto: &AsyncCryptoSystemGuard<'_>) { "{}:7lxDEabK_qgjbe38RtBa3IZLrud84P6NhGP-pRTZzdQ", vcrypto.kind() ); - let tk1 = TypedKey::from_str(&tks1).expect("failed"); + let tk1 = TypedPublicKey::from_str(&tks1).expect("failed"); let tks1x = tk1.to_string(); assert_eq!(tks1, tks1x); @@ -266,27 +266,27 @@ pub fn test_typed_convert(vcrypto: &AsyncCryptoSystemGuard<'_>) { "{}:7lxDEabK_qgjbe38RtBa3IZLrud84P6NhGP-pRTZzd", vcrypto.kind() ); - let _tk2 = TypedKey::from_str(&tks2).expect_err("succeeded when it shouldnt have"); + let _tk2 = TypedPublicKey::from_str(&tks2).expect_err("succeeded when it shouldnt have"); let tks3 = "XXXX:7lxDEabK_qgjbe38RtBa3IZLrud84P6NhGP-pRTZzdQ".to_string(); - let tk3 = TypedKey::from_str(&tks3).expect("failed"); + let tk3 = TypedPublicKey::from_str(&tks3).expect("failed"); let tks3x = tk3.to_string(); assert_eq!(tks3, tks3x); let tks4 = "XXXX:7lxDEabK_qgjbe38RtBa3IZLrud84P6NhGP-pRTZzd".to_string(); - let _tk4 = TypedKey::from_str(&tks4).expect_err("succeeded when it shouldnt have"); + let _tk4 = TypedPublicKey::from_str(&tks4).expect_err("succeeded when it shouldnt have"); let tks5 = "XXX:7lxDEabK_qgjbe38RtBa3IZLrud84P6NhGP-pRTZzdQ".to_string(); - let _tk5 = TypedKey::from_str(&tks5).expect_err("succeeded when it shouldnt have"); + let _tk5 = TypedPublicKey::from_str(&tks5).expect_err("succeeded when it shouldnt have"); let tks6 = "7lxDEabK_qgjbe38RtBa3IZLrud84P6NhGP-pRTZzdQ".to_string(); - let tk6 = TypedKey::from_str(&tks6).expect("failed"); + let tk6 = TypedPublicKey::from_str(&tks6).expect("failed"); let tks6x = tk6.to_string(); assert!(tks6x.ends_with(&tks6)); } async fn test_hash(vcrypto: &AsyncCryptoSystemGuard<'_>) { - let mut s = BTreeSet::::new(); + let mut s = BTreeSet::::new(); let k1 = vcrypto.generate_hash("abc".as_bytes()).await; let k2 = vcrypto.generate_hash("abcd".as_bytes()).await; @@ -381,24 +381,24 @@ async fn test_operations(vcrypto: &AsyncCryptoSystemGuard<'_>) { assert_eq!(d4.first_nonzero_bit(), Some(0)); } -pub fn test_crypto_key_ordering() { - let k1 = CryptoKey::new([ +pub fn test_public_key_ordering() { + let k1 = PublicKey::new([ 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]); - let k2 = CryptoKey::new([ + let k2 = PublicKey::new([ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]); - let k3 = CryptoKey::new([ + let k3 = PublicKey::new([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, ]); - let k4 = CryptoKey::new([ + let k4 = PublicKey::new([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, ]); - let k5 = CryptoKey::new([ + let k5 = PublicKey::new([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]); @@ -413,7 +413,7 @@ pub async fn test_all() { let api = crypto_tests_startup().await; let crypto = api.crypto().unwrap(); - test_crypto_key_ordering(); + test_public_key_ordering(); // Test versions for v in VALID_CRYPTO_KINDS { diff --git a/veilid-core/src/crypto/types/byte_array_types.rs b/veilid-core/src/crypto/types/byte_array_types.rs index 241a2112..610e10bf 100644 --- a/veilid-core/src/crypto/types/byte_array_types.rs +++ b/veilid-core/src/crypto/types/byte_array_types.rs @@ -39,24 +39,24 @@ pub const NONCE_LENGTH: usize = 24; /// Length of a nonce in bytes after encoding to base64url #[allow(dead_code)] pub const NONCE_LENGTH_ENCODED: usize = 32; -/// Length of a shared secret in bytes -#[allow(dead_code)] -pub const SHARED_SECRET_LENGTH: usize = CRYPTO_KEY_LENGTH; -/// Length of a shared secret in bytes after encoding to base64url -#[allow(dead_code)] -pub const SHARED_SECRET_LENGTH_ENCODED: usize = CRYPTO_KEY_LENGTH_ENCODED; -/// Length of a route id in bytes -#[allow(dead_code)] -pub const ROUTE_ID_LENGTH: usize = CRYPTO_KEY_LENGTH; -/// Length of a route id in bytes after encoding to base64url -#[allow(dead_code)] -pub const ROUTE_ID_LENGTH_ENCODED: usize = CRYPTO_KEY_LENGTH_ENCODED; /// Length of a hash digest in bytes #[allow(dead_code)] pub const HASH_DIGEST_LENGTH: usize = CRYPTO_KEY_LENGTH; /// Length of a hash digest in bytes after encoding to base64url #[allow(dead_code)] pub const HASH_DIGEST_LENGTH_ENCODED: usize = CRYPTO_KEY_LENGTH_ENCODED; +/// Length of a shared secret in bytes +#[allow(dead_code)] +pub const SHARED_SECRET_LENGTH: usize = HASH_DIGEST_LENGTH; +/// Length of a shared secret in bytes after encoding to base64url +#[allow(dead_code)] +pub const SHARED_SECRET_LENGTH_ENCODED: usize = HASH_DIGEST_LENGTH_ENCODED; +/// Length of a route id in bytes +#[allow(dead_code)] +pub const ROUTE_ID_LENGTH: usize = HASH_DIGEST_LENGTH; +/// Length of a route id in bytes after encoding to base64url +#[allow(dead_code)] +pub const ROUTE_ID_LENGTH_ENCODED: usize = HASH_DIGEST_LENGTH_ENCODED; ////////////////////////////////////////////////////////////////////// @@ -292,20 +292,84 @@ macro_rules! byte_array_type { ///////////////////////////////////////// -byte_array_type!(CryptoKey, CRYPTO_KEY_LENGTH, CRYPTO_KEY_LENGTH_ENCODED); - -#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] -pub type PublicKey = CryptoKey; -#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] -pub type SecretKey = CryptoKey; -#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] -pub type HashDigest = CryptoKey; -#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] -pub type SharedSecret = CryptoKey; -#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] -pub type RouteId = CryptoKey; -#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] -pub type CryptoKeyDistance = CryptoKey; - +byte_array_type!(PublicKey, PUBLIC_KEY_LENGTH, PUBLIC_KEY_LENGTH_ENCODED); +byte_array_type!(SecretKey, SECRET_KEY_LENGTH, SECRET_KEY_LENGTH_ENCODED); byte_array_type!(Signature, SIGNATURE_LENGTH, SIGNATURE_LENGTH_ENCODED); byte_array_type!(Nonce, NONCE_LENGTH, NONCE_LENGTH_ENCODED); + +/* +Notes: + - These are actually HashDigest types, but not interchangable: + - RouteId (eventually will be a RecordKey type with DHT Routes) + - RecordKey + - SharedSecret +*/ + +// HashDigest sub-types +byte_array_type!(HashDigest, HASH_DIGEST_LENGTH, HASH_DIGEST_LENGTH_ENCODED); +byte_array_type!( + SharedSecret, + SHARED_SECRET_LENGTH, + SHARED_SECRET_LENGTH_ENCODED +); +byte_array_type!(RouteId, ROUTE_ID_LENGTH, ROUTE_ID_LENGTH_ENCODED); +byte_array_type!(RecordKey, HASH_DIGEST_LENGTH, HASH_DIGEST_LENGTH_ENCODED); +byte_array_type!(HashDistance, HASH_DIGEST_LENGTH, HASH_DIGEST_LENGTH_ENCODED); + +// NodeId is currently the same as PublicKey, but will eventually be a sub-type of HashDigest. +byte_array_type!(NodeId, PUBLIC_KEY_LENGTH, PUBLIC_KEY_LENGTH_ENCODED); + +// Temporary adapters for converting to/from HashDigest types +// Removing these will show where there's still issues. +impl From for SharedSecret { + fn from(value: HashDigest) -> Self { + Self::new(value.bytes) + } +} + +impl From for RecordKey { + fn from(value: HashDigest) -> Self { + Self::new(value.bytes) + } +} + +impl From for HashDigest { + fn from(value: RecordKey) -> Self { + Self::new(value.bytes) + } +} + +impl From for HashDigest { + fn from(value: NodeId) -> Self { + Self::new(value.bytes) + } +} + +// Removing this will show where PublicKey might need to be converted to NodeId or RecordKey. +impl From for HashDigest { + fn from(value: PublicKey) -> Self { + Self::new(value.bytes) + } +} + +impl From for PublicKey { + fn from(value: HashDigest) -> Self { + Self::new(value.bytes) + } +} + +/* +- NodeId currently equals PublicKey, but should be distinct from PublicKey. + - NodeId eventually should be a HashDigest type that's constructable from a PublicKey +*/ +impl From for NodeId { + fn from(value: PublicKey) -> Self { + Self::new(value.bytes) + } +} + +impl From for PublicKey { + fn from(value: NodeId) -> Self { + Self::new(value.bytes) + } +} diff --git a/veilid-core/src/crypto/types/mod.rs b/veilid-core/src/crypto/types/mod.rs index 19ab37d9..05d28161 100644 --- a/veilid-core/src/crypto/types/mod.rs +++ b/veilid-core/src/crypto/types/mod.rs @@ -55,23 +55,50 @@ pub use crypto_typed_group::*; pub use keypair::*; #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] -pub type TypedKey = CryptoTyped; +pub type TypedPublicKey = CryptoTyped; #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] -pub type TypedSecret = CryptoTyped; +pub type TypedSecretKey = CryptoTyped; #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] pub type TypedKeyPair = CryptoTyped; #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] pub type TypedSignature = CryptoTyped; #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] pub type TypedSharedSecret = CryptoTyped; +#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] +pub type TypedRouteId = CryptoTyped; +#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] +pub type TypedRecordKey = CryptoTyped; +#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] +pub type TypedNodeId = CryptoTyped; +#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] +pub type TypedHashDigest = CryptoTyped; #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] -pub type TypedKeyGroup = CryptoTypedGroup; +pub type TypedPublicKeyGroup = CryptoTypedGroup; #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] -pub type TypedSecretGroup = CryptoTypedGroup; +pub type TypedSecretKeyGroup = CryptoTypedGroup; #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] pub type TypedKeyPairGroup = CryptoTypedGroup; #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] pub type TypedSignatureGroup = CryptoTypedGroup; #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] pub type TypedSharedSecretGroup = CryptoTypedGroup; +#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] +pub type TypedRouteIdGroup = CryptoTypedGroup; +#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] +pub type TypedRecordKeyGroup = CryptoTypedGroup; +#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] +pub type TypedNodeIdGroup = CryptoTypedGroup; +#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), declare)] +pub type TypedHashDigestGroup = CryptoTypedGroup; + +impl From for TypedHashDigest { + fn from(value: TypedPublicKey) -> Self { + TypedHashDigest::new(value.kind, value.value.into()) + } +} +impl From for TypedHashDigest { + fn from(value: TypedRecordKey) -> Self { + TypedHashDigest::new(value.kind, value.value.into()) + } +} diff --git a/veilid-core/src/crypto/vld0/mod.rs b/veilid-core/src/crypto/vld0/mod.rs index c7080a8e..b9e4fed9 100644 --- a/veilid-core/src/crypto/vld0/mod.rs +++ b/veilid-core/src/crypto/vld0/mod.rs @@ -40,10 +40,10 @@ pub fn vld0_generate_keypair() -> KeyPair { let mut csprng = VeilidRng {}; let signing_key = ed::SigningKey::generate(&mut csprng); let verifying_key = signing_key.verifying_key(); - let dht_key = PublicKey::new(verifying_key.to_bytes()); - let dht_key_secret = SecretKey::new(signing_key.to_bytes()); + let public_key = PublicKey::new(verifying_key.to_bytes()); + let secret_key = SecretKey::new(signing_key.to_bytes()); - KeyPair::new(dht_key, dht_key_secret) + KeyPair::new(public_key, secret_key) } /// V0 CryptoSystem @@ -163,8 +163,8 @@ impl CryptoSystem for CryptoSystemVLD0 { } #[instrument(level = "trace", target = "crypto", skip_all)] - fn generate_hash(&self, data: &[u8]) -> PublicKey { - PublicKey::new(*blake3::hash(data).as_bytes()) + fn generate_hash(&self, data: &[u8]) -> HashDigest { + HashDigest::new(*blake3::hash(data).as_bytes()) } #[instrument(level = "trace", target = "crypto", skip_all)] @@ -176,61 +176,61 @@ impl CryptoSystem for CryptoSystemVLD0 { // Validation #[instrument(level = "trace", target = "crypto", skip_all)] - fn validate_keypair(&self, dht_key: &PublicKey, dht_key_secret: &SecretKey) -> bool { + fn validate_keypair(&self, public_key: &PublicKey, secret_key: &SecretKey) -> bool { let data = vec![0u8; 512]; - let Ok(sig) = self.sign(dht_key, dht_key_secret, &data) else { + let Ok(sig) = self.sign(public_key, secret_key, &data) else { return false; }; - let Ok(v) = self.verify(dht_key, &data, &sig) else { + let Ok(v) = self.verify(public_key, &data, &sig) else { return false; }; v } #[instrument(level = "trace", target = "crypto", skip_all)] - fn validate_hash(&self, data: &[u8], dht_key: &PublicKey) -> bool { + fn validate_hash(&self, data: &[u8], hash_digest: &HashDigest) -> bool { let bytes = *blake3::hash(data).as_bytes(); - bytes == dht_key.bytes + bytes == hash_digest.bytes } #[instrument(level = "trace", target = "crypto", skip_all)] fn validate_hash_reader( &self, reader: &mut dyn std::io::Read, - dht_key: &PublicKey, + hash_digest: &HashDigest, ) -> VeilidAPIResult { let mut hasher = blake3::Hasher::new(); std::io::copy(reader, &mut hasher).map_err(VeilidAPIError::generic)?; let bytes = *hasher.finalize().as_bytes(); - Ok(bytes == dht_key.bytes) + Ok(bytes == hash_digest.bytes) } // Distance Metric #[instrument(level = "trace", target = "crypto", skip_all)] - fn distance(&self, key1: &PublicKey, key2: &PublicKey) -> CryptoKeyDistance { + fn distance(&self, hash1: &HashDigest, hash2: &HashDigest) -> HashDistance { let mut bytes = [0u8; CRYPTO_KEY_LENGTH]; (0..CRYPTO_KEY_LENGTH).for_each(|n| { - bytes[n] = key1.bytes[n] ^ key2.bytes[n]; + bytes[n] = hash1.bytes[n] ^ hash2.bytes[n]; }); - CryptoKeyDistance::new(bytes) + HashDistance::new(bytes) } // Authentication #[instrument(level = "trace", target = "crypto", skip_all)] fn sign( &self, - dht_key: &PublicKey, - dht_key_secret: &SecretKey, + public_key: &PublicKey, + secret_key: &SecretKey, data: &[u8], ) -> VeilidAPIResult { let mut kpb: [u8; SECRET_KEY_LENGTH + PUBLIC_KEY_LENGTH] = [0u8; SECRET_KEY_LENGTH + PUBLIC_KEY_LENGTH]; - kpb[..SECRET_KEY_LENGTH].copy_from_slice(&dht_key_secret.bytes); - kpb[SECRET_KEY_LENGTH..].copy_from_slice(&dht_key.bytes); + kpb[..SECRET_KEY_LENGTH].copy_from_slice(&secret_key.bytes); + kpb[SECRET_KEY_LENGTH..].copy_from_slice(&public_key.bytes); let keypair = ed::SigningKey::from_keypair_bytes(&kpb) .map_err(|e| VeilidAPIError::parse_error("Keypair is invalid", e))?; @@ -243,7 +243,7 @@ impl CryptoSystem for CryptoSystemVLD0 { let sig = Signature::new(sig_bytes.to_bytes()); - if !self.verify(dht_key, data, &sig)? { + if !self.verify(public_key, data, &sig)? { apibail_internal!("newly created signature does not verify"); } @@ -252,11 +252,11 @@ impl CryptoSystem for CryptoSystemVLD0 { #[instrument(level = "trace", target = "crypto", skip_all)] fn verify( &self, - dht_key: &PublicKey, + public_key: &PublicKey, data: &[u8], signature: &Signature, ) -> VeilidAPIResult { - let pk = ed::VerifyingKey::from_bytes(&dht_key.bytes) + let pk = ed::VerifyingKey::from_bytes(&public_key.bytes) .map_err(|e| VeilidAPIError::parse_error("Public key is invalid", e))?; let sig = ed::Signature::from_bytes(&signature.bytes); @@ -342,13 +342,9 @@ impl CryptoSystem for CryptoSystemVLD0 { // NoAuth Encrypt/Decrypt #[instrument(level = "trace", target = "crypto", skip_all)] - fn crypt_in_place_no_auth( - &self, - body: &mut [u8], - nonce: &[u8; NONCE_LENGTH], - shared_secret: &SharedSecret, - ) { - let mut cipher = ::new(&shared_secret.bytes.into(), nonce.into()); + fn crypt_in_place_no_auth(&self, body: &mut [u8], nonce: &Nonce, shared_secret: &SharedSecret) { + let mut cipher = + ::new(&shared_secret.bytes.into(), &nonce.bytes.into()); cipher.apply_keystream(body); } @@ -357,10 +353,11 @@ impl CryptoSystem for CryptoSystemVLD0 { &self, in_buf: &[u8], out_buf: &mut [u8], - nonce: &[u8; NONCE_LENGTH], + nonce: &Nonce, shared_secret: &SharedSecret, ) { - let mut cipher = ::new(&shared_secret.bytes.into(), nonce.into()); + let mut cipher = + ::new(&shared_secret.bytes.into(), &nonce.bytes.into()); cipher.apply_keystream_b2b(in_buf, out_buf).unwrap(); } @@ -368,7 +365,7 @@ impl CryptoSystem for CryptoSystemVLD0 { fn crypt_no_auth_aligned_8( &self, in_buf: &[u8], - nonce: &[u8; NONCE_LENGTH], + nonce: &Nonce, shared_secret: &SharedSecret, ) -> Vec { let mut out_buf = unsafe { aligned_8_u8_vec_uninit(in_buf.len()) }; @@ -380,7 +377,7 @@ impl CryptoSystem for CryptoSystemVLD0 { fn crypt_no_auth_unaligned( &self, in_buf: &[u8], - nonce: &[u8; NONCE_LENGTH], + nonce: &Nonce, shared_secret: &SharedSecret, ) -> Vec { let mut out_buf = unsafe { unaligned_u8_vec_uninit(in_buf.len()) }; diff --git a/veilid-core/src/network_manager/address_filter.rs b/veilid-core/src/network_manager/address_filter.rs index b66f51ef..918d80c4 100644 --- a/veilid-core/src/network_manager/address_filter.rs +++ b/veilid-core/src/network_manager/address_filter.rs @@ -30,7 +30,7 @@ struct AddressFilterInner { conn_timestamps_by_ip6_prefix: BTreeMap>, punishments_by_ip4: BTreeMap, punishments_by_ip6_prefix: BTreeMap, - punishments_by_node_id: BTreeMap, + punishments_by_node_id: BTreeMap, dial_info_failures: BTreeMap, } @@ -178,7 +178,7 @@ impl AddressFilter { } // node id { - let mut dead_keys = Vec::::new(); + let mut dead_keys = Vec::::new(); for (key, value) in &mut inner.punishments_by_node_id { // Drop punishments older than the punishment duration if cur_ts.as_u64().saturating_sub(value.timestamp.as_u64()) @@ -297,19 +297,23 @@ impl AddressFilter { }; } - fn is_node_id_punished_inner(&self, inner: &AddressFilterInner, node_id: TypedKey) -> bool { + fn is_node_id_punished_inner( + &self, + inner: &AddressFilterInner, + node_id: TypedPublicKey, + ) -> bool { if inner.punishments_by_node_id.contains_key(&node_id) { return true; } false } - pub fn is_node_id_punished(&self, node_id: TypedKey) -> bool { + pub fn is_node_id_punished(&self, node_id: TypedPublicKey) -> bool { let inner = self.inner.lock(); self.is_node_id_punished_inner(&inner, node_id) } - pub fn punish_node_id(&self, node_id: TypedKey, reason: PunishmentReason) { + pub fn punish_node_id(&self, node_id: TypedPublicKey, reason: PunishmentReason) { if let Ok(Some(nr)) = self.routing_table().lookup_node_ref(node_id) { // make the entry dead if it's punished nr.operate_mut(|_rti, e| e.set_punished(Some(reason))); diff --git a/veilid-core/src/network_manager/bootstrap/bootstrap_record.rs b/veilid-core/src/network_manager/bootstrap/bootstrap_record.rs index c255cfe3..861fc7c9 100644 --- a/veilid-core/src/network_manager/bootstrap/bootstrap_record.rs +++ b/veilid-core/src/network_manager/bootstrap/bootstrap_record.rs @@ -4,7 +4,7 @@ impl_veilid_log_facility!("net"); #[derive(Clone, Debug, PartialEq, Eq)] pub struct BootstrapRecord { - node_ids: TypedKeyGroup, + node_ids: TypedPublicKeyGroup, envelope_support: Vec, dial_info_details: Vec, timestamp_secs: Option, @@ -13,7 +13,7 @@ pub struct BootstrapRecord { impl BootstrapRecord { pub fn new( - node_ids: TypedKeyGroup, + node_ids: TypedPublicKeyGroup, mut envelope_support: Vec, mut dial_info_details: Vec, timestamp_secs: Option, @@ -31,7 +31,7 @@ impl BootstrapRecord { } } - pub fn node_ids(&self) -> &TypedKeyGroup { + pub fn node_ids(&self) -> &TypedPublicKeyGroup { &self.node_ids } pub fn envelope_support(&self) -> &[u8] { @@ -226,7 +226,7 @@ impl BootstrapRecord { network_manager: &NetworkManager, dial_info_converter: &dyn DialInfoConverter, record_str: &str, - signing_keys: &[TypedKey], + signing_keys: &[TypedPublicKey], ) -> EyreResult> { // All formats split on '|' character let fields: Vec = record_str @@ -309,10 +309,10 @@ impl BootstrapRecord { envelope_support.dedup(); // Node Id - let mut node_ids = TypedKeyGroup::new(); + let mut node_ids = TypedPublicKeyGroup::new(); for node_id_str in fields[2].split(',') { let node_id_str = node_id_str.trim(); - let node_id = match TypedKey::from_str(node_id_str) { + let node_id = match TypedPublicKey::from_str(node_id_str) { Ok(v) => v, Err(e) => { bail!( @@ -376,7 +376,7 @@ impl BootstrapRecord { dial_info_converter: &dyn DialInfoConverter, record_str: &str, fields: &[String], - signing_keys: &[TypedKey], + signing_keys: &[TypedPublicKey], ) -> EyreResult> { if fields.len() < 7 { bail!("invalid number of fields in bootstrap v1 txt record"); @@ -432,10 +432,10 @@ impl BootstrapRecord { envelope_support.dedup(); // Node Id - let mut node_ids = TypedKeyGroup::new(); + let mut node_ids = TypedPublicKeyGroup::new(); for node_id_str in fields[2].split(',') { let node_id_str = node_id_str.trim(); - let node_id = match TypedKey::from_str(node_id_str) { + let node_id = match TypedPublicKey::from_str(node_id_str) { Ok(v) => v, Err(e) => { bail!( diff --git a/veilid-core/src/network_manager/mod.rs b/veilid-core/src/network_manager/mod.rs index c96a27ea..d992a372 100644 --- a/veilid-core/src/network_manager/mod.rs +++ b/veilid-core/src/network_manager/mod.rs @@ -193,7 +193,7 @@ impl Default for NetworkManagerStartupContext { #[derive(Debug)] struct NetworkManagerInner { stats: NetworkManagerStats, - client_allowlist: LruCache, + client_allowlist: LruCache, node_contact_method_cache: NodeContactMethodCache, address_check: Option, peer_info_change_subscription: Option, @@ -531,7 +531,7 @@ impl NetworkManager { } #[expect(dead_code)] - pub fn update_client_allowlist(&self, client: TypedKey) { + pub fn update_client_allowlist(&self, client: TypedPublicKey) { let mut inner = self.inner.lock(); match inner.client_allowlist.entry(client) { hashlink::lru_cache::Entry::Occupied(mut entry) => { @@ -546,7 +546,7 @@ impl NetworkManager { } #[instrument(level = "trace", skip(self), ret)] - pub fn check_client_allowlist(&self, client: TypedKey) -> bool { + pub fn check_client_allowlist(&self, client: TypedPublicKey) -> bool { let mut inner = self.inner.lock(); match inner.client_allowlist.entry(client) { @@ -874,7 +874,7 @@ impl NetworkManager { #[instrument(level = "trace", target = "net", skip_all)] fn build_envelope>( &self, - dest_node_id: TypedKey, + dest_node_id: TypedPublicKey, version: u8, body: B, ) -> EyreResult> { diff --git a/veilid-core/src/network_manager/node_contact_method_cache.rs b/veilid-core/src/network_manager/node_contact_method_cache.rs index 7017587a..bbb7282a 100644 --- a/veilid-core/src/network_manager/node_contact_method_cache.rs +++ b/veilid-core/src/network_manager/node_contact_method_cache.rs @@ -4,7 +4,7 @@ pub const NODE_CONTACT_METHOD_CACHE_SIZE: usize = 1024; #[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)] pub struct NodeContactMethodCacheKey { - pub node_ids: TypedKeyGroup, + pub node_ids: TypedPublicKeyGroup, pub own_node_info_ts: Timestamp, pub target_node_info_ts: Timestamp, pub target_node_ref_filter: NodeRefFilter, diff --git a/veilid-core/src/network_manager/tests/test_bootstrap.rs b/veilid-core/src/network_manager/tests/test_bootstrap.rs index a7858027..372725fa 100644 --- a/veilid-core/src/network_manager/tests/test_bootstrap.rs +++ b/veilid-core/src/network_manager/tests/test_bootstrap.rs @@ -4,7 +4,7 @@ use crate::tests::mock_registry; fn make_mock_bootstrap_record(include_timestamp: bool) -> BootstrapRecord { let mut node_ids = CryptoTypedGroup::new(); node_ids.add( - TypedKey::from_str("VLD0:f8G4Ckr1UR8YXnmAllwfvBEvXGgfYicZllb7jEpJeSU") + TypedPublicKey::from_str("VLD0:f8G4Ckr1UR8YXnmAllwfvBEvXGgfYicZllb7jEpJeSU") .expect("should parse key"), ); let envelope_support = VALID_ENVELOPE_VERSIONS.to_vec(); @@ -68,7 +68,7 @@ pub async fn test_bootstrap_v1() { TypedKeyPair::from_str("VLD0:v6XPfyOoCP_ZP-CWFNrf_pF_dpxsq74p2LW_Q5Q4yPQ:n-DhHtOU7KWQkdp5to8cpBa_u0RFt2IDZzXPqMTq4O0").expect("should parse keypair")]; let signing_keys = signing_key_pairs .iter() - .map(|skp| TypedKey::new(skp.kind, skp.value.key)) + .map(|skp| TypedPublicKey::new(skp.kind, skp.value.key)) .collect::>(); let v1str = bsrec .to_v1_string(&network_manager, &dial_info_converter, signing_key_pairs[0]) diff --git a/veilid-core/src/network_manager/tests/test_signed_node_info.rs b/veilid-core/src/network_manager/tests/test_signed_node_info.rs index 43ad1081..0f36db16 100644 --- a/veilid-core/src/network_manager/tests/test_signed_node_info.rs +++ b/veilid-core/src/network_manager/tests/test_signed_node_info.rs @@ -35,7 +35,7 @@ pub async fn test_signed_node_info() { node_info.clone(), ) .unwrap(); - let tks: TypedKeyGroup = TypedKey::new(ck, keypair.key).into(); + let tks: TypedPublicKeyGroup = TypedPublicKey::new(ck, keypair.key).into(); let oldtkslen = tks.len(); let sdni = SignedDirectNodeInfo::new( node_info.clone(), @@ -48,7 +48,7 @@ pub async fn test_signed_node_info() { // Test incorrect validation let keypair1 = vcrypto.generate_keypair(); - let tks1: TypedKeyGroup = TypedKey::new(ck, keypair1.key).into(); + let tks1: TypedPublicKeyGroup = TypedPublicKey::new(ck, keypair1.key).into(); let sdni = SignedDirectNodeInfo::new( node_info.clone(), sni.timestamp(), @@ -58,11 +58,11 @@ pub async fn test_signed_node_info() { // Test unsupported cryptosystem validation let fake_crypto_kind: CryptoKind = FourCC::from([0, 1, 2, 3]); - let mut tksfake: TypedKeyGroup = - TypedKey::new(fake_crypto_kind, PublicKey::default()).into(); + let mut tksfake: TypedPublicKeyGroup = + TypedPublicKey::new(fake_crypto_kind, PublicKey::default()).into(); let mut sigsfake = sni.signatures().to_vec(); sigsfake.push(TypedSignature::new(fake_crypto_kind, Signature::default())); - tksfake.add(TypedKey::new(ck, keypair.key)); + tksfake.add(TypedPublicKey::new(ck, keypair.key)); let sdnifake = SignedDirectNodeInfo::new(node_info.clone(), sni.timestamp(), sigsfake.clone()); let tksfake_validated = sdnifake.validate(&tksfake, &crypto).unwrap(); @@ -85,7 +85,7 @@ pub async fn test_signed_node_info() { // Test correct validation let keypair2 = vcrypto.generate_keypair(); - let tks2: TypedKeyGroup = TypedKey::new(ck, keypair2.key).into(); + let tks2: TypedPublicKeyGroup = TypedPublicKey::new(ck, keypair2.key).into(); let oldtks2len = tks2.len(); let sni2 = SignedRelayedNodeInfo::make_signatures( @@ -110,7 +110,7 @@ pub async fn test_signed_node_info() { // Test incorrect validation let keypair3 = vcrypto.generate_keypair(); - let tks3: TypedKeyGroup = TypedKey::new(ck, keypair3.key).into(); + let tks3: TypedPublicKeyGroup = TypedPublicKey::new(ck, keypair3.key).into(); let srni = SignedRelayedNodeInfo::new( node_info2.clone(), @@ -123,11 +123,11 @@ pub async fn test_signed_node_info() { // Test unsupported cryptosystem validation let fake_crypto_kind: CryptoKind = FourCC::from([0, 1, 2, 3]); - let mut tksfake3: TypedKeyGroup = - TypedKey::new(fake_crypto_kind, PublicKey::default()).into(); + let mut tksfake3: TypedPublicKeyGroup = + TypedPublicKey::new(fake_crypto_kind, PublicKey::default()).into(); let mut sigsfake3 = sni2.signatures().to_vec(); sigsfake3.push(TypedSignature::new(fake_crypto_kind, Signature::default())); - tksfake3.add(TypedKey::new(ck, keypair2.key)); + tksfake3.add(TypedPublicKey::new(ck, keypair2.key)); let srnifake = SignedRelayedNodeInfo::new( node_info2.clone(), tks.clone(), diff --git a/veilid-core/src/routing_table/bucket.rs b/veilid-core/src/routing_table/bucket.rs index 49de6b91..121577f2 100644 --- a/veilid-core/src/routing_table/bucket.rs +++ b/veilid-core/src/routing_table/bucket.rs @@ -83,7 +83,10 @@ impl Bucket { veilid_log!(self trace "Node added: {}:{}", self.kind, node_id_key); // Add new entry - let entry = Arc::new(BucketEntry::new(TypedKey::new(self.kind, node_id_key))); + let entry = Arc::new(BucketEntry::new(TypedPublicKey::new( + self.kind, + node_id_key, + ))); self.entries.insert(node_id_key, entry.clone()); // Return the new entry diff --git a/veilid-core/src/routing_table/bucket_entry.rs b/veilid-core/src/routing_table/bucket_entry.rs index 22ed6b25..f0d1ba62 100644 --- a/veilid-core/src/routing_table/bucket_entry.rs +++ b/veilid-core/src/routing_table/bucket_entry.rs @@ -163,9 +163,9 @@ impl fmt::Display for BucketEntryLocalNetwork { #[derive(Debug, Serialize, Deserialize)] pub(crate) struct BucketEntryInner { /// The node ids matching this bucket entry, with the cryptography versions supported by this node as the 'kind' field - validated_node_ids: TypedKeyGroup, + validated_node_ids: TypedPublicKeyGroup, /// The node ids claimed by the remote node that use cryptography versions we do not support - unsupported_node_ids: TypedKeyGroup, + unsupported_node_ids: TypedPublicKeyGroup, /// The set of envelope versions supported by the node inclusive of the requirements of any relay the node may be using envelope_support: Vec, /// If this node has updated it's SignedNodeInfo since our network @@ -283,7 +283,7 @@ impl BucketEntryInner { } /// Get all node ids - pub fn node_ids(&self) -> TypedKeyGroup { + pub fn node_ids(&self) -> TypedPublicKeyGroup { let mut node_ids = self.validated_node_ids.clone(); node_ids.add_all(&self.unsupported_node_ids); node_ids @@ -293,7 +293,7 @@ impl BucketEntryInner { /// Returns Ok(Some(node)) any previous existing node id associated with that crypto kind /// Returns Ok(None) if no previous existing node id was associated with that crypto kind, or one existed but nothing changed. /// Results Err() if this operation would add more crypto kinds than we support - pub fn add_node_id(&mut self, node_id: TypedKey) -> EyreResult> { + pub fn add_node_id(&mut self, node_id: TypedPublicKey) -> EyreResult> { let total_node_id_count = self.validated_node_ids.len() + self.unsupported_node_ids.len(); let node_ids = if VALID_CRYPTO_KINDS.contains(&node_id.kind) { &mut self.validated_node_ids @@ -331,7 +331,7 @@ impl BucketEntryInner { /// Remove a node id for a particular crypto kind. /// Returns Some(node) any previous existing node id associated with that crypto kind /// Returns None if no previous existing node id was associated with that crypto kind - pub fn remove_node_id(&mut self, crypto_kind: CryptoKind) -> Option { + pub fn remove_node_id(&mut self, crypto_kind: CryptoKind) -> Option { let node_ids = if VALID_CRYPTO_KINDS.contains(&crypto_kind) { &mut self.validated_node_ids } else { @@ -348,7 +348,7 @@ impl BucketEntryInner { opt_dead_id } - pub fn best_node_id(&self) -> TypedKey { + pub fn best_node_id(&self) -> TypedPublicKey { self.validated_node_ids.best().unwrap() } @@ -1217,14 +1217,14 @@ pub(crate) struct BucketEntry { } impl BucketEntry { - pub(super) fn new(first_node_id: TypedKey) -> Self { + pub(super) fn new(first_node_id: TypedPublicKey) -> Self { // First node id should always be one we support since TypedKeySets are sorted and we must have at least one supported key assert!(VALID_CRYPTO_KINDS.contains(&first_node_id.kind)); let now = Timestamp::now(); let inner = BucketEntryInner { - validated_node_ids: TypedKeyGroup::from(first_node_id), - unsupported_node_ids: TypedKeyGroup::new(), + validated_node_ids: TypedPublicKeyGroup::from(first_node_id), + unsupported_node_ids: TypedPublicKeyGroup::new(), envelope_support: Vec::new(), updated_since_last_network_change: false, last_flows: BTreeMap::new(), diff --git a/veilid-core/src/routing_table/debug.rs b/veilid-core/src/routing_table/debug.rs index a0b10dfa..15c24614 100644 --- a/veilid-core/src/routing_table/debug.rs +++ b/veilid-core/src/routing_table/debug.rs @@ -87,7 +87,7 @@ impl RoutingTable { fn format_entry( cur_ts: Timestamp, - node: TypedKey, + node: TypedPublicKey, e: &BucketEntryInner, relay_tag: &str, ) -> String { @@ -245,7 +245,12 @@ impl RoutingTable { out += " "; out += &e.1.with(inner, |_rti, e| { - Self::format_entry(cur_ts, TypedKey::new(*ck, node), e, &relay_tag) + Self::format_entry( + cur_ts, + TypedPublicKey::new(*ck, node), + e, + &relay_tag, + ) }); out += "\n"; } diff --git a/veilid-core/src/routing_table/find_peers.rs b/veilid-core/src/routing_table/find_peers.rs index 8840bd90..9a124cb7 100644 --- a/veilid-core/src/routing_table/find_peers.rs +++ b/veilid-core/src/routing_table/find_peers.rs @@ -8,7 +8,7 @@ impl RoutingTable { pub fn find_preferred_closest_peers( &self, routing_domain: RoutingDomain, - key: TypedKey, + key: TypedPublicKey, capabilities: &[Capability], ) -> NetworkResult>> { if Crypto::validate_crypto_kind(key.kind).is_err() { @@ -48,7 +48,7 @@ impl RoutingTable { let closest_nodes = match self.find_preferred_closest_nodes( node_count, - key, + key.into(), filters, // transform |rti, entry| { @@ -72,7 +72,7 @@ impl RoutingTable { pub fn find_preferred_peers_closer_to_key( &self, routing_domain: RoutingDomain, - key: TypedKey, + key: TypedRecordKey, required_capabilities: Vec, ) -> NetworkResult>> { // add node information for the requesting node to our routing table @@ -87,7 +87,10 @@ impl RoutingTable { }; let vcrypto = &vcrypto; - let own_distance = vcrypto.distance(&own_node_id.value, &key.value); + let own_distance = vcrypto.distance( + &HashDigest::from(own_node_id.value), + &HashDigest::from(key.value), + ); let filter = Box::new( move |rti: &RoutingTableInner, opt_entry: Option>| { @@ -112,7 +115,10 @@ impl RoutingTable { let Some(entry_node_id) = e.node_ids().get(crypto_kind) else { return false; }; - let entry_distance = vcrypto.distance(&entry_node_id.value, &key.value); + let entry_distance = vcrypto.distance( + &HashDigest::from(entry_node_id.value), + &HashDigest::from(key.value), + ); if entry_distance >= own_distance { return false; } @@ -129,7 +135,7 @@ impl RoutingTable { // let closest_nodes = match self.find_preferred_closest_nodes( node_count, - key, + key.into(), filters, // transform |rti, entry| { @@ -147,7 +153,12 @@ impl RoutingTable { // Validate peers returned are, in fact, closer to the key than the node we sent this to // This same test is used on the other side so we vet things here - let valid = match Self::verify_peers_closer(vcrypto, own_node_id, key, &closest_nodes) { + let valid = match Self::verify_peers_closer( + vcrypto, + own_node_id.into(), + key.into(), + &closest_nodes, + ) { Ok(v) => v, Err(e) => { panic!("missing cryptosystem in peers node ids: {}", e); @@ -167,8 +178,8 @@ impl RoutingTable { #[instrument(level = "trace", target = "rtab", skip_all, err)] pub fn verify_peers_closer( vcrypto: &crypto::CryptoSystemGuard<'_>, - key_far: TypedKey, - key_near: TypedKey, + key_far: TypedHashDigest, + key_near: TypedHashDigest, peers: &[Arc], ) -> EyreResult { let kind = vcrypto.kind(); @@ -183,7 +194,7 @@ impl RoutingTable { let Some(key_peer) = peer.node_ids().get(kind) else { bail!("peers need to have a key with the same cryptosystem"); }; - let d_near = vcrypto.distance(&key_near.value, &key_peer.value); + let d_near = vcrypto.distance(&key_near.value, &key_peer.value.into()); if d_far < d_near { let warning = format!( r#"peer: {} diff --git a/veilid-core/src/routing_table/mod.rs b/veilid-core/src/routing_table/mod.rs index adec22b4..0a87f21f 100644 --- a/veilid-core/src/routing_table/mod.rs +++ b/veilid-core/src/routing_table/mod.rs @@ -304,7 +304,7 @@ impl RoutingTable { /////////////////////////////////////////////////////////////////// - pub fn node_id(&self, kind: CryptoKind) -> TypedKey { + pub fn node_id(&self, kind: CryptoKind) -> TypedPublicKey { self.config() .with(|c| c.network.routing_table.node_id.get(kind).unwrap()) } @@ -315,7 +315,7 @@ impl RoutingTable { .value } - pub fn node_ids(&self) -> TypedKeyGroup { + pub fn node_ids(&self) -> TypedPublicKeyGroup { self.config() .with(|c| c.network.routing_table.node_id.clone()) } @@ -331,7 +331,7 @@ impl RoutingTable { tkps } - pub fn matches_own_node_id(&self, node_ids: &[TypedKey]) -> bool { + pub fn matches_own_node_id(&self, node_ids: &[TypedPublicKey]) -> bool { for ni in node_ids { if let Some(v) = self.node_ids().get(ni.kind) { if v.value == ni.value { @@ -351,14 +351,17 @@ impl RoutingTable { false } - pub fn calculate_bucket_index(&self, node_id: &TypedKey) -> BucketIndex { + pub fn calculate_bucket_index(&self, node_id: &TypedPublicKey) -> BucketIndex { let crypto = self.crypto(); let self_node_id_key = self.node_id(node_id.kind).value; let vcrypto = crypto.get(node_id.kind).unwrap(); ( node_id.kind, vcrypto - .distance(&node_id.value, &self_node_id_key) + .distance( + &HashDigest::from(node_id.value), + &HashDigest::from(self_node_id_key), + ) .first_nonzero_bit() .unwrap(), ) @@ -659,7 +662,7 @@ impl RoutingTable { .get_nodes_needing_ping(routing_domain, cur_ts) } - fn queue_bucket_kicks(&self, node_ids: TypedKeyGroup) { + fn queue_bucket_kicks(&self, node_ids: TypedPublicKeyGroup) { for node_id in node_ids.iter() { // Skip node ids we didn't add to buckets if !VALID_CRYPTO_KINDS.contains(&node_id.kind) { @@ -678,7 +681,7 @@ impl RoutingTable { } /// Resolve an existing routing table entry and return a reference to it - pub fn lookup_node_ref(&self, node_id: TypedKey) -> EyreResult> { + pub fn lookup_node_ref(&self, node_id: TypedPublicKey) -> EyreResult> { self.inner.read().lookup_node_ref(node_id) } @@ -686,7 +689,7 @@ impl RoutingTable { #[instrument(level = "trace", skip_all)] pub fn lookup_and_filter_noderef( &self, - node_id: TypedKey, + node_id: TypedPublicKey, routing_domain_set: RoutingDomainSet, dial_info_filter: DialInfoFilter, ) -> EyreResult> { @@ -716,7 +719,7 @@ impl RoutingTable { pub fn register_node_with_id( &self, routing_domain: RoutingDomain, - node_id: TypedKey, + node_id: TypedPublicKey, timestamp: Timestamp, ) -> EyreResult { self.inner @@ -736,7 +739,7 @@ impl RoutingTable { } #[instrument(level = "trace", skip_all)] - pub fn get_recent_peers(&self) -> Vec<(TypedKey, RecentPeersEntry)> { + pub fn get_recent_peers(&self) -> Vec<(TypedPublicKey, RecentPeersEntry)> { let mut recent_peers = Vec::new(); let mut dead_peers = Vec::new(); let mut out = Vec::new(); @@ -908,7 +911,7 @@ impl RoutingTable { pub fn find_preferred_closest_nodes<'a, T, O>( &self, node_count: usize, - node_id: TypedKey, + node_id: TypedHashDigest, filters: VecDeque, transform: T, ) -> VeilidAPIResult> @@ -922,7 +925,7 @@ impl RoutingTable { pub fn sort_and_clean_closest_noderefs( &self, - node_id: TypedKey, + node_id: TypedHashDigest, closest_nodes: &[NodeRef], ) -> Vec { self.inner @@ -960,7 +963,7 @@ impl RoutingTable { pub async fn find_nodes_close_to_node_id( &self, node_ref: FilteredNodeRef, - node_id: TypedKey, + node_id: TypedPublicKey, capabilities: Vec, ) -> EyreResult>> { let rpc_processor = self.rpc_processor(); @@ -1077,7 +1080,7 @@ impl RoutingTable { #[instrument(level = "trace", skip(self, filter, metric), ret)] pub fn get_node_speed_percentile( &self, - node_id: TypedKey, + node_id: TypedPublicKey, cur_ts: Timestamp, filter: impl Fn(&BucketEntryInner) -> bool, metric: impl Fn(&LatencyStats) -> TimestampDuration, diff --git a/veilid-core/src/routing_table/node_ref/traits.rs b/veilid-core/src/routing_table/node_ref/traits.rs index 4c499afd..c445af76 100644 --- a/veilid-core/src/routing_table/node_ref/traits.rs +++ b/veilid-core/src/routing_table/node_ref/traits.rs @@ -39,10 +39,10 @@ pub(crate) trait NodeRefCommonTrait: NodeRefAccessorsTrait + NodeRefOperateTrait Arc::ptr_eq(&self.entry(), entry) } - fn node_ids(&self) -> TypedKeyGroup { + fn node_ids(&self) -> TypedPublicKeyGroup { self.operate(|_rti, e| e.node_ids()) } - fn best_node_id(&self) -> TypedKey { + fn best_node_id(&self) -> TypedPublicKey { self.operate(|_rti, e| e.best_node_id()) } diff --git a/veilid-core/src/routing_table/privacy.rs b/veilid-core/src/routing_table/privacy.rs index cba4ef40..1221e0bf 100644 --- a/veilid-core/src/routing_table/privacy.rs +++ b/veilid-core/src/routing_table/privacy.rs @@ -47,7 +47,7 @@ impl RouteNode { match self { RouteNode::NodeId(id) => { // - match routing_table.lookup_node_ref(TypedKey::new(crypto_kind, *id)) { + match routing_table.lookup_node_ref(TypedPublicKey::new(crypto_kind, *id)) { Ok(nr) => nr, Err(e) => { veilid_log!(routing_table debug "failed to look up route node: {}", e); @@ -71,7 +71,7 @@ impl RouteNode { pub fn describe(&self, crypto_kind: CryptoKind) -> String { match self { RouteNode::NodeId(id) => { - format!("{}", TypedKey::new(crypto_kind, *id)) + format!("{}", TypedPublicKey::new(crypto_kind, *id)) } RouteNode::PeerInfo(pi) => match pi.node_ids().get(crypto_kind) { Some(id) => format!("{}", id), @@ -121,14 +121,14 @@ impl PrivateRouteHops { #[derive(Clone, Debug)] pub(crate) struct PrivateRoute { /// The public key used for the entire route - pub public_key: TypedKey, + pub public_key: TypedPublicKey, pub hop_count: u8, pub hops: PrivateRouteHops, } impl PrivateRoute { /// Stub route is the form used when no privacy is required, but you need to specify the destination for a safety route - pub fn new_stub(public_key: TypedKey, node: RouteNode) -> Self { + pub fn new_stub(public_key: TypedPublicKey, node: RouteNode) -> Self { Self { public_key, hop_count: 1, @@ -182,14 +182,14 @@ impl PrivateRoute { } } - pub fn first_hop_node_id(&self) -> Option { + pub fn first_hop_node_id(&self) -> Option { let PrivateRouteHops::FirstHop(pr_first_hop) = &self.hops else { return None; }; // Get the safety route to use from the spec Some(match &pr_first_hop.node { - RouteNode::NodeId(n) => TypedKey::new(self.public_key.kind, *n), + RouteNode::NodeId(n) => TypedPublicKey::new(self.public_key.kind, *n), RouteNode::PeerInfo(p) => p.node_ids().get(self.public_key.kind).unwrap(), }) } @@ -232,14 +232,14 @@ pub(crate) enum SafetyRouteHops { #[derive(Clone, Debug)] pub(crate) struct SafetyRoute { - pub public_key: TypedKey, + pub public_key: TypedPublicKey, pub hop_count: u8, pub hops: SafetyRouteHops, } impl SafetyRoute { /// Stub route is the form used when no privacy is required, but you need to directly contact a private route - pub fn new_stub(public_key: TypedKey, private_route: PrivateRoute) -> Self { + pub fn new_stub(public_key: TypedPublicKey, private_route: PrivateRoute) -> Self { // First hop should have already been popped off for stubbed safety routes since // we are sending directly to the first hop assert!(matches!(private_route.hops, PrivateRouteHops::Data(_))); diff --git a/veilid-core/src/routing_table/route_spec_store/mod.rs b/veilid-core/src/routing_table/route_spec_store/mod.rs index ad3b39b2..9ea12159 100644 --- a/veilid-core/src/routing_table/route_spec_store/mod.rs +++ b/veilid-core/src/routing_table/route_spec_store/mod.rs @@ -166,7 +166,7 @@ impl RouteSpecStore { crypto_kinds: &[CryptoKind], safety_spec: &SafetySpec, directions: DirectionSet, - avoid_nodes: &[TypedKey], + avoid_nodes: &[TypedPublicKey], automatic: bool, ) -> VeilidAPIResult { let inner = &mut *self.inner.lock(); @@ -193,7 +193,7 @@ impl RouteSpecStore { crypto_kinds: &[CryptoKind], safety_spec: &SafetySpec, directions: DirectionSet, - avoid_nodes: &[TypedKey], + avoid_nodes: &[TypedPublicKey], automatic: bool, ) -> VeilidAPIResult { use core::cmp::Ordering; @@ -517,7 +517,7 @@ impl RouteSpecStore { } // Ensure the route doesn't contain both a node and its relay - let mut seen_nodes: HashSet = HashSet::new(); + let mut seen_nodes: HashSet = HashSet::new(); for n in permutation { let node = nodes.get(*n).unwrap(); if !seen_nodes.insert(node.locked(rti).best_node_id()) { @@ -698,7 +698,7 @@ impl RouteSpecStore { #[instrument(level = "trace", target = "route", skip(self, data, callback), ret)] pub fn with_signature_validated_route( &self, - public_key: &TypedKey, + public_key: &TypedPublicKey, signatures: &[Signature], data: &[u8], last_hop_id: PublicKey, @@ -948,7 +948,7 @@ impl RouteSpecStore { stability: Stability, sequencing: Sequencing, directions: DirectionSet, - avoid_nodes: &[TypedKey], + avoid_nodes: &[TypedPublicKey], ) -> Option { let cur_ts = Timestamp::now(); @@ -1105,7 +1105,7 @@ impl RouteSpecStore { let opt_first_hop = match pr_first_hop_node { RouteNode::NodeId(id) => rti - .lookup_node_ref(TypedKey::new(crypto_kind, id)) + .lookup_node_ref(TypedPublicKey::new(crypto_kind, id)) .map_err(VeilidAPIError::internal)?, RouteNode::PeerInfo(pi) => Some( rti.register_node_with_peer_info(pi, false) @@ -1252,7 +1252,8 @@ impl RouteSpecStore { RouteNode::NodeId(safety_rsd.hops[h]) } else { // Full peer info, required until we are sure the route has been fully established - let node_id = TypedKey::new(safety_rsd.crypto_kind, safety_rsd.hops[h]); + let node_id = + TypedPublicKey::new(safety_rsd.crypto_kind, safety_rsd.hops[h]); let pi = rti .with_node_entry(node_id, |entry| { entry.with(rti, |_rti, e| { @@ -1301,7 +1302,7 @@ impl RouteSpecStore { // Build safety route let safety_route = SafetyRoute { - public_key: TypedKey::new(crypto_kind, sr_pubkey), + public_key: TypedPublicKey::new(crypto_kind, sr_pubkey), hop_count: safety_spec.hop_count as u8, hops, }; @@ -1334,7 +1335,7 @@ impl RouteSpecStore { crypto_kind: CryptoKind, safety_spec: &SafetySpec, direction: DirectionSet, - avoid_nodes: &[TypedKey], + avoid_nodes: &[TypedPublicKey], ) -> VeilidAPIResult { // Ensure the total hop count isn't too long for our config let max_route_hop_count = self.max_route_hop_count; @@ -1410,7 +1411,7 @@ impl RouteSpecStore { &self, crypto_kind: CryptoKind, safety_spec: &SafetySpec, - avoid_nodes: &[TypedKey], + avoid_nodes: &[TypedPublicKey], ) -> VeilidAPIResult { let inner = &mut *self.inner.lock(); let routing_table = self.routing_table(); @@ -1496,7 +1497,7 @@ impl RouteSpecStore { RouteNode::NodeId(rsd.hops[h]) } else { // Full peer info, required until we are sure the route has been fully established - let node_id = TypedKey::new(rsd.crypto_kind, rsd.hops[h]); + let node_id = TypedPublicKey::new(rsd.crypto_kind, rsd.hops[h]); let pi = rti .with_node_entry(node_id, |entry| { entry.with(rti, |_rti, e| { @@ -1514,7 +1515,7 @@ impl RouteSpecStore { } let private_route = PrivateRoute { - public_key: TypedKey::new(rsd.crypto_kind, *key), + public_key: TypedPublicKey::new(rsd.crypto_kind, *key), // add hop for 'FirstHop' hop_count: (hop_count + 1).try_into().unwrap(), hops: PrivateRouteHops::FirstHop(Box::new(route_hop)), diff --git a/veilid-core/src/routing_table/route_spec_store/route_set_spec_detail.rs b/veilid-core/src/routing_table/route_spec_store/route_set_spec_detail.rs index 42203d51..97c0acb1 100644 --- a/veilid-core/src/routing_table/route_spec_store/route_set_spec_detail.rs +++ b/veilid-core/src/routing_table/route_spec_store/route_set_spec_detail.rs @@ -57,10 +57,10 @@ impl RouteSetSpecDetail { pub fn get_route_by_key(&self, key: &PublicKey) -> Option<&RouteSpecDetail> { self.route_set.get(key) } - pub fn get_route_set_keys(&self) -> TypedKeyGroup { - let mut tks = TypedKeyGroup::new(); + pub fn get_route_set_keys(&self) -> TypedPublicKeyGroup { + let mut tks = TypedPublicKeyGroup::new(); for (k, v) in &self.route_set { - tks.add(TypedKey::new(v.crypto_kind, *k)); + tks.add(TypedPublicKey::new(v.crypto_kind, *k)); } tks } @@ -109,7 +109,7 @@ impl RouteSetSpecDetail { Sequencing::EnsureOrdered => self.can_do_sequenced, } } - pub fn contains_nodes(&self, nodes: &[TypedKey]) -> bool { + pub fn contains_nodes(&self, nodes: &[TypedPublicKey]) -> bool { for tk in nodes { for rsd in self.route_set.values() { if rsd.crypto_kind == tk.kind && rsd.hops.contains(&tk.value) { diff --git a/veilid-core/src/routing_table/route_spec_store/route_spec_store_cache.rs b/veilid-core/src/routing_table/route_spec_store/route_spec_store_cache.rs index eb9f5d81..eafab499 100644 --- a/veilid-core/src/routing_table/route_spec_store/route_spec_store_cache.rs +++ b/veilid-core/src/routing_table/route_spec_store/route_spec_store_cache.rs @@ -139,14 +139,14 @@ impl RouteSpecStoreCache { } /// calculate how many times a node with a particular node id set has been used anywhere in the path of our allocated routes - pub fn get_used_node_count(&self, node_ids: &TypedKeyGroup) -> usize { + pub fn get_used_node_count(&self, node_ids: &TypedPublicKeyGroup) -> usize { node_ids.iter().fold(0usize, |acc, k| { acc + self.used_nodes.get(&k.value).cloned().unwrap_or_default() }) } /// calculate how many times a node with a particular node id set has been used at the end of the path of our allocated routes - pub fn get_used_end_node_count(&self, node_ids: &TypedKeyGroup) -> usize { + pub fn get_used_end_node_count(&self, node_ids: &TypedPublicKeyGroup) -> usize { node_ids.iter().fold(0usize, |acc, k| { acc + self .used_end_nodes diff --git a/veilid-core/src/routing_table/route_spec_store/route_spec_store_content.rs b/veilid-core/src/routing_table/route_spec_store/route_spec_store_content.rs index f30c2b0f..e3ee72db 100644 --- a/veilid-core/src/routing_table/route_spec_store/route_spec_store_content.rs +++ b/veilid-core/src/routing_table/route_spec_store/route_spec_store_content.rs @@ -35,7 +35,7 @@ impl RouteSpecStoreContent { let mut hop_node_refs = Vec::with_capacity(rsd.hops.len()); for h in &rsd.hops { let Ok(Some(nr)) = - routing_table.lookup_node_ref(TypedKey::new(rsd.crypto_kind, *h)) + routing_table.lookup_node_ref(TypedPublicKey::new(rsd.crypto_kind, *h)) else { dead_ids.push(*rsid); break; diff --git a/veilid-core/src/routing_table/routing_table_inner/mod.rs b/veilid-core/src/routing_table/routing_table_inner/mod.rs index 083f8ebf..32fea8b9 100644 --- a/veilid-core/src/routing_table/routing_table_inner/mod.rs +++ b/veilid-core/src/routing_table/routing_table_inner/mod.rs @@ -55,7 +55,7 @@ pub struct RoutingTableInner { /// Statistics about the total bandwidth to/from this node pub(super) self_transfer_stats: TransferStatsDownUp, /// Peers we have recently communicated with - pub(super) recent_peers: LruCache, + pub(super) recent_peers: LruCache, /// Async tagged critical sections table /// Tag: "tick" -> in ticker pub(super) critical_sections: AsyncTagLockTable<&'static str>, @@ -655,7 +655,7 @@ impl RoutingTableInner { fn update_bucket_entry_node_ids( &mut self, entry: Arc, - node_ids: &[TypedKey], + node_ids: &[TypedPublicKey], ) -> EyreResult<()> { let routing_table = self.routing_table(); @@ -745,7 +745,7 @@ impl RoutingTableInner { #[instrument(level = "trace", skip_all, err)] fn create_node_ref( &mut self, - node_ids: &TypedKeyGroup, + node_ids: &TypedPublicKeyGroup, update_func: F, ) -> EyreResult where @@ -827,7 +827,7 @@ impl RoutingTableInner { #[instrument(level = "trace", skip_all, err)] pub fn lookup_any_node_ref(&self, node_id_key: PublicKey) -> EyreResult> { for ck in VALID_CRYPTO_KINDS { - if let Some(nr) = self.lookup_node_ref(TypedKey::new(ck, node_id_key))? { + if let Some(nr) = self.lookup_node_ref(TypedPublicKey::new(ck, node_id_key))? { return Ok(Some(nr)); } } @@ -836,7 +836,7 @@ impl RoutingTableInner { /// Resolve an existing routing table entry and return a reference to it #[instrument(level = "trace", skip_all, err)] - pub fn lookup_node_ref(&self, node_id: TypedKey) -> EyreResult> { + pub fn lookup_node_ref(&self, node_id: TypedPublicKey) -> EyreResult> { if self.routing_table().matches_own_node_id(&[node_id]) { bail!("can't look up own node id in routing table"); } @@ -855,7 +855,7 @@ impl RoutingTableInner { #[instrument(level = "trace", skip_all, err)] pub fn lookup_and_filter_noderef( &self, - node_id: TypedKey, + node_id: TypedPublicKey, routing_domain_set: RoutingDomainSet, dial_info_filter: DialInfoFilter, ) -> EyreResult> { @@ -870,7 +870,7 @@ impl RoutingTableInner { } /// Resolve an existing routing table entry and call a function on its entry without using a noderef - pub fn with_node_entry(&self, node_id: TypedKey, f: F) -> Option + pub fn with_node_entry(&self, node_id: TypedPublicKey, f: F) -> Option where F: FnOnce(Arc) -> R, { @@ -970,10 +970,10 @@ impl RoutingTableInner { pub fn register_node_with_id( &mut self, routing_domain: RoutingDomain, - node_id: TypedKey, + node_id: TypedPublicKey, timestamp: Timestamp, ) -> EyreResult { - let nr = self.create_node_ref(&TypedKeyGroup::from(node_id), |_rti, e| { + let nr = self.create_node_ref(&TypedPublicKeyGroup::from(node_id), |_rti, e| { //e.make_not_dead(timestamp); e.touch_last_seen(timestamp); })?; @@ -1091,7 +1091,7 @@ impl RoutingTableInner { } } - pub fn touch_recent_peer(&mut self, node_id: TypedKey, last_connection: Flow) { + pub fn touch_recent_peer(&mut self, node_id: TypedPublicKey, last_connection: Flow) { self.recent_peers .insert(node_id, RecentPeersEntry { last_connection }); } @@ -1319,7 +1319,7 @@ impl RoutingTableInner { pub fn find_preferred_closest_nodes( &self, node_count: usize, - node_id: TypedKey, + node_id: TypedHashDigest, mut filters: VecDeque, transform: T, ) -> VeilidAPIResult> @@ -1393,8 +1393,8 @@ impl RoutingTableInner { }; // distance is the next metric, closer nodes first - let da = vcrypto.distance(&a_key.value, &node_id.value); - let db = vcrypto.distance(&b_key.value, &node_id.value); + let da = vcrypto.distance(&HashDigest::from(a_key.value), &node_id.value); + let db = vcrypto.distance(&HashDigest::from(b_key.value), &node_id.value); da.cmp(&db) }; @@ -1407,7 +1407,7 @@ impl RoutingTableInner { #[instrument(level = "trace", skip_all)] pub fn sort_and_clean_closest_noderefs( &self, - node_id: TypedKey, + node_id: TypedHashDigest, closest_nodes: &[NodeRef], ) -> Vec { // Lock all noderefs @@ -1525,7 +1525,7 @@ impl RoutingTableInner { #[instrument(level = "trace", skip(self, filter, metric), ret)] pub fn get_node_relative_performance( &self, - node_id: TypedKey, + node_id: TypedPublicKey, cur_ts: Timestamp, filter: impl Fn(&BucketEntryInner) -> bool, metric: impl Fn(&LatencyStats) -> TimestampDuration, @@ -1584,7 +1584,7 @@ impl RoutingTableInner { #[instrument(level = "trace", skip_all)] pub fn make_closest_noderef_sort<'a>( crypto: &'a Crypto, - node_id: TypedKey, + node_id: TypedHashDigest, ) -> impl Fn(&LockedNodeRef, &LockedNodeRef) -> core::cmp::Ordering + 'a { let kind = node_id.kind; // Get cryptoversion to check distance with @@ -1603,8 +1603,8 @@ pub fn make_closest_noderef_sort<'a>( let b_key = b_entry.node_ids().get(kind).unwrap(); // distance is the next metric, closer nodes first - let da = vcrypto.distance(&a_key.value, &node_id.value); - let db = vcrypto.distance(&b_key.value, &node_id.value); + let da = vcrypto.distance(&HashDigest::from(a_key.value), &node_id.value); + let db = vcrypto.distance(&HashDigest::from(b_key.value), &node_id.value); da.cmp(&db) }) }) @@ -1613,16 +1613,16 @@ pub fn make_closest_noderef_sort<'a>( pub fn make_closest_node_id_sort( crypto: &Crypto, - node_id: TypedKey, -) -> impl Fn(&CryptoKey, &CryptoKey) -> core::cmp::Ordering + '_ { + node_id: TypedPublicKey, +) -> impl Fn(&PublicKey, &PublicKey) -> core::cmp::Ordering + '_ { let kind = node_id.kind; // Get cryptoversion to check distance with let vcrypto = crypto.get(kind).unwrap(); - move |a: &CryptoKey, b: &CryptoKey| -> core::cmp::Ordering { + move |a: &PublicKey, b: &PublicKey| -> core::cmp::Ordering { // distance is the next metric, closer nodes first - let da = vcrypto.distance(a, &node_id.value); - let db = vcrypto.distance(b, &node_id.value); + let da = vcrypto.distance(&HashDigest::from(*a), &HashDigest::from(node_id.value)); + let db = vcrypto.distance(&HashDigest::from(*b), &HashDigest::from(node_id.value)); da.cmp(&db) } } diff --git a/veilid-core/src/routing_table/tasks/bootstrap.rs b/veilid-core/src/routing_table/tasks/bootstrap.rs index db02376c..2ae72df5 100644 --- a/veilid-core/src/routing_table/tasks/bootstrap.rs +++ b/veilid-core/src/routing_table/tasks/bootstrap.rs @@ -62,7 +62,7 @@ impl RoutingTable { })); } - let mut bootstrapped_peer_id_set = HashSet::::new(); + let mut bootstrapped_peer_id_set = HashSet::::new(); let mut bootstrapped_peers = vec![]; loop { match unord.next().timeout_at(stop_token.clone()).await { diff --git a/veilid-core/src/routing_table/tasks/closest_peers_refresh.rs b/veilid-core/src/routing_table/tasks/closest_peers_refresh.rs index ef86dc3c..19d66ffc 100644 --- a/veilid-core/src/routing_table/tasks/closest_peers_refresh.rs +++ b/veilid-core/src/routing_table/tasks/closest_peers_refresh.rs @@ -55,7 +55,7 @@ impl RoutingTable { let noderefs = self .find_preferred_closest_nodes( CLOSEST_PEERS_REQUEST_COUNT, - self_node_id, + self_node_id.into(), filters, |_rti, entry: Option>| { NodeRef::new(self.registry(), entry.unwrap().clone()) diff --git a/veilid-core/src/routing_table/tasks/kick_buckets.rs b/veilid-core/src/routing_table/tasks/kick_buckets.rs index 72f3b644..675ab9a0 100644 --- a/veilid-core/src/routing_table/tasks/kick_buckets.rs +++ b/veilid-core/src/routing_table/tasks/kick_buckets.rs @@ -32,7 +32,7 @@ impl RoutingTable { }; let sort = make_closest_node_id_sort(&crypto, our_node_id); - let mut closest_peers = BTreeSet::::new(); + let mut closest_peers = BTreeSet::::new(); let mut closest_unreliable_count = 0usize; let mut closest_reliable_count = 0usize; diff --git a/veilid-core/src/routing_table/tests/test_serialize_routing_table.rs b/veilid-core/src/routing_table/tests/test_serialize_routing_table.rs index 68ce2c2e..805a6a42 100644 --- a/veilid-core/src/routing_table/tests/test_serialize_routing_table.rs +++ b/veilid-core/src/routing_table/tests/test_serialize_routing_table.rs @@ -58,10 +58,10 @@ pub async fn test_routingtable_buckets_round_trip() { } pub fn test_round_trip_peerinfo() { - let mut tks = TypedKeyGroup::new(); - tks.add(TypedKey::new( + let mut tks = TypedPublicKeyGroup::new(); + tks.add(TypedPublicKey::new( CRYPTO_KIND_VLD0, - CryptoKey::new([ + PublicKey::new([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ]), diff --git a/veilid-core/src/routing_table/types/contact_method.rs b/veilid-core/src/routing_table/types/contact_method.rs index 173db16b..4b5f6e44 100644 --- a/veilid-core/src/routing_table/types/contact_method.rs +++ b/veilid-core/src/routing_table/types/contact_method.rs @@ -10,11 +10,11 @@ pub enum ContactMethod { /// Contact the node directly Direct(DialInfo), /// Request via signal the node connect back directly (relay, target) - SignalReverse(TypedKey, TypedKey), + SignalReverse(TypedPublicKey, TypedPublicKey), /// Request via signal the node negotiate a hole punch (relay, target) - SignalHolePunch(TypedKey, TypedKey), + SignalHolePunch(TypedPublicKey, TypedPublicKey), /// Must use an inbound relay to reach the node - InboundRelay(TypedKey), + InboundRelay(TypedPublicKey), /// Must use outbound relay to reach the node - OutboundRelay(TypedKey), + OutboundRelay(TypedPublicKey), } diff --git a/veilid-core/src/routing_table/types/peer_info.rs b/veilid-core/src/routing_table/types/peer_info.rs index 2d9e319f..a9503ec2 100644 --- a/veilid-core/src/routing_table/types/peer_info.rs +++ b/veilid-core/src/routing_table/types/peer_info.rs @@ -7,7 +7,7 @@ pub struct PeerInfo { skip_serializing_if = "is_default_routing_domain" )] routing_domain: RoutingDomain, - node_ids: TypedKeyGroup, + node_ids: TypedPublicKeyGroup, signed_node_info: SignedNodeInfo, } @@ -32,7 +32,7 @@ impl fmt::Display for PeerInfo { impl PeerInfo { pub fn new( routing_domain: RoutingDomain, - node_ids: TypedKeyGroup, + node_ids: TypedPublicKeyGroup, signed_node_info: SignedNodeInfo, ) -> Self { assert!(!node_ids.is_empty() && node_ids.len() <= MAX_CRYPTO_KINDS); @@ -55,13 +55,13 @@ impl PeerInfo { pub fn routing_domain(&self) -> RoutingDomain { self.routing_domain } - pub fn node_ids(&self) -> &TypedKeyGroup { + pub fn node_ids(&self) -> &TypedPublicKeyGroup { &self.node_ids } pub fn signed_node_info(&self) -> &SignedNodeInfo { &self.signed_node_info } - pub fn destructure(self) -> (RoutingDomain, TypedKeyGroup, SignedNodeInfo) { + pub fn destructure(self) -> (RoutingDomain, TypedPublicKeyGroup, SignedNodeInfo) { (self.routing_domain, self.node_ids, self.signed_node_info) } diff --git a/veilid-core/src/routing_table/types/signed_direct_node_info.rs b/veilid-core/src/routing_table/types/signed_direct_node_info.rs index 011aab45..034d482c 100644 --- a/veilid-core/src/routing_table/types/signed_direct_node_info.rs +++ b/veilid-core/src/routing_table/types/signed_direct_node_info.rs @@ -35,9 +35,9 @@ impl SignedDirectNodeInfo { pub fn validate( &self, - node_ids: &TypedKeyGroup, + node_ids: &TypedPublicKeyGroup, crypto: &Crypto, - ) -> VeilidAPIResult { + ) -> VeilidAPIResult { let node_info_bytes = Self::make_signature_bytes(&self.node_info, self.timestamp)?; // Verify the signatures that we can diff --git a/veilid-core/src/routing_table/types/signed_node_info.rs b/veilid-core/src/routing_table/types/signed_node_info.rs index b0cace81..955ea9f3 100644 --- a/veilid-core/src/routing_table/types/signed_node_info.rs +++ b/veilid-core/src/routing_table/types/signed_node_info.rs @@ -26,9 +26,9 @@ impl fmt::Display for SignedNodeInfo { impl SignedNodeInfo { pub fn validate( &self, - node_ids: &TypedKeyGroup, + node_ids: &TypedPublicKeyGroup, crypto: &Crypto, - ) -> VeilidAPIResult { + ) -> VeilidAPIResult { match self { SignedNodeInfo::Direct(d) => d.validate(node_ids, crypto), SignedNodeInfo::Relayed(r) => r.validate(node_ids, crypto), @@ -54,9 +54,9 @@ impl SignedNodeInfo { SignedNodeInfo::Relayed(r) => r.node_info(), } } - pub fn relay_ids(&self) -> TypedKeyGroup { + pub fn relay_ids(&self) -> TypedPublicKeyGroup { match self { - SignedNodeInfo::Direct(_) => TypedKeyGroup::new(), + SignedNodeInfo::Direct(_) => TypedPublicKeyGroup::new(), SignedNodeInfo::Relayed(r) => r.relay_ids().clone(), } } diff --git a/veilid-core/src/routing_table/types/signed_relayed_node_info.rs b/veilid-core/src/routing_table/types/signed_relayed_node_info.rs index a0950912..8fee99be 100644 --- a/veilid-core/src/routing_table/types/signed_relayed_node_info.rs +++ b/veilid-core/src/routing_table/types/signed_relayed_node_info.rs @@ -4,7 +4,7 @@ use super::*; #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct SignedRelayedNodeInfo { node_info: NodeInfo, - relay_ids: TypedKeyGroup, + relay_ids: TypedPublicKeyGroup, relay_info: SignedDirectNodeInfo, timestamp: Timestamp, signatures: Vec, @@ -32,7 +32,7 @@ impl SignedRelayedNodeInfo { /// All signatures are stored however, as this can be passed to other nodes that may be able to validate those signatures. pub fn new( node_info: NodeInfo, - relay_ids: TypedKeyGroup, + relay_ids: TypedPublicKeyGroup, relay_info: SignedDirectNodeInfo, timestamp: Timestamp, signatures: Vec, @@ -48,9 +48,9 @@ impl SignedRelayedNodeInfo { pub fn validate( &self, - node_ids: &TypedKeyGroup, + node_ids: &TypedPublicKeyGroup, crypto: &Crypto, - ) -> VeilidAPIResult { + ) -> VeilidAPIResult { // Ensure the relay info for the node has a superset of the crypto kinds of the node it is relaying if common_crypto_kinds( self.node_info.crypto_support(), @@ -84,7 +84,7 @@ impl SignedRelayedNodeInfo { crypto: &Crypto, typed_key_pairs: Vec, node_info: NodeInfo, - relay_ids: TypedKeyGroup, + relay_ids: TypedPublicKeyGroup, relay_info: SignedDirectNodeInfo, ) -> VeilidAPIResult { let timestamp = Timestamp::now(); @@ -105,7 +105,7 @@ impl SignedRelayedNodeInfo { fn make_signature_bytes( node_info: &NodeInfo, - relay_ids: &[TypedKey], + relay_ids: &[TypedPublicKey], relay_info: &SignedDirectNodeInfo, timestamp: Timestamp, ) -> VeilidAPIResult> { @@ -148,7 +148,7 @@ impl SignedRelayedNodeInfo { pub fn timestamp(&self) -> Timestamp { self.timestamp } - pub fn relay_ids(&self) -> &TypedKeyGroup { + pub fn relay_ids(&self) -> &TypedPublicKeyGroup { &self.relay_ids } pub fn relay_info(&self) -> &SignedDirectNodeInfo { diff --git a/veilid-core/src/rpc_processor/coders/operations/operation_find_block.rs b/veilid-core/src/rpc_processor/coders/operations/operation_find_block.rs index 096436ed..aa699d20 100644 --- a/veilid-core/src/rpc_processor/coders/operations/operation_find_block.rs +++ b/veilid-core/src/rpc_processor/coders/operations/operation_find_block.rs @@ -6,22 +6,22 @@ const MAX_FIND_BLOCK_A_PEERS_LEN: usize = 10; #[derive(Debug, Clone)] pub(in crate::rpc_processor) struct RPCOperationFindBlockQ { - block_id: TypedKey, + block_id: TypedPublicKey, } impl RPCOperationFindBlockQ { - pub fn new(block_id: TypedKey) -> Self { + pub fn new(block_id: TypedPublicKey) -> Self { Self { block_id } } pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> { Ok(()) } - pub fn block_id(&self) -> TypedKey { + pub fn block_id(&self) -> TypedPublicKey { self.block_id } - pub fn destructure(self) -> TypedKey { + pub fn destructure(self) -> TypedPublicKey { self.block_id } diff --git a/veilid-core/src/rpc_processor/coders/operations/operation_find_node.rs b/veilid-core/src/rpc_processor/coders/operations/operation_find_node.rs index 8e01280b..661098ed 100644 --- a/veilid-core/src/rpc_processor/coders/operations/operation_find_node.rs +++ b/veilid-core/src/rpc_processor/coders/operations/operation_find_node.rs @@ -4,12 +4,12 @@ const MAX_FIND_NODE_A_PEERS_LEN: usize = 20; #[derive(Debug, Clone)] pub(in crate::rpc_processor) struct RPCOperationFindNodeQ { - node_id: TypedKey, + node_id: TypedPublicKey, capabilities: Vec, } impl RPCOperationFindNodeQ { - pub fn new(node_id: TypedKey, capabilities: Vec) -> Self { + pub fn new(node_id: TypedPublicKey, capabilities: Vec) -> Self { Self { node_id, capabilities, @@ -26,7 +26,7 @@ impl RPCOperationFindNodeQ { // &self.capabilities // } - pub fn destructure(self) -> (TypedKey, Vec) { + pub fn destructure(self) -> (TypedPublicKey, Vec) { (self.node_id, self.capabilities) } diff --git a/veilid-core/src/rpc_processor/coders/operations/operation_get_value.rs b/veilid-core/src/rpc_processor/coders/operations/operation_get_value.rs index 1c95cac2..39200e6d 100644 --- a/veilid-core/src/rpc_processor/coders/operations/operation_get_value.rs +++ b/veilid-core/src/rpc_processor/coders/operations/operation_get_value.rs @@ -22,13 +22,13 @@ impl fmt::Debug for ValidateGetValueContext { #[derive(Debug, Clone)] pub(in crate::rpc_processor) struct RPCOperationGetValueQ { - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, want_descriptor: bool, } impl RPCOperationGetValueQ { - pub fn new(key: TypedKey, subkey: ValueSubkey, want_descriptor: bool) -> Self { + pub fn new(key: TypedRecordKey, subkey: ValueSubkey, want_descriptor: bool) -> Self { Self { key, subkey, @@ -48,7 +48,7 @@ impl RPCOperationGetValueQ { // pub fn want_descriptor(&self) -> bool { // self.want_descriptor // } - pub fn destructure(self) -> (TypedKey, ValueSubkey, bool) { + pub fn destructure(self) -> (TypedRecordKey, ValueSubkey, bool) { (self.key, self.subkey, self.want_descriptor) } @@ -57,7 +57,7 @@ impl RPCOperationGetValueQ { reader: &veilid_capnp::operation_get_value_q::Reader, ) -> Result { let k_reader = reader.reborrow().get_key().map_err(RPCError::protocol)?; - let key = decode_typed_key(&k_reader)?; + let key = decode_typed_record_key(&k_reader)?; let subkey = reader.reborrow().get_subkey(); let want_descriptor = reader.reborrow().get_want_descriptor(); Ok(Self { @@ -71,7 +71,7 @@ impl RPCOperationGetValueQ { builder: &mut veilid_capnp::operation_get_value_q::Builder, ) -> Result<(), RPCError> { let mut k_builder = builder.reborrow().init_key(); - encode_typed_key(&self.key, &mut k_builder); + encode_typed_record_key(&self.key, &mut k_builder); builder.set_subkey(self.subkey); builder.set_want_descriptor(self.want_descriptor); Ok(()) diff --git a/veilid-core/src/rpc_processor/coders/operations/operation_inspect_value.rs b/veilid-core/src/rpc_processor/coders/operations/operation_inspect_value.rs index cf38133b..506860e2 100644 --- a/veilid-core/src/rpc_processor/coders/operations/operation_inspect_value.rs +++ b/veilid-core/src/rpc_processor/coders/operations/operation_inspect_value.rs @@ -14,14 +14,14 @@ pub(in crate::rpc_processor) struct ValidateInspectValueContext { #[derive(Debug, Clone)] pub(in crate::rpc_processor) struct RPCOperationInspectValueQ { - key: TypedKey, + key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, want_descriptor: bool, } impl RPCOperationInspectValueQ { pub fn new( - key: TypedKey, + key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, want_descriptor: bool, ) -> Result { @@ -44,7 +44,7 @@ impl RPCOperationInspectValueQ { // pub fn want_descriptor(&self) -> bool { // self.want_descriptor // } - pub fn destructure(self) -> (TypedKey, ValueSubkeyRangeSet, bool) { + pub fn destructure(self) -> (TypedRecordKey, ValueSubkeyRangeSet, bool) { (self.key, self.subkeys, self.want_descriptor) } @@ -53,7 +53,7 @@ impl RPCOperationInspectValueQ { reader: &veilid_capnp::operation_inspect_value_q::Reader, ) -> Result { let k_reader = reader.reborrow().get_key().map_err(RPCError::protocol)?; - let key = decode_typed_key(&k_reader)?; + let key = decode_typed_record_key(&k_reader)?; let sk_reader = reader.get_subkeys().map_err(RPCError::protocol)?; // Maximum number of ranges that can hold the maximum number of subkeys is one subkey per range if sk_reader.len() as usize > MAX_INSPECT_VALUE_Q_SUBKEY_RANGES_LEN { @@ -87,7 +87,7 @@ impl RPCOperationInspectValueQ { builder: &mut veilid_capnp::operation_inspect_value_q::Builder, ) -> Result<(), RPCError> { let mut k_builder = builder.reborrow().init_key(); - encode_typed_key(&self.key, &mut k_builder); + encode_typed_record_key(&self.key, &mut k_builder); let mut sk_builder = builder.reborrow().init_subkeys( self.subkeys diff --git a/veilid-core/src/rpc_processor/coders/operations/operation_set_value.rs b/veilid-core/src/rpc_processor/coders/operations/operation_set_value.rs index 0a0198e5..261d7a72 100644 --- a/veilid-core/src/rpc_processor/coders/operations/operation_set_value.rs +++ b/veilid-core/src/rpc_processor/coders/operations/operation_set_value.rs @@ -22,7 +22,7 @@ impl fmt::Debug for ValidateSetValueContext { #[derive(Debug, Clone)] pub(in crate::rpc_processor) struct RPCOperationSetValueQ { - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, value: SignedValueData, descriptor: Option, @@ -30,7 +30,7 @@ pub(in crate::rpc_processor) struct RPCOperationSetValueQ { impl RPCOperationSetValueQ { pub fn new( - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, value: SignedValueData, descriptor: Option, @@ -64,7 +64,7 @@ impl RPCOperationSetValueQ { pub fn destructure( self, ) -> ( - TypedKey, + TypedRecordKey, ValueSubkey, SignedValueData, Option, @@ -77,7 +77,7 @@ impl RPCOperationSetValueQ { reader: &veilid_capnp::operation_set_value_q::Reader, ) -> Result { let k_reader = reader.get_key().map_err(RPCError::protocol)?; - let key = decode_typed_key(&k_reader)?; + let key = decode_typed_record_key(&k_reader)?; let subkey = reader.get_subkey(); let v_reader = reader.get_value().map_err(RPCError::protocol)?; let value = decode_signed_value_data(&v_reader)?; @@ -100,7 +100,7 @@ impl RPCOperationSetValueQ { builder: &mut veilid_capnp::operation_set_value_q::Builder, ) -> Result<(), RPCError> { let mut k_builder = builder.reborrow().init_key(); - encode_typed_key(&self.key, &mut k_builder); + encode_typed_record_key(&self.key, &mut k_builder); builder.set_subkey(self.subkey); let mut v_builder = builder.reborrow().init_value(); encode_signed_value_data(&self.value, &mut v_builder)?; diff --git a/veilid-core/src/rpc_processor/coders/operations/operation_supply_block.rs b/veilid-core/src/rpc_processor/coders/operations/operation_supply_block.rs index 3b60ca60..8069201b 100644 --- a/veilid-core/src/rpc_processor/coders/operations/operation_supply_block.rs +++ b/veilid-core/src/rpc_processor/coders/operations/operation_supply_block.rs @@ -4,22 +4,22 @@ const MAX_SUPPLY_BLOCK_A_PEERS_LEN: usize = 20; #[derive(Debug, Clone)] pub(in crate::rpc_processor) struct RPCOperationSupplyBlockQ { - block_id: TypedKey, + block_id: TypedPublicKey, } impl RPCOperationSupplyBlockQ { - pub fn new(block_id: TypedKey) -> Self { + pub fn new(block_id: TypedPublicKey) -> Self { Self { block_id } } pub fn validate(&mut self, _validate_context: &RPCValidateContext) -> Result<(), RPCError> { Ok(()) } - pub fn block_id(&self) -> &TypedKey { + pub fn block_id(&self) -> &TypedPublicKey { &self.block_id } - pub fn destructure(self) -> TypedKey { + pub fn destructure(self) -> TypedPublicKey { self.block_id } diff --git a/veilid-core/src/rpc_processor/coders/operations/operation_value_changed.rs b/veilid-core/src/rpc_processor/coders/operations/operation_value_changed.rs index bd11b2e6..fea74e7f 100644 --- a/veilid-core/src/rpc_processor/coders/operations/operation_value_changed.rs +++ b/veilid-core/src/rpc_processor/coders/operations/operation_value_changed.rs @@ -5,7 +5,7 @@ const MAX_VALUE_CHANGED_SUBKEY_RANGES_LEN: usize = 512; #[derive(Debug, Clone)] pub(in crate::rpc_processor) struct RPCOperationValueChanged { - key: TypedKey, + key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, count: u32, watch_id: u64, @@ -14,7 +14,7 @@ pub(in crate::rpc_processor) struct RPCOperationValueChanged { impl RPCOperationValueChanged { pub fn new( - key: TypedKey, + key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, count: u32, watch_id: u64, @@ -58,7 +58,7 @@ impl RPCOperationValueChanged { } #[expect(dead_code)] - pub fn key(&self) -> &TypedKey { + pub fn key(&self) -> &TypedRecordKey { &self.key } @@ -85,7 +85,7 @@ impl RPCOperationValueChanged { pub fn destructure( self, ) -> ( - TypedKey, + TypedRecordKey, ValueSubkeyRangeSet, u32, u64, @@ -105,7 +105,7 @@ impl RPCOperationValueChanged { reader: &veilid_capnp::operation_value_changed::Reader, ) -> Result { let k_reader = reader.get_key().map_err(RPCError::protocol)?; - let key = decode_typed_key(&k_reader)?; + let key = decode_typed_record_key(&k_reader)?; let sk_reader = reader.get_subkeys().map_err(RPCError::protocol)?; if sk_reader.len() as usize > MAX_VALUE_CHANGED_SUBKEY_RANGES_LEN { @@ -151,7 +151,7 @@ impl RPCOperationValueChanged { builder: &mut veilid_capnp::operation_value_changed::Builder, ) -> Result<(), RPCError> { let mut k_builder = builder.reborrow().init_key(); - encode_typed_key(&self.key, &mut k_builder); + encode_typed_record_key(&self.key, &mut k_builder); let mut sk_builder = builder.reborrow().init_subkeys( self.subkeys diff --git a/veilid-core/src/rpc_processor/coders/operations/operation_watch_value.rs b/veilid-core/src/rpc_processor/coders/operations/operation_watch_value.rs index 68dcad35..e48337d7 100644 --- a/veilid-core/src/rpc_processor/coders/operations/operation_watch_value.rs +++ b/veilid-core/src/rpc_processor/coders/operations/operation_watch_value.rs @@ -5,7 +5,7 @@ const MAX_WATCH_VALUE_A_PEERS_LEN: usize = 20; #[derive(Debug, Clone)] pub(in crate::rpc_processor) struct RPCOperationWatchValueQ { - key: TypedKey, + key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, expiration: u64, count: u32, @@ -16,7 +16,7 @@ pub(in crate::rpc_processor) struct RPCOperationWatchValueQ { impl RPCOperationWatchValueQ { pub fn new( - key: TypedKey, + key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, expiration: u64, count: u32, @@ -51,7 +51,7 @@ impl RPCOperationWatchValueQ { // signature covers: key, subkeys, expiration, count, using watcher key fn make_signature_data( - key: &TypedKey, + key: &TypedRecordKey, subkeys: &ValueSubkeyRangeSet, expiration: u64, count: u32, @@ -104,7 +104,7 @@ impl RPCOperationWatchValueQ { } #[expect(dead_code)] - pub fn key(&self) -> &TypedKey { + pub fn key(&self) -> &TypedRecordKey { &self.key } @@ -139,7 +139,7 @@ impl RPCOperationWatchValueQ { pub fn destructure( self, ) -> ( - TypedKey, + TypedRecordKey, ValueSubkeyRangeSet, u64, u32, @@ -163,7 +163,7 @@ impl RPCOperationWatchValueQ { reader: &veilid_capnp::operation_watch_value_q::Reader, ) -> Result { let k_reader = reader.get_key().map_err(RPCError::protocol)?; - let key = decode_typed_key(&k_reader)?; + let key = decode_typed_record_key(&k_reader)?; let sk_reader = reader.get_subkeys().map_err(RPCError::protocol)?; if sk_reader.len() as usize > MAX_WATCH_VALUE_Q_SUBKEY_RANGES_LEN { @@ -215,7 +215,7 @@ impl RPCOperationWatchValueQ { builder: &mut veilid_capnp::operation_watch_value_q::Builder, ) -> Result<(), RPCError> { let mut k_builder = builder.reborrow().init_key(); - encode_typed_key(&self.key, &mut k_builder); + encode_typed_record_key(&self.key, &mut k_builder); let mut sk_builder = builder.reborrow().init_subkeys( self.subkeys diff --git a/veilid-core/src/rpc_processor/coders/peer_info.rs b/veilid-core/src/rpc_processor/coders/peer_info.rs index 68daac18..5937fe50 100644 --- a/veilid-core/src/rpc_processor/coders/peer_info.rs +++ b/veilid-core/src/rpc_processor/coders/peer_info.rs @@ -39,7 +39,7 @@ pub fn decode_peer_info( .reborrow() .get_signed_node_info() .map_err(RPCError::protocol)?; - let mut node_ids = TypedKeyGroup::with_capacity(nids_reader.len() as usize); + let mut node_ids = TypedPublicKeyGroup::with_capacity(nids_reader.len() as usize); for nid_reader in nids_reader.iter() { node_ids.add(decode_typed_key(&nid_reader)?); } diff --git a/veilid-core/src/rpc_processor/coders/signed_relayed_node_info.rs b/veilid-core/src/rpc_processor/coders/signed_relayed_node_info.rs index 8c99e0a7..f5555414 100644 --- a/veilid-core/src/rpc_processor/coders/signed_relayed_node_info.rs +++ b/veilid-core/src/rpc_processor/coders/signed_relayed_node_info.rs @@ -69,7 +69,7 @@ pub fn decode_signed_relayed_node_info( if rid_count > MAX_CRYPTO_KINDS { return Err(RPCError::protocol("too many relay ids")); } - let mut relay_ids = TypedKeyGroup::with_capacity(rid_count); + let mut relay_ids = TypedPublicKeyGroup::with_capacity(rid_count); for rid_reader in rids_reader { let relay_id = decode_typed_key(&rid_reader)?; relay_ids.add(relay_id); diff --git a/veilid-core/src/rpc_processor/coders/typed_key.rs b/veilid-core/src/rpc_processor/coders/typed_key.rs index d6908b94..0e347cc1 100644 --- a/veilid-core/src/rpc_processor/coders/typed_key.rs +++ b/veilid-core/src/rpc_processor/coders/typed_key.rs @@ -1,19 +1,47 @@ use super::*; -pub fn decode_typed_key(typed_key: &veilid_capnp::typed_key::Reader) -> Result { +pub fn decode_typed_key( + typed_key: &veilid_capnp::typed_key::Reader, +) -> Result { let key_reader = typed_key .get_key() .map_err(RPCError::map_invalid_format("invalid typed key"))?; let kind = typed_key.get_kind(); - Ok(TypedKey::new( + Ok(TypedPublicKey::new( CryptoKind::from(kind.to_be_bytes()), decode_key256(&key_reader), )) } -pub fn encode_typed_key(typed_key: &TypedKey, builder: &mut veilid_capnp::typed_key::Builder) { +pub fn encode_typed_key( + typed_key: &TypedPublicKey, + builder: &mut veilid_capnp::typed_key::Builder, +) { builder.set_kind(u32::from_be_bytes(typed_key.kind.0)); let mut key_builder = builder.reborrow().init_key(); encode_key256(&typed_key.value, &mut key_builder); } + +pub fn decode_typed_record_key( + typed_key: &veilid_capnp::typed_key::Reader, +) -> Result { + let key_reader = typed_key + .get_key() + .map_err(RPCError::map_invalid_format("invalid typed key"))?; + let kind = typed_key.get_kind(); + + Ok(TypedRecordKey::new( + CryptoKind::from(kind.to_be_bytes()), + RecordKey::new(decode_key256(&key_reader).bytes), + )) +} + +pub fn encode_typed_record_key( + typed_key: &TypedRecordKey, + builder: &mut veilid_capnp::typed_key::Builder, +) { + builder.set_kind(u32::from_be_bytes(typed_key.kind.0)); + let mut key_builder = builder.reborrow().init_key(); + encode_key256(&PublicKey::new(typed_key.value.bytes), &mut key_builder); +} diff --git a/veilid-core/src/rpc_processor/destination.rs b/veilid-core/src/rpc_processor/destination.rs index 3fbaa148..3a53cd95 100644 --- a/veilid-core/src/rpc_processor/destination.rs +++ b/veilid-core/src/rpc_processor/destination.rs @@ -107,7 +107,7 @@ impl Destination { } } - pub fn get_target_node_ids(&self) -> Option { + pub fn get_target_node_ids(&self) -> Option { match self { Destination::Direct { node, diff --git a/veilid-core/src/rpc_processor/fanout/fanout_call.rs b/veilid-core/src/rpc_processor/fanout/fanout_call.rs index f497cd3e..04f9a7eb 100644 --- a/veilid-core/src/rpc_processor/fanout/fanout_call.rs +++ b/veilid-core/src/rpc_processor/fanout/fanout_call.rs @@ -112,7 +112,7 @@ pub enum FanoutCallDisposition { } pub type FanoutCallResult = Result; -pub type FanoutNodeInfoFilter = Arc bool) + Send + Sync>; +pub type FanoutNodeInfoFilter = Arc bool) + Send + Sync>; pub type FanoutCheckDone = Arc bool) + Send + Sync>; pub type FanoutCallRoutine = Arc PinBoxFutureStatic) + Send + Sync>; @@ -146,7 +146,7 @@ pub fn capability_fanout_node_info_filter(caps: Vec) -> FanoutNodeIn /// in the given time pub(crate) struct FanoutCall<'a> { routing_table: &'a RoutingTable, - node_id: TypedKey, + hash_coordinate: TypedHashDigest, node_count: usize, fanout_tasks: usize, consensus_count: usize, @@ -166,7 +166,7 @@ impl<'a> FanoutCall<'a> { #[allow(clippy::too_many_arguments)] pub fn new( routing_table: &'a RoutingTable, - node_id: TypedKey, + hash_coordinate: TypedHashDigest, node_count: usize, fanout_tasks: usize, consensus_count: usize, @@ -177,7 +177,7 @@ impl<'a> FanoutCall<'a> { ) -> Self { Self { routing_table, - node_id, + hash_coordinate, node_count, fanout_tasks, consensus_count, @@ -398,7 +398,12 @@ impl<'a> FanoutCall<'a> { }; self.routing_table - .find_preferred_closest_nodes(self.node_count, self.node_id, filters, transform) + .find_preferred_closest_nodes( + self.node_count, + self.hash_coordinate, + filters, + transform, + ) .map_err(RPCError::invalid_format)? }; context.fanout_queue.add(&closest_nodes); @@ -410,24 +415,26 @@ impl<'a> FanoutCall<'a> { pub async fn run(&self, init_fanout_queue: Vec) -> Result { // Create context for this run let crypto = self.routing_table.crypto(); - let Some(vcrypto) = crypto.get(self.node_id.kind) else { + let Some(vcrypto) = crypto.get(self.hash_coordinate.kind) else { return Err(RPCError::internal( "should not try this on crypto we don't support", )); }; let node_sort = Box::new( - |a_key: &CryptoTyped, - b_key: &CryptoTyped| + |a_key: &CryptoTyped, + b_key: &CryptoTyped| -> core::cmp::Ordering { - let da = vcrypto.distance(&a_key.value, &self.node_id.value); - let db = vcrypto.distance(&b_key.value, &self.node_id.value); + let da = + vcrypto.distance(&HashDigest::from(a_key.value), &self.hash_coordinate.value); + let db = + vcrypto.distance(&HashDigest::from(b_key.value), &self.hash_coordinate.value); da.cmp(&db) }, ); let context = Arc::new(Mutex::new(FanoutContext { fanout_queue: FanoutQueue::new( self.routing_table.registry(), - self.node_id.kind, + self.hash_coordinate.kind, node_sort, self.consensus_count, ), diff --git a/veilid-core/src/rpc_processor/fanout/fanout_queue.rs b/veilid-core/src/rpc_processor/fanout/fanout_queue.rs index 9380c5cc..9db3e7f6 100644 --- a/veilid-core/src/rpc_processor/fanout/fanout_queue.rs +++ b/veilid-core/src/rpc_processor/fanout/fanout_queue.rs @@ -32,7 +32,8 @@ pub struct FanoutNode { pub status: FanoutNodeStatus, } -pub type FanoutQueueSort<'a> = Box core::cmp::Ordering + Send + 'a>; +pub type FanoutQueueSort<'a> = + Box core::cmp::Ordering + Send + 'a>; pub struct FanoutQueue<'a> { /// Link back to veilid component registry for logging @@ -40,9 +41,9 @@ pub struct FanoutQueue<'a> { /// Crypto kind in use for this queue crypto_kind: CryptoKind, /// The status of all the nodes we have added so far - nodes: HashMap, + nodes: HashMap, /// Closer nodes to the record key are at the front of the list - sorted_nodes: Vec, + sorted_nodes: Vec, /// The sort function to use for the nodes node_sort: FanoutQueueSort<'a>, /// The channel to receive work requests to process @@ -264,7 +265,10 @@ impl<'a> FanoutQueue<'a> { } /// Review the nodes in the queue - pub fn with_nodes, &[TypedKey]) -> R>( + pub fn with_nodes< + R, + F: FnOnce(&HashMap, &[TypedPublicKey]) -> R, + >( &self, func: F, ) -> R { diff --git a/veilid-core/src/rpc_processor/message_header.rs b/veilid-core/src/rpc_processor/message_header.rs index d6d42313..fd732076 100644 --- a/veilid-core/src/rpc_processor/message_header.rs +++ b/veilid-core/src/rpc_processor/message_header.rs @@ -72,7 +72,7 @@ impl MessageHeader { RPCMessageHeaderDetail::PrivateRouted(p) => p.direct.routing_domain, } } - pub fn direct_sender_node_id(&self) -> TypedKey { + pub fn direct_sender_node_id(&self) -> TypedPublicKey { match &self.detail { RPCMessageHeaderDetail::Direct(d) => d.envelope.get_sender_typed_id(), RPCMessageHeaderDetail::SafetyRouted(s) => s.direct.envelope.get_sender_typed_id(), diff --git a/veilid-core/src/rpc_processor/mod.rs b/veilid-core/src/rpc_processor/mod.rs index c3054368..8725d5f0 100644 --- a/veilid-core/src/rpc_processor/mod.rs +++ b/veilid-core/src/rpc_processor/mod.rs @@ -303,7 +303,7 @@ impl RPCProcessor { fn process_sender_peer_info( &self, routing_domain: RoutingDomain, - sender_node_id: TypedKey, + sender_node_id: TypedPublicKey, sender_peer_info: &SenderPeerInfo, ) -> RPCNetworkResult> { let Some(peer_info) = sender_peer_info.opt_peer_info.clone() else { @@ -364,7 +364,7 @@ impl RPCProcessor { #[instrument(level = "trace", target = "rpc", skip_all)] async fn public_internet_peer_search( &self, - node_id: TypedKey, + node_id: TypedPublicKey, count: usize, fanout: usize, timeout_us: TimestampDuration, @@ -436,7 +436,7 @@ impl RPCProcessor { let routing_table = self.routing_table(); let fanout_call = FanoutCall::new( &routing_table, - node_id, + node_id.into(), count, fanout, 0, @@ -464,7 +464,7 @@ impl RPCProcessor { #[instrument(level = "trace", target = "rpc", skip_all)] pub fn resolve_node( &self, - node_id: TypedKey, + node_id: TypedPublicKey, safety_selection: SafetySelection, ) -> PinBoxFuture, RPCError>> { let registry = self.registry(); diff --git a/veilid-core/src/rpc_processor/rpc_find_node.rs b/veilid-core/src/rpc_processor/rpc_find_node.rs index 2d67549a..27170a39 100644 --- a/veilid-core/src/rpc_processor/rpc_find_node.rs +++ b/veilid-core/src/rpc_processor/rpc_find_node.rs @@ -13,7 +13,7 @@ impl RPCProcessor { pub async fn rpc_call_find_node( &self, dest: Destination, - node_id: TypedKey, + node_id: TypedPublicKey, capabilities: Vec, ) -> RPCNetworkResult>>> { let _guard = self diff --git a/veilid-core/src/rpc_processor/rpc_get_value.rs b/veilid-core/src/rpc_processor/rpc_get_value.rs index 204990ab..9feba0bb 100644 --- a/veilid-core/src/rpc_processor/rpc_get_value.rs +++ b/veilid-core/src/rpc_processor/rpc_get_value.rs @@ -28,7 +28,7 @@ impl RPCProcessor { pub async fn rpc_call_get_value( &self, dest: Destination, - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, last_descriptor: Option, ) -> RPCNetworkResult> { @@ -140,7 +140,12 @@ impl RPCProcessor { } // Validate peers returned are, in fact, closer to the key than the node we sent this to - let valid = match RoutingTable::verify_peers_closer(&vcrypto, target_node_id, key, &peers) { + let valid = match RoutingTable::verify_peers_closer( + &vcrypto, + target_node_id.into(), + key.into(), + &peers, + ) { Ok(v) => v, Err(e) => { return Ok(NetworkResult::invalid_message(format!( diff --git a/veilid-core/src/rpc_processor/rpc_inspect_value.rs b/veilid-core/src/rpc_processor/rpc_inspect_value.rs index 4dcfb158..6ce5df31 100644 --- a/veilid-core/src/rpc_processor/rpc_inspect_value.rs +++ b/veilid-core/src/rpc_processor/rpc_inspect_value.rs @@ -30,7 +30,7 @@ impl RPCProcessor { pub async fn rpc_call_inspect_value( &self, dest: Destination, - key: TypedKey, + key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, last_descriptor: Option, ) -> RPCNetworkResult> { @@ -135,7 +135,12 @@ impl RPCProcessor { } // Validate peers returned are, in fact, closer to the key than the node we sent this to - let valid = match RoutingTable::verify_peers_closer(&vcrypto, target_node_id, key, &peers) { + let valid = match RoutingTable::verify_peers_closer( + &vcrypto, + target_node_id.into(), + key.into(), + &peers, + ) { Ok(v) => v, Err(e) => { return Ok(NetworkResult::invalid_message(format!( diff --git a/veilid-core/src/rpc_processor/rpc_route.rs b/veilid-core/src/rpc_processor/rpc_route.rs index 2726449a..5d1db1b8 100644 --- a/veilid-core/src/rpc_processor/rpc_route.rs +++ b/veilid-core/src/rpc_processor/rpc_route.rs @@ -64,7 +64,7 @@ impl RPCProcessor { &self, routed_operation: RoutedOperation, next_route_node: RouteNode, - safety_route_public_key: TypedKey, + safety_route_public_key: TypedPublicKey, next_private_route: PrivateRoute, ) -> RPCNetworkResult<()> { // Make sure hop count makes sense @@ -116,7 +116,7 @@ impl RPCProcessor { detail: RPCMessageHeaderDetailDirect, vcrypto: &CryptoSystemGuard<'_>, routed_operation: RoutedOperation, - remote_sr_pubkey: TypedKey, + remote_sr_pubkey: TypedPublicKey, ) -> RPCNetworkResult<()> { // Now that things are valid, decrypt the routed operation with DEC(nonce, DH(the SR's public key, the PR's (or node's) secret) // xxx: punish nodes that send messages that fail to decrypt eventually? How to do this for safety routes? @@ -162,8 +162,8 @@ impl RPCProcessor { detail: RPCMessageHeaderDetailDirect, vcrypto: &CryptoSystemGuard<'_>, routed_operation: RoutedOperation, - remote_sr_pubkey: TypedKey, - pr_pubkey: TypedKey, + remote_sr_pubkey: TypedPublicKey, + pr_pubkey: TypedPublicKey, ) -> RPCNetworkResult<()> { // Get sender id of the peer with the crypto kind of the route let Some(sender_id) = detail.sender_noderef.node_ids().get(pr_pubkey.kind) else { @@ -237,8 +237,8 @@ impl RPCProcessor { detail: RPCMessageHeaderDetailDirect, vcrypto: &CryptoSystemGuard<'_>, routed_operation: RoutedOperation, - remote_sr_pubkey: TypedKey, - pr_pubkey: TypedKey, + remote_sr_pubkey: TypedPublicKey, + pr_pubkey: TypedPublicKey, ) -> RPCNetworkResult<()> { // If the private route public key is our node id, then this was sent via safety route to our node directly // so there will be no signatures to validate @@ -266,7 +266,7 @@ impl RPCProcessor { async fn process_private_route_first_hop( &self, mut routed_operation: RoutedOperation, - sr_pubkey: TypedKey, + sr_pubkey: TypedPublicKey, mut private_route: PrivateRoute, ) -> RPCNetworkResult<()> { let Some(pr_first_hop) = private_route.pop_first_hop() else { @@ -331,7 +331,7 @@ impl RPCProcessor { fn decrypt_private_route_hop_data( &self, route_hop_data: &RouteHopData, - pr_pubkey: &TypedKey, + pr_pubkey: &TypedPublicKey, routed_operation: &mut RoutedOperation, ) -> RPCNetworkResult { // Get crypto kind diff --git a/veilid-core/src/rpc_processor/rpc_set_value.rs b/veilid-core/src/rpc_processor/rpc_set_value.rs index 70e47ecc..506d15af 100644 --- a/veilid-core/src/rpc_processor/rpc_set_value.rs +++ b/veilid-core/src/rpc_processor/rpc_set_value.rs @@ -30,7 +30,7 @@ impl RPCProcessor { pub async fn rpc_call_set_value( &self, dest: Destination, - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, value: SignedValueData, descriptor: SignedValueDescriptor, @@ -154,7 +154,12 @@ impl RPCProcessor { } // Validate peers returned are, in fact, closer to the key than the node we sent this to - let valid = match RoutingTable::verify_peers_closer(&vcrypto, target_node_id, key, &peers) { + let valid = match RoutingTable::verify_peers_closer( + &vcrypto, + target_node_id.into(), + key.into(), + &peers, + ) { Ok(v) => v, Err(e) => { return Ok(NetworkResult::invalid_message(format!( diff --git a/veilid-core/src/rpc_processor/rpc_value_changed.rs b/veilid-core/src/rpc_processor/rpc_value_changed.rs index bd6202b8..649a80ba 100644 --- a/veilid-core/src/rpc_processor/rpc_value_changed.rs +++ b/veilid-core/src/rpc_processor/rpc_value_changed.rs @@ -9,7 +9,7 @@ impl RPCProcessor { pub async fn rpc_call_value_changed( &self, dest: Destination, - key: TypedKey, + key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, count: u32, watch_id: u64, @@ -63,7 +63,7 @@ impl RPCProcessor { )); } RPCMessageHeaderDetail::PrivateRouted(p) => { - TypedKey::new(p.direct.envelope.get_crypto_kind(), p.remote_safety_route) + TypedPublicKey::new(p.direct.envelope.get_crypto_kind(), p.remote_safety_route) } }; diff --git a/veilid-core/src/rpc_processor/rpc_watch_value.rs b/veilid-core/src/rpc_processor/rpc_watch_value.rs index b5d1c1c2..d548a2d0 100644 --- a/veilid-core/src/rpc_processor/rpc_watch_value.rs +++ b/veilid-core/src/rpc_processor/rpc_watch_value.rs @@ -27,7 +27,7 @@ impl RPCProcessor { pub async fn rpc_call_watch_value( &self, dest: Destination, - key: TypedKey, + key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, expiration: Timestamp, count: u32, @@ -153,7 +153,12 @@ impl RPCProcessor { } // Validate peers returned are, in fact, closer to the key than the node we sent this to - let valid = match RoutingTable::verify_peers_closer(&vcrypto, target_node_id, key, &peers) { + let valid = match RoutingTable::verify_peers_closer( + &vcrypto, + target_node_id.into(), + key.into(), + &peers, + ) { Ok(v) => v, Err(e) => { return Ok(NetworkResult::invalid_message(format!( diff --git a/veilid-core/src/storage_manager/debug.rs b/veilid-core/src/storage_manager/debug.rs index e3254f2f..9a395aa8 100644 --- a/veilid-core/src/storage_manager/debug.rs +++ b/veilid-core/src/storage_manager/debug.rs @@ -79,7 +79,7 @@ impl StorageManager { pub async fn debug_local_record_subkey_info( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkey: ValueSubkey, ) -> String { let inner = self.inner.lock().await; @@ -92,7 +92,7 @@ impl StorageManager { } pub async fn debug_remote_record_subkey_info( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkey: ValueSubkey, ) -> String { let inner = self.inner.lock().await; @@ -103,7 +103,7 @@ impl StorageManager { .debug_record_subkey_info(record_key, subkey) .await } - pub async fn debug_local_record_info(&self, record_key: TypedKey) -> String { + pub async fn debug_local_record_info(&self, record_key: TypedRecordKey) -> String { let inner = self.inner.lock().await; let Some(local_record_store) = &inner.local_record_store else { return "not initialized".to_owned(); @@ -119,7 +119,7 @@ impl StorageManager { format!("{}\n{}", local_debug, opened_debug) } - pub async fn debug_remote_record_info(&self, record_key: TypedKey) -> String { + pub async fn debug_remote_record_info(&self, record_key: TypedRecordKey) -> String { let inner = self.inner.lock().await; let Some(remote_record_store) = &inner.remote_record_store else { return "not initialized".to_owned(); diff --git a/veilid-core/src/storage_manager/get_value.rs b/veilid-core/src/storage_manager/get_value.rs index a9ee6802..4053e08c 100644 --- a/veilid-core/src/storage_manager/get_value.rs +++ b/veilid-core/src/storage_manager/get_value.rs @@ -28,7 +28,7 @@ impl StorageManager { #[instrument(level = "trace", target = "dht", skip_all, err)] pub(super) async fn outbound_get_value( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkey: ValueSubkey, safety_selection: SafetySelection, last_get_result: GetResult, @@ -255,7 +255,7 @@ impl StorageManager { let routing_table = registry.routing_table(); let fanout_call = FanoutCall::new( &routing_table, - record_key, + record_key.into(), key_count, fanout, consensus_count, @@ -306,7 +306,7 @@ impl StorageManager { pub(super) fn process_deferred_outbound_get_value_result( &self, res_rx: flume::Receiver>, - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, last_seq: ValueSeqNum, ) { @@ -358,7 +358,7 @@ impl StorageManager { #[instrument(level = "trace", target = "dht", skip_all)] pub(super) async fn process_outbound_get_value_result( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkey: ValueSubkey, opt_last_seq: Option, result: get_value::OutboundGetValueResult, @@ -406,7 +406,7 @@ impl StorageManager { #[instrument(level = "trace", target = "dht", skip_all)] pub async fn inbound_get_value( &self, - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, want_descriptor: bool, ) -> VeilidAPIResult> { diff --git a/veilid-core/src/storage_manager/inspect_value.rs b/veilid-core/src/storage_manager/inspect_value.rs index f69c9537..a04fdfc1 100644 --- a/veilid-core/src/storage_manager/inspect_value.rs +++ b/veilid-core/src/storage_manager/inspect_value.rs @@ -57,7 +57,7 @@ impl StorageManager { #[instrument(level = "trace", target = "dht", skip_all, err)] pub(super) async fn outbound_inspect_value( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, safety_selection: SafetySelection, local_inspect_result: InspectResult, @@ -290,7 +290,7 @@ impl StorageManager { let routing_table = self.routing_table(); let fanout_call = FanoutCall::new( &routing_table, - record_key, + record_key.into(), key_count, fanout, consensus_count, @@ -358,7 +358,7 @@ impl StorageManager { #[instrument(level = "trace", target = "dht", skip_all)] pub async fn inbound_inspect_value( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, want_descriptor: bool, ) -> VeilidAPIResult> { diff --git a/veilid-core/src/storage_manager/mod.rs b/veilid-core/src/storage_manager/mod.rs index a59c004c..5669f85b 100644 --- a/veilid-core/src/storage_manager/mod.rs +++ b/veilid-core/src/storage_manager/mod.rs @@ -65,7 +65,7 @@ const REHYDRATION_REQUESTS: &[u8] = b"rehydration_requests"; /// A single 'value changed' message to send struct ValueChangedInfo { target: Target, - record_key: TypedKey, + record_key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, count: u32, watch_id: u64, @@ -76,18 +76,18 @@ struct ValueChangedInfo { #[derive(Default)] struct StorageManagerInner { /// Records that have been 'opened' and are not yet closed - pub opened_records: HashMap, + pub opened_records: HashMap, /// Records that have ever been 'created' or 'opened' by this node, things we care about that we must republish to keep alive pub local_record_store: Option>, /// Records that have been pushed to this node for distribution by other nodes, that we make an effort to republish pub remote_record_store: Option>, /// Record subkeys that have not been pushed to the network because they were written to offline pub offline_subkey_writes: - LinkedHashMap, + LinkedHashMap, /// Record subkeys that are currently being written to in the foreground - pub active_subkey_writes: HashMap, + pub active_subkey_writes: HashMap, /// Records that have rehydration requests - pub rehydration_requests: HashMap, + pub rehydration_requests: HashMap, /// State management for outbound watches pub outbound_watch_manager: OutboundWatchManager, /// Storage manager metadata that is persistent, including copy of offline subkey writes @@ -137,7 +137,7 @@ pub(crate) struct StorageManager { // Outbound watch operation lock // Keeps changes to watches to one-at-a-time per record - outbound_watch_lock_table: AsyncTagLockTable, + outbound_watch_lock_table: AsyncTagLockTable, // Background operation processor // for offline subkey writes, watch changes, and any other @@ -492,7 +492,7 @@ impl StorageManager { kind: CryptoKind, schema: DHTSchema, owner_key: &PublicKey, - ) -> VeilidAPIResult { + ) -> VeilidAPIResult { // Get cryptosystem let crypto = self.crypto(); let Some(vcrypto) = crypto.get(kind) else { @@ -536,7 +536,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip_all)] pub async fn open_record( &self, - record_key: TypedKey, + record_key: TypedRecordKey, writer: Option, safety_selection: SafetySelection, ) -> VeilidAPIResult { @@ -615,7 +615,7 @@ impl StorageManager { /// Close an opened local record #[instrument(level = "trace", target = "stor", skip_all)] - pub async fn close_record(&self, record_key: TypedKey) -> VeilidAPIResult<()> { + pub async fn close_record(&self, record_key: TypedRecordKey) -> VeilidAPIResult<()> { // Attempt to close the record, returning the opened record if it wasn't already closed let mut inner = self.inner.lock().await; Self::close_record_inner(&mut inner, record_key)?; @@ -637,7 +637,7 @@ impl StorageManager { /// Delete a local record #[instrument(level = "trace", target = "stor", skip_all)] - pub async fn delete_record(&self, record_key: TypedKey) -> VeilidAPIResult<()> { + pub async fn delete_record(&self, record_key: TypedRecordKey) -> VeilidAPIResult<()> { // Ensure the record is closed let mut inner = self.inner.lock().await; Self::close_record_inner(&mut inner, record_key)?; @@ -655,7 +655,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip_all)] pub async fn get_value( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkey: ValueSubkey, force_refresh: bool, ) -> VeilidAPIResult> { @@ -731,7 +731,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip_all)] pub async fn set_value( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkey: ValueSubkey, data: Vec, writer: Option, @@ -934,7 +934,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip_all)] pub async fn watch_values( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, expiration: Timestamp, count: u32, @@ -950,7 +950,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip_all)] async fn watch_values_inner( &self, - watch_lock: AsyncTagLockGuard, + watch_lock: AsyncTagLockGuard, subkeys: ValueSubkeyRangeSet, expiration: Timestamp, count: u32, @@ -1034,7 +1034,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip_all)] pub async fn cancel_watch_values( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, ) -> VeilidAPIResult { // Obtain the watch change lock @@ -1102,7 +1102,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip_all)] pub async fn inspect_record( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, scope: DHTReportScope, ) -> VeilidAPIResult { @@ -1263,7 +1263,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip(self, value))] fn update_callback_value_change( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, count: u32, value: Option, @@ -1280,7 +1280,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip_all)] fn check_fanout_set_offline( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkey: ValueSubkey, fanout_result: &FanoutResult, ) -> bool { @@ -1333,7 +1333,7 @@ impl StorageManager { schema: DHTSchema, owner: Option, safety_selection: SafetySelection, - ) -> VeilidAPIResult<(TypedKey, KeyPair)> { + ) -> VeilidAPIResult<(TypedRecordKey, KeyPair)> { // Get cryptosystem let crypto = self.crypto(); let Some(vcrypto) = crypto.get(kind) else { @@ -1392,7 +1392,7 @@ impl StorageManager { async fn move_remote_record_to_local_inner( &self, inner: &mut StorageManagerInner, - record_key: TypedKey, + record_key: TypedRecordKey, safety_selection: SafetySelection, ) -> VeilidAPIResult> { // Get local record store @@ -1467,7 +1467,7 @@ impl StorageManager { pub async fn open_existing_record_inner( &self, inner: &mut StorageManagerInner, - record_key: TypedKey, + record_key: TypedRecordKey, writer: Option, safety_selection: SafetySelection, ) -> VeilidAPIResult> { @@ -1534,7 +1534,7 @@ impl StorageManager { pub async fn open_new_record_inner( &self, inner: &mut StorageManagerInner, - record_key: TypedKey, + record_key: TypedRecordKey, writer: Option, inspect_result: InspectResult, safety_selection: SafetySelection, @@ -1591,7 +1591,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip_all, err)] pub async fn get_value_nodes( &self, - record_key: TypedKey, + record_key: TypedRecordKey, ) -> VeilidAPIResult>> { let inner = self.inner.lock().await; // Get local record store @@ -1609,7 +1609,7 @@ impl StorageManager { .copied() .filter_map(|x| { routing_table - .lookup_node_ref(TypedKey::new(record_key.kind, x)) + .lookup_node_ref(TypedPublicKey::new(record_key.kind, x)) .ok() .flatten() }) @@ -1625,7 +1625,7 @@ impl StorageManager { >( inner: &mut StorageManagerInner, vcrypto: &CryptoSystemGuard<'_>, - record_key: TypedKey, + record_key: TypedRecordKey, subkey_results_iter: I, is_set: bool, consensus_count: usize, @@ -1665,8 +1665,10 @@ impl StorageManager { return res; } // Distance is the next metric, closer nodes first - let da = vcrypto.distance(&a.0, &record_key.value); - let db = vcrypto.distance(&b.0, &record_key.value); + let da = + vcrypto.distance(&HashDigest::from(a.0), &HashDigest::from(record_key.value)); + let db = + vcrypto.distance(&HashDigest::from(b.0), &HashDigest::from(record_key.value)); da.cmp(&db) }); @@ -1678,7 +1680,7 @@ impl StorageManager { fn close_record_inner( inner: &mut StorageManagerInner, - record_key: TypedKey, + record_key: TypedRecordKey, ) -> VeilidAPIResult<()> { let Some(local_record_store) = inner.local_record_store.as_mut() else { apibail_not_initialized!(); @@ -1701,7 +1703,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip_all, err)] async fn handle_get_local_value_inner( inner: &mut StorageManagerInner, - record_key: TypedKey, + record_key: TypedRecordKey, subkey: ValueSubkey, want_descriptor: bool, ) -> VeilidAPIResult { @@ -1725,7 +1727,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip_all, err)] pub(super) async fn handle_set_local_value_inner( inner: &mut StorageManagerInner, - record_key: TypedKey, + record_key: TypedRecordKey, subkey: ValueSubkey, signed_value_data: Arc, watch_update_mode: InboundWatchUpdateMode, @@ -1747,7 +1749,7 @@ impl StorageManager { pub(super) async fn handle_inspect_local_value_inner( &self, inner: &mut StorageManagerInner, - record_key: TypedKey, + record_key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, want_descriptor: bool, ) -> VeilidAPIResult { @@ -1775,7 +1777,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip_all, err)] pub(super) async fn handle_get_remote_value_inner( inner: &mut StorageManagerInner, - record_key: TypedKey, + record_key: TypedRecordKey, subkey: ValueSubkey, want_descriptor: bool, ) -> VeilidAPIResult { @@ -1799,7 +1801,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip_all, err)] pub(super) async fn handle_set_remote_value_inner( inner: &mut StorageManagerInner, - record_key: TypedKey, + record_key: TypedRecordKey, subkey: ValueSubkey, signed_value_data: Arc, signed_value_descriptor: Arc, @@ -1838,7 +1840,7 @@ impl StorageManager { pub(super) async fn handle_inspect_remote_value_inner( &self, inner: &mut StorageManagerInner, - record_key: TypedKey, + record_key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, want_descriptor: bool, ) -> VeilidAPIResult { @@ -1867,19 +1869,19 @@ impl StorageManager { vcrypto: &CryptoSystemGuard<'_>, owner_key: &PublicKey, schema_data: &[u8], - ) -> TypedKey { + ) -> TypedRecordKey { let mut hash_data = Vec::::with_capacity(PUBLIC_KEY_LENGTH + 4 + schema_data.len()); hash_data.extend_from_slice(&vcrypto.kind().0); hash_data.extend_from_slice(&owner_key.bytes); hash_data.extend_from_slice(schema_data); let hash = vcrypto.generate_hash(&hash_data); - TypedKey::new(vcrypto.kind(), hash) + TypedRecordKey::new(vcrypto.kind(), RecordKey::from(hash)) } #[instrument(level = "trace", target = "stor", skip_all)] pub(super) fn add_offline_subkey_write_inner( inner: &mut StorageManagerInner, - record_key: TypedKey, + record_key: TypedRecordKey, subkey: ValueSubkey, safety_selection: SafetySelection, ) { diff --git a/veilid-core/src/storage_manager/outbound_watch_manager/mod.rs b/veilid-core/src/storage_manager/outbound_watch_manager/mod.rs index 7b691739..8cd31c72 100644 --- a/veilid-core/src/storage_manager/outbound_watch_manager/mod.rs +++ b/veilid-core/src/storage_manager/outbound_watch_manager/mod.rs @@ -19,13 +19,13 @@ impl_veilid_log_facility!("stor"); pub(in crate::storage_manager) struct OutboundWatchManager { /// Each watch per record key #[serde(skip)] - pub outbound_watches: HashMap, + pub outbound_watches: HashMap, /// Last known active watch per node+record #[serde_as(as = "Vec<(_, _)>")] pub per_node_states: HashMap, /// Value changed updates that need inpection to determine if they should be reported #[serde(skip)] - pub needs_change_inspection: HashMap, + pub needs_change_inspection: HashMap, } impl fmt::Display for OutboundWatchManager { @@ -116,7 +116,7 @@ impl OutboundWatchManager { pub fn set_desired_watch( &mut self, - record_key: TypedKey, + record_key: TypedRecordKey, desired_watch: Option, ) { match self.outbound_watches.get_mut(&record_key) { @@ -139,7 +139,7 @@ impl OutboundWatchManager { } } - pub fn set_next_reconcile_ts(&mut self, record_key: TypedKey, next_ts: Timestamp) { + pub fn set_next_reconcile_ts(&mut self, record_key: TypedRecordKey, next_ts: Timestamp) { if let Some(outbound_watch) = self.outbound_watches.get_mut(&record_key) { if let Some(state) = outbound_watch.state_mut() { state.edit(&self.per_node_states, |editor| { @@ -204,7 +204,11 @@ impl OutboundWatchManager { } /// Set a record up to be inspected for changed subkeys - pub fn enqueue_change_inspect(&mut self, record_key: TypedKey, subkeys: ValueSubkeyRangeSet) { + pub fn enqueue_change_inspect( + &mut self, + record_key: TypedRecordKey, + subkeys: ValueSubkeyRangeSet, + ) { self.needs_change_inspection .entry(record_key) .and_modify(|x| *x = x.union(&subkeys)) diff --git a/veilid-core/src/storage_manager/outbound_watch_manager/outbound_watch.rs b/veilid-core/src/storage_manager/outbound_watch_manager/outbound_watch.rs index b6fcd4a0..4ce870df 100644 --- a/veilid-core/src/storage_manager/outbound_watch_manager/outbound_watch.rs +++ b/veilid-core/src/storage_manager/outbound_watch_manager/outbound_watch.rs @@ -5,7 +5,7 @@ impl_veilid_log_facility!("stor"); #[derive(Clone, Debug, Serialize, Deserialize)] pub(in crate::storage_manager) struct OutboundWatch { /// Record key being watched - record_key: TypedKey, + record_key: TypedRecordKey, /// Current state /// None means inactive/cancelled @@ -37,7 +37,7 @@ impl fmt::Display for OutboundWatch { impl OutboundWatch { /// Create new outbound watch with desired parameters - pub fn new(record_key: TypedKey, desired: OutboundWatchParameters) -> Self { + pub fn new(record_key: TypedRecordKey, desired: OutboundWatchParameters) -> Self { Self { record_key, state: None, diff --git a/veilid-core/src/storage_manager/outbound_watch_manager/per_node_state.rs b/veilid-core/src/storage_manager/outbound_watch_manager/per_node_state.rs index e18509c9..fc5a032b 100644 --- a/veilid-core/src/storage_manager/outbound_watch_manager/per_node_state.rs +++ b/veilid-core/src/storage_manager/outbound_watch_manager/per_node_state.rs @@ -5,9 +5,9 @@ impl_veilid_log_facility!("stor"); #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)] pub(in crate::storage_manager) struct PerNodeKey { /// Watched record key - pub record_key: TypedKey, + pub record_key: TypedRecordKey, /// Watching node id - pub node_id: TypedKey, + pub node_id: TypedPublicKey, } impl fmt::Display for PerNodeKey { @@ -23,8 +23,8 @@ impl FromStr for PerNodeKey { .split_once('@') .ok_or_else(|| VeilidAPIError::parse_error("invalid per-node key", s))?; Ok(PerNodeKey { - record_key: TypedKey::from_str(rkey)?, - node_id: TypedKey::from_str(nid)?, + record_key: TypedRecordKey::from_str(rkey)?, + node_id: TypedPublicKey::from_str(nid)?, }) } } diff --git a/veilid-core/src/storage_manager/record_store/inspect_cache.rs b/veilid-core/src/storage_manager/record_store/inspect_cache.rs index 469dafef..a84d3725 100644 --- a/veilid-core/src/storage_manager/record_store/inspect_cache.rs +++ b/veilid-core/src/storage_manager/record_store/inspect_cache.rs @@ -22,7 +22,7 @@ impl InspectCacheL2 { #[derive(Debug)] pub struct InspectCache { - cache: LruCache, + cache: LruCache, } impl InspectCache { @@ -34,7 +34,7 @@ impl InspectCache { pub fn get( &mut self, - key: &TypedKey, + key: &TypedRecordKey, subkeys: &ValueSubkeyRangeSet, ) -> Option { if let Some(l2c) = self.cache.get_mut(key) { @@ -45,7 +45,12 @@ impl InspectCache { None } - pub fn put(&mut self, key: TypedKey, subkeys: ValueSubkeyRangeSet, value: InspectCacheL2Value) { + pub fn put( + &mut self, + key: TypedRecordKey, + subkeys: ValueSubkeyRangeSet, + value: InspectCacheL2Value, + ) { self.cache .entry(key) .or_insert_with(|| InspectCacheL2::new(L2_CACHE_DEPTH)) @@ -53,11 +58,16 @@ impl InspectCache { .insert(subkeys, value); } - pub fn invalidate(&mut self, key: &TypedKey) { + pub fn invalidate(&mut self, key: &TypedRecordKey) { self.cache.remove(key); } - pub fn replace_subkey_seq(&mut self, key: &TypedKey, subkey: ValueSubkey, seq: ValueSeqNum) { + pub fn replace_subkey_seq( + &mut self, + key: &TypedRecordKey, + subkey: ValueSubkey, + seq: ValueSeqNum, + ) { let Some(l2) = self.cache.get_mut(key) else { return; }; diff --git a/veilid-core/src/storage_manager/record_store/keys.rs b/veilid-core/src/storage_manager/record_store/keys.rs index 547e4aa9..99302f43 100644 --- a/veilid-core/src/storage_manager/record_store/keys.rs +++ b/veilid-core/src/storage_manager/record_store/keys.rs @@ -2,13 +2,13 @@ use super::*; #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct RecordTableKey { - pub key: TypedKey, + pub key: TypedRecordKey, } impl RecordTableKey { - pub fn bytes(&self) -> [u8; PUBLIC_KEY_LENGTH + 4] { - let mut bytes = [0u8; PUBLIC_KEY_LENGTH + 4]; + pub fn bytes(&self) -> [u8; HASH_DIGEST_LENGTH + 4] { + let mut bytes = [0u8; HASH_DIGEST_LENGTH + 4]; bytes[0..4].copy_from_slice(&self.key.kind.0); - bytes[4..PUBLIC_KEY_LENGTH + 4].copy_from_slice(&self.key.value.bytes); + bytes[4..HASH_DIGEST_LENGTH + 4].copy_from_slice(&self.key.value.bytes); bytes } } @@ -16,28 +16,28 @@ impl RecordTableKey { impl TryFrom<&[u8]> for RecordTableKey { type Error = EyreReport; fn try_from(bytes: &[u8]) -> Result { - if bytes.len() != PUBLIC_KEY_LENGTH + 4 { + if bytes.len() != HASH_DIGEST_LENGTH + 4 { bail!("invalid bytes length"); } let kind = FourCC::try_from(&bytes[0..4]).wrap_err("invalid kind")?; let value = - PublicKey::try_from(&bytes[4..PUBLIC_KEY_LENGTH + 4]).wrap_err("invalid value")?; - let key = TypedKey::new(kind, value); + RecordKey::try_from(&bytes[4..HASH_DIGEST_LENGTH + 4]).wrap_err("invalid value")?; + let key = TypedRecordKey::new(kind, value); Ok(RecordTableKey { key }) } } #[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] pub struct SubkeyTableKey { - pub key: TypedKey, + pub key: TypedRecordKey, pub subkey: ValueSubkey, } impl SubkeyTableKey { - pub fn bytes(&self) -> [u8; PUBLIC_KEY_LENGTH + 4 + 4] { - let mut bytes = [0u8; PUBLIC_KEY_LENGTH + 4 + 4]; + pub fn bytes(&self) -> [u8; HASH_DIGEST_LENGTH + 4 + 4] { + let mut bytes = [0u8; HASH_DIGEST_LENGTH + 4 + 4]; bytes[0..4].copy_from_slice(&self.key.kind.0); - bytes[4..PUBLIC_KEY_LENGTH + 4].copy_from_slice(&self.key.value.bytes); - bytes[PUBLIC_KEY_LENGTH + 4..PUBLIC_KEY_LENGTH + 4 + 4] + bytes[4..HASH_DIGEST_LENGTH + 4].copy_from_slice(&self.key.value.bytes); + bytes[HASH_DIGEST_LENGTH + 4..HASH_DIGEST_LENGTH + 4 + 4] .copy_from_slice(&self.subkey.to_le_bytes()); bytes } @@ -45,19 +45,19 @@ impl SubkeyTableKey { impl TryFrom<&[u8]> for SubkeyTableKey { type Error = EyreReport; fn try_from(bytes: &[u8]) -> Result { - if bytes.len() != PUBLIC_KEY_LENGTH + 4 { + if bytes.len() != HASH_DIGEST_LENGTH + 4 { bail!("invalid bytes length"); } let kind = FourCC::try_from(&bytes[0..4]).wrap_err("invalid kind")?; let value = - PublicKey::try_from(&bytes[4..PUBLIC_KEY_LENGTH + 4]).wrap_err("invalid value")?; + RecordKey::try_from(&bytes[4..HASH_DIGEST_LENGTH + 4]).wrap_err("invalid value")?; let subkey = ValueSubkey::from_le_bytes( - bytes[PUBLIC_KEY_LENGTH + 4..PUBLIC_KEY_LENGTH + 4 + 4] + bytes[HASH_DIGEST_LENGTH + 4..HASH_DIGEST_LENGTH + 4 + 4] .try_into() .wrap_err("invalid subkey")?, ); - let key = TypedKey::new(kind, value); + let key = TypedRecordKey::new(kind, value); Ok(SubkeyTableKey { key, subkey }) } } diff --git a/veilid-core/src/storage_manager/record_store/mod.rs b/veilid-core/src/storage_manager/record_store/mod.rs index 15cf8aba..2caf5dff 100644 --- a/veilid-core/src/storage_manager/record_store/mod.rs +++ b/veilid-core/src/storage_manager/record_store/mod.rs @@ -469,7 +469,11 @@ where } #[instrument(level = "trace", target = "stor", skip_all, err)] - pub async fn new_record(&mut self, key: TypedKey, record: Record) -> VeilidAPIResult<()> { + pub async fn new_record( + &mut self, + key: TypedRecordKey, + record: Record, + ) -> VeilidAPIResult<()> { let rtk = RecordTableKey { key }; if self.record_index.contains_key(&rtk) { apibail_internal!("record already exists"); @@ -510,7 +514,7 @@ where } #[instrument(level = "trace", target = "stor", skip_all, err)] - pub async fn delete_record(&mut self, key: TypedKey) -> VeilidAPIResult<()> { + pub async fn delete_record(&mut self, key: TypedRecordKey) -> VeilidAPIResult<()> { // Get the record table key let rtk = RecordTableKey { key }; @@ -536,13 +540,13 @@ where } #[instrument(level = "trace", target = "stor", skip_all)] - pub(super) fn contains_record(&mut self, key: TypedKey) -> bool { + pub(super) fn contains_record(&mut self, key: TypedRecordKey) -> bool { let rtk = RecordTableKey { key }; self.record_index.contains_key(&rtk) } #[instrument(level = "trace", target = "stor", skip_all)] - pub(super) fn with_record(&mut self, key: TypedKey, f: F) -> Option + pub(super) fn with_record(&mut self, key: TypedRecordKey, f: F) -> Option where F: FnOnce(&Record) -> R, { @@ -566,7 +570,7 @@ where } #[instrument(level = "trace", target = "stor", skip_all)] - pub(super) fn peek_record(&self, key: TypedKey, f: F) -> Option + pub(super) fn peek_record(&self, key: TypedRecordKey, f: F) -> Option where F: FnOnce(&Record) -> R, { @@ -581,7 +585,7 @@ where } #[instrument(level = "trace", target = "stor", skip_all)] - pub(super) fn with_record_mut(&mut self, key: TypedKey, f: F) -> Option + pub(super) fn with_record_mut(&mut self, key: TypedRecordKey, f: F) -> Option where F: FnOnce(&mut Record) -> R, { @@ -607,7 +611,7 @@ where #[instrument(level = "trace", target = "stor", skip_all, err)] pub async fn get_subkey( &mut self, - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, want_descriptor: bool, ) -> VeilidAPIResult> { @@ -675,7 +679,7 @@ where #[instrument(level = "trace", target = "stor", skip_all, err)] pub async fn peek_subkey( &self, - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, want_descriptor: bool, ) -> VeilidAPIResult> { @@ -740,7 +744,7 @@ where #[instrument(level = "trace", target = "stor", skip_all)] async fn update_watched_value( &mut self, - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, watch_update_mode: InboundWatchUpdateMode, ) { @@ -779,7 +783,7 @@ where #[instrument(level = "trace", target = "stor", skip_all, err)] pub async fn set_subkey( &mut self, - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, signed_value_data: Arc, watch_update_mode: InboundWatchUpdateMode, @@ -883,7 +887,7 @@ where #[instrument(level = "trace", target = "stor", skip_all, err)] pub async fn inspect_record( &mut self, - key: TypedKey, + key: TypedRecordKey, subkeys: &ValueSubkeyRangeSet, want_descriptor: bool, ) -> VeilidAPIResult> { @@ -967,7 +971,7 @@ where #[instrument(level = "trace", target = "stor", skip_all, err)] pub async fn _change_existing_watch( &mut self, - key: TypedKey, + key: TypedRecordKey, params: InboundWatchParameters, watch_id: u64, ) -> VeilidAPIResult { @@ -1004,7 +1008,7 @@ where #[instrument(level = "trace", target = "stor", skip_all, err)] pub async fn _create_new_watch( &mut self, - key: TypedKey, + key: TypedRecordKey, params: InboundWatchParameters, member_check: Box bool + Send>, ) -> VeilidAPIResult { @@ -1095,7 +1099,7 @@ where #[instrument(level = "trace", target = "stor", skip_all, err)] pub async fn watch_record( &mut self, - key: TypedKey, + key: TypedRecordKey, mut params: InboundWatchParameters, opt_watch_id: Option, ) -> VeilidAPIResult { @@ -1153,7 +1157,7 @@ where #[instrument(level = "trace", target = "stor", skip_all, err)] async fn cancel_watch( &mut self, - key: TypedKey, + key: TypedRecordKey, watch_id: u64, watcher: PublicKey, ) -> VeilidAPIResult { @@ -1193,7 +1197,7 @@ where #[instrument(level = "trace", target = "stor", skip_all)] pub fn move_watches( &mut self, - key: TypedKey, + key: TypedRecordKey, in_watch: Option<(InboundWatchList, bool)>, ) -> Option<(InboundWatchList, bool)> { let rtk = RecordTableKey { key }; @@ -1231,7 +1235,7 @@ where // ValueChangedInfo but without the subkey data that requires a double mutable borrow to get struct EarlyValueChangedInfo { target: Target, - key: TypedKey, + key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, count: u32, watch_id: u64, @@ -1369,7 +1373,7 @@ where out } - pub fn debug_record_info(&self, key: TypedKey) -> String { + pub fn debug_record_info(&self, key: TypedRecordKey) -> String { let record_info = self .peek_record(key, |r| format!("{:#?}", r)) .unwrap_or("Not found".to_owned()); @@ -1382,7 +1386,11 @@ where format!("{}\n{}\n", record_info, watched_record) } - pub async fn debug_record_subkey_info(&self, key: TypedKey, subkey: ValueSubkey) -> String { + pub async fn debug_record_subkey_info( + &self, + key: TypedRecordKey, + subkey: ValueSubkey, + ) -> String { match self.peek_subkey(key, subkey, true).await { Ok(Some(v)) => { format!("{:#?}", v) diff --git a/veilid-core/src/storage_manager/rehydrate.rs b/veilid-core/src/storage_manager/rehydrate.rs index d85f1263..afb39cc9 100644 --- a/veilid-core/src/storage_manager/rehydrate.rs +++ b/veilid-core/src/storage_manager/rehydrate.rs @@ -4,7 +4,7 @@ use super::{inspect_value::OutboundInspectValueResult, *}; #[derive(Debug, Clone)] pub struct RehydrateReport { /// The record key rehydrated - record_key: TypedKey, + record_key: TypedRecordKey, /// The requested range of subkeys to rehydrate if necessary subkeys: ValueSubkeyRangeSet, /// The requested consensus count, @@ -24,7 +24,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip_all)] pub async fn add_rehydration_request( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, consensus_count: usize, ) { @@ -55,7 +55,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip(self), ret, err)] pub(super) async fn rehydrate_record( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, consensus_count: usize, ) -> VeilidAPIResult { @@ -143,7 +143,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip(self), ret, err)] pub(super) async fn rehydrate_all_subkeys( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, consensus_count: usize, safety_selection: SafetySelection, @@ -185,7 +185,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip(self), ret, err)] pub(super) async fn rehydrate_required_subkeys( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, consensus_count: usize, safety_selection: SafetySelection, diff --git a/veilid-core/src/storage_manager/set_value.rs b/veilid-core/src/storage_manager/set_value.rs index 6354d3e5..50ae4490 100644 --- a/veilid-core/src/storage_manager/set_value.rs +++ b/veilid-core/src/storage_manager/set_value.rs @@ -28,7 +28,7 @@ impl StorageManager { #[instrument(level = "trace", target = "dht", skip_all, err)] pub(super) async fn outbound_set_value( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkey: ValueSubkey, safety_selection: SafetySelection, value: Arc, @@ -228,7 +228,7 @@ impl StorageManager { let routing_table = registry.routing_table(); let fanout_call = FanoutCall::new( &routing_table, - record_key, + record_key.into(), key_count, fanout, consensus_count, @@ -277,7 +277,7 @@ impl StorageManager { pub(super) fn process_deferred_outbound_set_value_result( &self, res_rx: flume::Receiver>, - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, last_value_data: ValueData, safety_selection: SafetySelection, @@ -343,7 +343,7 @@ impl StorageManager { #[instrument(level = "trace", target = "stor", skip_all, err)] pub(super) async fn process_outbound_set_value_result( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkey: ValueSubkey, last_value_data: ValueData, safety_selection: SafetySelection, @@ -402,7 +402,7 @@ impl StorageManager { #[instrument(level = "trace", target = "dht", skip_all)] pub async fn inbound_set_value( &self, - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, value: Arc, descriptor: Option>, diff --git a/veilid-core/src/storage_manager/tasks/offline_subkey_writes.rs b/veilid-core/src/storage_manager/tasks/offline_subkey_writes.rs index 7c0f1ab4..18a7a599 100644 --- a/veilid-core/src/storage_manager/tasks/offline_subkey_writes.rs +++ b/veilid-core/src/storage_manager/tasks/offline_subkey_writes.rs @@ -21,7 +21,7 @@ enum OfflineSubkeyWriteResult { #[derive(Debug)] struct WorkItem { - record_key: TypedKey, + record_key: TypedRecordKey, safety_selection: SafetySelection, subkeys: ValueSubkeyRangeSet, } @@ -39,7 +39,7 @@ impl StorageManager { async fn write_single_offline_subkey( &self, stop_token: StopToken, - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, safety_selection: SafetySelection, ) -> EyreResult { diff --git a/veilid-core/src/storage_manager/watch_value.rs b/veilid-core/src/storage_manager/watch_value.rs index b4f0e46a..86f4ad75 100644 --- a/veilid-core/src/storage_manager/watch_value.rs +++ b/veilid-core/src/storage_manager/watch_value.rs @@ -47,7 +47,7 @@ impl StorageManager { #[instrument(target = "watch", level = "debug", skip_all, err)] pub(super) async fn outbound_watch_value_cancel( &self, - watch_lock: AsyncTagLockGuard, + watch_lock: AsyncTagLockGuard, opt_watcher: Option, safety_selection: SafetySelection, watch_node: NodeRef, @@ -95,7 +95,7 @@ impl StorageManager { #[instrument(target = "watch", level = "debug", skip_all, err)] pub(super) async fn outbound_watch_value_change( &self, - watch_lock: AsyncTagLockGuard, + watch_lock: AsyncTagLockGuard, params: OutboundWatchParameters, watch_node: NodeRef, watch_id: u64, @@ -165,7 +165,7 @@ impl StorageManager { #[instrument(target = "watch", level = "debug", skip_all, err)] pub(super) async fn outbound_watch_value( &self, - watch_lock: AsyncTagLockGuard, + watch_lock: AsyncTagLockGuard, params: OutboundWatchParameters, per_node_state: HashMap, ) -> VeilidAPIResult { @@ -332,7 +332,7 @@ impl StorageManager { let routing_table = self.routing_table(); let fanout_call = FanoutCall::new( &routing_table, - record_key, + record_key.into(), key_count, fanout, consensus_count, @@ -374,7 +374,7 @@ impl StorageManager { /// Remove dead watches from the table pub(super) async fn process_outbound_watch_dead( &self, - watch_lock: AsyncTagLockGuard, + watch_lock: AsyncTagLockGuard, ) { let record_key = watch_lock.tag(); @@ -404,7 +404,7 @@ impl StorageManager { /// and call their nodes to cancel the watch pub(super) async fn process_outbound_watch_cancel( &self, - watch_lock: AsyncTagLockGuard, + watch_lock: AsyncTagLockGuard, ) { let record_key = watch_lock.tag(); @@ -520,7 +520,7 @@ impl StorageManager { /// and drop the ones that can't be or are dead pub(super) async fn process_outbound_watch_renew( &self, - watch_lock: AsyncTagLockGuard, + watch_lock: AsyncTagLockGuard, ) { let record_key = watch_lock.tag(); @@ -619,7 +619,7 @@ impl StorageManager { /// Perform fanout to add or update per-node watches to an outbound watch pub(super) async fn process_outbound_watch_reconcile( &self, - watch_lock: AsyncTagLockGuard, + watch_lock: AsyncTagLockGuard, ) { let record_key = watch_lock.tag(); @@ -734,7 +734,7 @@ impl StorageManager { fn process_outbound_watch_value_result_inner( &self, inner: &mut StorageManagerInner, - record_key: TypedKey, + record_key: TypedRecordKey, owvresult: OutboundWatchValueResult, ) { let Some(outbound_watch) = inner @@ -842,8 +842,8 @@ impl StorageManager { /// Can be processed in the foreground, or by the background operation queue pub(super) fn get_next_outbound_watch_operation( &self, - key: TypedKey, - opt_watch_lock: Option>, + key: TypedRecordKey, + opt_watch_lock: Option>, cur_ts: Timestamp, outbound_watch: &mut OutboundWatch, ) -> Option> { @@ -918,7 +918,7 @@ impl StorageManager { /// Can be processed in the foreground, or by the background operation queue pub(super) fn get_change_inspection_operation( &self, - record_key: TypedKey, + record_key: TypedRecordKey, subkeys: ValueSubkeyRangeSet, ) -> PinBoxFutureStatic<()> { let fut = { @@ -1014,7 +1014,7 @@ impl StorageManager { #[instrument(level = "trace", target = "dht", skip_all)] pub async fn inbound_watch_value( &self, - key: TypedKey, + key: TypedRecordKey, params: InboundWatchParameters, watch_id: Option, ) -> VeilidAPIResult> { @@ -1055,11 +1055,11 @@ impl StorageManager { #[instrument(level = "debug", target = "watch", skip_all)] pub async fn inbound_value_changed( &self, - record_key: TypedKey, + record_key: TypedRecordKey, mut subkeys: ValueSubkeyRangeSet, count: u32, value: Option>, - inbound_node_id: TypedKey, + inbound_node_id: TypedPublicKey, watch_id: u64, ) -> VeilidAPIResult> { // Operate on the watch for this record diff --git a/veilid-core/src/table_store/table_db.rs b/veilid-core/src/table_store/table_db.rs index a561da95..5c9fb132 100644 --- a/veilid-core/src/table_store/table_db.rs +++ b/veilid-core/src/table_store/table_db.rs @@ -150,7 +150,7 @@ impl TableDB { vcrypto.crypt_b2b_no_auth( &data, encout, - (nonce as &[u8]).try_into().unwrap(), + &Nonce::try_from(&nonce[0..NONCE_LENGTH]).unwrap(), &ei.typed_key.value, ); out @@ -175,7 +175,7 @@ impl TableDB { vcrypto.crypt_b2b_no_auth( &data[NONCE_LENGTH..], &mut out, - (&data[0..NONCE_LENGTH]).try_into().unwrap(), + &Nonce::try_from(&data[0..NONCE_LENGTH]).unwrap(), &di.typed_key.value, ); decompress_size_prepended(&out, None) diff --git a/veilid-core/src/table_store/tests/test_table_store.rs b/veilid-core/src/table_store/tests/test_table_store.rs index 07c9b970..08ec7485 100644 --- a/veilid-core/src/table_store/tests/test_table_store.rs +++ b/veilid-core/src/table_store/tests/test_table_store.rs @@ -196,7 +196,7 @@ pub async fn test_json(vcrypto: &AsyncCryptoSystemGuard<'_>, ts: &TableStore) { ); assert!( - db.load_json::(1, b"foo").await.is_err(), + db.load_json::(1, b"foo").await.is_err(), "should fail to unfreeze" ); } diff --git a/veilid-core/src/tests/common/test_dht.rs b/veilid-core/src/tests/common/test_dht.rs index 602322b2..76983c0f 100644 --- a/veilid-core/src/tests/common/test_dht.rs +++ b/veilid-core/src/tests/common/test_dht.rs @@ -4,9 +4,9 @@ use crate::*; use lazy_static::*; lazy_static! { - static ref BOGUS_KEY: TypedKey = TypedKey::from(CryptoTyped::new( + static ref BOGUS_KEY: TypedRecordKey = TypedRecordKey::from(CryptoTyped::new( CRYPTO_KIND_VLD0, - CryptoKey::new([0u8; 32]) + RecordKey::new([0u8; 32]) )); } diff --git a/veilid-core/src/tests/common/test_veilid_config.rs b/veilid-core/src/tests/common/test_veilid_config.rs index 55f81f84..a1a22d22 100644 --- a/veilid-core/src/tests/common/test_veilid_config.rs +++ b/veilid-core/src/tests/common/test_veilid_config.rs @@ -204,8 +204,8 @@ pub fn config_callback(key: String) -> ConfigCallbackReturn { "network.reverse_connection_receipt_time_ms" => Ok(Box::new(5_000u32)), "network.hole_punch_receipt_time_ms" => Ok(Box::new(5_000u32)), "network.network_key_password" => Ok(Box::new(Option::::None)), - "network.routing_table.node_id" => Ok(Box::new(TypedKeyGroup::new())), - "network.routing_table.node_id_secret" => Ok(Box::new(TypedSecretGroup::new())), + "network.routing_table.node_id" => Ok(Box::new(TypedPublicKeyGroup::new())), + "network.routing_table.node_id_secret" => Ok(Box::new(TypedSecretKeyGroup::new())), // "network.routing_table.bootstrap" => Ok(Box::new(Vec::::new())), #[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] "network.routing_table.bootstrap" => { @@ -216,9 +216,9 @@ pub fn config_callback(key: String) -> ConfigCallbackReturn { "ws://bootstrap-v1.veilid.net:5150/ws".to_string(), ])), "network.routing_table.bootstrap_keys" => Ok(Box::new(vec![ - TypedKey::from_str("VLD0:Vj0lKDdUQXmQ5Ol1SZdlvXkBHUccBcQvGLN9vbLSI7k").unwrap(), - TypedKey::from_str("VLD0:QeQJorqbXtC7v3OlynCZ_W3m76wGNeB5NTF81ypqHAo").unwrap(), - TypedKey::from_str("VLD0:QNdcl-0OiFfYVj9331XVR6IqZ49NG-E18d5P7lwi4TA").unwrap(), + TypedPublicKey::from_str("VLD0:Vj0lKDdUQXmQ5Ol1SZdlvXkBHUccBcQvGLN9vbLSI7k").unwrap(), + TypedPublicKey::from_str("VLD0:QeQJorqbXtC7v3OlynCZ_W3m76wGNeB5NTF81ypqHAo").unwrap(), + TypedPublicKey::from_str("VLD0:QNdcl-0OiFfYVj9331XVR6IqZ49NG-E18d5P7lwi4TA").unwrap(), ])), "network.routing_table.limit_over_attached" => Ok(Box::new(64u32)), "network.routing_table.limit_fully_attached" => Ok(Box::new(32u32)), @@ -370,9 +370,9 @@ pub fn test_config() { assert_eq!( inner.network.routing_table.bootstrap_keys, vec![ - TypedKey::from_str("VLD0:Vj0lKDdUQXmQ5Ol1SZdlvXkBHUccBcQvGLN9vbLSI7k").unwrap(), - TypedKey::from_str("VLD0:QeQJorqbXtC7v3OlynCZ_W3m76wGNeB5NTF81ypqHAo").unwrap(), - TypedKey::from_str("VLD0:QNdcl-0OiFfYVj9331XVR6IqZ49NG-E18d5P7lwi4TA").unwrap(), + TypedPublicKey::from_str("VLD0:Vj0lKDdUQXmQ5Ol1SZdlvXkBHUccBcQvGLN9vbLSI7k").unwrap(), + TypedPublicKey::from_str("VLD0:QeQJorqbXtC7v3OlynCZ_W3m76wGNeB5NTF81ypqHAo").unwrap(), + TypedPublicKey::from_str("VLD0:QNdcl-0OiFfYVj9331XVR6IqZ49NG-E18d5P7lwi4TA").unwrap(), ], ); assert_eq!(inner.network.routing_table.limit_over_attached, 64u32); diff --git a/veilid-core/src/veilid_api/api.rs b/veilid-core/src/veilid_api/api.rs index d38f6d38..cc896a4a 100644 --- a/veilid-core/src/veilid_api/api.rs +++ b/veilid-core/src/veilid_api/api.rs @@ -246,7 +246,7 @@ impl VeilidAPI { } // Is this a node id? - if let Ok(nid) = TypedKey::from_str(&s) { + if let Ok(nid) = TypedPublicKey::from_str(&s) { return Ok(Target::NodeId(nid)); } diff --git a/veilid-core/src/veilid_api/debug.rs b/veilid-core/src/veilid_api/debug.rs index 5f8c050d..11c004c3 100644 --- a/veilid-core/src/veilid_api/debug.rs +++ b/veilid-core/src/veilid_api/debug.rs @@ -14,7 +14,7 @@ impl_veilid_log_facility!("veilid_debug"); #[derive(Default)] pub(crate) struct DebugCache { pub imported_routes: Vec, - pub opened_record_contexts: Lazy>, + pub opened_record_contexts: Lazy>, } #[must_use] @@ -228,12 +228,18 @@ fn get_number(text: &str) -> Option { T::from_str(text).ok() } -fn get_typed_key(text: &str) -> Option { - TypedKey::from_str(text).ok() +fn get_typed_key(text: &str) -> Option { + TypedPublicKey::from_str(text).ok() +} +fn get_typed_record_key(text: &str) -> Option { + TypedRecordKey::from_str(text).ok() } fn get_public_key(text: &str) -> Option { PublicKey::from_str(text).ok() } +fn get_record_key(text: &str) -> Option { + RecordKey::from_str(text).ok() +} fn get_keypair(text: &str) -> Option { KeyPair::from_str(text).ok() } @@ -251,10 +257,10 @@ fn get_crypto_system_version<'a>( } } -fn get_dht_key_no_safety(text: &str) -> Option { - let key = if let Some(key) = get_public_key(text) { - TypedKey::new(best_crypto_kind(), key) - } else if let Some(key) = get_typed_key(text) { +fn get_dht_key_no_safety(text: &str) -> Option { + let key = if let Some(key) = get_record_key(text) { + TypedRecordKey::new(best_crypto_kind(), key) + } else if let Some(key) = get_typed_record_key(text) { key } else { return None; @@ -265,7 +271,7 @@ fn get_dht_key_no_safety(text: &str) -> Option { fn get_dht_key( registry: VeilidComponentRegistry, -) -> impl FnOnce(&str) -> Option<(TypedKey, Option)> { +) -> impl FnOnce(&str) -> Option<(TypedRecordKey, Option)> { move |text| { // Safety selection let (text, ss) = if let Some((first, second)) = text.split_once('+') { @@ -278,9 +284,9 @@ fn get_dht_key( return None; } - let key = if let Some(key) = get_public_key(text) { - TypedKey::new(best_crypto_kind(), key) - } else if let Some(key) = get_typed_key(text) { + let key = if let Some(key) = get_record_key(text) { + TypedRecordKey::new(best_crypto_kind(), key) + } else if let Some(key) = get_typed_record_key(text) { key } else { return None; @@ -298,7 +304,7 @@ fn resolve_node_ref( let text = text.to_owned(); Box::pin(async move { let nr = if let Some(key) = get_public_key(&text) { - let node_id = TypedKey::new(best_crypto_kind(), key); + let node_id = TypedPublicKey::new(best_crypto_kind(), key); registry .rpc_processor() .resolve_node(node_id, safety_selection) @@ -333,7 +339,7 @@ fn resolve_filtered_node_ref( .unwrap_or((&text, None)); let nr = if let Some(key) = get_public_key(text) { - let node_id = TypedKey::new(best_crypto_kind(), key); + let node_id = TypedPublicKey::new(best_crypto_kind(), key); registry .rpc_processor() .resolve_node(node_id, safety_selection) @@ -2433,7 +2439,7 @@ TableDB Operations: context: &str, key: &str, arg: usize, - ) -> VeilidAPIResult<(TypedKey, RoutingContext)> { + ) -> VeilidAPIResult<(TypedRecordKey, RoutingContext)> { let key = match get_debug_argument_at(args, arg, context, key, get_dht_key_no_safety) .ok() .or_else(|| { diff --git a/veilid-core/src/veilid_api/error.rs b/veilid-core/src/veilid_api/error.rs index 1607ee90..7b52addc 100644 --- a/veilid-core/src/veilid_api/error.rs +++ b/veilid-core/src/veilid_api/error.rs @@ -132,7 +132,7 @@ pub enum VeilidAPIError { #[error("Key not found: {key}")] KeyNotFound { #[schemars(with = "String")] - key: TypedKey, + key: TypedRecordKey, }, #[error("Internal: {message}")] Internal { message: String }, @@ -180,7 +180,7 @@ impl VeilidAPIError { message: msg.to_string(), } } - pub fn key_not_found(key: TypedKey) -> Self { + pub fn key_not_found(key: TypedRecordKey) -> Self { Self::KeyNotFound { key } } pub fn internal(msg: T) -> Self { diff --git a/veilid-core/src/veilid_api/json_api/crypto_system.rs b/veilid-core/src/veilid_api/json_api/crypto_system.rs index a4261f4c..8124e23b 100644 --- a/veilid-core/src/veilid_api/json_api/crypto_system.rs +++ b/veilid-core/src/veilid_api/json_api/crypto_system.rs @@ -89,9 +89,9 @@ pub enum CryptoSystemRequestOp { }, Distance { #[schemars(with = "String")] - key1: CryptoKey, + key1: PublicKey, #[schemars(with = "String")] - key2: CryptoKey, + key2: PublicKey, }, Sign { #[schemars(with = "String")] @@ -215,7 +215,7 @@ pub enum CryptoSystemResponseOp { }, Distance { #[schemars(with = "String")] - value: CryptoKeyDistance, + value: HashDistance, }, Sign { #[serde(flatten)] diff --git a/veilid-core/src/veilid_api/json_api/mod.rs b/veilid-core/src/veilid_api/json_api/mod.rs index 64d2a5a3..125184bc 100644 --- a/veilid-core/src/veilid_api/json_api/mod.rs +++ b/veilid-core/src/veilid_api/json_api/mod.rs @@ -96,7 +96,7 @@ pub enum RequestOp { CryptoSystem(CryptoSystemRequest), VerifySignatures { #[schemars(with = "Vec")] - node_ids: Vec, + node_ids: Vec, #[serde(with = "as_human_base64")] #[schemars(with = "String")] data: Vec, @@ -207,7 +207,7 @@ pub enum ResponseOp { VerifySignatures { #[serde(flatten)] #[schemars(with = "ApiResult>>")] - result: ApiResultWithOptVecString>, + result: ApiResultWithOptVecString>, }, GenerateSignatures { #[serde(flatten)] diff --git a/veilid-core/src/veilid_api/json_api/process.rs b/veilid-core/src/veilid_api/json_api/process.rs index 71efb9ed..ca2a70e3 100644 --- a/veilid-core/src/veilid_api/json_api/process.rs +++ b/veilid-core/src/veilid_api/json_api/process.rs @@ -227,7 +227,7 @@ impl JsonRequestProcessor { } // Is this a node id? - if let Ok(nid) = TypedKey::from_str(&s) { + if let Ok(nid) = TypedPublicKey::from_str(&s) { return Ok(Target::NodeId(nid)); } @@ -551,7 +551,7 @@ impl JsonRequestProcessor { } } CryptoSystemRequestOp::Distance { key1, key2 } => CryptoSystemResponseOp::Distance { - value: csv.distance(&key1, &key2), + value: csv.distance(&HashDigest::from(key1), &HashDigest::from(key2)), }, CryptoSystemRequestOp::Sign { key, secret, data } => CryptoSystemResponseOp::Sign { result: to_json_api_result_with_string(csv.sign(&key, &secret, &data)), diff --git a/veilid-core/src/veilid_api/json_api/routing_context.rs b/veilid-core/src/veilid_api/json_api/routing_context.rs index 35af652f..297be42c 100644 --- a/veilid-core/src/veilid_api/json_api/routing_context.rs +++ b/veilid-core/src/veilid_api/json_api/routing_context.rs @@ -47,27 +47,27 @@ pub enum RoutingContextRequestOp { }, OpenDhtRecord { #[schemars(with = "String")] - key: TypedKey, + key: TypedRecordKey, #[schemars(with = "Option")] writer: Option, }, CloseDhtRecord { #[schemars(with = "String")] - key: TypedKey, + key: TypedRecordKey, }, DeleteDhtRecord { #[schemars(with = "String")] - key: TypedKey, + key: TypedRecordKey, }, GetDhtValue { #[schemars(with = "String")] - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, force_refresh: bool, }, SetDhtValue { #[schemars(with = "String")] - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, #[serde(with = "as_human_base64")] #[schemars(with = "String")] @@ -77,19 +77,19 @@ pub enum RoutingContextRequestOp { }, WatchDhtValues { #[schemars(with = "String")] - key: TypedKey, + key: TypedRecordKey, subkeys: Option, expiration: Option, count: Option, }, CancelDhtWatch { #[schemars(with = "String")] - key: TypedKey, + key: TypedRecordKey, subkeys: Option, }, InspectDhtRecord { #[schemars(with = "String")] - key: TypedKey, + key: TypedRecordKey, subkeys: Option, #[schemars(default)] scope: DHTReportScope, diff --git a/veilid-core/src/veilid_api/routing_context.rs b/veilid-core/src/veilid_api/routing_context.rs index c7c738cb..d16ecbd1 100644 --- a/veilid-core/src/veilid_api/routing_context.rs +++ b/veilid-core/src/veilid_api/routing_context.rs @@ -9,7 +9,7 @@ impl_veilid_log_facility!("veilid_api"); #[must_use] pub enum Target { /// Node by its public key. - NodeId(TypedKey), + NodeId(TypedPublicKey), /// Remote private route by its id. PrivateRoute(RouteId), } @@ -300,7 +300,7 @@ impl RoutingContext { schema: DHTSchema, owner_key: &PublicKey, kind: Option, - ) -> VeilidAPIResult { + ) -> VeilidAPIResult { veilid_log!(self debug "RoutingContext::get_dht_record_key(self: {:?}, schema: {:?}, owner_key: {:?}, kind: {:?})", self, schema, owner_key, kind); schema.validate()?; @@ -358,7 +358,7 @@ impl RoutingContext { #[instrument(target = "veilid_api", level = "debug", fields(__VEILID_LOG_KEY = self.log_key()), ret, err)] pub async fn open_dht_record( &self, - key: TypedKey, + key: TypedRecordKey, default_writer: Option, ) -> VeilidAPIResult { veilid_log!(self debug @@ -376,7 +376,7 @@ impl RoutingContext { /// /// Closing a record allows you to re-open it with a different routing context. #[instrument(target = "veilid_api", level = "debug", fields(__VEILID_LOG_KEY = self.log_key()), ret, err)] - pub async fn close_dht_record(&self, key: TypedKey) -> VeilidAPIResult<()> { + pub async fn close_dht_record(&self, key: TypedRecordKey) -> VeilidAPIResult<()> { veilid_log!(self debug "RoutingContext::close_dht_record(self: {:?}, key: {:?})", self, key); @@ -392,7 +392,7 @@ impl RoutingContext { /// Deleting a record does not delete it from the network, but will remove the storage of the record /// locally, and will prevent its value from being refreshed on the network by this node. #[instrument(target = "veilid_api", level = "debug", fields(__VEILID_LOG_KEY = self.log_key()), ret, err)] - pub async fn delete_dht_record(&self, key: TypedKey) -> VeilidAPIResult<()> { + pub async fn delete_dht_record(&self, key: TypedRecordKey) -> VeilidAPIResult<()> { veilid_log!(self debug "RoutingContext::delete_dht_record(self: {:?}, key: {:?})", self, key); @@ -411,7 +411,7 @@ impl RoutingContext { #[instrument(target = "veilid_api", level = "debug", fields(__VEILID_LOG_KEY = self.log_key()), ret, err)] pub async fn get_dht_value( &self, - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, force_refresh: bool, ) -> VeilidAPIResult> { @@ -434,7 +434,7 @@ impl RoutingContext { #[instrument(target = "veilid_api", level = "debug", skip(data), fields(__VEILID_LOG_KEY = self.log_key(), data = print_data(&data, Some(64))), ret, err)] pub async fn set_dht_value( &self, - key: TypedKey, + key: TypedRecordKey, subkey: ValueSubkey, data: Vec, writer: Option, @@ -476,7 +476,7 @@ impl RoutingContext { #[instrument(target = "veilid_api", level = "debug", fields(__VEILID_LOG_KEY = self.log_key()), ret, err)] pub async fn watch_dht_values( &self, - key: TypedKey, + key: TypedRecordKey, subkeys: Option, expiration: Option, count: Option, @@ -508,7 +508,7 @@ impl RoutingContext { #[instrument(target = "veilid_api", level = "debug", fields(__VEILID_LOG_KEY = self.log_key()), ret, err)] pub async fn cancel_dht_watch( &self, - key: TypedKey, + key: TypedRecordKey, subkeys: Option, ) -> VeilidAPIResult { veilid_log!(self debug @@ -564,7 +564,7 @@ impl RoutingContext { #[instrument(target = "veilid_api", level = "debug", fields(__VEILID_LOG_KEY = self.log_key()), ret, err)] pub async fn inspect_dht_record( &self, - key: TypedKey, + key: TypedRecordKey, subkeys: Option, scope: DHTReportScope, ) -> VeilidAPIResult { diff --git a/veilid-core/src/veilid_api/tests/fixtures.rs b/veilid-core/src/veilid_api/tests/fixtures.rs index a1b0a966..2e7fc88a 100644 --- a/veilid-core/src/veilid_api/tests/fixtures.rs +++ b/veilid-core/src/veilid_api/tests/fixtures.rs @@ -90,21 +90,48 @@ pub fn fix_peerstats() -> PeerStats { } } -pub fn fix_cryptokey() -> CryptoKey { +pub fn fix_publickey() -> PublicKey { let mut fake_key = [0u8; CRYPTO_KEY_LENGTH]; random_bytes(&mut fake_key); - CryptoKey::new(fake_key) + PublicKey::new(fake_key) } -pub fn fix_typedkey() -> TypedKey { +pub fn fix_recordkey() -> RecordKey { let mut fake_key = [0u8; CRYPTO_KEY_LENGTH]; random_bytes(&mut fake_key); - TypedKey { + RecordKey::new(fake_key) +} + +pub fn fix_routeid() -> RouteId { + let mut fake_key = [0u8; CRYPTO_KEY_LENGTH]; + random_bytes(&mut fake_key); + RouteId::new(fake_key) +} + +pub fn fix_typedkey() -> TypedPublicKey { + let mut fake_key = [0u8; CRYPTO_KEY_LENGTH]; + random_bytes(&mut fake_key); + TypedPublicKey { kind: FourCC::from_str("FAKE").unwrap(), - value: fix_cryptokey(), + value: fix_publickey(), } } +pub fn fix_typedrecordkey() -> TypedRecordKey { + let mut fake_key = [0u8; CRYPTO_KEY_LENGTH]; + random_bytes(&mut fake_key); + TypedRecordKey { + kind: FourCC::from_str("FAKE").unwrap(), + value: fix_recordkey(), + } +} + +pub fn fix_secretkey() -> SecretKey { + let mut fake_key = [0u8; CRYPTO_KEY_LENGTH]; + random_bytes(&mut fake_key); + SecretKey::new(fake_key) +} + pub fn fix_peertabledata() -> PeerTableData { PeerTableData { node_ids: vec![fix_typedkey()], @@ -148,10 +175,10 @@ pub fn fix_veilidconfig() -> VeilidConfig { hole_punch_receipt_time_ms: 9000, network_key_password: None, routing_table: VeilidConfigRoutingTable { - node_id: TypedKeyGroup::new(), - node_id_secret: TypedSecretGroup::new(), + node_id: TypedPublicKeyGroup::new(), + node_id_secret: TypedSecretKeyGroup::new(), bootstrap: vec!["boots".to_string()], - bootstrap_keys: vec![TypedKey::from_str( + bootstrap_keys: vec![TypedPublicKey::from_str( "VLD0:qrxwD1-aM9xiUw4IAPVXE_4qgoIfyR4Y6MEPyaDl_GQ", ) .unwrap()], @@ -262,9 +289,9 @@ pub fn fix_veilidconfig() -> VeilidConfig { pub fn fix_veilidvaluechange() -> VeilidValueChange { VeilidValueChange { - key: fix_typedkey(), + key: fix_typedrecordkey(), subkeys: ValueSubkeyRangeSet::new(), count: 5, - value: Some(ValueData::new_with_seq(23, b"ValueData".to_vec(), fix_cryptokey()).unwrap()), + value: Some(ValueData::new_with_seq(23, b"ValueData".to_vec(), fix_publickey()).unwrap()), } } diff --git a/veilid-core/src/veilid_api/tests/test_types.rs b/veilid-core/src/veilid_api/tests/test_types.rs index 5a1c46e7..90a3d4e0 100644 --- a/veilid-core/src/veilid_api/tests/test_types.rs +++ b/veilid-core/src/veilid_api/tests/test_types.rs @@ -15,7 +15,7 @@ pub fn test_alignedu64() { pub fn test_veilidappmessage() { let orig = VeilidAppMessage::new( Some(fix_typedkey()), - Some(fix_cryptokey()), + Some(fix_routeid()), b"Hi there!".to_vec(), ); let copy = deserialize_json(&serialize_json(&orig)).unwrap(); @@ -26,7 +26,7 @@ pub fn test_veilidappmessage() { pub fn test_veilidappcall() { let orig = VeilidAppCall::new( Some(fix_typedkey()), - Some(fix_cryptokey()), + Some(fix_routeid()), b"Well, hello!".to_vec(), OperationId::from(123), ); @@ -69,7 +69,7 @@ pub fn test_safetyselection() { pub fn test_safetyspec() { let orig = SafetySpec { - preferred_route: Some(fix_cryptokey()), + preferred_route: Some(fix_routeid()), hop_count: 23, stability: Stability::default(), sequencing: Sequencing::default(), @@ -242,8 +242,8 @@ pub fn test_veilidstatenetwork() { pub fn test_veilidroutechange() { let orig = VeilidRouteChange { - dead_routes: vec![fix_cryptokey()], - dead_remote_routes: vec![fix_cryptokey()], + dead_routes: vec![fix_routeid()], + dead_remote_routes: vec![fix_routeid()], }; let copy = deserialize_json(&serialize_json(&orig)).unwrap(); diff --git a/veilid-core/src/veilid_api/tests/test_types_dht.rs b/veilid-core/src/veilid_api/tests/test_types_dht.rs index fdbb4149..3d4e2c7a 100644 --- a/veilid-core/src/veilid_api/tests/test_types_dht.rs +++ b/veilid-core/src/veilid_api/tests/test_types_dht.rs @@ -6,9 +6,9 @@ use range_set_blaze::*; pub fn test_dhtrecorddescriptor() { let orig = DHTRecordDescriptor::new( - fix_typedkey(), - fix_cryptokey(), - Some(fix_cryptokey()), + fix_typedrecordkey(), + fix_publickey(), + Some(fix_secretkey()), DHTSchema::dflt(4321).unwrap(), ); let copy = deserialize_json(&serialize_json(&orig)).unwrap(); @@ -19,7 +19,7 @@ pub fn test_dhtrecorddescriptor() { // value_data pub fn test_valuedata() { - let orig = ValueData::new_with_seq(42, b"Brent Spiner".to_vec(), fix_cryptokey()); + let orig = ValueData::new_with_seq(42, b"Brent Spiner".to_vec(), fix_publickey()); let copy = deserialize_json(&serialize_json(&orig)).unwrap(); assert_eq!(orig, copy); diff --git a/veilid-core/src/veilid_api/tests/test_types_dht_schema.rs b/veilid-core/src/veilid_api/tests/test_types_dht_schema.rs index d7ddcd8b..aa7f7845 100644 --- a/veilid-core/src/veilid_api/tests/test_types_dht_schema.rs +++ b/veilid-core/src/veilid_api/tests/test_types_dht_schema.rs @@ -18,11 +18,11 @@ pub fn test_dhtschema() { 91, vec![ DHTSchemaSMPLMember { - m_key: fix_cryptokey(), + m_key: fix_publickey(), m_cnt: 5, }, DHTSchemaSMPLMember { - m_key: fix_cryptokey(), + m_key: fix_publickey(), m_cnt: 6, }, ], @@ -38,7 +38,7 @@ pub fn test_dhtschema() { pub fn test_dhtschemasmplmember() { let orig = DHTSchemaSMPLMember { - m_key: fix_cryptokey(), + m_key: fix_publickey(), m_cnt: 7, }; let copy = deserialize_json(&serialize_json(&orig)).unwrap(); @@ -51,11 +51,11 @@ pub fn test_dhtschemasmpl() { 91, vec![ DHTSchemaSMPLMember { - m_key: fix_cryptokey(), + m_key: fix_publickey(), m_cnt: 8, }, DHTSchemaSMPLMember { - m_key: fix_cryptokey(), + m_key: fix_publickey(), m_cnt: 9, }, ], diff --git a/veilid-core/src/veilid_api/types/app_message_call.rs b/veilid-core/src/veilid_api/types/app_message_call.rs index 80896e21..bd18eb91 100644 --- a/veilid-core/src/veilid_api/types/app_message_call.rs +++ b/veilid-core/src/veilid_api/types/app_message_call.rs @@ -11,7 +11,7 @@ pub struct VeilidAppMessage { all(target_arch = "wasm32", target_os = "unknown"), tsify(optional, type = "string") )] - sender: Option, + sender: Option, #[serde(with = "as_human_opt_string")] #[schemars(with = "Option")] @@ -35,7 +35,11 @@ pub struct VeilidAppMessage { } impl VeilidAppMessage { - pub fn new(sender: Option, route_id: Option, message: Vec) -> Self { + pub fn new( + sender: Option, + route_id: Option, + message: Vec, + ) -> Self { Self { sender, route_id, @@ -45,7 +49,7 @@ impl VeilidAppMessage { /// Some(sender) if the message was sent directly, None if received via a private/safety route. #[must_use] - pub fn sender(&self) -> Option<&TypedKey> { + pub fn sender(&self) -> Option<&TypedPublicKey> { self.sender.as_ref() } @@ -70,7 +74,7 @@ pub struct VeilidAppCall { #[serde(with = "as_human_opt_string")] #[schemars(with = "Option")] #[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), tsify(optional))] - sender: Option, + sender: Option, #[serde(with = "as_human_opt_string")] #[schemars(with = "Option")] @@ -99,7 +103,7 @@ pub struct VeilidAppCall { impl VeilidAppCall { pub fn new( - sender: Option, + sender: Option, route_id: Option, message: Vec, call_id: OperationId, @@ -114,7 +118,7 @@ impl VeilidAppCall { /// Some(sender) if the request was sent directly, None if received via a private/safety route. #[must_use] - pub fn sender(&self) -> Option<&TypedKey> { + pub fn sender(&self) -> Option<&TypedPublicKey> { self.sender.as_ref() } diff --git a/veilid-core/src/veilid_api/types/dht/dht_record_descriptor.rs b/veilid-core/src/veilid_api/types/dht/dht_record_descriptor.rs index 169fd7f0..13b85458 100644 --- a/veilid-core/src/veilid_api/types/dht/dht_record_descriptor.rs +++ b/veilid-core/src/veilid_api/types/dht/dht_record_descriptor.rs @@ -11,7 +11,7 @@ use super::*; pub struct DHTRecordDescriptor { /// DHT Key = Hash(ownerKeyKind) of: [ ownerKeyValue, schema ] #[schemars(with = "String")] - key: TypedKey, + key: TypedRecordKey, /// The public key of the owner #[schemars(with = "String")] owner: PublicKey, @@ -26,7 +26,7 @@ pub struct DHTRecordDescriptor { impl DHTRecordDescriptor { pub(crate) fn new( - key: TypedKey, + key: TypedRecordKey, owner: PublicKey, owner_secret: Option, schema: DHTSchema, @@ -39,7 +39,7 @@ impl DHTRecordDescriptor { } } - pub fn key(&self) -> &TypedKey { + pub fn key(&self) -> &TypedRecordKey { &self.key } pub fn owner(&self) -> &PublicKey { diff --git a/veilid-core/src/veilid_api/types/dht/value_data.rs b/veilid-core/src/veilid_api/types/dht/value_data.rs index a71dc208..970d3877 100644 --- a/veilid-core/src/veilid_api/types/dht/value_data.rs +++ b/veilid-core/src/veilid_api/types/dht/value_data.rs @@ -95,11 +95,11 @@ mod tests { #[test] fn value_data_ok() { - assert!(ValueData::new(vec![0; ValueData::MAX_LEN], CryptoKey { bytes: [0; 32] }).is_ok()); + assert!(ValueData::new(vec![0; ValueData::MAX_LEN], PublicKey { bytes: [0; 32] }).is_ok()); assert!(ValueData::new_with_seq( 0, vec![0; ValueData::MAX_LEN], - CryptoKey { bytes: [0; 32] } + PublicKey { bytes: [0; 32] } ) .is_ok()); } @@ -108,13 +108,13 @@ mod tests { fn value_data_too_long() { assert!(ValueData::new( vec![0; ValueData::MAX_LEN + 1], - CryptoKey { bytes: [0; 32] } + PublicKey { bytes: [0; 32] } ) .is_err()); assert!(ValueData::new_with_seq( 0, vec![0; ValueData::MAX_LEN + 1], - CryptoKey { bytes: [0; 32] } + PublicKey { bytes: [0; 32] } ) .is_err()); } diff --git a/veilid-core/src/veilid_api/types/veilid_state.rs b/veilid-core/src/veilid_api/types/veilid_state.rs index 4886d2de..8f42e78f 100644 --- a/veilid-core/src/veilid_api/types/veilid_state.rs +++ b/veilid-core/src/veilid_api/types/veilid_state.rs @@ -107,7 +107,7 @@ pub struct PeerTableData { all(target_arch = "wasm32", target_os = "unknown"), tsify(type = "string[]") )] - pub node_ids: Vec, + pub node_ids: Vec, /// The peer's human readable address. pub peer_address: String, /// Statistics we have collected on this peer. @@ -162,7 +162,7 @@ pub struct VeilidStateConfig { pub struct VeilidValueChange { /// The DHT Record key that changed #[schemars(with = "String")] - pub key: TypedKey, + pub key: TypedRecordKey, /// The portion of the DHT Record's subkeys that have changed /// If the subkey range is empty, any watch present on the value has died. pub subkeys: ValueSubkeyRangeSet, diff --git a/veilid-core/src/veilid_config.rs b/veilid-core/src/veilid_config.rs index 52169625..51daa81e 100644 --- a/veilid-core/src/veilid_config.rs +++ b/veilid-core/src/veilid_config.rs @@ -496,12 +496,12 @@ impl Default for VeilidConfigRPC { #[must_use] pub struct VeilidConfigRoutingTable { #[schemars(with = "Vec")] - pub node_id: TypedKeyGroup, + pub node_id: TypedPublicKeyGroup, #[schemars(with = "Vec")] - pub node_id_secret: TypedSecretGroup, + pub node_id_secret: TypedSecretKeyGroup, pub bootstrap: Vec, #[schemars(with = "Vec")] - pub bootstrap_keys: Vec, + pub bootstrap_keys: Vec, pub limit_over_attached: u32, pub limit_fully_attached: u32, pub limit_attached_strong: u32, @@ -522,16 +522,16 @@ impl Default for VeilidConfigRoutingTable { } let bootstrap_keys = vec![ // Primary Veilid Foundation bootstrap signing key - TypedKey::from_str("VLD0:Vj0lKDdUQXmQ5Ol1SZdlvXkBHUccBcQvGLN9vbLSI7k").unwrap(), + TypedPublicKey::from_str("VLD0:Vj0lKDdUQXmQ5Ol1SZdlvXkBHUccBcQvGLN9vbLSI7k").unwrap(), // Secondary Veilid Foundation bootstrap signing key - TypedKey::from_str("VLD0:QeQJorqbXtC7v3OlynCZ_W3m76wGNeB5NTF81ypqHAo").unwrap(), + TypedPublicKey::from_str("VLD0:QeQJorqbXtC7v3OlynCZ_W3m76wGNeB5NTF81ypqHAo").unwrap(), // Backup Veilid Foundation bootstrap signing key - TypedKey::from_str("VLD0:QNdcl-0OiFfYVj9331XVR6IqZ49NG-E18d5P7lwi4TA").unwrap(), + TypedPublicKey::from_str("VLD0:QNdcl-0OiFfYVj9331XVR6IqZ49NG-E18d5P7lwi4TA").unwrap(), ]; Self { - node_id: TypedKeyGroup::default(), - node_id_secret: TypedSecretGroup::default(), + node_id: TypedPublicKeyGroup::default(), + node_id_secret: TypedSecretKeyGroup::default(), bootstrap, bootstrap_keys, limit_over_attached: 64, @@ -1082,7 +1082,7 @@ impl VeilidStartupOptions { let mut safe_cfg = self.inner.read().clone(); // Remove secrets - safe_cfg.network.routing_table.node_id_secret = TypedSecretGroup::new(); + safe_cfg.network.routing_table.node_id_secret = TypedSecretKeyGroup::new(); "".clone_into(&mut safe_cfg.protected_store.device_encryption_key_password); safe_cfg.protected_store.new_device_encryption_key_password = None; @@ -1093,7 +1093,7 @@ impl VeilidStartupOptions { let mut safe_cfg = self.inner.read().clone(); // Remove secrets - safe_cfg.network.routing_table.node_id_secret = TypedSecretGroup::new(); + safe_cfg.network.routing_table.node_id_secret = TypedSecretKeyGroup::new(); "".clone_into(&mut safe_cfg.protected_store.device_encryption_key_password); safe_cfg.protected_store.new_device_encryption_key_password = None; diff --git a/veilid-flutter/rust/src/dart_ffi.rs b/veilid-flutter/rust/src/dart_ffi.rs index 19bcba6b..f017fc6a 100644 --- a/veilid-flutter/rust/src/dart_ffi.rs +++ b/veilid-flutter/rust/src/dart_ffi.rs @@ -713,7 +713,7 @@ pub extern "C" fn routing_context_create_dht_record( #[no_mangle] #[instrument(level = "trace", target = "ffi", skip_all)] pub extern "C" fn routing_context_open_dht_record(port: i64, id: u32, key: FfiStr, writer: FfiStr) { - let key: veilid_core::TypedKey = + let key: veilid_core::TypedRecordKey = veilid_core::deserialize_opt_json(key.into_opt_string()).unwrap(); let writer: Option = writer .into_opt_string() @@ -732,7 +732,7 @@ pub extern "C" fn routing_context_open_dht_record(port: i64, id: u32, key: FfiSt #[no_mangle] #[instrument(level = "trace", target = "ffi", skip_all)] pub extern "C" fn routing_context_close_dht_record(port: i64, id: u32, key: FfiStr) { - let key: veilid_core::TypedKey = + let key: veilid_core::TypedRecordKey = veilid_core::deserialize_opt_json(key.into_opt_string()).unwrap(); DartIsolateWrapper::new(port).spawn_result( async move { @@ -748,7 +748,7 @@ pub extern "C" fn routing_context_close_dht_record(port: i64, id: u32, key: FfiS #[no_mangle] #[instrument(level = "trace", target = "ffi", skip_all)] pub extern "C" fn routing_context_delete_dht_record(port: i64, id: u32, key: FfiStr) { - let key: veilid_core::TypedKey = + let key: veilid_core::TypedRecordKey = veilid_core::deserialize_opt_json(key.into_opt_string()).unwrap(); DartIsolateWrapper::new(port).spawn_result( async move { @@ -770,7 +770,7 @@ pub extern "C" fn routing_context_get_dht_value( subkey: u32, force_refresh: bool, ) { - let key: veilid_core::TypedKey = + let key: veilid_core::TypedRecordKey = veilid_core::deserialize_opt_json(key.into_opt_string()).unwrap(); DartIsolateWrapper::new(port).spawn_result_json( async move { @@ -795,7 +795,7 @@ pub extern "C" fn routing_context_set_dht_value( data: FfiStr, writer: FfiStr, ) { - let key: veilid_core::TypedKey = + let key: veilid_core::TypedRecordKey = veilid_core::deserialize_opt_json(key.into_opt_string()).unwrap(); let data: Vec = data_encoding::BASE64URL_NOPAD .decode(data.into_opt_string().unwrap().as_bytes()) @@ -827,7 +827,7 @@ pub extern "C" fn routing_context_watch_dht_values( expiration: u64, count: u32, ) { - let key: veilid_core::TypedKey = + let key: veilid_core::TypedRecordKey = veilid_core::deserialize_opt_json(key.into_opt_string()).unwrap(); let subkeys: veilid_core::ValueSubkeyRangeSet = veilid_core::deserialize_opt_json(subkeys.into_opt_string()).unwrap(); @@ -854,7 +854,7 @@ pub extern "C" fn routing_context_cancel_dht_watch( key: FfiStr, subkeys: FfiStr, ) { - let key: veilid_core::TypedKey = + let key: veilid_core::TypedRecordKey = veilid_core::deserialize_opt_json(key.into_opt_string()).unwrap(); let subkeys: veilid_core::ValueSubkeyRangeSet = veilid_core::deserialize_opt_json(subkeys.into_opt_string()).unwrap(); @@ -879,7 +879,7 @@ pub extern "C" fn routing_context_inspect_dht_record( subkeys: FfiStr, scope: FfiStr, ) { - let key: veilid_core::TypedKey = + let key: veilid_core::TypedRecordKey = veilid_core::deserialize_opt_json(key.into_opt_string()).unwrap(); let subkeys: veilid_core::ValueSubkeyRangeSet = veilid_core::deserialize_opt_json(subkeys.into_opt_string()).unwrap(); @@ -1289,7 +1289,7 @@ pub extern "C" fn best_crypto_kind() -> u32 { #[no_mangle] #[instrument(level = "trace", target = "ffi", skip_all)] pub extern "C" fn verify_signatures(port: i64, node_ids: FfiStr, data: FfiStr, signatures: FfiStr) { - let node_ids: Vec = + let node_ids: Vec = veilid_core::deserialize_opt_json(node_ids.into_opt_string()).unwrap(); let data: Vec = data_encoding::BASE64URL_NOPAD @@ -1740,9 +1740,9 @@ pub extern "C" fn crypto_validate_hash(port: i64, kind: u32, data: FfiStr, hash: pub extern "C" fn crypto_distance(port: i64, kind: u32, key1: FfiStr, key2: FfiStr) { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind); - let key1: veilid_core::CryptoKey = + let key1: veilid_core::HashDigest = veilid_core::deserialize_opt_json(key1.into_opt_string()).unwrap(); - let key2: veilid_core::CryptoKey = + let key2: veilid_core::HashDigest = veilid_core::deserialize_opt_json(key2.into_opt_string()).unwrap(); DartIsolateWrapper::new(port).spawn_result_json( @@ -1768,9 +1768,9 @@ pub extern "C" fn crypto_distance(port: i64, kind: u32, key1: FfiStr, key2: FfiS pub extern "C" fn crypto_sign(port: i64, kind: u32, key: FfiStr, secret: FfiStr, data: FfiStr) { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind); - let key: veilid_core::CryptoKey = + let key: veilid_core::PublicKey = veilid_core::deserialize_opt_json(key.into_opt_string()).unwrap(); - let secret: veilid_core::CryptoKey = + let secret: veilid_core::SecretKey = veilid_core::deserialize_opt_json(secret.into_opt_string()).unwrap(); let data: Vec = data_encoding::BASE64URL_NOPAD .decode(data.into_opt_string().unwrap().as_bytes()) @@ -1805,7 +1805,7 @@ pub extern "C" fn crypto_verify( ) { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind); - let key: veilid_core::CryptoKey = + let key: veilid_core::PublicKey = veilid_core::deserialize_opt_json(key.into_opt_string()).unwrap(); let data: Vec = data_encoding::BASE64URL_NOPAD .decode(data.into_opt_string().unwrap().as_bytes()) diff --git a/veilid-python/veilid/api.py b/veilid-python/veilid/api.py index e7adfece..ec71258e 100644 --- a/veilid-python/veilid/api.py +++ b/veilid-python/veilid/api.py @@ -296,8 +296,8 @@ class CryptoSystem(ABC): @abstractmethod async def distance( - self, key1: types.CryptoKey, key2: types.CryptoKey - ) -> types.CryptoKeyDistance: + self, key1: types.HashDigest, key2: types.HashDigest + ) -> types.HashDistance: pass @abstractmethod diff --git a/veilid-python/veilid/json_api.py b/veilid-python/veilid/json_api.py index 8db31a69..221f2ba1 100644 --- a/veilid-python/veilid/json_api.py +++ b/veilid-python/veilid/json_api.py @@ -20,8 +20,7 @@ from .operations import ( ) from .state import VeilidState, VeilidUpdate from .types import ( - CryptoKey, - CryptoKeyDistance, + HashDistance, CryptoKind, DHTRecordDescriptor, DHTRecordReport, @@ -1311,11 +1310,11 @@ class _JsonCryptoSystem(CryptoSystem): ) ) - async def distance(self, key1: CryptoKey, key2: CryptoKey) -> CryptoKeyDistance: - assert isinstance(key1, CryptoKey) - assert isinstance(key2, CryptoKey) + async def distance(self, key1: HashDigest, key2: HashDigest) -> HashDistance: + assert isinstance(key1, HashDigest) + assert isinstance(key2, HashDigest) - return CryptoKeyDistance( + return HashDistance( raise_api_result( await self.api.send_ndjson_request( Operation.CRYPTO_SYSTEM, diff --git a/veilid-python/veilid/types.py b/veilid-python/veilid/types.py index edd83522..44a24722 100644 --- a/veilid-python/veilid/types.py +++ b/veilid-python/veilid/types.py @@ -126,27 +126,23 @@ class EncodedString(str): return cls(urlsafe_b64encode_no_pad(b)) -class CryptoKey(EncodedString): +class HashDistance(EncodedString): pass -class CryptoKeyDistance(CryptoKey): +class PublicKey(EncodedString): pass -class PublicKey(CryptoKey): +class SecretKey(EncodedString): pass -class SecretKey(CryptoKey): +class SharedSecret(EncodedString): pass -class SharedSecret(CryptoKey): - pass - - -class HashDigest(CryptoKey): +class HashDigest(EncodedString): pass diff --git a/veilid-server/src/main.rs b/veilid-server/src/main.rs index 471829e5..89c2b5b8 100644 --- a/veilid-server/src/main.rs +++ b/veilid-server/src/main.rs @@ -20,7 +20,7 @@ use clap::{Args, Parser}; use server::*; use settings::LogLevel; use tools::*; -use veilid_core::{TypedKeyGroup, TypedSecretGroup}; +use veilid_core::{TypedPublicKeyGroup, TypedSecretKeyGroup}; use veilid_logs::*; #[derive(Args, Debug, Clone)] @@ -315,13 +315,13 @@ fn main() -> EyreResult<()> { settingsrw.logging.terminal.enabled = false; // Split or get secret - let tks = TypedKeyGroup::from_str(&key_set) + let tks = TypedPublicKeyGroup::from_str(&key_set) .wrap_err("failed to decode node id set from command line")?; let buffer = rpassword::prompt_password("Enter secret key set (will not echo): ") .wrap_err("invalid secret key")?; let buffer = buffer.trim().to_string(); - let tss = TypedSecretGroup::from_str(&buffer).wrap_err("failed to decode secret set")?; + let tss = TypedSecretKeyGroup::from_str(&buffer).wrap_err("failed to decode secret set")?; settingsrw.core.network.routing_table.node_id = Some(tks); settingsrw.core.network.routing_table.node_id_secret = Some(tss); @@ -345,10 +345,10 @@ fn main() -> EyreResult<()> { if let Some(bootstrap_keys) = args.bootstrap_keys { println!("Overriding bootstrap keys with: "); - let mut bootstrap_keys_list: Vec = Vec::new(); + let mut bootstrap_keys_list: Vec = Vec::new(); for x in bootstrap_keys.split(',') { let x = x.trim(); - let key = match veilid_core::TypedKey::from_str(x) { + let key = match veilid_core::TypedPublicKey::from_str(x) { Ok(v) => v, Err(e) => { bail!("Failed to parse bootstrap key: {}\n{}", e, x) @@ -425,13 +425,13 @@ fn main() -> EyreResult<()> { // --- Generate DHT Key --- if let Some(ckstr) = args.generate_key_pair { if ckstr.is_empty() { - let mut tks = veilid_core::TypedKeyGroup::new(); - let mut tss = veilid_core::TypedSecretGroup::new(); + let mut tks = veilid_core::TypedPublicKeyGroup::new(); + let mut tss = veilid_core::TypedSecretKeyGroup::new(); for ck in veilid_core::VALID_CRYPTO_KINDS { let tkp = veilid_core::Crypto::generate_keypair(ck).wrap_err("invalid crypto kind")?; - tks.add(veilid_core::TypedKey::new(tkp.kind, tkp.value.key)); - tss.add(veilid_core::TypedSecret::new(tkp.kind, tkp.value.secret)); + tks.add(veilid_core::TypedPublicKey::new(tkp.kind, tkp.value.key)); + tss.add(veilid_core::TypedSecretKey::new(tkp.kind, tkp.value.secret)); } println!("Public Keys:\n{}\nSecret Keys:\n{}\n", tks, tss); } else { diff --git a/veilid-server/src/settings.rs b/veilid-server/src/settings.rs index 70bcfcb5..590b2675 100644 --- a/veilid-server/src/settings.rs +++ b/veilid-server/src/settings.rs @@ -698,10 +698,10 @@ pub struct Dht { #[derive(Debug, Deserialize, Serialize)] pub struct RoutingTable { - pub node_id: Option, - pub node_id_secret: Option, + pub node_id: Option, + pub node_id_secret: Option, pub bootstrap: Vec, - pub bootstrap_keys: Vec, + pub bootstrap_keys: Vec, pub limit_over_attached: u32, pub limit_fully_attached: u32, pub limit_attached_strong: u32, @@ -1848,9 +1848,12 @@ mod tests { assert_eq!( s.core.network.routing_table.bootstrap_keys, vec![ - TypedKey::from_str("VLD0:Vj0lKDdUQXmQ5Ol1SZdlvXkBHUccBcQvGLN9vbLSI7k").unwrap(), - TypedKey::from_str("VLD0:QeQJorqbXtC7v3OlynCZ_W3m76wGNeB5NTF81ypqHAo").unwrap(), - TypedKey::from_str("VLD0:QNdcl-0OiFfYVj9331XVR6IqZ49NG-E18d5P7lwi4TA").unwrap(), + TypedPublicKey::from_str("VLD0:Vj0lKDdUQXmQ5Ol1SZdlvXkBHUccBcQvGLN9vbLSI7k") + .unwrap(), + TypedPublicKey::from_str("VLD0:QeQJorqbXtC7v3OlynCZ_W3m76wGNeB5NTF81ypqHAo") + .unwrap(), + TypedPublicKey::from_str("VLD0:QNdcl-0OiFfYVj9331XVR6IqZ49NG-E18d5P7lwi4TA") + .unwrap(), ] ); // diff --git a/veilid-wasm/src/lib.rs b/veilid-wasm/src/lib.rs index 91601bcd..fec6c7e5 100644 --- a/veilid-wasm/src/lib.rs +++ b/veilid-wasm/src/lib.rs @@ -527,7 +527,7 @@ pub fn routing_context_create_dht_record( #[wasm_bindgen()] pub fn routing_context_open_dht_record(id: u32, key: String, writer: Option) -> Promise { wrap_api_future_json(async move { - let key: veilid_core::TypedKey = + let key: veilid_core::TypedRecordKey = veilid_core::deserialize_json(&key).map_err(VeilidAPIError::generic)?; let writer: Option = match writer { Some(s) => Some(veilid_core::deserialize_json(&s).map_err(VeilidAPIError::generic)?), @@ -544,7 +544,7 @@ pub fn routing_context_open_dht_record(id: u32, key: String, writer: Option Promise { wrap_api_future_void(async move { - let key: veilid_core::TypedKey = + let key: veilid_core::TypedRecordKey = veilid_core::deserialize_json(&key).map_err(VeilidAPIError::generic)?; let routing_context = get_routing_context(id, "routing_context_close_dht_record")?; @@ -557,7 +557,7 @@ pub fn routing_context_close_dht_record(id: u32, key: String) -> Promise { #[wasm_bindgen()] pub fn routing_context_delete_dht_record(id: u32, key: String) -> Promise { wrap_api_future_void(async move { - let key: veilid_core::TypedKey = + let key: veilid_core::TypedRecordKey = veilid_core::deserialize_json(&key).map_err(VeilidAPIError::generic)?; let routing_context = get_routing_context(id, "routing_context_delete_dht_record")?; @@ -575,7 +575,7 @@ pub fn routing_context_get_dht_value( force_refresh: bool, ) -> Promise { wrap_api_future_json(async move { - let key: veilid_core::TypedKey = + let key: veilid_core::TypedRecordKey = veilid_core::deserialize_json(&key).map_err(VeilidAPIError::generic)?; let routing_context = get_routing_context(id, "routing_context_get_dht_value")?; @@ -596,7 +596,7 @@ pub fn routing_context_set_dht_value( writer: Option, ) -> Promise { wrap_api_future_json(async move { - let key: veilid_core::TypedKey = + let key: veilid_core::TypedRecordKey = veilid_core::deserialize_json(&key).map_err(VeilidAPIError::generic)?; let data: Vec = data_encoding::BASE64URL_NOPAD .decode(data.as_bytes()) @@ -624,7 +624,7 @@ pub fn routing_context_watch_dht_values( count: u32, ) -> Promise { wrap_api_future_plain(async move { - let key: veilid_core::TypedKey = + let key: veilid_core::TypedRecordKey = veilid_core::deserialize_json(&key).map_err(VeilidAPIError::generic)?; let subkeys: veilid_core::ValueSubkeyRangeSet = veilid_core::deserialize_json(&subkeys).map_err(VeilidAPIError::generic)?; @@ -644,7 +644,7 @@ pub fn routing_context_watch_dht_values( #[wasm_bindgen()] pub fn routing_context_cancel_dht_watch(id: u32, key: String, subkeys: String) -> Promise { wrap_api_future_plain(async move { - let key: veilid_core::TypedKey = + let key: veilid_core::TypedRecordKey = veilid_core::deserialize_json(&key).map_err(VeilidAPIError::generic)?; let subkeys: veilid_core::ValueSubkeyRangeSet = veilid_core::deserialize_json(&subkeys).map_err(VeilidAPIError::generic)?; @@ -664,7 +664,7 @@ pub fn routing_context_inspect_dht_record( scope: String, ) -> Promise { wrap_api_future_json(async move { - let key: veilid_core::TypedKey = + let key: veilid_core::TypedRecordKey = veilid_core::deserialize_json(&key).map_err(VeilidAPIError::generic)?; let subkeys: veilid_core::ValueSubkeyRangeSet = veilid_core::deserialize_json(&subkeys).map_err(VeilidAPIError::generic)?; @@ -1002,7 +1002,7 @@ pub fn best_crypto_kind() -> u32 { #[wasm_bindgen()] pub fn verify_signatures(node_ids: String, data: String, signatures: String) -> Promise { wrap_api_future_json(async move { - let node_ids: Vec = + let node_ids: Vec = veilid_core::deserialize_json(&node_ids).map_err(VeilidAPIError::generic)?; let data: Vec = data_encoding::BASE64URL_NOPAD @@ -1371,9 +1371,9 @@ pub fn crypto_distance(kind: u32, key1: String, key2: String) -> Promise { wrap_api_future_json(async move { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind); - let key1: veilid_core::CryptoKey = + let key1: veilid_core::HashDigest = veilid_core::deserialize_json(&key1).map_err(VeilidAPIError::generic)?; - let key2: veilid_core::CryptoKey = + let key2: veilid_core::HashDigest = veilid_core::deserialize_json(&key2).map_err(VeilidAPIError::generic)?; let veilid_api = get_veilid_api()?; @@ -1395,9 +1395,9 @@ pub fn crypto_sign(kind: u32, key: String, secret: String, data: String) -> Prom wrap_api_future_json(async move { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind); - let key: veilid_core::CryptoKey = + let key: veilid_core::PublicKey = veilid_core::deserialize_json(&key).map_err(VeilidAPIError::generic)?; - let secret: veilid_core::CryptoKey = + let secret: veilid_core::SecretKey = veilid_core::deserialize_json(&secret).map_err(VeilidAPIError::generic)?; let data: Vec = data_encoding::BASE64URL_NOPAD @@ -1419,7 +1419,7 @@ pub fn crypto_verify(kind: u32, key: String, data: String, signature: String) -> wrap_api_future_plain(async move { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from(kind); - let key: veilid_core::CryptoKey = + let key: veilid_core::PublicKey = veilid_core::deserialize_json(&key).map_err(VeilidAPIError::generic)?; let data: Vec = data_encoding::BASE64URL_NOPAD .decode(data.as_bytes()) diff --git a/veilid-wasm/src/veilid_crypto_js.rs b/veilid-wasm/src/veilid_crypto_js.rs index 15bffaea..64909d5d 100644 --- a/veilid-wasm/src/veilid_crypto_js.rs +++ b/veilid-wasm/src/veilid_crypto_js.rs @@ -23,10 +23,8 @@ impl VeilidCrypto { veilid_core::best_crypto_kind().to_string() } - pub fn cachedDh(kind: String, key: String, secret: String) -> APIResult { + pub fn cachedDh(kind: String, key: PublicKey, secret: SecretKey) -> APIResult { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; - let key: veilid_core::PublicKey = veilid_core::PublicKey::from_str(&key)?; - let secret: veilid_core::SecretKey = veilid_core::SecretKey::from_str(&secret)?; let veilid_api = get_veilid_api()?; let crypto = veilid_api.crypto()?; @@ -38,15 +36,12 @@ impl VeilidCrypto { ) })?; let out = crypto_system.cached_dh(&key, &secret)?; - APIResult::Ok(out.to_string()) + APIResult::Ok(out) } - pub fn computeDh(kind: String, key: String, secret: String) -> APIResult { + pub fn computeDh(kind: String, key: PublicKey, secret: SecretKey) -> APIResult { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; - let key: veilid_core::PublicKey = veilid_core::PublicKey::from_str(&key)?; - let secret: veilid_core::SecretKey = veilid_core::SecretKey::from_str(&secret)?; - let veilid_api = get_veilid_api()?; let crypto = veilid_api.crypto()?; let crypto_system = crypto.get(kind).ok_or_else(|| { @@ -57,20 +52,17 @@ impl VeilidCrypto { ) })?; let out = crypto_system.compute_dh(&key, &secret)?; - APIResult::Ok(out.to_string()) + APIResult::Ok(out) } pub fn generateSharedSecret( kind: String, - key: String, - secret: String, + key: PublicKey, + secret: SecretKey, domain: Box<[u8]>, - ) -> APIResult { + ) -> APIResult { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; - let key: veilid_core::PublicKey = veilid_core::PublicKey::from_str(&key)?; - let secret: veilid_core::SecretKey = veilid_core::SecretKey::from_str(&secret)?; - let veilid_api = get_veilid_api()?; let crypto = veilid_api.crypto()?; let crypto_system = crypto.get(kind).ok_or_else(|| { @@ -81,7 +73,7 @@ impl VeilidCrypto { ) })?; let out = crypto_system.generate_shared_secret(&key, &secret, &domain)?; - APIResult::Ok(out.to_string()) + APIResult::Ok(out) } pub fn randomBytes(kind: String, len: u32) -> APIResult> { @@ -157,7 +149,7 @@ impl VeilidCrypto { kind: String, password: Box<[u8]>, salt: Box<[u8]>, - ) -> APIResult { + ) -> APIResult { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; let veilid_api = get_veilid_api()?; @@ -170,10 +162,10 @@ impl VeilidCrypto { ) })?; let out = crypto_system.derive_shared_secret(&password, &salt)?; - APIResult::Ok(out.to_string()) + APIResult::Ok(out) } - pub fn randomNonce(kind: String) -> APIResult { + pub fn randomNonce(kind: String) -> APIResult { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; let veilid_api = get_veilid_api()?; @@ -186,10 +178,10 @@ impl VeilidCrypto { ) })?; let out = crypto_system.random_nonce(); - APIResult::Ok(out.to_string()) + APIResult::Ok(out) } - pub fn randomSharedSecret(kind: String) -> APIResult { + pub fn randomSharedSecret(kind: String) -> APIResult { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; let veilid_api = get_veilid_api()?; @@ -202,7 +194,7 @@ impl VeilidCrypto { ) })?; let out = crypto_system.random_shared_secret(); - APIResult::Ok(out.to_string()) + APIResult::Ok(out) } pub fn verifySignatures( @@ -211,10 +203,10 @@ impl VeilidCrypto { signatures: StringArray, ) -> VeilidAPIResult> { let node_ids = into_unchecked_string_vec(node_ids); - let node_ids: Vec = node_ids + let node_ids: Vec = node_ids .iter() .map(|k| { - veilid_core::TypedKey::from_str(k).map_err(|e| { + veilid_core::TypedPublicKey::from_str(k).map_err(|e| { VeilidAPIError::invalid_argument( "verifySignatures()", format!("error decoding nodeid in node_ids[]: {}", e), @@ -222,7 +214,7 @@ impl VeilidCrypto { ) }) }) - .collect::>>()?; + .collect::>>()?; let typed_signatures = into_unchecked_string_vec(signatures); let typed_signatures: Vec = typed_signatures @@ -293,7 +285,7 @@ impl VeilidCrypto { APIResult::Ok(out) } - pub fn generateHash(kind: String, data: Box<[u8]>) -> APIResult { + pub fn generateHash(kind: String, data: Box<[u8]>) -> APIResult { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; let veilid_api = get_veilid_api()?; @@ -306,7 +298,7 @@ impl VeilidCrypto { ) })?; let out = crypto_system.generate_hash(&data); - APIResult::Ok(out.to_string()) + APIResult::Ok(out) } pub fn validateKeyPair(kind: String, key: String, secret: String) -> APIResult { @@ -328,11 +320,9 @@ impl VeilidCrypto { APIResult::Ok(out) } - pub fn validateHash(kind: String, data: Box<[u8]>, hash: String) -> APIResult { + pub fn validateHash(kind: String, data: Box<[u8]>, hash: HashDigest) -> APIResult { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; - let hash: veilid_core::HashDigest = veilid_core::HashDigest::from_str(&hash)?; - let veilid_api = get_veilid_api()?; let crypto = veilid_api.crypto()?; let crypto_system = crypto.get(kind).ok_or_else(|| { @@ -346,12 +336,9 @@ impl VeilidCrypto { APIResult::Ok(out) } - pub fn distance(kind: String, key1: String, key2: String) -> APIResult { + pub fn distance(kind: String, hash1: HashDigest, hash2: HashDigest) -> APIResult { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; - let key1: veilid_core::CryptoKey = veilid_core::CryptoKey::from_str(&key1)?; - let key2: veilid_core::CryptoKey = veilid_core::CryptoKey::from_str(&key2)?; - let veilid_api = get_veilid_api()?; let crypto = veilid_api.crypto()?; let crypto_system = crypto.get(kind).ok_or_else(|| { @@ -361,11 +348,16 @@ impl VeilidCrypto { kind.to_string(), ) })?; - let out = crypto_system.distance(&key1, &key2); - APIResult::Ok(out.to_string()) + let out = crypto_system.distance(&hash1, &hash2); + APIResult::Ok(out) } - pub fn sign(kind: String, key: String, secret: String, data: Box<[u8]>) -> APIResult { + pub fn sign( + kind: String, + key: String, + secret: String, + data: Box<[u8]>, + ) -> APIResult { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; let key: veilid_core::PublicKey = veilid_core::PublicKey::from_str(&key)?; @@ -377,20 +369,17 @@ impl VeilidCrypto { veilid_core::VeilidAPIError::invalid_argument("crypto_sign", "kind", kind.to_string()) })?; let out = crypto_system.sign(&key, &secret, &data)?; - APIResult::Ok(out.to_string()) + APIResult::Ok(out) } pub fn verify( kind: String, - key: String, + key: PublicKey, data: Box<[u8]>, - signature: String, + signature: Signature, ) -> APIResult { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; - let key: veilid_core::PublicKey = veilid_core::PublicKey::from_str(&key)?; - let signature: veilid_core::Signature = veilid_core::Signature::from_str(&signature)?; - let veilid_api = get_veilid_api()?; let crypto = veilid_api.crypto()?; let crypto_system = crypto.get(kind).ok_or_else(|| { @@ -419,17 +408,12 @@ impl VeilidCrypto { pub fn decryptAead( kind: String, body: Box<[u8]>, - nonce: String, - shared_secret: String, + nonce: Nonce, + shared_secret: SharedSecret, associated_data: Option>, ) -> APIResult> { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; - let nonce: veilid_core::Nonce = veilid_core::Nonce::from_str(&nonce)?; - - let shared_secret: veilid_core::SharedSecret = - veilid_core::SharedSecret::from_str(&shared_secret)?; - let veilid_api = get_veilid_api()?; let crypto = veilid_api.crypto()?; let crypto_system = crypto.get(kind).ok_or_else(|| { @@ -455,17 +439,12 @@ impl VeilidCrypto { pub fn encryptAead( kind: String, body: Box<[u8]>, - nonce: String, - shared_secret: String, + nonce: Nonce, + shared_secret: SharedSecret, associated_data: Option>, ) -> APIResult> { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; - let nonce: veilid_core::Nonce = veilid_core::Nonce::from_str(&nonce)?; - - let shared_secret: veilid_core::SharedSecret = - veilid_core::SharedSecret::from_str(&shared_secret)?; - let veilid_api = get_veilid_api()?; let crypto = veilid_api.crypto()?; let crypto_system = crypto.get(kind).ok_or_else(|| { @@ -490,16 +469,11 @@ impl VeilidCrypto { pub fn cryptNoAuth( kind: String, mut body: Box<[u8]>, - nonce: String, - shared_secret: String, + nonce: Nonce, + shared_secret: SharedSecret, ) -> APIResult> { let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; - let nonce: veilid_core::Nonce = veilid_core::Nonce::from_str(&nonce)?; - - let shared_secret: veilid_core::SharedSecret = - veilid_core::SharedSecret::from_str(&shared_secret)?; - let veilid_api = get_veilid_api()?; let crypto = veilid_api.crypto()?; let crypto_system = crypto.get(kind).ok_or_else(|| { diff --git a/veilid-wasm/src/veilid_routing_context_js.rs b/veilid-wasm/src/veilid_routing_context_js.rs index 0c4d550e..1ca5288c 100644 --- a/veilid-wasm/src/veilid_routing_context_js.rs +++ b/veilid-wasm/src/veilid_routing_context_js.rs @@ -258,7 +258,7 @@ impl VeilidRoutingContext { key: String, default_writer: Option, ) -> APIResult { - let key = TypedKey::from_str(&key)?; + let key = TypedRecordKey::from_str(&key)?; let default_writer = default_writer .map(|default_writer| KeyPair::from_str(&default_writer)) .map_or(APIResult::Ok(None), |r| r.map(Some))?; @@ -272,7 +272,7 @@ impl VeilidRoutingContext { /// /// Closing a record allows you to re-open it with a different routing context pub async fn closeDhtRecord(&self, key: String) -> APIResult<()> { - let key = TypedKey::from_str(&key)?; + let key = TypedRecordKey::from_str(&key)?; let routing_context = self.getRoutingContext()?; routing_context.close_dht_record(key).await?; APIRESULT_UNDEFINED @@ -284,7 +284,7 @@ impl VeilidRoutingContext { /// Deleting a record does not delete it from the network, but will remove the storage of the record /// locally, and will prevent its value from being refreshed on the network by this node. pub async fn deleteDhtRecord(&self, key: String) -> APIResult<()> { - let key = TypedKey::from_str(&key)?; + let key = TypedRecordKey::from_str(&key)?; let routing_context = self.getRoutingContext()?; routing_context.delete_dht_record(key).await?; APIRESULT_UNDEFINED @@ -302,7 +302,7 @@ impl VeilidRoutingContext { subkey: u32, forceRefresh: bool, ) -> APIResult> { - let key = TypedKey::from_str(&key)?; + let key = TypedRecordKey::from_str(&key)?; let routing_context = self.getRoutingContext()?; let res = routing_context .get_dht_value(key, subkey, forceRefresh) @@ -324,7 +324,7 @@ impl VeilidRoutingContext { data: Box<[u8]>, writer: Option, ) -> APIResult> { - let key = TypedKey::from_str(&key)?; + let key = TypedRecordKey::from_str(&key)?; let data = data.into_vec(); let writer = writer .map(|writer| KeyPair::from_str(&writer)) @@ -369,7 +369,7 @@ impl VeilidRoutingContext { expiration: Option, count: Option, ) -> APIResult { - let key = TypedKey::from_str(&key)?; + let key = TypedRecordKey::from_str(&key)?; let expiration = if let Some(expiration) = expiration { Some(veilid_core::Timestamp::new( u64::from_str(&expiration).map_err(VeilidAPIError::generic)?, @@ -399,7 +399,7 @@ impl VeilidRoutingContext { key: String, subkeys: Option, ) -> APIResult { - let key = TypedKey::from_str(&key)?; + let key = TypedRecordKey::from_str(&key)?; let routing_context = self.getRoutingContext()?; let res = routing_context.cancel_dht_watch(key, subkeys).await?; APIResult::Ok(res) @@ -450,7 +450,7 @@ impl VeilidRoutingContext { subkeys: Option, scope: Option, ) -> APIResult { - let key = TypedKey::from_str(&key)?; + let key = TypedRecordKey::from_str(&key)?; let scope = scope.unwrap_or_default(); let routing_context = self.getRoutingContext()?; diff --git a/veilid-wasm/tests/package-lock.json b/veilid-wasm/tests/package-lock.json index 9d4fda73..2eb3f9b1 100644 --- a/veilid-wasm/tests/package-lock.json +++ b/veilid-wasm/tests/package-lock.json @@ -21,7 +21,7 @@ }, "../pkg": { "name": "veilid-wasm", - "version": "0.4.5", + "version": "0.4.6", "dev": true, "license": "MPL-2.0" },