diff --git a/veilid-core/src/intf/native/network_interfaces/mod.rs b/veilid-core/src/intf/native/network_interfaces/mod.rs index 84180b9f..59f1ae49 100644 --- a/veilid-core/src/intf/native/network_interfaces/mod.rs +++ b/veilid-core/src/intf/native/network_interfaces/mod.rs @@ -395,10 +395,16 @@ impl NetworkInterfaces { continue; } if let Some(pipv4) = intf.primary_ipv4() { - intf_addrs.push(pipv4); + // Skip temporary addresses because they're going to change + if !pipv4.is_temporary() { + intf_addrs.push(pipv4); + } } if let Some(pipv6) = intf.primary_ipv6() { - intf_addrs.push(pipv6); + // Skip temporary addresses because they're going to change + if !pipv6.is_temporary() { + intf_addrs.push(pipv6); + } } } diff --git a/veilid-core/src/rpc_processor/mod.rs b/veilid-core/src/rpc_processor/mod.rs index 4d3dee31..b4a06628 100644 --- a/veilid-core/src/rpc_processor/mod.rs +++ b/veilid-core/src/rpc_processor/mod.rs @@ -1678,7 +1678,10 @@ impl RPCProcessor { let send_channel = { let inner = self.inner.lock(); - inner.send_channel.as_ref().unwrap().clone() + let Some(send_channel) = inner.send_channel.as_ref().cloned() else { + bail!("send channel is closed"); + }; + send_channel }; let span_id = Span::current().id(); send_channel @@ -1714,7 +1717,10 @@ impl RPCProcessor { }; let send_channel = { let inner = self.inner.lock(); - inner.send_channel.as_ref().unwrap().clone() + let Some(send_channel) = inner.send_channel.as_ref().cloned() else { + bail!("send channel is closed"); + }; + send_channel }; let span_id = Span::current().id(); send_channel @@ -1753,7 +1759,10 @@ impl RPCProcessor { let send_channel = { let inner = self.inner.lock(); - inner.send_channel.as_ref().unwrap().clone() + let Some(send_channel) = inner.send_channel.as_ref().cloned() else { + bail!("send channel is closed"); + }; + send_channel }; let span_id = Span::current().id(); send_channel diff --git a/veilid-core/src/storage_manager/mod.rs b/veilid-core/src/storage_manager/mod.rs index cf5222ff..b6dad7a2 100644 --- a/veilid-core/src/storage_manager/mod.rs +++ b/veilid-core/src/storage_manager/mod.rs @@ -380,7 +380,7 @@ impl StorageManager { // Add to offline writes to flush inner.offline_subkey_writes.entry(key).and_modify(|x| { x.insert(subkey); } ).or_insert(ValueSubkeyRangeSet::single(subkey)); - return Ok(Some(signed_value_data.into_value_data())) + return Ok(None) }; // Drop the lock for network access @@ -393,7 +393,7 @@ impl StorageManager { key, subkey, safety_selection, - signed_value_data, + signed_value_data.clone(), descriptor, ) .await?; @@ -404,7 +404,13 @@ impl StorageManager { .handle_set_local_value(key, subkey, final_signed_value_data.clone()) .await?; - Ok(Some(final_signed_value_data.into_value_data())) + // Return the new value if it differs from what was asked to set + if final_signed_value_data.value_data() != signed_value_data.value_data() { + return Ok(Some(final_signed_value_data.into_value_data())); + } + + // If the original value was set, return None + Ok(None) } pub async fn watch_values( diff --git a/veilid-flutter/example/pubspec.lock b/veilid-flutter/example/pubspec.lock index f3b8af02..b121351f 100644 --- a/veilid-flutter/example/pubspec.lock +++ b/veilid-flutter/example/pubspec.lock @@ -357,6 +357,14 @@ packages: url: "https://pub.dev" source: hosted version: "3.0.2" + system_info_plus: + dependency: transitive + description: + name: system_info_plus + sha256: b915c811c6605b802f3988859bc2bb79c95f735762a75b5451741f7a2b949d1b + url: "https://pub.dev" + source: hosted + version: "0.0.5" term_glyph: dependency: transitive description: @@ -395,7 +403,7 @@ packages: path: ".." relative: true source: path - version: "0.1.1" + version: "0.1.6" win32: dependency: transitive description: diff --git a/veilid-flutter/lib/default_config.dart b/veilid-flutter/lib/default_config.dart index 3917842f..815c0793 100644 --- a/veilid-flutter/lib/default_config.dart +++ b/veilid-flutter/lib/default_config.dart @@ -1,7 +1,10 @@ +import 'dart:io'; + import 'package:flutter/foundation.dart' show kIsWeb; import 'package:path_provider/path_provider.dart'; import 'package:path/path.dart' as p; import 'package:system_info2/system_info2.dart' as sysinfo; +import 'package:system_info_plus/system_info_plus.dart'; import 'veilid.dart'; const int megaByte = 1024 * 1024; @@ -13,10 +16,13 @@ int getLocalSubkeyCacheSize() { return 1024; } -int getLocalMaxSubkeyCacheMemoryMb() { +Future getLocalMaxSubkeyCacheMemoryMb() async { if (kIsWeb) { return 256; } + if (Platform.isIOS || Platform.isAndroid) { + return (await SystemInfoPlus.physicalMemory ?? 2048) ~/ 32; + } return sysinfo.SysInfo.getTotalPhysicalMemory() ~/ 32 ~/ megaByte; } @@ -34,10 +40,13 @@ int getRemoteMaxRecords() { return 128; } -int getRemoteMaxSubkeyCacheMemoryMb() { +Future getRemoteMaxSubkeyCacheMemoryMb() async { if (kIsWeb) { return 256; } + if (Platform.isIOS || Platform.isAndroid) { + return (await SystemInfoPlus.physicalMemory ?? 2048) ~/ 32; + } return sysinfo.SysInfo.getTotalPhysicalMemory() ~/ 32 ~/ megaByte; } @@ -121,10 +130,10 @@ Future getDefaultVeilidConfig(String programName) async { minPeerRefreshTimeMs: 60000, validateDialInfoReceiptTimeMs: 2000, localSubkeyCacheSize: getLocalSubkeyCacheSize(), - localMaxSubkeyCacheMemoryMb: getLocalMaxSubkeyCacheMemoryMb(), + localMaxSubkeyCacheMemoryMb: await getLocalMaxSubkeyCacheMemoryMb(), remoteSubkeyCacheSize: getRemoteSubkeyCacheSize(), remoteMaxRecords: getRemoteMaxRecords(), - remoteMaxSubkeyCacheMemoryMb: getRemoteMaxSubkeyCacheMemoryMb(), + remoteMaxSubkeyCacheMemoryMb: await getRemoteMaxSubkeyCacheMemoryMb(), remoteMaxStorageSpaceMb: getRemoteMaxStorageSpaceMb()), upnp: true, detectAddressChanges: true, diff --git a/veilid-flutter/lib/routing_context.dart b/veilid-flutter/lib/routing_context.dart index 56c2de6a..f71cd285 100644 --- a/veilid-flutter/lib/routing_context.dart +++ b/veilid-flutter/lib/routing_context.dart @@ -5,7 +5,6 @@ import 'package:change_case/change_case.dart'; import 'package:equatable/equatable.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; -import 'veilid_encoding.dart'; import 'veilid.dart'; part 'routing_context.freezed.dart'; @@ -51,8 +50,8 @@ sealed class DHTSchema with _$DHTSchema { {required int oCnt, required List members}) = DHTSchemaSMPL; - factory DHTSchema.fromJson(Map json) => - _$DHTSchemaFromJson(json); + factory DHTSchema.fromJson(dynamic json) => + _$DHTSchemaFromJson(json as Map); } const DHTSchema defaultDHTSchema = DHTSchema.dflt(oCnt: 1); @@ -65,8 +64,8 @@ class DHTSchemaMember with _$DHTSchemaMember { required int mCnt, }) = _DHTSchemaMember; - factory DHTSchemaMember.fromJson(Map json) => - _$DHTSchemaMemberFromJson(json); + factory DHTSchemaMember.fromJson(dynamic json) => + _$DHTSchemaMemberFromJson(json as Map); } ////////////////////////////////////// @@ -80,8 +79,8 @@ class DHTRecordDescriptor with _$DHTRecordDescriptor { PublicKey? ownerSecret, required DHTSchema schema, }) = _DHTRecordDescriptor; - factory DHTRecordDescriptor.fromJson(Map json) => - _$DHTRecordDescriptorFromJson(json); + factory DHTRecordDescriptor.fromJson(dynamic json) => + _$DHTRecordDescriptorFromJson(json as Map); } extension DHTRecordDescriptorExt on DHTRecordDescriptor { @@ -112,8 +111,8 @@ class ValueSubkeyRange with _$ValueSubkeyRange { required int high, }) = _ValueSubkeyRange; - factory ValueSubkeyRange.fromJson(Map json) => - _$ValueSubkeyRangeFromJson(json); + factory ValueSubkeyRange.fromJson(dynamic json) => + _$ValueSubkeyRangeFromJson(json as Map); } ////////////////////////////////////// @@ -128,8 +127,8 @@ class ValueData with _$ValueData { required PublicKey writer, }) = _ValueData; - factory ValueData.fromJson(Map json) => - _$ValueDataFromJson(json); + factory ValueData.fromJson(dynamic json) => + _$ValueDataFromJson(json as Map); } ////////////////////////////////////// @@ -140,8 +139,8 @@ enum Stability { reliable; String toJson() => name.toPascalCase(); - factory Stability.fromJson(String j) => - Stability.values.byName(j.toCamelCase()); + factory Stability.fromJson(dynamic j) => + Stability.values.byName((j as String).toCamelCase()); } ////////////////////////////////////// @@ -153,8 +152,8 @@ enum Sequencing { ensureOrdered; String toJson() => name.toPascalCase(); - factory Sequencing.fromJson(String j) => - Sequencing.values.byName(j.toCamelCase()); + factory Sequencing.fromJson(dynamic j) => + Sequencing.values.byName((j as String).toCamelCase()); } ////////////////////////////////////// @@ -162,7 +161,8 @@ enum Sequencing { @immutable abstract class SafetySelection extends Equatable { - factory SafetySelection.fromJson(Map json) { + factory SafetySelection.fromJson(dynamic jsond) { + final json = jsond as Map; if (json.containsKey("Unsafe")) { return SafetySelectionUnsafe( sequencing: Sequencing.fromJson(json["Unsafe"])); @@ -223,8 +223,8 @@ class SafetySpec with _$SafetySpec { required Sequencing sequencing, }) = _SafetySpec; - factory SafetySpec.fromJson(Map json) => - _$SafetySpecFromJson(json); + factory SafetySpec.fromJson(dynamic json) => + _$SafetySpecFromJson(json as Map); } ////////////////////////////////////// @@ -234,8 +234,8 @@ class RouteBlob with _$RouteBlob { const factory RouteBlob( {required String routeId, @Uint8ListJsonConverter() required Uint8List blob}) = _RouteBlob; - factory RouteBlob.fromJson(Map json) => - _$RouteBlobFromJson(json); + factory RouteBlob.fromJson(dynamic json) => + _$RouteBlobFromJson(json as Map); } ////////////////////////////////////// diff --git a/veilid-flutter/lib/veilid.dart b/veilid-flutter/lib/veilid.dart index 4c774228..b9c370e6 100644 --- a/veilid-flutter/lib/veilid.dart +++ b/veilid-flutter/lib/veilid.dart @@ -156,7 +156,7 @@ abstract class Veilid { Future releasePrivateRoute(String key); // App calls - Future appCallReply(String id, Uint8List message); + Future appCallReply(String callId, Uint8List message); // TableStore Future openTableDB(String name, int columnCount); diff --git a/veilid-flutter/lib/veilid_config.dart b/veilid-flutter/lib/veilid_config.dart index 3f6f2224..bce2437f 100644 --- a/veilid-flutter/lib/veilid_config.dart +++ b/veilid-flutter/lib/veilid_config.dart @@ -17,8 +17,8 @@ class VeilidFFIConfigLoggingTerminal with _$VeilidFFIConfigLoggingTerminal { required VeilidConfigLogLevel level, }) = _VeilidFFIConfigLoggingTerminal; - factory VeilidFFIConfigLoggingTerminal.fromJson(Map json) => - _$VeilidFFIConfigLoggingTerminalFromJson(json); + factory VeilidFFIConfigLoggingTerminal.fromJson(dynamic json) => + _$VeilidFFIConfigLoggingTerminalFromJson(json as Map); } @freezed @@ -30,8 +30,8 @@ class VeilidFFIConfigLoggingOtlp with _$VeilidFFIConfigLoggingOtlp { required String serviceName, }) = _VeilidFFIConfigLoggingOtlp; - factory VeilidFFIConfigLoggingOtlp.fromJson(Map json) => - _$VeilidFFIConfigLoggingOtlpFromJson(json); + factory VeilidFFIConfigLoggingOtlp.fromJson(dynamic json) => + _$VeilidFFIConfigLoggingOtlpFromJson(json as Map); } @freezed @@ -41,8 +41,8 @@ class VeilidFFIConfigLoggingApi with _$VeilidFFIConfigLoggingApi { required VeilidConfigLogLevel level, }) = _VeilidFFIConfigLoggingApi; - factory VeilidFFIConfigLoggingApi.fromJson(Map json) => - _$VeilidFFIConfigLoggingApiFromJson(json); + factory VeilidFFIConfigLoggingApi.fromJson(dynamic json) => + _$VeilidFFIConfigLoggingApiFromJson(json as Map); } @freezed @@ -52,8 +52,8 @@ class VeilidFFIConfigLogging with _$VeilidFFIConfigLogging { required VeilidFFIConfigLoggingOtlp otlp, required VeilidFFIConfigLoggingApi api}) = _VeilidFFIConfigLogging; - factory VeilidFFIConfigLogging.fromJson(Map json) => - _$VeilidFFIConfigLoggingFromJson(json); + factory VeilidFFIConfigLogging.fromJson(dynamic json) => + _$VeilidFFIConfigLoggingFromJson(json as Map); } @freezed @@ -62,8 +62,8 @@ class VeilidFFIConfig with _$VeilidFFIConfig { required VeilidFFIConfigLogging logging, }) = _VeilidFFIConfig; - factory VeilidFFIConfig.fromJson(Map json) => - _$VeilidFFIConfigFromJson(json); + factory VeilidFFIConfig.fromJson(dynamic json) => + _$VeilidFFIConfigFromJson(json as Map); } ////////////////////////////////////////////////////////// @@ -79,9 +79,9 @@ class VeilidWASMConfigLoggingPerformance required bool logsInConsole, }) = _VeilidWASMConfigLoggingPerformance; - factory VeilidWASMConfigLoggingPerformance.fromJson( - Map json) => - _$VeilidWASMConfigLoggingPerformanceFromJson(json); + factory VeilidWASMConfigLoggingPerformance.fromJson(dynamic json) => + _$VeilidWASMConfigLoggingPerformanceFromJson( + json as Map); } @freezed @@ -91,8 +91,8 @@ class VeilidWASMConfigLoggingApi with _$VeilidWASMConfigLoggingApi { required VeilidConfigLogLevel level, }) = _VeilidWASMConfigLoggingApi; - factory VeilidWASMConfigLoggingApi.fromJson(Map json) => - _$VeilidWASMConfigLoggingApiFromJson(json); + factory VeilidWASMConfigLoggingApi.fromJson(dynamic json) => + _$VeilidWASMConfigLoggingApiFromJson(json as Map); } @freezed @@ -101,8 +101,8 @@ class VeilidWASMConfigLogging with _$VeilidWASMConfigLogging { {required VeilidWASMConfigLoggingPerformance performance, required VeilidWASMConfigLoggingApi api}) = _VeilidWASMConfigLogging; - factory VeilidWASMConfigLogging.fromJson(Map json) => - _$VeilidWASMConfigLoggingFromJson(json); + factory VeilidWASMConfigLogging.fromJson(dynamic json) => + _$VeilidWASMConfigLoggingFromJson(json as Map); } @freezed @@ -111,8 +111,8 @@ class VeilidWASMConfig with _$VeilidWASMConfig { required VeilidWASMConfigLogging logging, }) = _VeilidWASMConfig; - factory VeilidWASMConfig.fromJson(Map json) => - _$VeilidWASMConfigFromJson(json); + factory VeilidWASMConfig.fromJson(dynamic json) => + _$VeilidWASMConfigFromJson(json as Map); } ////////////////////////////////////// @@ -147,8 +147,8 @@ class VeilidConfigHTTPS with _$VeilidConfigHTTPS { String? url, }) = _VeilidConfigHTTPS; - factory VeilidConfigHTTPS.fromJson(Map json) => - _$VeilidConfigHTTPSFromJson(json); + factory VeilidConfigHTTPS.fromJson(dynamic json) => + _$VeilidConfigHTTPSFromJson(json as Map); } //////////// @@ -162,8 +162,8 @@ class VeilidConfigHTTP with _$VeilidConfigHTTP { String? url, }) = _VeilidConfigHTTP; - factory VeilidConfigHTTP.fromJson(Map json) => - _$VeilidConfigHTTPFromJson(json); + factory VeilidConfigHTTP.fromJson(dynamic json) => + _$VeilidConfigHTTPFromJson(json as Map); } //////////// @@ -175,8 +175,8 @@ class VeilidConfigApplication with _$VeilidConfigApplication { required VeilidConfigHTTP http, }) = _VeilidConfigApplication; - factory VeilidConfigApplication.fromJson(Map json) => - _$VeilidConfigApplicationFromJson(json); + factory VeilidConfigApplication.fromJson(dynamic json) => + _$VeilidConfigApplicationFromJson(json as Map); } //////////// @@ -188,8 +188,8 @@ class VeilidConfigUDP with _$VeilidConfigUDP { required String listenAddress, String? publicAddress}) = _VeilidConfigUDP; - factory VeilidConfigUDP.fromJson(Map json) => - _$VeilidConfigUDPFromJson(json); + factory VeilidConfigUDP.fromJson(dynamic json) => + _$VeilidConfigUDPFromJson(json as Map); } //////////// @@ -202,8 +202,8 @@ class VeilidConfigTCP with _$VeilidConfigTCP { required String listenAddress, String? publicAddress}) = _VeilidConfigTCP; - factory VeilidConfigTCP.fromJson(Map json) => - _$VeilidConfigTCPFromJson(json); + factory VeilidConfigTCP.fromJson(dynamic json) => + _$VeilidConfigTCPFromJson(json as Map); } //////////// @@ -217,8 +217,8 @@ class VeilidConfigWS with _$VeilidConfigWS { required String path, String? url}) = _VeilidConfigWS; - factory VeilidConfigWS.fromJson(Map json) => - _$VeilidConfigWSFromJson(json); + factory VeilidConfigWS.fromJson(dynamic json) => + _$VeilidConfigWSFromJson(json as Map); } //////////// @@ -232,8 +232,8 @@ class VeilidConfigWSS with _$VeilidConfigWSS { required String path, String? url}) = _VeilidConfigWSS; - factory VeilidConfigWSS.fromJson(Map json) => - _$VeilidConfigWSSFromJson(json); + factory VeilidConfigWSS.fromJson(dynamic json) => + _$VeilidConfigWSSFromJson(json as Map); } //////////// @@ -247,8 +247,8 @@ class VeilidConfigProtocol with _$VeilidConfigProtocol { required VeilidConfigWSS wss, }) = _VeilidConfigProtocol; - factory VeilidConfigProtocol.fromJson(Map json) => - _$VeilidConfigProtocolFromJson(json); + factory VeilidConfigProtocol.fromJson(dynamic json) => + _$VeilidConfigProtocolFromJson(json as Map); } //////////// @@ -261,8 +261,8 @@ class VeilidConfigTLS with _$VeilidConfigTLS { required int connectionInitialTimeoutMs, }) = _VeilidConfigTLS; - factory VeilidConfigTLS.fromJson(Map json) => - _$VeilidConfigTLSFromJson(json); + factory VeilidConfigTLS.fromJson(dynamic json) => + _$VeilidConfigTLSFromJson(json as Map); } //////////// @@ -289,8 +289,8 @@ class VeilidConfigDHT with _$VeilidConfigDHT { required int remoteMaxSubkeyCacheMemoryMb, required int remoteMaxStorageSpaceMb}) = _VeilidConfigDHT; - factory VeilidConfigDHT.fromJson(Map json) => - _$VeilidConfigDHTFromJson(json); + factory VeilidConfigDHT.fromJson(dynamic json) => + _$VeilidConfigDHTFromJson(json as Map); } //////////// @@ -306,8 +306,8 @@ class VeilidConfigRPC with _$VeilidConfigRPC { required int maxRouteHopCount, required int defaultRouteHopCount}) = _VeilidConfigRPC; - factory VeilidConfigRPC.fromJson(Map json) => - _$VeilidConfigRPCFromJson(json); + factory VeilidConfigRPC.fromJson(dynamic json) => + _$VeilidConfigRPCFromJson(json as Map); } //////////// @@ -325,8 +325,8 @@ class VeilidConfigRoutingTable with _$VeilidConfigRoutingTable { required int limitAttachedWeak, }) = _VeilidConfigRoutingTable; - factory VeilidConfigRoutingTable.fromJson(Map json) => - _$VeilidConfigRoutingTableFromJson(json); + factory VeilidConfigRoutingTable.fromJson(dynamic json) => + _$VeilidConfigRoutingTableFromJson(json as Map); } //////////// @@ -355,8 +355,8 @@ class VeilidConfigNetwork with _$VeilidConfigNetwork { required VeilidConfigProtocol protocol, }) = _VeilidConfigNetwork; - factory VeilidConfigNetwork.fromJson(Map json) => - _$VeilidConfigNetworkFromJson(json); + factory VeilidConfigNetwork.fromJson(dynamic json) => + _$VeilidConfigNetworkFromJson(json as Map); } //////////// @@ -368,8 +368,8 @@ class VeilidConfigTableStore with _$VeilidConfigTableStore { required bool delete, }) = _VeilidConfigTableStore; - factory VeilidConfigTableStore.fromJson(Map json) => - _$VeilidConfigTableStoreFromJson(json); + factory VeilidConfigTableStore.fromJson(dynamic json) => + _$VeilidConfigTableStoreFromJson(json as Map); } //////////// @@ -381,8 +381,8 @@ class VeilidConfigBlockStore with _$VeilidConfigBlockStore { required bool delete, }) = _VeilidConfigBlockStore; - factory VeilidConfigBlockStore.fromJson(Map json) => - _$VeilidConfigBlockStoreFromJson(json); + factory VeilidConfigBlockStore.fromJson(dynamic json) => + _$VeilidConfigBlockStoreFromJson(json as Map); } //////////// @@ -397,8 +397,8 @@ class VeilidConfigProtectedStore with _$VeilidConfigProtectedStore { required String deviceEncryptionKeyPassword, String? newDeviceEncryptionKeyPassword}) = _VeilidConfigProtectedStore; - factory VeilidConfigProtectedStore.fromJson(Map json) => - _$VeilidConfigProtectedStoreFromJson(json); + factory VeilidConfigProtectedStore.fromJson(dynamic json) => + _$VeilidConfigProtectedStoreFromJson(json as Map); } //////////// @@ -409,8 +409,8 @@ class VeilidConfigCapabilities with _$VeilidConfigCapabilities { required List disable, }) = _VeilidConfigCapabilities; - factory VeilidConfigCapabilities.fromJson(Map json) => - _$VeilidConfigCapabilitiesFromJson(json); + factory VeilidConfigCapabilities.fromJson(dynamic json) => + _$VeilidConfigCapabilitiesFromJson(json as Map); } //////////// @@ -427,6 +427,6 @@ class VeilidConfig with _$VeilidConfig { required VeilidConfigNetwork network, }) = _VeilidConfig; - factory VeilidConfig.fromJson(Map json) => - _$VeilidConfigFromJson(json); + factory VeilidConfig.fromJson(dynamic json) => + _$VeilidConfigFromJson(json as Map); } diff --git a/veilid-flutter/lib/veilid_crypto.dart b/veilid-flutter/lib/veilid_crypto.dart index b41a410d..98c20f90 100644 --- a/veilid-flutter/lib/veilid_crypto.dart +++ b/veilid-flutter/lib/veilid_crypto.dart @@ -18,17 +18,14 @@ const CryptoKind cryptoKindNONE = $N << 0 | $O << 8 | $N << 16 | $E << 24; // "NONE" String cryptoKindToString(CryptoKind kind) { - return "${String.fromCharCode(kind & 0xFF)}${String.fromCharCode((kind >> 8) & 0xFF)}${String.fromCharCode((kind >> 16) & 0xFF)}${String.fromCharCode((kind >> 24) & 0xFF)}"; + return cryptoKindToBytes(kind).map((c) => String.fromCharCode(c)).join(); } const CryptoKind bestCryptoKind = cryptoKindVLD0; Uint8List cryptoKindToBytes(CryptoKind kind) { var b = Uint8List(4); - b[0] = kind & 0xFF; - b[1] = (kind >> 8) & 0xFF; - b[2] = (kind >> 16) & 0xFF; - b[3] = (kind >> 24) & 0xFF; + ByteData.sublistView(b).setUint32(0, kind, Endian.big); return b; } @@ -36,10 +33,8 @@ CryptoKind cryptoKindFromString(String s) { if (s.codeUnits.length != 4) { throw const FormatException("malformed string"); } - CryptoKind kind = s.codeUnits[0] | - s.codeUnits[1] << 8 | - s.codeUnits[2] << 16 | - s.codeUnits[3] << 24; + CryptoKind kind = ByteData.sublistView(Uint8List.fromList(s.codeUnits)) + .getUint32(0, Endian.big); return kind; } @@ -71,9 +66,10 @@ class Typed extends Equatable { } Uint8List decode() { - var b = cryptoKindToBytes(kind); - b.addAll(value.decode()); - return b; + var b = BytesBuilder(); + b.add(cryptoKindToBytes(kind)); + b.add(value.decode()); + return b.toBytes(); } String toJson() => toString(); diff --git a/veilid-flutter/lib/veilid_encoding.dart b/veilid-flutter/lib/veilid_encoding.dart index d6334139..d10c1f28 100644 --- a/veilid-flutter/lib/veilid_encoding.dart +++ b/veilid-flutter/lib/veilid_encoding.dart @@ -27,7 +27,7 @@ class Uint8ListJsonConverter implements JsonConverter { const Uint8ListJsonConverter(); @override - Uint8List fromJson(String json) => base64UrlNoPadDecode(json); + Uint8List fromJson(dynamic json) => base64UrlNoPadDecode(json as String); @override String toJson(Uint8List data) => base64UrlNoPadEncode(data); } diff --git a/veilid-flutter/lib/veilid_ffi.dart b/veilid-flutter/lib/veilid_ffi.dart index 545641cc..73880aa3 100644 --- a/veilid-flutter/lib/veilid_ffi.dart +++ b/veilid-flutter/lib/veilid_ffi.dart @@ -368,7 +368,7 @@ Future processFuturePlain(Future future) { } Future processFutureJson( - T Function(Map) jsonConstructor, Future future) { + T Function(dynamic) jsonConstructor, Future future) { return future.then((value) { final list = value as List; switch (list[0] as int) { @@ -541,7 +541,7 @@ Future> processFutureStream( } Stream processStreamJson( - T Function(Map) jsonConstructor, ReceivePort port) async* { + T Function(dynamic) jsonConstructor, ReceivePort port) async* { try { await for (var value in port) { final list = value as List; @@ -688,7 +688,7 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { _ctx.ensureValid(); final nativeKey = jsonEncode(key).toNativeUtf8(); final nativeWriter = - writer != null ? jsonEncode(key).toNativeUtf8() : nullptr; + writer != null ? jsonEncode(writer).toNativeUtf8() : nullptr; final recvPort = ReceivePort("routing_context_open_dht_record"); final sendPort = recvPort.sendPort; _ctx.ffi._routingContextOpenDHTRecord( @@ -729,7 +729,7 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { final sendPort = recvPort.sendPort; _ctx.ffi._routingContextGetDHTValue( sendPort.nativePort, _ctx.id!, nativeKey, subkey, forceRefresh); - final valueData = await processFutureJson( + final valueData = await processFutureOptJson( optFromJson(ValueData.fromJson), recvPort.first); return valueData; } @@ -745,7 +745,7 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { final sendPort = recvPort.sendPort; _ctx.ffi._routingContextSetDHTValue( sendPort.nativePort, _ctx.id!, nativeKey, subkey, nativeData); - final valueData = await processFutureJson( + final valueData = await processFutureOptJson( optFromJson(ValueData.fromJson), recvPort.first); return valueData; } @@ -1651,8 +1651,8 @@ class VeilidFFI extends Veilid { } @override - Future appCallReply(String call_id, Uint8List message) { - final nativeCallId = call_id.toNativeUtf8(); + Future appCallReply(String callId, Uint8List message) { + final nativeCallId = callId.toNativeUtf8(); final nativeEncodedMessage = base64UrlNoPadEncode(message).toNativeUtf8(); final recvPort = ReceivePort("app_call_reply"); final sendPort = recvPort.sendPort; @@ -1681,15 +1681,15 @@ class VeilidFFI extends Veilid { @override List validCryptoKinds() { final vckString = _validCryptoKinds(); - final vck = jsonDecode(vckString.toDartString()); + final vck = jsonDecode(vckString.toDartString()) as List; _freeString(vckString); - return vck; + return vck.map((v) => v as CryptoKind).toList(); } @override Future getCryptoSystem(CryptoKind kind) async { if (!validCryptoKinds().contains(kind)) { - throw VeilidAPIExceptionGeneric("unsupported cryptosystem"); + throw const VeilidAPIExceptionGeneric("unsupported cryptosystem"); } return VeilidCryptoSystemFFI._(this, kind); } diff --git a/veilid-flutter/lib/veilid_js.dart b/veilid-flutter/lib/veilid_js.dart index eadd6d28..42045dd1 100644 --- a/veilid-flutter/lib/veilid_js.dart +++ b/veilid-flutter/lib/veilid_js.dart @@ -571,13 +571,15 @@ class VeilidJS extends Veilid { @override List validCryptoKinds() { - return jsonDecode(js_util.callMethod(wasm, "valid_crypto_kinds", [])); + final vck = jsonDecode(js_util.callMethod(wasm, "valid_crypto_kinds", [])) + as List; + return vck.map((v) => v as CryptoKind).toList(); } @override Future getCryptoSystem(CryptoKind kind) async { if (!validCryptoKinds().contains(kind)) { - throw VeilidAPIExceptionGeneric("unsupported cryptosystem"); + throw const VeilidAPIExceptionGeneric("unsupported cryptosystem"); } return VeilidCryptoSystemJS._(this, kind); } diff --git a/veilid-flutter/lib/veilid_state.dart b/veilid-flutter/lib/veilid_state.dart index 00927a4d..9a7146d5 100644 --- a/veilid-flutter/lib/veilid_state.dart +++ b/veilid-flutter/lib/veilid_state.dart @@ -22,8 +22,8 @@ enum AttachmentState { detaching; String toJson() => name.toPascalCase(); - factory AttachmentState.fromJson(String j) => - AttachmentState.values.byName(j.toCamelCase()); + factory AttachmentState.fromJson(dynamic j) => + AttachmentState.values.byName((j as String).toCamelCase()); } ////////////////////////////////////// @@ -37,8 +37,8 @@ enum VeilidLogLevel { trace; String toJson() => name.toPascalCase(); - factory VeilidLogLevel.fromJson(String j) => - VeilidLogLevel.values.byName(j.toCamelCase()); + factory VeilidLogLevel.fromJson(dynamic j) => + VeilidLogLevel.values.byName((j as String).toCamelCase()); } //////////// @@ -51,8 +51,8 @@ class LatencyStats with _$LatencyStats { required TimestampDuration slowest, }) = _LatencyStats; - factory LatencyStats.fromJson(Map json) => - _$LatencyStatsFromJson(json); + factory LatencyStats.fromJson(dynamic json) => + _$LatencyStatsFromJson(json as Map); } //////////// @@ -66,8 +66,8 @@ class TransferStats with _$TransferStats { required BigInt minimum, }) = _TransferStats; - factory TransferStats.fromJson(Map json) => - _$TransferStatsFromJson(json); + factory TransferStats.fromJson(dynamic json) => + _$TransferStatsFromJson(json as Map); } //////////// @@ -79,8 +79,8 @@ class TransferStatsDownUp with _$TransferStatsDownUp { required TransferStats up, }) = _TransferStatsDownUp; - factory TransferStatsDownUp.fromJson(Map json) => - _$TransferStatsDownUpFromJson(json); + factory TransferStatsDownUp.fromJson(dynamic json) => + _$TransferStatsDownUpFromJson(json as Map); } //////////// @@ -98,8 +98,8 @@ class RPCStats with _$RPCStats { required int failedToSend, }) = _RPCStats; - factory RPCStats.fromJson(Map json) => - _$RPCStatsFromJson(json); + factory RPCStats.fromJson(dynamic json) => + _$RPCStatsFromJson(json as Map); } //////////// @@ -113,8 +113,8 @@ class PeerStats with _$PeerStats { required TransferStatsDownUp transfer, }) = _PeerStats; - factory PeerStats.fromJson(Map json) => - _$PeerStatsFromJson(json); + factory PeerStats.fromJson(dynamic json) => + _$PeerStatsFromJson(json as Map); } //////////// @@ -127,8 +127,8 @@ class PeerTableData with _$PeerTableData { required PeerStats peerStats, }) = _PeerTableData; - factory PeerTableData.fromJson(Map json) => - _$PeerTableDataFromJson(json); + factory PeerTableData.fromJson(dynamic json) => + _$PeerTableDataFromJson(json as Map); } ////////////////////////////////////// @@ -173,8 +173,8 @@ sealed class VeilidUpdate with _$VeilidUpdate { required ValueData valueData, }) = VeilidUpdateValueChange; - factory VeilidUpdate.fromJson(Map json) => - _$VeilidUpdateFromJson(json); + factory VeilidUpdate.fromJson(dynamic json) => + _$VeilidUpdateFromJson(json as Map); } ////////////////////////////////////// @@ -187,8 +187,8 @@ class VeilidStateAttachment with _$VeilidStateAttachment { required bool publicInternetReady, required bool localNetworkReady}) = _VeilidStateAttachment; - factory VeilidStateAttachment.fromJson(Map json) => - _$VeilidStateAttachmentFromJson(json); + factory VeilidStateAttachment.fromJson(dynamic json) => + _$VeilidStateAttachmentFromJson(json as Map); } ////////////////////////////////////// @@ -202,8 +202,8 @@ class VeilidStateNetwork with _$VeilidStateNetwork { required BigInt bpsUp, required List peers}) = _VeilidStateNetwork; - factory VeilidStateNetwork.fromJson(Map json) => - _$VeilidStateNetworkFromJson(json); + factory VeilidStateNetwork.fromJson(dynamic json) => + _$VeilidStateNetworkFromJson(json as Map); } ////////////////////////////////////// @@ -215,8 +215,8 @@ class VeilidStateConfig with _$VeilidStateConfig { required VeilidConfig config, }) = _VeilidStateConfig; - factory VeilidStateConfig.fromJson(Map json) => - _$VeilidStateConfigFromJson(json); + factory VeilidStateConfig.fromJson(dynamic json) => + _$VeilidStateConfigFromJson(json as Map); } ////////////////////////////////////// @@ -230,6 +230,6 @@ class VeilidState with _$VeilidState { required VeilidStateConfig config, }) = _VeilidState; - factory VeilidState.fromJson(Map json) => - _$VeilidStateFromJson(json); + factory VeilidState.fromJson(dynamic json) => + _$VeilidStateFromJson(json as Map); } diff --git a/veilid-flutter/pubspec.yaml b/veilid-flutter/pubspec.yaml index 1adadf0e..d4638878 100644 --- a/veilid-flutter/pubspec.yaml +++ b/veilid-flutter/pubspec.yaml @@ -17,6 +17,7 @@ dependencies: path_provider: ^2.0.9 path: ^1.8.0 system_info2: ^3.0.2 + system_info_plus: ^0.0.5 charcode: ^1.3.1 freezed_annotation: ^2.2.0 json_annotation: ^4.8.1 diff --git a/veilid-flutter/rust/src/dart_ffi.rs b/veilid-flutter/rust/src/dart_ffi.rs index b7c6111f..452944f3 100644 --- a/veilid-flutter/rust/src/dart_ffi.rs +++ b/veilid-flutter/rust/src/dart_ffi.rs @@ -367,9 +367,8 @@ pub extern "C" fn shutdown_veilid_core(port: i64) { }); } -fn add_routing_context(routing_context: veilid_core::RoutingContext) -> u32 { +fn add_routing_context(rc: &mut BTreeMap, routing_context: veilid_core::RoutingContext) -> u32 { let mut next_id: u32 = 1; - let mut rc = ROUTING_CONTEXTS.lock(); while rc.contains_key(&next_id) { next_id += 1; } @@ -382,7 +381,8 @@ pub extern "C" fn routing_context(port: i64) { DartIsolateWrapper::new(port).spawn_result(async move { let veilid_api = get_veilid_api().await?; let routing_context = veilid_api.routing_context(); - let new_id = add_routing_context(routing_context); + let mut rc = ROUTING_CONTEXTS.lock(); + let new_id = add_routing_context(&mut *rc, routing_context); APIResult::Ok(new_id) }); } @@ -398,14 +398,14 @@ pub extern "C" fn release_routing_context(id: u32) -> i32 { #[no_mangle] pub extern "C" fn routing_context_with_privacy(id: u32) -> u32 { - let rc = ROUTING_CONTEXTS.lock(); + let mut rc = ROUTING_CONTEXTS.lock(); let Some(routing_context) = rc.get(&id) else { return 0; }; let Ok(routing_context) = routing_context.clone().with_privacy() else { return 0; }; - let new_id = add_routing_context(routing_context); + let new_id = add_routing_context(&mut rc, routing_context); new_id } @@ -414,14 +414,14 @@ pub extern "C" fn routing_context_with_custom_privacy(id: u32, safety_selection: let safety_selection: veilid_core::SafetySelection = veilid_core::deserialize_opt_json(safety_selection.into_opt_string()).unwrap(); - let rc = ROUTING_CONTEXTS.lock(); + let mut rc = ROUTING_CONTEXTS.lock(); let Some(routing_context) = rc.get(&id) else { return 0; }; let Ok(routing_context) = routing_context.clone().with_custom_privacy(safety_selection) else { return 0; }; - let new_id = add_routing_context(routing_context); + let new_id = add_routing_context(&mut rc, routing_context); new_id } @@ -430,12 +430,12 @@ pub extern "C" fn routing_context_with_sequencing(id: u32, sequencing: FfiStr) - let sequencing: veilid_core::Sequencing = veilid_core::deserialize_opt_json(sequencing.into_opt_string()).unwrap(); - let rc = ROUTING_CONTEXTS.lock(); + let mut rc = ROUTING_CONTEXTS.lock(); let Some(routing_context) = rc.get(&id) else { return 0; }; let routing_context = routing_context.clone().with_sequencing(sequencing); - let new_id = add_routing_context(routing_context); + let new_id = add_routing_context(&mut rc, routing_context); new_id } @@ -569,7 +569,7 @@ pub extern "C" fn routing_context_delete_dht_record(port: i64, id: u32, key: Ffi #[no_mangle] pub extern "C" fn routing_context_get_dht_value(port: i64, id: u32, key: FfiStr, subkey: u32, force_refresh: bool) { let key: veilid_core::TypedKey = veilid_core::deserialize_opt_json(key.into_opt_string()).unwrap(); - DartIsolateWrapper::new(port).spawn_result_opt_json(async move { + DartIsolateWrapper::new(port).spawn_result_json(async move { let routing_context = { let rc = ROUTING_CONTEXTS.lock(); let Some(routing_context) = rc.get(&id) else { @@ -594,7 +594,7 @@ pub extern "C" fn routing_context_set_dht_value(port: i64, id: u32, key: FfiStr, ) .unwrap(); - DartIsolateWrapper::new(port).spawn_result_opt_json(async move { + DartIsolateWrapper::new(port).spawn_result_json(async move { let routing_context = { let rc = ROUTING_CONTEXTS.lock(); let Some(routing_context) = rc.get(&id) else { @@ -1404,7 +1404,7 @@ pub extern "C" fn crypto_decrypt_aead(port: i64, kind: u32, body: FfiStr, nonce: let associated_data: Option> = associated_data.into_opt_string().map(|s| data_encoding::BASE64URL_NOPAD.decode(s.as_bytes()).unwrap()); - DartIsolateWrapper::new(port).spawn_result_json(async move { + DartIsolateWrapper::new(port).spawn_result(async move { let veilid_api = get_veilid_api().await?; let crypto = veilid_api.crypto()?; let csv = crypto.get(kind).ok_or_else(|| veilid_core::VeilidAPIError::invalid_argument("crypto_decrypt_aead", "kind", kind.to_string()))?; @@ -1412,6 +1412,7 @@ pub extern "C" fn crypto_decrypt_aead(port: i64, kind: u32, body: FfiStr, nonce: Some(ad) => Some(ad.as_slice()), None => None })?; + let out = data_encoding::BASE64URL_NOPAD.encode(&out); APIResult::Ok(out) }); } @@ -1436,7 +1437,7 @@ pub extern "C" fn crypto_encrypt_aead(port: i64, kind: u32, body: FfiStr, nonce: let associated_data: Option> = associated_data.into_opt_string().map(|s| data_encoding::BASE64URL_NOPAD.decode(s.as_bytes()).unwrap()); - DartIsolateWrapper::new(port).spawn_result_json(async move { + DartIsolateWrapper::new(port).spawn_result(async move { let veilid_api = get_veilid_api().await?; let crypto = veilid_api.crypto()?; let csv = crypto.get(kind).ok_or_else(|| veilid_core::VeilidAPIError::invalid_argument("crypto_encrypt_aead", "kind", kind.to_string()))?; @@ -1444,6 +1445,7 @@ pub extern "C" fn crypto_encrypt_aead(port: i64, kind: u32, body: FfiStr, nonce: Some(ad) => Some(ad.as_slice()), None => None })?; + let out = data_encoding::BASE64URL_NOPAD.encode(&out); APIResult::Ok(out) }); } @@ -1468,11 +1470,12 @@ pub extern "C" fn crypto_crypt_no_auth(port: i64, kind: u32, body: FfiStr, nonce let shared_secret: veilid_core::SharedSecret = veilid_core::deserialize_opt_json(shared_secret.into_opt_string()).unwrap(); - DartIsolateWrapper::new(port).spawn_result_json(async move { + DartIsolateWrapper::new(port).spawn_result(async move { let veilid_api = get_veilid_api().await?; let crypto = veilid_api.crypto()?; let csv = crypto.get(kind).ok_or_else(|| veilid_core::VeilidAPIError::invalid_argument("crypto_crypt_no_auth", "kind", kind.to_string()))?; csv.crypt_in_place_no_auth(&mut body, &nonce, &shared_secret); + let body = data_encoding::BASE64URL_NOPAD.encode(&body); APIResult::Ok(body) }); } diff --git a/veilid-flutter/rust/src/dart_isolate_wrapper.rs b/veilid-flutter/rust/src/dart_isolate_wrapper.rs index 3adc7d50..2c0bb371 100644 --- a/veilid-flutter/rust/src/dart_isolate_wrapper.rs +++ b/veilid-flutter/rust/src/dart_isolate_wrapper.rs @@ -52,17 +52,6 @@ impl DartIsolateWrapper { }); } - pub fn spawn_result_opt_json(self, future: F) - where - F: Future, E>> + Send + 'static, - T: Serialize + Debug, - E: Serialize + Debug, - { - spawn(async move { - self.result_opt_json(future.await); - }); - } - pub fn result(self, result: Result) -> bool { match result { Ok(v) => self.ok(v), @@ -78,16 +67,6 @@ impl DartIsolateWrapper { Err(e) => self.err_json(e), } } - pub fn result_opt_json( - self, - result: Result, E>, - ) -> bool { - match result { - Ok(Some(v)) => self.ok_json(v), - Ok(None) => self.ok(()), - Err(e) => self.err_json(e), - } - } pub fn ok(self, value: T) -> bool { self.isolate .post(vec![MESSAGE_OK.into_dart(), value.into_dart()]) diff --git a/veilid-wasm/src/lib.rs b/veilid-wasm/src/lib.rs index 48e4f7d0..085e3f3f 100644 --- a/veilid-wasm/src/lib.rs +++ b/veilid-wasm/src/lib.rs @@ -60,12 +60,6 @@ fn take_veilid_api() -> Result(val: T) -> JsValue { JsValue::from_str(&serialize_json(val)) } -pub fn to_opt_json(val: Option) -> JsValue { - match val { - Some(v) => JsValue::from_str(&serialize_json(v)), - None => JsValue::UNDEFINED, - } -} pub fn to_jsvalue(val: T) -> JsValue where @@ -120,14 +114,6 @@ where future_to_promise(future.map(|res| res.map(|v| to_json(v)).map_err(|e| to_json(e)))) } -pub fn wrap_api_future_opt_json(future: F) -> Promise -where - F: Future>> + 'static, - T: Serialize + Debug + 'static, -{ - future_to_promise(future.map(|res| res.map(|v| to_opt_json(v)).map_err(|e| to_json(e)))) -} - pub fn wrap_api_future_plain(future: F) -> Promise where F: Future> + 'static, @@ -507,7 +493,7 @@ pub fn routing_context_get_dht_value( force_refresh: bool, ) -> Promise { let key: veilid_core::TypedKey = veilid_core::deserialize_json(&key).unwrap(); - wrap_api_future_opt_json(async move { + wrap_api_future_json(async move { let routing_context = { let rc = (*ROUTING_CONTEXTS).borrow(); let Some(routing_context) = rc.get(&id) else { @@ -529,7 +515,7 @@ pub fn routing_context_set_dht_value(id: u32, key: String, subkey: u32, data: St .decode(&data.as_bytes()) .unwrap(); - wrap_api_future_opt_json(async move { + wrap_api_future_json(async move { let routing_context = { let rc = (*ROUTING_CONTEXTS).borrow(); let Some(routing_context) = rc.get(&id) else { @@ -1361,7 +1347,7 @@ pub fn crypto_decrypt_aead( .unwrap() }); - wrap_api_future_json(async move { + wrap_api_future(async move { let veilid_api = get_veilid_api()?; let crypto = veilid_api.crypto()?; let csv = crypto.get(kind).ok_or_else(|| { @@ -1380,6 +1366,7 @@ pub fn crypto_decrypt_aead( None => None, }, )?; + let out = data_encoding::BASE64URL_NOPAD.encode(&out); APIResult::Ok(out) }) } @@ -1409,7 +1396,7 @@ pub fn crypto_encrypt_aead( .unwrap() }); - wrap_api_future_json(async move { + wrap_api_future(async move { let veilid_api = get_veilid_api()?; let crypto = veilid_api.crypto()?; let csv = crypto.get(kind).ok_or_else(|| { @@ -1428,6 +1415,7 @@ pub fn crypto_encrypt_aead( None => None, }, )?; + let out = data_encoding::BASE64URL_NOPAD.encode(&out); APIResult::Ok(out) }) } @@ -1450,7 +1438,7 @@ pub fn crypto_crypt_no_auth( let shared_secret: veilid_core::SharedSecret = veilid_core::deserialize_json(&shared_secret).unwrap(); - wrap_api_future_json(async move { + wrap_api_future(async move { let veilid_api = get_veilid_api()?; let crypto = veilid_api.crypto()?; let csv = crypto.get(kind).ok_or_else(|| { @@ -1461,6 +1449,7 @@ pub fn crypto_crypt_no_auth( ) })?; csv.crypt_in_place_no_auth(&mut body, &nonce, &shared_secret); + let out = data_encoding::BASE64URL_NOPAD.encode(&out); APIResult::Ok(body) }) }