From 9e6722c4bdc60d359b46805e3dcf67ef385d2f99 Mon Sep 17 00:00:00 2001 From: John Whelan Date: Thu, 20 Jul 2023 21:49:59 -0700 Subject: [PATCH 01/42] Adding changelog to track changes between versions. --- CHANGELOG.md | 1 + veilid-cli/CHANGELOG.md | 1 + 2 files changed, 2 insertions(+) create mode 100644 CHANGELOG.md create mode 100644 veilid-cli/CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..5c6af991 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1 @@ +Changes in Veilid 0.1.5 diff --git a/veilid-cli/CHANGELOG.md b/veilid-cli/CHANGELOG.md new file mode 100644 index 00000000..5c6af991 --- /dev/null +++ b/veilid-cli/CHANGELOG.md @@ -0,0 +1 @@ +Changes in Veilid 0.1.5 From 674a4d26f3cf59e26ed95e1c09acab3698f669bc Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Fri, 21 Jul 2023 09:44:14 -0400 Subject: [PATCH 02/42] attempt to fix suspend issue --- veilid-core/src/intf/native/network_interfaces/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/veilid-core/src/intf/native/network_interfaces/mod.rs b/veilid-core/src/intf/native/network_interfaces/mod.rs index 3a05cb98..84180b9f 100644 --- a/veilid-core/src/intf/native/network_interfaces/mod.rs +++ b/veilid-core/src/intf/native/network_interfaces/mod.rs @@ -344,9 +344,9 @@ impl NetworkInterfaces { let mut last_interfaces = { let mut last_interfaces = BTreeMap::::new(); let mut platform_support = PlatformSupport::new()?; - platform_support - .get_interfaces(&mut last_interfaces) - .await?; + if let Err(e) = platform_support.get_interfaces(&mut last_interfaces).await { + debug!("no network interfaces are enabled: {}", e); + } last_interfaces }; From 19424cf2026b1cfba5675f89eeb0d2586d078058 Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Fri, 21 Jul 2023 09:48:08 -0400 Subject: [PATCH 03/42] selector --- veilid-flutter/lib/routing_context.dart | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/veilid-flutter/lib/routing_context.dart b/veilid-flutter/lib/routing_context.dart index 778c3fbe..32d51213 100644 --- a/veilid-flutter/lib/routing_context.dart +++ b/veilid-flutter/lib/routing_context.dart @@ -84,6 +84,15 @@ class DHTRecordDescriptor with _$DHTRecordDescriptor { _$DHTRecordDescriptorFromJson(json); } +extension DHTRecordDescriptorExt on DHTRecordDescriptor { + KeyPair? ownerKeyPair() { + if (ownerSecret == null) { + return null; + } + return KeyPair(key: owner, secret: ownerSecret!); + } +} + ////////////////////////////////////// /// ValueSubkeyRange From 3f59f3bde3ad46c5ef0213ccdf829b93dce00e4a Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Fri, 21 Jul 2023 09:48:27 -0400 Subject: [PATCH 04/42] cleanup warning --- veilid-core/src/intf/native/network_interfaces/apple.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/veilid-core/src/intf/native/network_interfaces/apple.rs b/veilid-core/src/intf/native/network_interfaces/apple.rs index b0c21ee1..a34174fb 100644 --- a/veilid-core/src/intf/native/network_interfaces/apple.rs +++ b/veilid-core/src/intf/native/network_interfaces/apple.rs @@ -1,3 +1,4 @@ +#![allow(non_camel_case_types)] use super::*; use libc::{ From 9d3e847a68560a650aa4bb2bf5c6779cadfc9b4f Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Fri, 21 Jul 2023 14:30:10 -0400 Subject: [PATCH 05/42] more punishment cleanup --- veilid-core/src/crypto/envelope.rs | 8 ++++++++ veilid-core/src/crypto/receipt.rs | 5 +++++ veilid-core/src/network_manager/mod.rs | 14 +++++++++----- .../src/network_manager/network_connection.rs | 2 +- veilid-core/src/rpc_processor/destination.rs | 5 +---- veilid-core/src/rpc_processor/mod.rs | 19 ++++--------------- .../rpc_processor/rpc_validate_dial_info.rs | 5 +---- veilid-flutter/lib/routing_context.dart | 7 +++++++ veilid-flutter/lib/veilid_crypto.dart | 4 ++++ 9 files changed, 40 insertions(+), 29 deletions(-) diff --git a/veilid-core/src/crypto/envelope.rs b/veilid-core/src/crypto/envelope.rs index f7116e59..86ecc657 100644 --- a/veilid-core/src/crypto/envelope.rs +++ b/veilid-core/src/crypto/envelope.rs @@ -330,7 +330,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_recipient_id(&self) -> PublicKey { self.recipient_id } + + pub fn get_recipient_typed_id(&self) -> TypedKey { + TypedKey::new(self.crypto_kind, self.recipient_id) + } } diff --git a/veilid-core/src/crypto/receipt.rs b/veilid-core/src/crypto/receipt.rs index 4f8d4b15..56bd4c90 100644 --- a/veilid-core/src/crypto/receipt.rs +++ b/veilid-core/src/crypto/receipt.rs @@ -207,6 +207,11 @@ impl Receipt { pub fn get_sender_id(&self) -> PublicKey { self.sender_id } + + pub fn get_sender_typed_id(&self) -> TypedKey { + TypedKey::new(self.crypto_kind, self.sender_id) + } + pub fn get_extra_data(&self) -> &[u8] { &self.extra_data } diff --git a/veilid-core/src/network_manager/mod.rs b/veilid-core/src/network_manager/mod.rs index 3f57f82f..a36569bb 100644 --- a/veilid-core/src/network_manager/mod.rs +++ b/veilid-core/src/network_manager/mod.rs @@ -954,6 +954,7 @@ impl NetworkManager { Ok(v) => v, Err(e) => { log_net!(debug "envelope failed to decode: {}", e); + // safe to punish here because relays also check here to ensure they arent forwarding things that don't decode self.address_filter().punish_ip_addr(remote_addr); return Ok(false); } @@ -1005,12 +1006,12 @@ impl NetworkManager { // Peek at header and see if we need to relay this // If the recipient id is not our node id, then it needs relaying - let sender_id = TypedKey::new(envelope.get_crypto_kind(), envelope.get_sender_id()); + let sender_id = envelope.get_sender_typed_id(); if self.address_filter().is_node_id_punished(sender_id) { return Ok(false); } - let recipient_id = TypedKey::new(envelope.get_crypto_kind(), envelope.get_recipient_id()); + let recipient_id = envelope.get_recipient_typed_id(); if !routing_table.matches_own_node_id(&[recipient_id]) { // See if the source node is allowed to resolve nodes // This is a costly operation, so only outbound-relay permitted @@ -1089,15 +1090,18 @@ impl NetworkManager { ) { Ok(v) => v, Err(e) => { - log_net!(debug "failed to decrypt envelope body: {}",e); - self.address_filter().punish_ip_addr(remote_addr); + log_net!(debug "failed to decrypt envelope body: {}", e); + // Can't punish by ip address here because relaying can't decrypt envelope bodies to check + // But because the envelope was properly signed by the time it gets here, it is safe to + // punish by node id + self.address_filter().punish_node_id(sender_id); return Ok(false); } }; // Cache the envelope information in the routing table let source_noderef = match routing_table.register_node_with_existing_connection( - TypedKey::new(envelope.get_crypto_kind(), envelope.get_sender_id()), + envelope.get_sender_typed_id(), connection_descriptor, ts, ) { diff --git a/veilid-core/src/network_manager/network_connection.rs b/veilid-core/src/network_manager/network_connection.rs index dbda6da2..b96a36e3 100644 --- a/veilid-core/src/network_manager/network_connection.rs +++ b/veilid-core/src/network_manager/network_connection.rs @@ -315,7 +315,7 @@ impl NetworkConnection { return RecvLoopAction::Finish; } - // Punish invalid messages + // Punish invalid framing (tcp framing or websocket framing) if v.is_invalid_message() { address_filter.punish_ip_addr(peer_address.to_socket_addr().ip()); return RecvLoopAction::Finish; diff --git a/veilid-core/src/rpc_processor/destination.rs b/veilid-core/src/rpc_processor/destination.rs index 041d41a5..1ba68fec 100644 --- a/veilid-core/src/rpc_processor/destination.rs +++ b/veilid-core/src/rpc_processor/destination.rs @@ -319,10 +319,7 @@ impl RPCProcessor { }; // Reply directly to the request's source - let sender_node_id = TypedKey::new( - detail.envelope.get_crypto_kind(), - detail.envelope.get_sender_id(), - ); + let sender_node_id = detail.envelope.get_sender_typed_id(); // This may be a different node's reference than the 'sender' in the case of a relay let peer_noderef = detail.peer_noderef.clone(); diff --git a/veilid-core/src/rpc_processor/mod.rs b/veilid-core/src/rpc_processor/mod.rs index 44c07ba8..4d3dee31 100644 --- a/veilid-core/src/rpc_processor/mod.rs +++ b/veilid-core/src/rpc_processor/mod.rs @@ -126,17 +126,9 @@ impl RPCMessageHeader { } pub fn direct_sender_node_id(&self) -> TypedKey { match &self.detail { - RPCMessageHeaderDetail::Direct(d) => { - TypedKey::new(d.envelope.get_crypto_kind(), d.envelope.get_sender_id()) - } - RPCMessageHeaderDetail::SafetyRouted(s) => TypedKey::new( - s.direct.envelope.get_crypto_kind(), - s.direct.envelope.get_sender_id(), - ), - RPCMessageHeaderDetail::PrivateRouted(p) => TypedKey::new( - p.direct.envelope.get_crypto_kind(), - p.direct.envelope.get_sender_id(), - ), + RPCMessageHeaderDetail::Direct(d) => d.envelope.get_sender_typed_id(), + RPCMessageHeaderDetail::SafetyRouted(s) => s.direct.envelope.get_sender_typed_id(), + RPCMessageHeaderDetail::PrivateRouted(p) => p.direct.envelope.get_sender_typed_id(), } } } @@ -1464,10 +1456,7 @@ impl RPCProcessor { let msg = match &encoded_msg.header.detail { RPCMessageHeaderDetail::Direct(detail) => { // Get sender node id - let sender_node_id = TypedKey::new( - detail.envelope.get_crypto_kind(), - detail.envelope.get_sender_id(), - ); + let sender_node_id = detail.envelope.get_sender_typed_id(); // Decode and validate the RPC operation let operation = match self.decode_rpc_operation(&encoded_msg) { diff --git a/veilid-core/src/rpc_processor/rpc_validate_dial_info.rs b/veilid-core/src/rpc_processor/rpc_validate_dial_info.rs index ab6812e4..bd9823fd 100644 --- a/veilid-core/src/rpc_processor/rpc_validate_dial_info.rs +++ b/veilid-core/src/rpc_processor/rpc_validate_dial_info.rs @@ -104,10 +104,7 @@ impl RPCProcessor { // We filter on the -outgoing- protocol capability status not the node's dial info // Use the address type though, to ensure we reach an ipv6 capable node if this is // an ipv6 address - let sender_node_id = TypedKey::new( - detail.envelope.get_crypto_kind(), - detail.envelope.get_sender_id(), - ); + let sender_node_id = detail.envelope.get_sender_typed_id(); let routing_domain = detail.routing_domain; let node_count = { let c = self.config.get(); diff --git a/veilid-flutter/lib/routing_context.dart b/veilid-flutter/lib/routing_context.dart index 32d51213..56c2de6a 100644 --- a/veilid-flutter/lib/routing_context.dart +++ b/veilid-flutter/lib/routing_context.dart @@ -91,6 +91,13 @@ extension DHTRecordDescriptorExt on DHTRecordDescriptor { } return KeyPair(key: owner, secret: ownerSecret!); } + + TypedKeyPair? ownerTypedKeyPair() { + if (ownerSecret == null) { + return null; + } + return TypedKeyPair(kind: key.kind, key: owner, secret: ownerSecret!); + } } ////////////////////////////////////// diff --git a/veilid-flutter/lib/veilid_crypto.dart b/veilid-flutter/lib/veilid_crypto.dart index 4a38841f..b41a410d 100644 --- a/veilid-flutter/lib/veilid_crypto.dart +++ b/veilid-flutter/lib/veilid_crypto.dart @@ -21,6 +21,8 @@ String cryptoKindToString(CryptoKind kind) { return "${String.fromCharCode(kind & 0xFF)}${String.fromCharCode((kind >> 8) & 0xFF)}${String.fromCharCode((kind >> 16) & 0xFF)}${String.fromCharCode((kind >> 24) & 0xFF)}"; } +const CryptoKind bestCryptoKind = cryptoKindVLD0; + Uint8List cryptoKindToBytes(CryptoKind kind) { var b = Uint8List(4); b[0] = kind & 0xFF; @@ -140,6 +142,8 @@ class TypedKeyPair extends Equatable { String toJson() => toString(); factory TypedKeyPair.fromJson(dynamic json) => TypedKeyPair.fromString(json as String); + factory TypedKeyPair.fromKeyPair(CryptoKind kind, KeyPair keyPair) => + TypedKeyPair(kind: kind, key: keyPair.key, secret: keyPair.secret); } typedef CryptoKey = FixedEncodedString43; From b28330135ae8152bd1e9cd517b38563b22f4c3e1 Mon Sep 17 00:00:00 2001 From: TC Date: Fri, 21 Jul 2023 19:30:33 +0000 Subject: [PATCH 06/42] Added jobs to create and delete the ephemeral build machines. --- .gitlab-ci.yml | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a1eae362..394a59a1 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -25,10 +25,23 @@ test_amd64: - earthly --ci +unit-tests-linux-amd64 when: manual +create_build_machines: + stage: build_packages + only: + - stable + tags: + - build-orchestration + script: + - /home/gitlab-runner/build-machine-ctl.sh create amd64-deb + - /home/gitlab-runner/build-machine-ctl.sh create arm64-deb + - /home/gitlab-runner/build-machine-ctl.sh create amd64-rpm + package_amd64_deb: stage: build_packages only: - stable + needs: + - create_build_machines tags: - build-amd64-deb script: @@ -40,6 +53,8 @@ package_arm64_deb: stage: build_packages only: - stable + needs: + - create_build_machines tags: - build-arm64-deb script: @@ -51,6 +66,8 @@ package_amd64_rpm: stage: build_packages only: - stable + needs: + - create_build_machines tags: - build-amd64-rpm script: @@ -78,4 +95,13 @@ deploy_repos: script: - /home/gitlab-runner/deploy-repo.sh -#Note so merge works \ No newline at end of file +delete_build_machines: + stage: distribute + only: + - stable + tags: + - build-orchestration + script: + - /home/gitlab-runner/build-machine-ctl.sh delete amd64-deb + - /home/gitlab-runner/build-machine-ctl.sh delete arm64-deb + - /home/gitlab-runner/build-machine-ctl.sh delete amd64-rpm From deec053b10b72d3705ead93ad94bd9516ee0aaf9 Mon Sep 17 00:00:00 2001 From: John Whelan Date: Fri, 21 Jul 2023 20:47:52 +0000 Subject: [PATCH 07/42] Update CHANGELOG.md --- CHANGELOG.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c6af991..15723119 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1 +1,7 @@ -Changes in Veilid 0.1.5 +**Changes in Veilid 0.1.5** + +- Added Changelog +- Fix detachment issue with suspending network interfaces during operation +- Fix incorrect punishment on relayed undecryptable messages +- Minor API feature adds +- Relay bugfixes From e1ef4f14fbf055180ae187a84becb1c658d6948d Mon Sep 17 00:00:00 2001 From: TC Johnson Date: Fri, 21 Jul 2023 16:14:27 -0500 Subject: [PATCH 08/42] Removed CHANGELOG.md from veilid-cli directory --- veilid-cli/CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) delete mode 100644 veilid-cli/CHANGELOG.md diff --git a/veilid-cli/CHANGELOG.md b/veilid-cli/CHANGELOG.md deleted file mode 100644 index 5c6af991..00000000 --- a/veilid-cli/CHANGELOG.md +++ /dev/null @@ -1 +0,0 @@ -Changes in Veilid 0.1.5 From 1e529c24747d232a7f24ec2093c103ceae67f7bf Mon Sep 17 00:00:00 2001 From: Teknique Date: Fri, 21 Jul 2023 16:54:32 -0700 Subject: [PATCH 09/42] Automatically skip tests when veilid-server isn't running --- veilid-python/tests/api.py | 70 +++++++++++++++ veilid-python/tests/conftest.py | 31 +++---- veilid-python/tests/test_routing_context.py | 98 ++++++++++++--------- 3 files changed, 138 insertions(+), 61 deletions(-) create mode 100644 veilid-python/tests/api.py diff --git a/veilid-python/tests/api.py b/veilid-python/tests/api.py new file mode 100644 index 00000000..d6c44ecb --- /dev/null +++ b/veilid-python/tests/api.py @@ -0,0 +1,70 @@ +import errno +import os +import re +from collections.abc import Callable +from functools import cache + +from veilid.json_api import _JsonVeilidAPI + +import veilid + +ERRNO_PATTERN = re.compile(r"errno (\d+)", re.IGNORECASE) + + +class VeilidTestConnectionError(Exception): + """The test client could not connect to the veilid-server.""" + + pass + + +@cache +def server_info() -> tuple[str, int]: + """Return the hostname and port of the test server.""" + VEILID_SERVER = os.getenv("VEILID_SERVER") + if VEILID_SERVER is None: + return "localhost", 5959 + + hostname, *rest = VEILID_SERVER.split(":") + if rest: + return hostname, int(rest[0]) + return hostname, 5959 + + +async def api_connector(callback: Callable) -> _JsonVeilidAPI: + """Return an API connection if possible. + + If the connection fails due to an inability to connect to the + server's socket, raise an easy-to-catch VeilidTestConnectionError. + """ + + hostname, port = server_info() + try: + return await veilid.json_api_connect(hostname, port, callback) + except OSError as exc: + # This is a little goofy. The underlying Python library handles + # connection errors in 2 ways, depending on how many connections + # it attempted to make: + # + # - If it only tried to connect to one IP address socket, the + # library propagates the one single OSError it got. + # + # - If it tried to connect to multiple sockets, perhaps because + # the hostname resolved to several addresses (e.g. "localhost" + # => 127.0.0.1 and ::1), then the library raises one exception + # with all the failure exception strings joined together. + + # If errno is set, it's the first kind of exception. Check that + # it's the code we expected. + if exc.errno is not None: + if exc.errno == errno.ECONNREFUSED: + raise VeilidTestConnectionError + raise + + # If not, use a regular expression to find all the errno values + # in the combined error string. Check that all of them have the + # code we're looking for. + errnos = ERRNO_PATTERN.findall(str(exc)) + if all(int(err) == errno.ECONNREFUSED for err in errnos): + raise VeilidTestConnectionError + + raise diff --git a/veilid-python/tests/conftest.py b/veilid-python/tests/conftest.py index 6cabbd26..80ea7846 100644 --- a/veilid-python/tests/conftest.py +++ b/veilid-python/tests/conftest.py @@ -1,35 +1,30 @@ -import os -from functools import cache +"""Common test fixtures.""" + from typing import AsyncGenerator +import pytest import pytest_asyncio -import veilid from veilid.json_api import _JsonVeilidAPI +import veilid + +from .api import VeilidTestConnectionError, api_connector + pytest_plugins = ("pytest_asyncio",) -@cache -def server_info() -> tuple[str, int]: - """Return the hostname and port of the test server.""" - VEILID_SERVER = os.getenv("VEILID_SERVER") - if VEILID_SERVER is None: - return "localhost", 5959 - - hostname, *rest = VEILID_SERVER.split(":") - if rest: - return hostname, int(rest[0]) - return hostname, 5959 - - async def simple_update_callback(update: veilid.VeilidUpdate): print(f"VeilidUpdate: {update}") @pytest_asyncio.fixture async def api_connection() -> AsyncGenerator[_JsonVeilidAPI, None]: - hostname, port = server_info() - api = await veilid.json_api_connect(hostname, port, simple_update_callback) + try: + api = await api_connector(simple_update_callback) + except VeilidTestConnectionError: + pytest.skip("Unable to connect to veilid-server.") + return + async with api: # purge routes to ensure we start fresh await api.debug("purge routes") diff --git a/veilid-python/tests/test_routing_context.py b/veilid-python/tests/test_routing_context.py index 7ddc21af..1d602b3b 100644 --- a/veilid-python/tests/test_routing_context.py +++ b/veilid-python/tests/test_routing_context.py @@ -1,15 +1,15 @@ # Routing context veilid tests import asyncio +import os import random import sys -import os import pytest -import veilid -from veilid.types import OperationId -from .conftest import server_info +import veilid + +from .api import VeilidTestConnectionError, api_connector ################################################################## @@ -26,18 +26,24 @@ async def test_routing_contexts(api_connection: veilid.VeilidAPI): async with rcp: pass - rc = await (await api_connection.new_routing_context()).with_sequencing(veilid.Sequencing.ENSURE_ORDERED) + rc = await (await api_connection.new_routing_context()).with_sequencing( + veilid.Sequencing.ENSURE_ORDERED + ) async with rc: pass rc = await (await api_connection.new_routing_context()).with_custom_privacy( veilid.SafetySelection.safe( - veilid.SafetySpec(None, 2, veilid.Stability.RELIABLE, - veilid.Sequencing.ENSURE_ORDERED) - )) + veilid.SafetySpec( + None, 2, veilid.Stability.RELIABLE, veilid.Sequencing.ENSURE_ORDERED + ) + ) + ) await rc.release() - rc = await (await api_connection.new_routing_context()).with_custom_privacy(veilid.SafetySelection.unsafe(veilid.Sequencing.ENSURE_ORDERED)) + rc = await (await api_connection.new_routing_context()).with_custom_privacy( + veilid.SafetySelection.unsafe(veilid.Sequencing.ENSURE_ORDERED) + ) await rc.release() @@ -50,10 +56,12 @@ async def test_routing_context_app_message_loopback(): if update.kind == veilid.VeilidUpdateKind.APP_MESSAGE: await app_message_queue.put(update) - hostname, port = server_info() - api = await veilid.json_api_connect( - hostname, port, app_message_queue_update_callback - ) + try: + api = await api_connector(app_message_queue_update_callback) + except VeilidTestConnectionError: + pytest.skip("Unable to connect to veilid-server.") + return + async with api: # purge routes to ensure we start fresh await api.debug("purge routes") @@ -61,7 +69,6 @@ async def test_routing_context_app_message_loopback(): # make a routing context that uses a safety route rc = await (await api.new_routing_context()).with_privacy() async with rc: - # make a new local private route prl, blob = await api.new_private_route() @@ -89,8 +96,12 @@ async def test_routing_context_app_call_loopback(): if update.kind == veilid.VeilidUpdateKind.APP_CALL: await app_call_queue.put(update) - hostname, port = server_info() - api = await veilid.json_api_connect(hostname, port, app_call_queue_update_callback) + try: + api = await api_connector(app_call_queue_update_callback) + except VeilidTestConnectionError: + pytest.skip("Unable to connect to veilid-server.") + return + async with api: # purge routes to ensure we start fresh await api.debug("purge routes") @@ -98,7 +109,6 @@ async def test_routing_context_app_call_loopback(): # make a routing context that uses a safety route rc = await (await api.new_routing_context()).with_privacy() async with rc: - # make a new local private route prl, blob = await api.new_private_route() @@ -131,7 +141,6 @@ async def test_routing_context_app_call_loopback(): @pytest.mark.asyncio async def test_routing_context_app_message_loopback_big_packets(): - app_message_queue: asyncio.Queue = asyncio.Queue() global got_message @@ -146,18 +155,21 @@ async def test_routing_context_app_message_loopback_big_packets(): sent_messages: set[bytes] = set() - hostname, port = server_info() - api = await veilid.json_api_connect( - hostname, port, app_message_queue_update_callback - ) + try: + api = await api_connector(app_message_queue_update_callback) + except VeilidTestConnectionError: + pytest.skip("Unable to connect to veilid-server.") + return + async with api: # purge routes to ensure we start fresh await api.debug("purge routes") # make a routing context that uses a safety route - rc = await (await (await api.new_routing_context()).with_privacy()).with_sequencing(veilid.Sequencing.ENSURE_ORDERED) + rc = await ( + await (await api.new_routing_context()).with_privacy() + ).with_sequencing(veilid.Sequencing.ENSURE_ORDERED) async with rc: - # make a new local private route prl, blob = await api.new_private_route() @@ -166,7 +178,6 @@ async def test_routing_context_app_message_loopback_big_packets(): # do this test 1000 times for _ in range(1000): - # send a random sized random app message to our own private route message = random.randbytes(random.randint(0, 32768)) await rc.app_message(prr, message) @@ -208,10 +219,12 @@ async def test_routing_context_app_call_loopback_big_packets(): await api.app_call_reply(update.detail.call_id, update.detail.message) - hostname, port = server_info() - api = await veilid.json_api_connect( - hostname, port, app_call_queue_update_callback - ) + try: + api = await api_connector(app_call_queue_update_callback) + except VeilidTestConnectionError: + pytest.skip("Unable to connect to veilid-server.") + return + async with api: # purge routes to ensure we start fresh await api.debug("purge routes") @@ -221,9 +234,10 @@ async def test_routing_context_app_call_loopback_big_packets(): ) # make a routing context that uses a safety route - rc = await (await (await api.new_routing_context()).with_privacy()).with_sequencing(veilid.Sequencing.ENSURE_ORDERED) + rc = await ( + await (await api.new_routing_context()).with_privacy() + ).with_sequencing(veilid.Sequencing.ENSURE_ORDERED) async with rc: - # make a new local private route prl, blob = await api.new_private_route() @@ -232,7 +246,6 @@ async def test_routing_context_app_call_loopback_big_packets(): # do this test 10 times for _ in range(10): - # send a random sized random app message to our own private route message = random.randbytes(random.randint(0, 32768)) out_message = await rc.app_call(prr, message) @@ -242,20 +255,23 @@ async def test_routing_context_app_call_loopback_big_packets(): app_call_task.cancel() -@pytest.mark.skipif(os.getenv("NOSKIP") != "1", reason="unneeded test, only for performance check") +@pytest.mark.skipif( + os.getenv("NOSKIP") != "1", reason="unneeded test, only for performance check" +) @pytest.mark.asyncio async def test_routing_context_app_message_loopback_bandwidth(): - app_message_queue: asyncio.Queue = asyncio.Queue() async def app_message_queue_update_callback(update: veilid.VeilidUpdate): if update.kind == veilid.VeilidUpdateKind.APP_MESSAGE: await app_message_queue.put(True) - hostname, port = server_info() - api = await veilid.json_api_connect( - hostname, port, app_message_queue_update_callback - ) + try: + api = await api_connector(app_message_queue_update_callback) + except VeilidTestConnectionError: + pytest.skip("Unable to connect to veilid-server.") + return + async with api: # purge routes to ensure we start fresh await api.debug("purge routes") @@ -265,7 +281,6 @@ async def test_routing_context_app_message_loopback_bandwidth(): # rc = await (await api.new_routing_context()).with_privacy() rc = await api.new_routing_context() async with rc: - # make a new local private route prl, blob = await api.new_private_route() @@ -275,12 +290,9 @@ async def test_routing_context_app_message_loopback_bandwidth(): # do this test 1000 times message = random.randbytes(16384) for _ in range(10000): - # send a random sized random app message to our own private route await rc.app_message(prr, message) # we should get the same number of messages back (not storing all that data) for _ in range(10000): - await asyncio.wait_for( - app_message_queue.get(), timeout=10 - ) + await asyncio.wait_for(app_message_queue.get(), timeout=10) From c4620218aa09cb27f355d26c6556a3aea07dc684 Mon Sep 17 00:00:00 2001 From: Teknique Date: Fri, 21 Jul 2023 17:35:39 -0700 Subject: [PATCH 10/42] Worked around globals --- veilid-python/tests/test_routing_context.py | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/veilid-python/tests/test_routing_context.py b/veilid-python/tests/test_routing_context.py index 1d602b3b..9b6f95f9 100644 --- a/veilid-python/tests/test_routing_context.py +++ b/veilid-python/tests/test_routing_context.py @@ -143,14 +143,12 @@ async def test_routing_context_app_call_loopback(): async def test_routing_context_app_message_loopback_big_packets(): app_message_queue: asyncio.Queue = asyncio.Queue() - global got_message - got_message = 0 + count_hack = [0] async def app_message_queue_update_callback(update: veilid.VeilidUpdate): if update.kind == veilid.VeilidUpdateKind.APP_MESSAGE: - global got_message - got_message += 1 - sys.stdout.write("{} ".format(got_message)) + count_hack[0] += 1 + print(f"{count_hack[0]} ", end="") await app_message_queue.put(update) sent_messages: set[bytes] = set() @@ -198,8 +196,7 @@ async def test_routing_context_app_message_loopback_big_packets(): @pytest.mark.asyncio async def test_routing_context_app_call_loopback_big_packets(): - global got_message - got_message = 0 + count_hack = [0] app_call_queue: asyncio.Queue = asyncio.Queue() @@ -211,11 +208,8 @@ async def test_routing_context_app_call_loopback_big_packets(): while True: update = await app_call_queue.get() - global got_message - got_message += 1 - - sys.stdout.write("{} ".format(got_message)) - sys.stdout.flush() + count_hack[0] += 1 + print(f"{count_hack[0]} ", end="", flush=True) await api.app_call_reply(update.detail.call_id, update.detail.message) From 4951e983c769ebcdfc89cf44a294a37fa1ca268a Mon Sep 17 00:00:00 2001 From: TC Johnson Date: Sat, 22 Jul 2023 12:02:10 -0500 Subject: [PATCH 11/42] Release 0.1.5 --- .bumpversion.cfg | 2 +- Cargo.lock | 153 +++++++++++++++++++-------------- veilid-cli/Cargo.toml | 2 +- veilid-core/Cargo.toml | 2 +- veilid-flutter/pubspec.yaml | 2 +- veilid-flutter/rust/Cargo.toml | 2 +- veilid-python/pyproject.toml | 2 +- veilid-server/Cargo.toml | 2 +- veilid-tools/Cargo.toml | 2 +- veilid-wasm/Cargo.toml | 2 +- 10 files changed, 98 insertions(+), 73 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index cd3ecd0e..10aa3036 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.1.4 +current_version = 0.1.5 [bumpversion:file:veilid-server/Cargo.toml] search = name = "veilid-server" diff --git a/Cargo.lock b/Cargo.lock index bd74a9d7..26666032 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -252,7 +252,7 @@ dependencies = [ "async-lock", "async-task", "concurrent-queue", - "fastrand", + "fastrand 1.9.0", "futures-lite", "slab", ] @@ -286,7 +286,7 @@ dependencies = [ "log", "parking", "polling", - "rustix", + "rustix 0.37.23", "slab", "socket2 0.4.9", "waker-fn", @@ -314,7 +314,7 @@ dependencies = [ "cfg-if 1.0.0", "event-listener", "futures-lite", - "rustix", + "rustix 0.37.23", "signal-hook", "windows-sys 0.48.0", ] @@ -381,7 +381,7 @@ checksum = "16e62a023e7c117e27523144c5d2459f4397fcc3cab0085af8e2224f643a0193" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.27", ] [[package]] @@ -419,13 +419,13 @@ dependencies = [ [[package]] name = "async-trait" -version = "0.1.71" +version = "0.1.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a564d521dd56509c4c47480d00b80ee55f7e385ae48db5744c67ad50c92d2ebf" +checksum = "cc6dde6e4ed435a4c1ee4e73592f5ba9da2151af10076cc04858746af9352d09" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.27", ] [[package]] @@ -457,9 +457,9 @@ dependencies = [ [[package]] name = "async_executors" -version = "0.6.0" +version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0b2463773401e1f684136f9cdb956cf611f22172472cf3f049e72123f59e359" +checksum = "a982d2f86de6137cc05c9db9a915a19886c97911f9790d04f174cede74be01a5" dependencies = [ "async-std", "blanket", @@ -694,13 +694,13 @@ dependencies = [ [[package]] name = "blanket" -version = "0.2.0" +version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b04ce3d2372d05d1ef4ea3fdf427da6ae3c17ca06d688a107b5344836276bc3" +checksum = "e0b121a9fe0df916e362fb3271088d071159cdf11db0e4182d02152850756eff" dependencies = [ "proc-macro2", "quote", - "syn 1.0.109", + "syn 2.0.27", ] [[package]] @@ -753,7 +753,7 @@ dependencies = [ "async-lock", "async-task", "atomic-waker", - "fastrand", + "fastrand 1.9.0", "futures-lite", "log", ] @@ -1419,7 +1419,7 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.27", ] [[package]] @@ -1441,7 +1441,7 @@ checksum = "836a9bbc7ad63342d6d6e7b815ccab164bc77a2d95d84bc3117a8c0d5c98e2d5" dependencies = [ "darling_core 0.20.3", "quote", - "syn 2.0.26", + "syn 2.0.27", ] [[package]] @@ -1556,9 +1556,9 @@ dependencies = [ [[package]] name = "either" -version = "1.8.1" +version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91" +checksum = "a26ae43d7bcc3b814de94796a5e736d4029efb0ee900c12e2d54c993ad1a1e07" [[package]] name = "enum-as-inner" @@ -1589,7 +1589,7 @@ checksum = "8560b409800a72d2d7860f8e5f4e0b0bd22bea6a352ea2a9ce30ccdef7f16d2f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.27", ] [[package]] @@ -1632,7 +1632,7 @@ dependencies = [ "darling 0.20.3", "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.27", ] [[package]] @@ -1764,6 +1764,12 @@ dependencies = [ "instant", ] +[[package]] +name = "fastrand" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" + [[package]] name = "fdeflate" version = "0.3.0" @@ -1930,7 +1936,7 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49a9d51ce47660b1e808d3c990b4709f2f415d928835a17dfd16991515c46bce" dependencies = [ - "fastrand", + "fastrand 1.9.0", "futures-core", "futures-io", "memchr", @@ -1947,7 +1953,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.27", ] [[package]] @@ -2774,6 +2780,12 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" +[[package]] +name = "linux-raw-sys" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09fc20d2ca12cb9f044c93e3bd6d32d523e6e2ec3db4f7b2939cd99026ecd3f0" + [[package]] name = "lock_api" version = "0.4.10" @@ -3224,9 +3236,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.15" +version = "0.2.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd" +checksum = "f30b0abd723be7e2ffca1272140fac1a2f084c77ec3e123c192b66af1ee9e6c2" dependencies = [ "autocfg", ] @@ -3596,7 +3608,7 @@ dependencies = [ "pest_meta", "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.27", ] [[package]] @@ -3667,7 +3679,7 @@ checksum = "ec2e072ecce94ec471b13398d5402c188e76ac03cf74dd1a975161b23a3f6d9c" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.27", ] [[package]] @@ -4165,7 +4177,20 @@ dependencies = [ "errno", "io-lifetimes", "libc", - "linux-raw-sys", + "linux-raw-sys 0.3.8", + "windows-sys 0.48.0", +] + +[[package]] +name = "rustix" +version = "0.38.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a962918ea88d644592894bc6dc55acc6c0956488adcebbfb6e273506b7fd6e5" +dependencies = [ + "bitflags 2.3.3", + "errno", + "libc", + "linux-raw-sys 0.4.3", "windows-sys 0.48.0", ] @@ -4321,9 +4346,9 @@ dependencies = [ [[package]] name = "security-framework" -version = "2.9.1" +version = "2.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1fc758eb7bffce5b308734e9b0c1468893cae9ff70ebf13e7090be8dcbcc83a8" +checksum = "05b64fb303737d99b81884b2c63433e9ae28abebe5eb5045dcdd175dc2ecf4de" dependencies = [ "bitflags 1.3.2", "core-foundation 0.9.3", @@ -4334,9 +4359,9 @@ dependencies = [ [[package]] name = "security-framework-sys" -version = "2.9.0" +version = "2.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f51d0c0d83bec45f16480d0ce0058397a69e48fcdc52d1dc8855fb68acbd31a7" +checksum = "e932934257d3b408ed8f30db49d85ea163bfe74961f017f405b025af298f0c7a" dependencies = [ "core-foundation-sys 0.8.4", "libc", @@ -4380,9 +4405,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.173" +version = "1.0.174" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e91f70896d6720bc714a4a57d22fc91f1db634680e65c8efe13323f1fa38d53f" +checksum = "3b88756493a5bd5e5395d53baa70b194b05764ab85b59e43e4b8f4e1192fa9b1" dependencies = [ "serde_derive", ] @@ -4408,13 +4433,13 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.173" +version = "1.0.174" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a6250dde8342e0232232be9ca3db7aa40aceb5a3e5dd9bddbc00d99a007cde49" +checksum = "6e5c3a298c7f978e53536f95a63bdc4c4a64550582f31a0359a9afda6aede62e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.27", ] [[package]] @@ -4441,13 +4466,13 @@ dependencies = [ [[package]] name = "serde_repr" -version = "0.1.14" +version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d89a8107374290037607734c0b73a85db7ed80cae314b3c5791f192a496e731" +checksum = "e168eaaf71e8f9bd6037feb05190485708e019f4fd87d161b3c0a0d37daf85e5" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.27", ] [[package]] @@ -4461,9 +4486,9 @@ dependencies = [ [[package]] name = "serde_yaml" -version = "0.9.24" +version = "0.9.25" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd5f51e3fdb5b9cdd1577e1cb7a733474191b1aca6a72c2e50913241632c1180" +checksum = "1a49e178e4452f45cb61d0cd8cebc1b0fafd3e41929e996cef79aa3aca91f574" dependencies = [ "indexmap 2.0.0", "itoa", @@ -4831,9 +4856,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.26" +version = "2.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45c3457aacde3c65315de5031ec191ce46604304d2446e803d71ade03308d970" +checksum = "b60f673f44a8255b9c8c657daf66a596d435f2da81a555b06dc644d080ba45e0" dependencies = [ "proc-macro2", "quote", @@ -4874,15 +4899,14 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.6.0" +version = "3.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31c0432476357e58790aaa47a8efb0c5138f137343f3b5f23bd36a27e3b0a6d6" +checksum = "5486094ee78b2e5038a6382ed7645bc084dc2ec433426ca4c3cb61e2007b8998" dependencies = [ - "autocfg", "cfg-if 1.0.0", - "fastrand", + "fastrand 2.0.0", "redox_syscall 0.3.5", - "rustix", + "rustix 0.38.4", "windows-sys 0.48.0", ] @@ -4912,22 +4936,22 @@ checksum = "222a222a5bfe1bba4a77b45ec488a741b3cb8872e5e499451fd7d0129c9c7c3d" [[package]] name = "thiserror" -version = "1.0.43" +version = "1.0.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a35fc5b8971143ca348fa6df4f024d4d55264f3468c71ad1c2f365b0a4d58c42" +checksum = "611040a08a0439f8248d1990b111c95baa9c704c805fa1f62104b39655fd7f90" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.43" +version = "1.0.44" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "463fe12d7993d3b327787537ce8dd4dfa058de32fc2b195ef3cde03dc4771e8f" +checksum = "090198534930841fab3a5d1bb637cde49e339654e606195f8d9c76eeb081dc96" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.27", ] [[package]] @@ -5083,7 +5107,7 @@ checksum = "630bdcf245f78637c13ec01ffae6187cca34625e8c63150d424b59e55af2675e" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.27", ] [[package]] @@ -5292,7 +5316,7 @@ checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.27", ] [[package]] @@ -5627,7 +5651,7 @@ checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" [[package]] name = "veilid-cli" -version = "0.1.4" +version = "0.1.5" dependencies = [ "arboard", "async-std", @@ -5663,7 +5687,7 @@ dependencies = [ [[package]] name = "veilid-core" -version = "0.1.4" +version = "0.1.5" dependencies = [ "argon2", "async-io", @@ -5766,7 +5790,7 @@ dependencies = [ [[package]] name = "veilid-flutter" -version = "0.1.4" +version = "0.1.5" dependencies = [ "allo-isolate", "async-std", @@ -5795,7 +5819,7 @@ dependencies = [ [[package]] name = "veilid-server" -version = "0.1.4" +version = "0.1.5" dependencies = [ "ansi_term", "async-std", @@ -5845,7 +5869,7 @@ dependencies = [ [[package]] name = "veilid-tools" -version = "0.1.4" +version = "0.1.5" dependencies = [ "android-logd-logger", "async-lock", @@ -5872,6 +5896,7 @@ dependencies = [ "oslog", "paranoid-android", "parking_lot 0.11.2", + "parking_lot 0.12.1", "rand 0.7.3", "range-set-blaze", "rust-fsm", @@ -5896,7 +5921,7 @@ dependencies = [ [[package]] name = "veilid-wasm" -version = "0.1.4" +version = "0.1.5" dependencies = [ "cfg-if 1.0.0", "console_error_panic_hook", @@ -5996,7 +6021,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.27", "wasm-bindgen-shared", ] @@ -6030,7 +6055,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.27", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -6547,7 +6572,7 @@ dependencies = [ "byteorder", "derivative", "enumflags2", - "fastrand", + "fastrand 1.9.0", "futures", "nb-connect", "nix 0.22.3", @@ -6589,7 +6614,7 @@ checksum = "ce36e65b0d2999d2aafac989fb249189a141aee1f53c612c1f37d72631959f69" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.27", ] [[package]] diff --git a/veilid-cli/Cargo.toml b/veilid-cli/Cargo.toml index 0c29379f..c8a19548 100644 --- a/veilid-cli/Cargo.toml +++ b/veilid-cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-cli" -version = "0.1.4" +version = "0.1.5" authors = ["Veilid Team "] edition = "2021" license = "MPL-2.0" diff --git a/veilid-core/Cargo.toml b/veilid-core/Cargo.toml index da56f53d..9d4c6134 100644 --- a/veilid-core/Cargo.toml +++ b/veilid-core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-core" -version = "0.1.4" +version = "0.1.5" authors = ["Veilid Team "] edition = "2021" build = "build.rs" diff --git a/veilid-flutter/pubspec.yaml b/veilid-flutter/pubspec.yaml index 4a87bff8..61fd37ea 100644 --- a/veilid-flutter/pubspec.yaml +++ b/veilid-flutter/pubspec.yaml @@ -1,5 +1,5 @@ name: veilid -version: 0.1.4 +version: 0.1.5 description: Veilid Framework homepage: https://veilid.com publish_to: "none" # Remove this line if you wish to publish to pub.dev diff --git a/veilid-flutter/rust/Cargo.toml b/veilid-flutter/rust/Cargo.toml index cb0ab5b7..eab4d8a4 100644 --- a/veilid-flutter/rust/Cargo.toml +++ b/veilid-flutter/rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-flutter" -version = "0.1.4" +version = "0.1.5" authors = ["Veilid Team "] license = "MPL-2.0" edition = "2021" diff --git a/veilid-python/pyproject.toml b/veilid-python/pyproject.toml index 7cb1c1f8..8f291393 100644 --- a/veilid-python/pyproject.toml +++ b/veilid-python/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "veilid" -version = "0.1.4" +version = "0.1.5" description = "" authors = ["Veilid Team "] readme = "README.md" diff --git a/veilid-server/Cargo.toml b/veilid-server/Cargo.toml index 295d41ce..1c2e9822 100644 --- a/veilid-server/Cargo.toml +++ b/veilid-server/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-server" -version = "0.1.4" +version = "0.1.5" authors = ["Veilid Team "] license = "MPL-2.0" edition = "2021" diff --git a/veilid-tools/Cargo.toml b/veilid-tools/Cargo.toml index 09869c52..a2b16b68 100644 --- a/veilid-tools/Cargo.toml +++ b/veilid-tools/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-tools" -version = "0.1.4" +version = "0.1.5" authors = ["Veilid Team "] license = "MPL-2.0" edition = "2021" diff --git a/veilid-wasm/Cargo.toml b/veilid-wasm/Cargo.toml index f6f18ee0..8e52d264 100644 --- a/veilid-wasm/Cargo.toml +++ b/veilid-wasm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-wasm" -version = "0.1.4" +version = "0.1.5" authors = ["Veilid Team "] license = "MPL-2.0" edition = "2021" From 75c8b0c789c6a841c2ff321f7502f27435570ed7 Mon Sep 17 00:00:00 2001 From: TC Date: Sat, 22 Jul 2023 17:07:00 +0000 Subject: [PATCH 12/42] Added ephemeral create/delete to test stage. --- .gitlab-ci.yml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 394a59a1..b5f21093 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -12,6 +12,16 @@ stages: #before_script: # - earthly bootstrap +create_test_machine: + stage: test + only: + - main + - merge_requests + tags: + - build-orchestration + script: + - /home/gitlab-runner/build-machine-ctl.sh create amd64-deb + test_amd64: stage: test image: earthly/earthly:v0.6.30 @@ -25,6 +35,18 @@ test_amd64: - earthly --ci +unit-tests-linux-amd64 when: manual +delete_test_machine: + stage: test + only: + - main + - merge_requests + needs: + - test_amd64 + tags: + - build-orchestration + script: + - /home/gitlab-runner/build-machine-ctl.sh delete amd64-deb + create_build_machines: stage: build_packages only: From f4fb48b2190f30459e26d48e6cb81c7bcf982105 Mon Sep 17 00:00:00 2001 From: TC Date: Sat, 22 Jul 2023 17:11:51 +0000 Subject: [PATCH 13/42] Refining ephemeral earthly test machine process --- .gitlab-ci.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index b5f21093..5ade56b4 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -21,6 +21,7 @@ create_test_machine: - build-orchestration script: - /home/gitlab-runner/build-machine-ctl.sh create amd64-deb + when: manual test_amd64: stage: test @@ -28,12 +29,13 @@ test_amd64: only: - main - merge_requests + needs: + - create-test-machine tags: - - build-server + - earthly-tests script: - earthly bootstrap - earthly --ci +unit-tests-linux-amd64 - when: manual delete_test_machine: stage: test From 7382d70deb471169e5ed0a4981a886b417ad3ccd Mon Sep 17 00:00:00 2001 From: TC Date: Sat, 22 Jul 2023 17:13:08 +0000 Subject: [PATCH 14/42] Refine ephemeral earthly test machine process --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 5ade56b4..ff740a31 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -30,7 +30,7 @@ test_amd64: - main - merge_requests needs: - - create-test-machine + - create_test_machine tags: - earthly-tests script: From f836065f853296f40d49a393e07fc04a8eeaf1c7 Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Sat, 22 Jul 2023 13:35:03 -0400 Subject: [PATCH 15/42] flutter fixes --- veilid-flutter/lib/veilid_ffi.dart | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/veilid-flutter/lib/veilid_ffi.dart b/veilid-flutter/lib/veilid_ffi.dart index cfadb6a4..545641cc 100644 --- a/veilid-flutter/lib/veilid_ffi.dart +++ b/veilid-flutter/lib/veilid_ffi.dart @@ -338,8 +338,9 @@ Future processFuturePlain(Future future) { switch (list[0] as int) { case messageOk: { - if (list[1] == null) { - throw VeilidAPIExceptionInternal("Null MESSAGE_OK value"); + if (list[1] == null && null is! T) { + throw const VeilidAPIExceptionInternal( + "Null MESSAGE_OK value on non-nullable type"); } return list[1] as T; } @@ -377,10 +378,15 @@ Future processFutureJson( } case messageOkJson: { - if (list[1] == null) { - throw VeilidAPIExceptionInternal("Null MESSAGE_OK_JSON value"); + if (list[1] is! String) { + throw const VeilidAPIExceptionInternal( + "Non-string MESSAGE_OK_JSON value"); } var ret = jsonDecode(list[1] as String); + if (ret == null) { + throw const VeilidAPIExceptionInternal( + "Null JSON object on non nullable type"); + } return jsonConstructor(ret); } case messageErrJson: @@ -416,7 +422,14 @@ Future processFutureOptJson( if (list[1] == null) { return null; } + if (list[1] is! String) { + throw const VeilidAPIExceptionInternal( + "Non-string MESSAGE_OK_JSON optional value"); + } var ret = jsonDecode(list[1] as String); + if (ret == null) { + return null; + } return jsonConstructor(ret); } case messageErrJson: From 77303d575130fbac5eda426af150c9a13162a065 Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Sat, 22 Jul 2023 14:24:43 -0400 Subject: [PATCH 16/42] earthfile update --- Earthfile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Earthfile b/Earthfile index 1710abbd..c4571228 100644 --- a/Earthfile +++ b/Earthfile @@ -115,10 +115,12 @@ build-linux-arm64: # Unit tests unit-tests-linux-amd64: FROM +code-linux + ENV RUST_BACKTRACE=1 RUN cargo test --target x86_64-unknown-linux-gnu --release -p veilid-server -p veilid-cli -p veilid-tools -p veilid-core # unit-tests-linux-arm64: # FROM +code-linux +# ENV RUST_BACKTRACE=1 # RUN cargo test --target aarch64-unknown-linux-gnu --release -p veilid-server -p veilid-cli -p veilid-tools -p veilid-core # Package From c4db7654230410871d7f1ccbabc210e2280456bc Mon Sep 17 00:00:00 2001 From: John Smith Date: Sat, 22 Jul 2023 14:41:14 -0400 Subject: [PATCH 17/42] fixes for test --- Cargo.lock | 1 - veilid-tools/src/assembly_buffer.rs | 2 +- veilid-tools/src/tests/native/test_assembly_buffer.rs | 4 ++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 26666032..402ee9c9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5896,7 +5896,6 @@ dependencies = [ "oslog", "paranoid-android", "parking_lot 0.11.2", - "parking_lot 0.12.1", "rand 0.7.3", "range-set-blaze", "rust-fsm", diff --git a/veilid-tools/src/assembly_buffer.rs b/veilid-tools/src/assembly_buffer.rs index d2bdf077..44ed3d58 100644 --- a/veilid-tools/src/assembly_buffer.rs +++ b/veilid-tools/src/assembly_buffer.rs @@ -12,7 +12,7 @@ const MAX_LEN: usize = LengthType::MAX as usize; // XXX: keep statistics on all drops and why we dropped them // XXX: move to config eventually? -const FRAGMENT_LEN: usize = 1280 - HEADER_LEN; +pub const FRAGMENT_LEN: usize = 1280 - HEADER_LEN; const MAX_CONCURRENT_HOSTS: usize = 256; const MAX_ASSEMBLIES_PER_HOST: usize = 256; const MAX_BUFFER_PER_HOST: usize = 256 * 1024; diff --git a/veilid-tools/src/tests/native/test_assembly_buffer.rs b/veilid-tools/src/tests/native/test_assembly_buffer.rs index 73268516..95620ad8 100644 --- a/veilid-tools/src/tests/native/test_assembly_buffer.rs +++ b/veilid-tools/src/tests/native/test_assembly_buffer.rs @@ -86,7 +86,7 @@ pub async fn test_one_frag_out_in() { // Sending println!("sending"); for _ in 0..10000 { - let random_len = (get_random_u32() % 1000) as usize + 1280; + let random_len = (get_random_u32() % 1000) as usize + FRAGMENT_LEN; let mut message = vec![1u8; random_len]; random_bytes(&mut message); let remote_addr = random_sockaddr(); @@ -289,7 +289,7 @@ pub async fn test_many_frags_with_drops() { println!("sending"); for _ in 0..1000 { let random_len = (get_random_u32() % 65536) as usize; - if random_len > 1280 { + if random_len > FRAGMENT_LEN { total_fragged += 1; } total_sent_size += random_len; From cff955782b277036257db9838406c617334b6c0d Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Sat, 22 Jul 2023 15:37:15 -0400 Subject: [PATCH 18/42] fix fox nodes length issue --- veilid-core/src/routing_table/routing_table_inner.rs | 4 ++-- .../coders/operations/operation_find_node.rs | 8 ++++++-- .../coders/operations/operation_get_value.rs | 8 ++++++-- .../coders/operations/operation_set_value.rs | 8 ++++++-- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/veilid-core/src/routing_table/routing_table_inner.rs b/veilid-core/src/routing_table/routing_table_inner.rs index d3b88cae..e82f9f9d 100644 --- a/veilid-core/src/routing_table/routing_table_inner.rs +++ b/veilid-core/src/routing_table/routing_table_inner.rs @@ -1054,8 +1054,8 @@ impl RoutingTableInner { nodes.sort_by(|a, b| compare(self, a, b)); // return transformed vector for filtered+sorted nodes - let cnt = usize::min(node_count, nodes.len()); - let mut out = Vec::::with_capacity(cnt); + nodes.truncate(node_count); + let mut out = Vec::::with_capacity(nodes.len()); for node in nodes { let val = transform(self, node); out.push(val); 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 10848648..5292d4fc 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 @@ -81,7 +81,9 @@ pub struct RPCOperationFindNodeA { impl RPCOperationFindNodeA { pub fn new(peers: Vec) -> Result { if peers.len() > MAX_FIND_NODE_A_PEERS_LEN { - return Err(RPCError::protocol("find node peers length too long")); + return Err(RPCError::protocol( + "encoded find node peers length too long", + )); } Ok(Self { peers }) @@ -106,7 +108,9 @@ impl RPCOperationFindNodeA { let peers_reader = reader.get_peers().map_err(RPCError::protocol)?; if peers_reader.len() as usize > MAX_FIND_NODE_A_PEERS_LEN { - return Err(RPCError::protocol("find node peers length too long")); + return Err(RPCError::protocol( + "decoded find node peers length too long", + )); } let mut peers = Vec::::with_capacity( 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 72224952..bc255bd3 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 @@ -89,7 +89,9 @@ impl RPCOperationGetValueA { descriptor: Option, ) -> Result { if peers.len() > MAX_GET_VALUE_A_PEERS_LEN { - return Err(RPCError::protocol("GetValueA peers length too long")); + return Err(RPCError::protocol( + "encoded GetValueA peers length too long", + )); } Ok(Self { value, @@ -175,7 +177,9 @@ impl RPCOperationGetValueA { let peers_reader = reader.get_peers().map_err(RPCError::protocol)?; if peers_reader.len() as usize > MAX_GET_VALUE_A_PEERS_LEN { - return Err(RPCError::protocol("GetValueA peers length too long")); + return Err(RPCError::protocol( + "decoded GetValueA peers length too long", + )); } let mut peers = Vec::::with_capacity( peers_reader 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 c7fa4cf2..3ff5c71f 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 @@ -123,7 +123,9 @@ impl RPCOperationSetValueA { peers: Vec, ) -> Result { if peers.len() as usize > MAX_SET_VALUE_A_PEERS_LEN { - return Err(RPCError::protocol("SetValueA peers length too long")); + return Err(RPCError::protocol( + "encoded SetValueA peers length too long", + )); } Ok(Self { set, value, peers }) } @@ -182,7 +184,9 @@ impl RPCOperationSetValueA { }; let peers_reader = reader.get_peers().map_err(RPCError::protocol)?; if peers_reader.len() as usize > MAX_SET_VALUE_A_PEERS_LEN { - return Err(RPCError::protocol("SetValueA peers length too long")); + return Err(RPCError::protocol( + "decoded SetValueA peers length too long", + )); } let mut peers = Vec::::with_capacity( peers_reader From 1861650d443f2df07bd88eb65fd7dbf23fe893da Mon Sep 17 00:00:00 2001 From: John Smith Date: Sat, 22 Jul 2023 16:28:05 -0400 Subject: [PATCH 19/42] 0.1.6 --- .bumpversion.cfg | 2 +- CHANGELOG.md | 4 ++++ Cargo.lock | 12 ++++++------ veilid-cli/Cargo.toml | 2 +- veilid-core/Cargo.toml | 2 +- veilid-flutter/pubspec.yaml | 2 +- veilid-flutter/rust/Cargo.toml | 2 +- veilid-python/pyproject.toml | 2 +- veilid-server/Cargo.toml | 2 +- veilid-tools/Cargo.toml | 2 +- veilid-wasm/Cargo.toml | 2 +- 11 files changed, 19 insertions(+), 15 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 10aa3036..65e96b9b 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.1.5 +current_version = 0.1.6 [bumpversion:file:veilid-server/Cargo.toml] search = name = "veilid-server" diff --git a/CHANGELOG.md b/CHANGELOG.md index 15723119..7f169520 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +**Changes in Veilid 0.1.6** + +- Fix for 'find_node' too many nodes returned issue + **Changes in Veilid 0.1.5** - Added Changelog diff --git a/Cargo.lock b/Cargo.lock index 402ee9c9..ed055337 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5651,7 +5651,7 @@ checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" [[package]] name = "veilid-cli" -version = "0.1.5" +version = "0.1.6" dependencies = [ "arboard", "async-std", @@ -5687,7 +5687,7 @@ dependencies = [ [[package]] name = "veilid-core" -version = "0.1.5" +version = "0.1.6" dependencies = [ "argon2", "async-io", @@ -5790,7 +5790,7 @@ dependencies = [ [[package]] name = "veilid-flutter" -version = "0.1.5" +version = "0.1.6" dependencies = [ "allo-isolate", "async-std", @@ -5819,7 +5819,7 @@ dependencies = [ [[package]] name = "veilid-server" -version = "0.1.5" +version = "0.1.6" dependencies = [ "ansi_term", "async-std", @@ -5869,7 +5869,7 @@ dependencies = [ [[package]] name = "veilid-tools" -version = "0.1.5" +version = "0.1.6" dependencies = [ "android-logd-logger", "async-lock", @@ -5920,7 +5920,7 @@ dependencies = [ [[package]] name = "veilid-wasm" -version = "0.1.5" +version = "0.1.6" dependencies = [ "cfg-if 1.0.0", "console_error_panic_hook", diff --git a/veilid-cli/Cargo.toml b/veilid-cli/Cargo.toml index c8a19548..82b45638 100644 --- a/veilid-cli/Cargo.toml +++ b/veilid-cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-cli" -version = "0.1.5" +version = "0.1.6" authors = ["Veilid Team "] edition = "2021" license = "MPL-2.0" diff --git a/veilid-core/Cargo.toml b/veilid-core/Cargo.toml index 9d4c6134..da028eb2 100644 --- a/veilid-core/Cargo.toml +++ b/veilid-core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-core" -version = "0.1.5" +version = "0.1.6" authors = ["Veilid Team "] edition = "2021" build = "build.rs" diff --git a/veilid-flutter/pubspec.yaml b/veilid-flutter/pubspec.yaml index 61fd37ea..1adadf0e 100644 --- a/veilid-flutter/pubspec.yaml +++ b/veilid-flutter/pubspec.yaml @@ -1,5 +1,5 @@ name: veilid -version: 0.1.5 +version: 0.1.6 description: Veilid Framework homepage: https://veilid.com publish_to: "none" # Remove this line if you wish to publish to pub.dev diff --git a/veilid-flutter/rust/Cargo.toml b/veilid-flutter/rust/Cargo.toml index eab4d8a4..61f4d495 100644 --- a/veilid-flutter/rust/Cargo.toml +++ b/veilid-flutter/rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-flutter" -version = "0.1.5" +version = "0.1.6" authors = ["Veilid Team "] license = "MPL-2.0" edition = "2021" diff --git a/veilid-python/pyproject.toml b/veilid-python/pyproject.toml index 8f291393..4209cfba 100644 --- a/veilid-python/pyproject.toml +++ b/veilid-python/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "veilid" -version = "0.1.5" +version = "0.1.6" description = "" authors = ["Veilid Team "] readme = "README.md" diff --git a/veilid-server/Cargo.toml b/veilid-server/Cargo.toml index 1c2e9822..a2502104 100644 --- a/veilid-server/Cargo.toml +++ b/veilid-server/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-server" -version = "0.1.5" +version = "0.1.6" authors = ["Veilid Team "] license = "MPL-2.0" edition = "2021" diff --git a/veilid-tools/Cargo.toml b/veilid-tools/Cargo.toml index a2b16b68..4bba6dde 100644 --- a/veilid-tools/Cargo.toml +++ b/veilid-tools/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-tools" -version = "0.1.5" +version = "0.1.6" authors = ["Veilid Team "] license = "MPL-2.0" edition = "2021" diff --git a/veilid-wasm/Cargo.toml b/veilid-wasm/Cargo.toml index 8e52d264..b656bd78 100644 --- a/veilid-wasm/Cargo.toml +++ b/veilid-wasm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-wasm" -version = "0.1.5" +version = "0.1.6" authors = ["Veilid Team "] license = "MPL-2.0" edition = "2021" From b993063abaeca6f4a2ffb839d9ecd876fc6195c0 Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Sun, 23 Jul 2023 21:49:10 -0400 Subject: [PATCH 20/42] network and ios fixes --- veilid-core/src/rpc_processor/mod.rs | 15 ++++++++++++--- veilid-flutter/example/pubspec.lock | 10 +++++++++- veilid-flutter/lib/default_config.dart | 17 +++++++++++++---- veilid-flutter/pubspec.yaml | 1 + 4 files changed, 35 insertions(+), 8 deletions(-) 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-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/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 From a6666d3a6c5412d9a9b4916f2e1b77b4185512e8 Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Sun, 23 Jul 2023 23:13:42 -0400 Subject: [PATCH 21/42] addresses --- veilid-core/src/intf/native/network_interfaces/mod.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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); + } } } From f8bb97b39c0a534ec5b567885e6573dcea5acd6f Mon Sep 17 00:00:00 2001 From: Teknique Date: Mon, 24 Jul 2023 21:43:14 -0700 Subject: [PATCH 22/42] Fix(?) size check in try_from for DHTSchemaSMPL --- veilid-core/src/veilid_api/types/dht/schema/smpl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/veilid-core/src/veilid_api/types/dht/schema/smpl.rs b/veilid-core/src/veilid_api/types/dht/schema/smpl.rs index 942652fe..f1079a53 100644 --- a/veilid-core/src/veilid_api/types/dht/schema/smpl.rs +++ b/veilid-core/src/veilid_api/types/dht/schema/smpl.rs @@ -96,7 +96,7 @@ impl DHTSchemaSMPL { impl TryFrom<&[u8]> for DHTSchemaSMPL { type Error = VeilidAPIError; fn try_from(b: &[u8]) -> Result { - if b.len() != Self::FIXED_SIZE { + if b.len() < 4 { apibail_generic!("invalid size"); } if &b[0..4] != &Self::FCC { From 00aad2c728b2c99cf2762ce44e00d67d77fda01d Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Tue, 25 Jul 2023 01:04:22 -0400 Subject: [PATCH 23/42] bugfixes --- veilid-core/src/storage_manager/mod.rs | 12 +- veilid-flutter/lib/routing_context.dart | 40 +++---- veilid-flutter/lib/veilid.dart | 2 +- veilid-flutter/lib/veilid_config.dart | 110 +++++++++--------- veilid-flutter/lib/veilid_crypto.dart | 20 ++-- veilid-flutter/lib/veilid_encoding.dart | 2 +- veilid-flutter/lib/veilid_ffi.dart | 18 +-- veilid-flutter/lib/veilid_js.dart | 6 +- veilid-flutter/lib/veilid_state.dart | 52 ++++----- veilid-flutter/rust/src/dart_ffi.rs | 31 ++--- .../rust/src/dart_isolate_wrapper.rs | 21 ---- veilid-wasm/src/lib.rs | 27 ++--- 12 files changed, 158 insertions(+), 183 deletions(-) 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/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..4305ad60 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; @@ -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/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) }) } From 39baed5555ab2aa9ee212f837f6b527354bb7989 Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Tue, 25 Jul 2023 01:16:09 -0400 Subject: [PATCH 24/42] bugfix --- veilid-flutter/lib/veilid_ffi.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/veilid-flutter/lib/veilid_ffi.dart b/veilid-flutter/lib/veilid_ffi.dart index 4305ad60..73880aa3 100644 --- a/veilid-flutter/lib/veilid_ffi.dart +++ b/veilid-flutter/lib/veilid_ffi.dart @@ -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( From 7fa1df0474bbda78df52066f3b0b22f3a67e28e8 Mon Sep 17 00:00:00 2001 From: Teknique Date: Tue, 25 Jul 2023 08:39:15 -0700 Subject: [PATCH 25/42] Check b.len() < Self::FIXED_SIZE, not b.len() < 4 The original thinking was that if len(b) < FIXED_SIZE, then that would be picked up later by the "invalid member length" check. In that case, this only really *needs* to make sure that the check after this for "wrong fourcc" wouldn't fail. But if len(b) < FIXED_SIZE, it really is an invalid size, and should get that error message before even starting to validate its other qualities. --- veilid-core/src/veilid_api/types/dht/schema/smpl.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/veilid-core/src/veilid_api/types/dht/schema/smpl.rs b/veilid-core/src/veilid_api/types/dht/schema/smpl.rs index f1079a53..572b93e8 100644 --- a/veilid-core/src/veilid_api/types/dht/schema/smpl.rs +++ b/veilid-core/src/veilid_api/types/dht/schema/smpl.rs @@ -96,7 +96,7 @@ impl DHTSchemaSMPL { impl TryFrom<&[u8]> for DHTSchemaSMPL { type Error = VeilidAPIError; fn try_from(b: &[u8]) -> Result { - if b.len() < 4 { + if b.len() < Self::FIXED_SIZE { apibail_generic!("invalid size"); } if &b[0..4] != &Self::FCC { From f91a350bfc00e06330b1c0c91c2fd4be10042a17 Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Wed, 26 Jul 2023 14:20:17 -0400 Subject: [PATCH 26/42] lint work --- veilid-flutter/analysis_options.yaml | 3 +- veilid-flutter/example/analysis_options.yaml | 4 + veilid-flutter/lib/default_config.dart | 56 +- veilid-flutter/lib/routing_context.dart | 48 +- veilid-flutter/lib/veilid.dart | 54 +- veilid-flutter/lib/veilid_api_exception.dart | 224 ++--- veilid-flutter/lib/veilid_config.dart | 33 +- veilid-flutter/lib/veilid_crypto.dart | 103 +- veilid-flutter/lib/veilid_encoding.dart | 76 +- veilid-flutter/lib/veilid_ffi.dart | 909 ++++++++---------- veilid-flutter/lib/veilid_js.dart | 344 +++---- .../lib/veilid_plugin_stub_web.dart | 2 +- veilid-flutter/lib/veilid_state.dart | 10 +- veilid-flutter/lib/veilid_table_db.dart | 30 +- veilid-flutter/pubspec.yaml | 16 +- veilid-flutter/test/veilid_test.dart | 2 +- 16 files changed, 791 insertions(+), 1123 deletions(-) diff --git a/veilid-flutter/analysis_options.yaml b/veilid-flutter/analysis_options.yaml index e07e9e94..adefccd2 100644 --- a/veilid-flutter/analysis_options.yaml +++ b/veilid-flutter/analysis_options.yaml @@ -1,5 +1,4 @@ -include: package:flutter_lints/flutter.yaml - +include: package:lint_hard/all.yaml analyzer: errors: invalid_annotation_target: ignore diff --git a/veilid-flutter/example/analysis_options.yaml b/veilid-flutter/example/analysis_options.yaml index e07e9e94..bf4898d6 100644 --- a/veilid-flutter/example/analysis_options.yaml +++ b/veilid-flutter/example/analysis_options.yaml @@ -3,3 +3,7 @@ include: package:flutter_lints/flutter.yaml analyzer: errors: invalid_annotation_target: ignore + +linter: + rules: + - unawaited_futures \ No newline at end of file diff --git a/veilid-flutter/lib/default_config.dart b/veilid-flutter/lib/default_config.dart index 815c0793..ad27e415 100644 --- a/veilid-flutter/lib/default_config.dart +++ b/veilid-flutter/lib/default_config.dart @@ -1,10 +1,11 @@ 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:path_provider/path_provider.dart'; 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; @@ -57,31 +58,29 @@ int getRemoteMaxStorageSpaceMb() { return 256; } -Future getDefaultVeilidConfig(String programName) async { - return VeilidConfig( +Future getDefaultVeilidConfig(String programName) async => VeilidConfig( programName: programName, - namespace: "", + namespace: '', capabilities: const VeilidConfigCapabilities(disable: []), protectedStore: const VeilidConfigProtectedStore( allowInsecureFallback: false, alwaysUseInsecureStorage: false, - directory: "", + directory: '', delete: false, - deviceEncryptionKeyPassword: "", - newDeviceEncryptionKeyPassword: null, + deviceEncryptionKeyPassword: '', ), tableStore: VeilidConfigTableStore( directory: kIsWeb - ? "" + ? '' : p.join((await getApplicationSupportDirectory()).absolute.path, - "table_store"), + 'table_store'), delete: false, ), blockStore: VeilidConfigBlockStore( directory: kIsWeb - ? "" + ? '' : p.join((await getApplicationSupportDirectory()).absolute.path, - "block_store"), + 'block_store'), delete: false, ), network: VeilidConfigNetwork( @@ -98,8 +97,8 @@ Future getDefaultVeilidConfig(String programName) async { nodeId: [], nodeIdSecret: [], bootstrap: kIsWeb - ? ["ws://bootstrap.veilid.net:5150/ws"] - : ["bootstrap.veilid.net"], + ? ['ws://bootstrap.veilid.net:5150/ws'] + : ['bootstrap.veilid.net'], limitOverAttached: 64, limitFullyAttached: 32, limitAttachedStrong: 16, @@ -139,54 +138,47 @@ Future getDefaultVeilidConfig(String programName) async { detectAddressChanges: true, restrictedNatRetries: 0, tls: const VeilidConfigTLS( - certificatePath: "", - privateKeyPath: "", + certificatePath: '', + privateKeyPath: '', connectionInitialTimeoutMs: 2000, ), application: const VeilidConfigApplication( https: VeilidConfigHTTPS( enabled: false, - listenAddress: "", - path: "", - url: null, + listenAddress: '', + path: '', ), http: VeilidConfigHTTP( enabled: false, - listenAddress: "", - path: "", - url: null, + listenAddress: '', + path: '', )), protocol: const VeilidConfigProtocol( udp: VeilidConfigUDP( enabled: !kIsWeb, socketPoolSize: 0, - listenAddress: "", - publicAddress: null, + listenAddress: '', ), tcp: VeilidConfigTCP( connect: !kIsWeb, listen: !kIsWeb, maxConnections: 32, - listenAddress: "", - publicAddress: null, + listenAddress: '', ), ws: VeilidConfigWS( connect: true, listen: !kIsWeb, maxConnections: 16, - listenAddress: "", - path: "ws", - url: null, + listenAddress: '', + path: 'ws', ), wss: VeilidConfigWSS( connect: true, listen: false, maxConnections: 16, - listenAddress: "", - path: "ws", - url: null, + listenAddress: '', + path: 'ws', ), ), ), ); -} diff --git a/veilid-flutter/lib/routing_context.dart b/veilid-flutter/lib/routing_context.dart index f71cd285..eda071bb 100644 --- a/veilid-flutter/lib/routing_context.dart +++ b/veilid-flutter/lib/routing_context.dart @@ -26,7 +26,7 @@ extension ValidateDFLT on DHTSchemaDFLT { extension ValidateSMPL on DHTSchemaSMPL { bool validate() { - final totalsv = members.fold(0, (acc, v) => (acc + v.mCnt)) + oCnt; + final totalsv = members.fold(0, (acc, v) => acc + v.mCnt) + oCnt; if (totalsv > 65535) { return false; } @@ -76,8 +76,7 @@ class DHTRecordDescriptor with _$DHTRecordDescriptor { const factory DHTRecordDescriptor({ required TypedKey key, required PublicKey owner, - PublicKey? ownerSecret, - required DHTSchema schema, + required DHTSchema schema, PublicKey? ownerSecret, }) = _DHTRecordDescriptor; factory DHTRecordDescriptor.fromJson(dynamic json) => _$DHTRecordDescriptorFromJson(json as Map); @@ -163,13 +162,13 @@ enum Sequencing { abstract class SafetySelection extends Equatable { factory SafetySelection.fromJson(dynamic jsond) { final json = jsond as Map; - if (json.containsKey("Unsafe")) { + if (json.containsKey('Unsafe')) { return SafetySelectionUnsafe( - sequencing: Sequencing.fromJson(json["Unsafe"])); - } else if (json.containsKey("Safe")) { - return SafetySelectionSafe(safetySpec: SafetySpec.fromJson(json["Safe"])); + sequencing: Sequencing.fromJson(json['Unsafe'])); + } else if (json.containsKey('Safe')) { + return SafetySelectionSafe(safetySpec: SafetySpec.fromJson(json['Safe'])); } else { - throw const VeilidAPIExceptionInternal("Invalid SafetySelection"); + throw const VeilidAPIExceptionInternal('Invalid SafetySelection'); } } Map toJson(); @@ -177,50 +176,43 @@ abstract class SafetySelection extends Equatable { @immutable class SafetySelectionUnsafe implements SafetySelection { + + // + const SafetySelectionUnsafe({ + required this.sequencing, + }); final Sequencing sequencing; @override List get props => [sequencing]; @override bool? get stringify => null; - // - const SafetySelectionUnsafe({ - required this.sequencing, - }); - @override - Map toJson() { - return {'Unsafe': sequencing.toJson()}; - } + Map toJson() => {'Unsafe': sequencing.toJson()}; } @immutable class SafetySelectionSafe implements SafetySelection { + + // + const SafetySelectionSafe({ + required this.safetySpec, + }); final SafetySpec safetySpec; @override List get props => [safetySpec]; @override bool? get stringify => null; - // - const SafetySelectionSafe({ - required this.safetySpec, - }); - @override - Map toJson() { - return {'Safe': safetySpec.toJson()}; - } + Map toJson() => {'Safe': safetySpec.toJson()}; } /// Options for safety routes (sender privacy) @freezed class SafetySpec with _$SafetySpec { const factory SafetySpec({ - String? preferredRoute, - required int hopCount, - required Stability stability, - required Sequencing sequencing, + required int hopCount, required Stability stability, required Sequencing sequencing, String? preferredRoute, }) = _SafetySpec; factory SafetySpec.fromJson(dynamic json) => diff --git a/veilid-flutter/lib/veilid.dart b/veilid-flutter/lib/veilid.dart index b9c370e6..296f976b 100644 --- a/veilid-flutter/lib/veilid.dart +++ b/veilid-flutter/lib/veilid.dart @@ -4,28 +4,26 @@ import 'dart:typed_data'; import 'package:equatable/equatable.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; -import 'veilid_stub.dart' - if (dart.library.io) 'veilid_ffi.dart' - if (dart.library.js) 'veilid_js.dart'; - ////////////////////////////////////////////////////////// import 'routing_context.dart'; import 'veilid_config.dart'; import 'veilid_crypto.dart'; -import 'veilid_table_db.dart'; import 'veilid_state.dart'; +import 'veilid_stub.dart' + if (dart.library.io) 'veilid_ffi.dart' + if (dart.library.js) 'veilid_js.dart'; +import 'veilid_table_db.dart'; export 'default_config.dart'; export 'routing_context.dart'; - -export 'veilid_encoding.dart'; +export 'veilid.dart'; +export 'veilid_api_exception.dart'; export 'veilid_config.dart'; export 'veilid_crypto.dart'; -export 'veilid_table_db.dart'; -export 'veilid_api_exception.dart'; +export 'veilid_encoding.dart'; export 'veilid_state.dart'; -export 'veilid.dart'; +export 'veilid_table_db.dart'; ////////////////////////////////////// /// JSON Encode Helper @@ -42,56 +40,50 @@ Object? veilidApiToEncodable(Object? value) { } T? Function(dynamic) optFromJson( - T Function(Map) jsonConstructor) { - return (dynamic j) { + T Function(Map) jsonConstructor) => (dynamic j) { if (j == null) { return null; } else { return jsonConstructor(j); } }; -} List Function(dynamic) jsonListConstructor( - T Function(Map) jsonConstructor) { - return (dynamic j) { - return (j as List>) + T Function(Map) jsonConstructor) => (dynamic j) => (j as List>) .map((e) => jsonConstructor(e)) .toList(); - }; -} ////////////////////////////////////// /// VeilidVersion @immutable class VeilidVersion extends Equatable { + + const VeilidVersion(this.major, this.minor, this.patch); final int major; final int minor; final int patch; @override List get props => [major, minor, patch]; - - const VeilidVersion(this.major, this.minor, this.patch); } ////////////////////////////////////// /// Timestamp @immutable class Timestamp extends Equatable { + + const Timestamp({required this.value}); + factory Timestamp.fromString(String s) => Timestamp(value: BigInt.parse(s)); + factory Timestamp.fromJson(dynamic json) => + Timestamp.fromString(json as String); final BigInt value; @override List get props => [value]; - const Timestamp({required this.value}); - @override String toString() => value.toString(); - factory Timestamp.fromString(String s) => Timestamp(value: BigInt.parse(s)); String toJson() => toString(); - factory Timestamp.fromJson(dynamic json) => - Timestamp.fromString(json as String); TimestampDuration diff(Timestamp other) => TimestampDuration(value: value - other.value); @@ -102,20 +94,20 @@ class Timestamp extends Equatable { @immutable class TimestampDuration extends Equatable { + + const TimestampDuration({required this.value}); + factory TimestampDuration.fromString(String s) => + TimestampDuration(value: BigInt.parse(s)); + factory TimestampDuration.fromJson(dynamic json) => + TimestampDuration.fromString(json as String); final BigInt value; @override List get props => [value]; - const TimestampDuration({required this.value}); - @override String toString() => value.toString(); - factory TimestampDuration.fromString(String s) => - TimestampDuration(value: BigInt.parse(s)); String toJson() => toString(); - factory TimestampDuration.fromJson(dynamic json) => - TimestampDuration.fromString(json as String); int toMillis() => (value ~/ BigInt.from(1000)).toInt(); BigInt toMicros() => value; diff --git a/veilid-flutter/lib/veilid_api_exception.dart b/veilid-flutter/lib/veilid_api_exception.dart index e08525bb..bc0deb7d 100644 --- a/veilid-flutter/lib/veilid_api_exception.dart +++ b/veilid-flutter/lib/veilid_api_exception.dart @@ -6,64 +6,64 @@ import 'package:freezed_annotation/freezed_annotation.dart'; @immutable abstract class VeilidAPIException implements Exception { factory VeilidAPIException.fromJson(dynamic json) { - switch (json["kind"]) { - case "NotInitialized": + switch (json['kind']) { + case 'NotInitialized': { return VeilidAPIExceptionNotInitialized(); } - case "AlreadyInitialized": + case 'AlreadyInitialized': { return VeilidAPIExceptionAlreadyInitialized(); } - case "Timeout": + case 'Timeout': { return VeilidAPIExceptionTimeout(); } - case "TryAgain": + case 'TryAgain': { return VeilidAPIExceptionTryAgain(); } - case "Shutdown": + case 'Shutdown': { return VeilidAPIExceptionShutdown(); } - case "InvalidTarget": + case 'InvalidTarget': { return VeilidAPIExceptionInvalidTarget(); } - case "NoConnection": + case 'NoConnection': { - return VeilidAPIExceptionNoConnection(json["message"]); + return VeilidAPIExceptionNoConnection(json['message']); } - case "KeyNotFound": + case 'KeyNotFound': { - return VeilidAPIExceptionKeyNotFound(json["key"]); + return VeilidAPIExceptionKeyNotFound(json['key']); } - case "Internal": + case 'Internal': { - return VeilidAPIExceptionInternal(json["message"]); + return VeilidAPIExceptionInternal(json['message']); } - case "Unimplemented": + case 'Unimplemented': { - return VeilidAPIExceptionUnimplemented(json["unimplemented"]); + return VeilidAPIExceptionUnimplemented(json['unimplemented']); } - case "ParseError": + case 'ParseError': { - return VeilidAPIExceptionParseError(json["message"], json["value"]); + return VeilidAPIExceptionParseError(json['message'], json['value']); } - case "InvalidArgument": + case 'InvalidArgument': { return VeilidAPIExceptionInvalidArgument( - json["context"], json["argument"], json["value"]); + json['context'], json['argument'], json['value']); } - case "MissingArgument": + case 'MissingArgument': { return VeilidAPIExceptionMissingArgument( - json["context"], json["argument"]); + json['context'], json['argument']); } - case "Generic": + case 'Generic': { - return VeilidAPIExceptionGeneric(json["message"]); + return VeilidAPIExceptionGeneric(json['message']); } default: { @@ -79,224 +79,168 @@ abstract class VeilidAPIException implements Exception { @immutable class VeilidAPIExceptionNotInitialized implements VeilidAPIException { @override - String toString() { - return "VeilidAPIException: NotInitialized"; - } + String toString() => 'VeilidAPIException: NotInitialized'; @override - String toDisplayError() { - return "Not initialized"; - } + String toDisplayError() => 'Not initialized'; } @immutable class VeilidAPIExceptionAlreadyInitialized implements VeilidAPIException { @override - String toString() { - return "VeilidAPIException: AlreadyInitialized"; - } + String toString() => 'VeilidAPIException: AlreadyInitialized'; @override - String toDisplayError() { - return "Already initialized"; - } + String toDisplayError() => 'Already initialized'; } @immutable class VeilidAPIExceptionTimeout implements VeilidAPIException { @override - String toString() { - return "VeilidAPIException: Timeout"; - } + String toString() => 'VeilidAPIException: Timeout'; @override - String toDisplayError() { - return "Timeout"; - } + String toDisplayError() => 'Timeout'; } @immutable class VeilidAPIExceptionTryAgain implements VeilidAPIException { @override - String toString() { - return "VeilidAPIException: TryAgain"; - } + String toString() => 'VeilidAPIException: TryAgain'; @override - String toDisplayError() { - return "Try again"; - } + String toDisplayError() => 'Try again'; } @immutable class VeilidAPIExceptionShutdown implements VeilidAPIException { @override - String toString() { - return "VeilidAPIException: Shutdown"; - } + String toString() => 'VeilidAPIException: Shutdown'; @override - String toDisplayError() { - return "Currently shut down"; - } + String toDisplayError() => 'Currently shut down'; } @immutable class VeilidAPIExceptionInvalidTarget implements VeilidAPIException { @override - String toString() { - return "VeilidAPIException: InvalidTarget"; - } + String toString() => 'VeilidAPIException: InvalidTarget'; @override - String toDisplayError() { - return "Invalid target"; - } + String toDisplayError() => 'Invalid target'; } @immutable class VeilidAPIExceptionNoConnection implements VeilidAPIException { - final String message; - @override - String toString() { - return "VeilidAPIException: NoConnection (message: $message)"; - } - - @override - String toDisplayError() { - return "No connection: $message"; - } // const VeilidAPIExceptionNoConnection(this.message); + final String message; + @override + String toString() => 'VeilidAPIException: NoConnection (message: $message)'; + + @override + String toDisplayError() => 'No connection: $message'; } @immutable class VeilidAPIExceptionKeyNotFound implements VeilidAPIException { - final String key; - @override - String toString() { - return "VeilidAPIException: KeyNotFound (key: $key)"; - } - - @override - String toDisplayError() { - return "Key not found: $key"; - } // const VeilidAPIExceptionKeyNotFound(this.key); + final String key; + @override + String toString() => 'VeilidAPIException: KeyNotFound (key: $key)'; + + @override + String toDisplayError() => 'Key not found: $key'; } @immutable class VeilidAPIExceptionInternal implements VeilidAPIException { - final String message; - - @override - String toString() { - return "VeilidAPIException: Internal ($message)"; - } - - @override - String toDisplayError() { - return "Internal error: $message"; - } // const VeilidAPIExceptionInternal(this.message); + final String message; + + @override + String toString() => 'VeilidAPIException: Internal ($message)'; + + @override + String toDisplayError() => 'Internal error: $message'; } @immutable class VeilidAPIExceptionUnimplemented implements VeilidAPIException { - final String message; - - @override - String toString() { - return "VeilidAPIException: Unimplemented ($message)"; - } - - @override - String toDisplayError() { - return "Unimplemented: $message"; - } // const VeilidAPIExceptionUnimplemented(this.message); + final String message; + + @override + String toString() => 'VeilidAPIException: Unimplemented ($message)'; + + @override + String toDisplayError() => 'Unimplemented: $message'; } @immutable class VeilidAPIExceptionParseError implements VeilidAPIException { + + // + const VeilidAPIExceptionParseError(this.message, this.value); final String message; final String value; @override - String toString() { - return "VeilidAPIException: ParseError ($message)\n value: $value"; - } + String toString() => 'VeilidAPIException: ParseError ($message)\n value: $value'; @override - String toDisplayError() { - return "Parse error: $message"; - } - - // - const VeilidAPIExceptionParseError(this.message, this.value); + String toDisplayError() => 'Parse error: $message'; } @immutable class VeilidAPIExceptionInvalidArgument implements VeilidAPIException { + + // + const VeilidAPIExceptionInvalidArgument( + this.context, this.argument, this.value); final String context; final String argument; final String value; @override - String toString() { - return "VeilidAPIException: InvalidArgument ($context:$argument)\n value: $value"; - } + String toString() => 'VeilidAPIException: InvalidArgument ($context:$argument)\n value: $value'; @override - String toDisplayError() { - return "Invalid argument for $context: $argument"; - } - - // - const VeilidAPIExceptionInvalidArgument( - this.context, this.argument, this.value); + String toDisplayError() => 'Invalid argument for $context: $argument'; } @immutable class VeilidAPIExceptionMissingArgument implements VeilidAPIException { + + // + const VeilidAPIExceptionMissingArgument(this.context, this.argument); final String context; final String argument; @override - String toString() { - return "VeilidAPIException: MissingArgument ($context:$argument)"; - } + String toString() => 'VeilidAPIException: MissingArgument ($context:$argument)'; @override - String toDisplayError() { - return "Missing argument for $context: $argument"; - } - - // - const VeilidAPIExceptionMissingArgument(this.context, this.argument); + String toDisplayError() => 'Missing argument for $context: $argument'; } @immutable class VeilidAPIExceptionGeneric implements VeilidAPIException { - final String message; - - @override - String toString() { - return "VeilidAPIException: Generic (message: $message)"; - } - - @override - String toDisplayError() { - return message; - } // const VeilidAPIExceptionGeneric(this.message); + final String message; + + @override + String toString() => 'VeilidAPIException: Generic (message: $message)'; + + @override + String toDisplayError() => message; } diff --git a/veilid-flutter/lib/veilid_config.dart b/veilid-flutter/lib/veilid_config.dart index bce2437f..ab6b2ba2 100644 --- a/veilid-flutter/lib/veilid_config.dart +++ b/veilid-flutter/lib/veilid_config.dart @@ -1,9 +1,8 @@ -import 'package:freezed_annotation/freezed_annotation.dart'; -import 'package:flutter/foundation.dart'; import 'package:change_case/change_case.dart'; +import 'package:flutter/foundation.dart'; +import 'package:freezed_annotation/freezed_annotation.dart'; + import 'veilid.dart'; -import 'veilid_encoding.dart'; -import 'veilid_crypto.dart'; part 'veilid_config.freezed.dart'; part 'veilid_config.g.dart'; @@ -126,13 +125,9 @@ enum VeilidConfigLogLevel { debug, trace; - String toJson() { - return name.toPascalCase(); - } + String toJson() => name.toPascalCase(); - factory VeilidConfigLogLevel.fromJson(dynamic j) { - return VeilidConfigLogLevel.values.byName((j as String).toCamelCase()); - } + factory VeilidConfigLogLevel.fromJson(dynamic j) => VeilidConfigLogLevel.values.byName((j as String).toCamelCase()); } ////////////////////////////////////// @@ -300,11 +295,8 @@ class VeilidConfigRPC with _$VeilidConfigRPC { const factory VeilidConfigRPC( {required int concurrency, required int queueSize, - int? maxTimestampBehindMs, - int? maxTimestampAheadMs, - required int timeoutMs, - required int maxRouteHopCount, - required int defaultRouteHopCount}) = _VeilidConfigRPC; + required int timeoutMs, required int maxRouteHopCount, required int defaultRouteHopCount, int? maxTimestampBehindMs, + int? maxTimestampAheadMs}) = _VeilidConfigRPC; factory VeilidConfigRPC.fromJson(dynamic json) => _$VeilidConfigRPCFromJson(json as Map); @@ -343,16 +335,7 @@ class VeilidConfigNetwork with _$VeilidConfigNetwork { required int clientWhitelistTimeoutMs, required int reverseConnectionReceiptTimeMs, required int holePunchReceiptTimeMs, - String? networkKeyPassword, - required VeilidConfigRoutingTable routingTable, - required VeilidConfigRPC rpc, - required VeilidConfigDHT dht, - required bool upnp, - required bool detectAddressChanges, - required int restrictedNatRetries, - required VeilidConfigTLS tls, - required VeilidConfigApplication application, - required VeilidConfigProtocol protocol, + required VeilidConfigRoutingTable routingTable, required VeilidConfigRPC rpc, required VeilidConfigDHT dht, required bool upnp, required bool detectAddressChanges, required int restrictedNatRetries, required VeilidConfigTLS tls, required VeilidConfigApplication application, required VeilidConfigProtocol protocol, String? networkKeyPassword, }) = _VeilidConfigNetwork; factory VeilidConfigNetwork.fromJson(dynamic json) => diff --git a/veilid-flutter/lib/veilid_crypto.dart b/veilid-flutter/lib/veilid_crypto.dart index 98c20f90..8a267b42 100644 --- a/veilid-flutter/lib/veilid_crypto.dart +++ b/veilid-flutter/lib/veilid_crypto.dart @@ -5,7 +5,6 @@ import 'package:charcode/charcode.dart'; import 'package:equatable/equatable.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; -import 'veilid_encoding.dart'; import 'veilid.dart'; ////////////////////////////////////// @@ -17,24 +16,22 @@ const CryptoKind cryptoKindVLD0 = const CryptoKind cryptoKindNONE = $N << 0 | $O << 8 | $N << 16 | $E << 24; // "NONE" -String cryptoKindToString(CryptoKind kind) { - return cryptoKindToBytes(kind).map((c) => String.fromCharCode(c)).join(); -} +String cryptoKindToString(CryptoKind kind) => cryptoKindToBytes(kind).map(String.fromCharCode).join(); const CryptoKind bestCryptoKind = cryptoKindVLD0; Uint8List cryptoKindToBytes(CryptoKind kind) { - var b = Uint8List(4); - ByteData.sublistView(b).setUint32(0, kind, Endian.big); + final b = Uint8List(4); + ByteData.sublistView(b).setUint32(0, kind); return b; } CryptoKind cryptoKindFromString(String s) { if (s.codeUnits.length != 4) { - throw const FormatException("malformed string"); + throw const FormatException('malformed string'); } - CryptoKind kind = ByteData.sublistView(Uint8List.fromList(s.codeUnits)) - .getUint32(0, Endian.big); + final kind = ByteData.sublistView(Uint8List.fromList(s.codeUnits)) + .getUint32(0); return kind; } @@ -43,103 +40,99 @@ CryptoKind cryptoKindFromString(String s) { @immutable class Typed extends Equatable { + + const Typed({required this.kind, required this.value}); + + factory Typed.fromString(String s) { + final parts = s.split(':'); + if (parts.length < 2 || parts[0].codeUnits.length != 4) { + throw const FormatException('malformed string'); + } + final kind = cryptoKindFromString(parts[0]); + final value = EncodedString.fromString(parts.sublist(1).join(':')); + return Typed(kind: kind, value: value); + } + factory Typed.fromJson(dynamic json) => Typed.fromString(json as String); final CryptoKind kind; final V value; @override List get props => [kind, value]; - const Typed({required this.kind, required this.value}); - @override - String toString() { - return "${cryptoKindToString(kind)}:$value"; - } - - factory Typed.fromString(String s) { - final parts = s.split(":"); - if (parts.length < 2 || parts[0].codeUnits.length != 4) { - throw const FormatException("malformed string"); - } - final kind = cryptoKindFromString(parts[0]); - final value = EncodedString.fromString(parts.sublist(1).join(":")); - return Typed(kind: kind, value: value); - } + String toString() => '${cryptoKindToString(kind)}:$value'; Uint8List decode() { - var b = BytesBuilder(); + final b = BytesBuilder(); b.add(cryptoKindToBytes(kind)); b.add(value.decode()); return b.toBytes(); } String toJson() => toString(); - factory Typed.fromJson(dynamic json) => Typed.fromString(json as String); } @immutable class KeyPair extends Equatable { - final PublicKey key; - final PublicKey secret; - @override - List get props => [key, secret]; const KeyPair({required this.key, required this.secret}); - @override - String toString() { - return "${key.toString()}:${secret.toString()}"; - } - factory KeyPair.fromString(String s) { - final parts = s.split(":"); + final parts = s.split(':'); if (parts.length != 2 || parts[0].codeUnits.length != 43 || parts[1].codeUnits.length != 43) { - throw const FormatException("malformed string"); + throw const FormatException('malformed string'); } final key = PublicKey.fromString(parts[0]); final secret = PublicKey.fromString(parts[1]); return KeyPair(key: key, secret: secret); } + factory KeyPair.fromJson(dynamic json) => KeyPair.fromString(json as String); + final PublicKey key; + final PublicKey secret; + @override + List get props => [key, secret]; + + @override + String toString() => '$key:$secret'; String toJson() => toString(); - factory KeyPair.fromJson(dynamic json) => KeyPair.fromString(json as String); } @immutable class TypedKeyPair extends Equatable { - final CryptoKind kind; - final PublicKey key; - final PublicKey secret; - @override - List get props => [kind, key, secret]; const TypedKeyPair( {required this.kind, required this.key, required this.secret}); - @override - String toString() => - "${cryptoKindToString(kind)}:${key.toString()}:${secret.toString()}"; - factory TypedKeyPair.fromString(String s) { - final parts = s.split(":"); + final parts = s.split(':'); if (parts.length != 3 || parts[0].codeUnits.length != 4 || parts[1].codeUnits.length != 43 || parts[2].codeUnits.length != 43) { - throw VeilidAPIExceptionInvalidArgument("malformed string", "s", s); + throw VeilidAPIExceptionInvalidArgument('malformed string', 's', s); } final kind = cryptoKindFromString(parts[0]); final key = PublicKey.fromString(parts[1]); final secret = PublicKey.fromString(parts[2]); return TypedKeyPair(kind: kind, key: key, secret: secret); } - - String toJson() => toString(); factory TypedKeyPair.fromJson(dynamic json) => TypedKeyPair.fromString(json as String); factory TypedKeyPair.fromKeyPair(CryptoKind kind, KeyPair keyPair) => TypedKeyPair(kind: kind, key: keyPair.key, secret: keyPair.secret); + final CryptoKind kind; + final PublicKey key; + final PublicKey secret; + @override + List get props => [kind, key, secret]; + + @override + String toString() => + '${cryptoKindToString(kind)}:$key:$secret'; + + String toJson() => toString(); } typedef CryptoKey = FixedEncodedString43; @@ -176,17 +169,13 @@ abstract class VeilidCryptoSystem { Future generateHash(Uint8List data); //Future generateHashReader(Stream> reader); Future validateKeyPair(PublicKey key, SecretKey secret); - Future validateKeyPairWithKeyPair(KeyPair keyPair) { - return validateKeyPair(keyPair.key, keyPair.secret); - } + Future validateKeyPairWithKeyPair(KeyPair keyPair) => validateKeyPair(keyPair.key, keyPair.secret); Future validateHash(Uint8List data, HashDigest hash); //Future validateHashReader(Stream> reader, HashDigest hash); Future distance(CryptoKey key1, CryptoKey key2); Future sign(PublicKey key, SecretKey secret, Uint8List data); - Future signWithKeyPair(KeyPair keyPair, Uint8List data) { - return sign(keyPair.key, keyPair.secret, data); - } + Future signWithKeyPair(KeyPair keyPair, Uint8List data) => sign(keyPair.key, keyPair.secret, data); Future verify(PublicKey key, Uint8List data, Signature signature); Future aeadOverhead(); diff --git a/veilid-flutter/lib/veilid_encoding.dart b/veilid-flutter/lib/veilid_encoding.dart index d10c1f28..316df202 100644 --- a/veilid-flutter/lib/veilid_encoding.dart +++ b/veilid-flutter/lib/veilid_encoding.dart @@ -34,15 +34,13 @@ class Uint8ListJsonConverter implements JsonConverter { @immutable abstract class EncodedString extends Equatable { + + const EncodedString(String s) : contents = s; final String contents; @override List get props => [contents]; - const EncodedString(String s) : contents = s; - - Uint8List decode() { - return base64UrlNoPadDecode(contents); - } + Uint8List decode() => base64UrlNoPadDecode(contents); @override String toString() => contents; @@ -76,96 +74,82 @@ abstract class EncodedString extends Equatable { @immutable class FixedEncodedString32 extends EncodedString { - const FixedEncodedString32._(String s) : super(s); - static int encodedLength() { - return 32; - } - - static int decodedLength() { - return 24; - } factory FixedEncodedString32.fromBytes(Uint8List bytes) { if (bytes.length != decodedLength()) { - throw Exception("length ${bytes.length} should be ${decodedLength()}"); + throw Exception('length ${bytes.length} should be ${decodedLength()}'); } return FixedEncodedString32._(base64UrlNoPadEncode(bytes)); } factory FixedEncodedString32.fromString(String s) { - var d = base64UrlNoPadDecode(s); + final d = base64UrlNoPadDecode(s); if (d.length != decodedLength()) { - throw Exception("length ${s.length} should be ${encodedLength()}"); + throw Exception('length ${s.length} should be ${encodedLength()}'); } return FixedEncodedString32._(s); } - - String toJson() => toString(); factory FixedEncodedString32.fromJson(dynamic json) => FixedEncodedString32.fromString(json as String); + const FixedEncodedString32._(super.s); + static int encodedLength() => 32; + + static int decodedLength() => 24; + + String toJson() => toString(); } @immutable class FixedEncodedString43 extends EncodedString { - const FixedEncodedString43._(String s) : super(s); - static int encodedLength() { - return 43; - } - - static int decodedLength() { - return 32; - } factory FixedEncodedString43.fromBytes(Uint8List bytes) { if (bytes.length != decodedLength()) { - throw Exception("length ${bytes.length} should be ${decodedLength()}"); + throw Exception('length ${bytes.length} should be ${decodedLength()}'); } return FixedEncodedString43._(base64UrlNoPadEncode(bytes)); } factory FixedEncodedString43.fromString(String s) { - var d = base64UrlNoPadDecode(s); + final d = base64UrlNoPadDecode(s); if (d.length != decodedLength()) { - throw Exception("length ${s.length} should be ${encodedLength()}"); + throw Exception('length ${s.length} should be ${encodedLength()}'); } return FixedEncodedString43._(s); } - - String toJson() => toString(); factory FixedEncodedString43.fromJson(dynamic json) => FixedEncodedString43.fromString(json as String); + const FixedEncodedString43._(super.s); + static int encodedLength() => 43; + + static int decodedLength() => 32; + + String toJson() => toString(); } @immutable class FixedEncodedString86 extends EncodedString { - const FixedEncodedString86._(String s) : super(s); - static int encodedLength() { - return 86; - } - - static int decodedLength() { - return 64; - } - - String toJson() { - return toString(); - } factory FixedEncodedString86.fromBytes(Uint8List bytes) { if (bytes.length != decodedLength()) { - throw Exception("length ${bytes.length} should be ${decodedLength()}"); + throw Exception('length ${bytes.length} should be ${decodedLength()}'); } return FixedEncodedString86._(base64UrlNoPadEncode(bytes)); } factory FixedEncodedString86.fromString(String s) { - var d = base64UrlNoPadDecode(s); + final d = base64UrlNoPadDecode(s); if (d.length != decodedLength()) { - throw Exception("length ${s.length} should be ${encodedLength()}"); + throw Exception('length ${s.length} should be ${encodedLength()}'); } return FixedEncodedString86._(s); } factory FixedEncodedString86.fromJson(dynamic json) => FixedEncodedString86.fromString(json as String); + const FixedEncodedString86._(super.s); + static int encodedLength() => 86; + + static int decodedLength() => 64; + + String toJson() => toString(); } diff --git a/veilid-flutter/lib/veilid_ffi.dart b/veilid-flutter/lib/veilid_ffi.dart index 73880aa3..83a1fe69 100644 --- a/veilid-flutter/lib/veilid_ffi.dart +++ b/veilid-flutter/lib/veilid_ffi.dart @@ -1,14 +1,13 @@ import 'dart:async'; +import 'dart:convert'; import 'dart:ffi'; import 'dart:io'; import 'dart:isolate'; -import 'dart:convert'; import 'dart:typed_data'; import 'package:ffi/ffi.dart'; import 'veilid.dart'; -import 'veilid_encoding.dart'; ////////////////////////////////////////////////////////// @@ -26,282 +25,178 @@ final _dylib = typedef _DartPostCObject = NativeFunction)>; // fn free_string(s: *mut std::os::raw::c_char) -typedef _FreeStringC = Void Function(Pointer); typedef _FreeStringDart = void Function(Pointer); // fn initialize_veilid_flutter(dart_post_c_object_ptr: ffi::DartPostCObjectFnType) -typedef _InitializeVeilidFlutterC = Void Function(Pointer<_DartPostCObject>); -typedef _InitializeVeilidFlutterDart = void Function(Pointer<_DartPostCObject>); // fn initialize_veilid_core(platform_config: FfiStr) -typedef _InitializeVeilidCoreC = Void Function(Pointer); typedef _InitializeVeilidCoreDart = void Function(Pointer); // fn change_log_level(layer: FfiStr, log_level: FfiStr) -typedef _ChangeLogLevelC = Void Function(Pointer, Pointer); typedef _ChangeLogLevelDart = void Function(Pointer, Pointer); // fn startup_veilid_core(port: i64, config: FfiStr) -typedef _StartupVeilidCoreC = Void Function(Int64, Int64, Pointer); typedef _StartupVeilidCoreDart = void Function(int, int, Pointer); // fn get_veilid_state(port: i64) -typedef _GetVeilidStateC = Void Function(Int64); typedef _GetVeilidStateDart = void Function(int); // fn attach(port: i64) -typedef _AttachC = Void Function(Int64); typedef _AttachDart = void Function(int); // fn detach(port: i64) -typedef _DetachC = Void Function(Int64); typedef _DetachDart = void Function(int); // fn routing_context(port: i64) -typedef _RoutingContextC = Void Function(Int64); typedef _RoutingContextDart = void Function(int); // fn release_routing_context(id: u32) -typedef _ReleaseRoutingContextC = Int32 Function(Uint32); typedef _ReleaseRoutingContextDart = int Function(int); // fn routing_context_with_privacy(id: u32) -> u32 -typedef _RoutingContextWithPrivacyC = Uint32 Function(Uint32); typedef _RoutingContextWithPrivacyDart = int Function(int); // fn routing_context_with_custom_privacy(id: u32, stability: FfiStr) -typedef _RoutingContextWithCustomPrivacyC = Uint32 Function( - Uint32, Pointer); typedef _RoutingContextWithCustomPrivacyDart = int Function(int, Pointer); // fn routing_context_with_sequencing(id: u32, sequencing: FfiStr) -typedef _RoutingContextWithSequencingC = Uint32 Function(Uint32, Pointer); typedef _RoutingContextWithSequencingDart = int Function(int, Pointer); // fn routing_context_app_call(port: i64, id: u32, target: FfiStr, request: FfiStr) -typedef _RoutingContextAppCallC = Void Function( - Int64, Uint32, Pointer, Pointer); typedef _RoutingContextAppCallDart = void Function( int, int, Pointer, Pointer); // fn routing_context_app_message(port: i64, id: u32, target: FfiStr, request: FfiStr) -typedef _RoutingContextAppMessageC = Void Function( - Int64, Uint32, Pointer, Pointer); typedef _RoutingContextAppMessageDart = void Function( int, int, Pointer, Pointer); // fn routing_context_create_dht_record(port: i64, id: u32, kind: u32, schema: FfiStr) -typedef _RoutingContextCreateDHTRecordC = Void Function( - Int64, Uint32, Pointer, Uint32); typedef _RoutingContextCreateDHTRecordDart = void Function( int, int, Pointer, int); // fn routing_context_open_dht_record(port: i64, id: u32, key: FfiStr, writer: FfiStr) -typedef _RoutingContextOpenDHTRecordC = Void Function( - Int64, Uint32, Pointer, Pointer); typedef _RoutingContextOpenDHTRecordDart = void Function( int, int, Pointer, Pointer); // fn routing_context_close_dht_record(port: i64, id: u32, key: FfiStr) -typedef _RoutingContextCloseDHTRecordC = Void Function( - Int64, Uint32, Pointer); typedef _RoutingContextCloseDHTRecordDart = void Function( int, int, Pointer); // fn routing_context_delete_dht_record(port: i64, id: u32, key: FfiStr) -typedef _RoutingContextDeleteDHTRecordC = Void Function( - Int64, Uint32, Pointer); typedef _RoutingContextDeleteDHTRecordDart = void Function( int, int, Pointer); // fn routing_context_get_dht_value(port: i64, id: u32, key: FfiStr, subkey: u32, force_refresh: bool) -typedef _RoutingContextGetDHTValueC = Void Function( - Int64, Uint32, Pointer, Uint32, Bool); typedef _RoutingContextGetDHTValueDart = void Function( int, int, Pointer, int, bool); // fn routing_context_set_dht_value(port: i64, id: u32, key: FfiStr, subkey: u32, data: FfiStr) -typedef _RoutingContextSetDHTValueC = Void Function( - Int64, Uint32, Pointer, Uint32, Pointer); typedef _RoutingContextSetDHTValueDart = void Function( int, int, Pointer, int, Pointer); // fn routing_context_watch_dht_values(port: i64, id: u32, key: FfiStr, subkeys: FfiStr, expiration: FfiStr, count: u32) -typedef _RoutingContextWatchDHTValuesC = Void Function( - Int64, Uint32, Pointer, Pointer, Uint64, Uint32); typedef _RoutingContextWatchDHTValuesDart = void Function( int, int, Pointer, Pointer, int, int); // fn routing_context_cancel_dht_watch(port: i64, id: u32, key: FfiStr, subkeys: FfiStr) -typedef _RoutingContextCancelDHTWatchC = Void Function( - Int64, Uint32, Pointer, Pointer); typedef _RoutingContextCancelDHTWatchDart = void Function( int, int, Pointer, Pointer); // fn new_private_route(port: i64) -typedef _NewPrivateRouteC = Void Function(Int64); typedef _NewPrivateRouteDart = void Function(int); // fn new_custom_private_route(port: i64, stability: FfiStr, sequencing: FfiStr) -typedef _NewCustomPrivateRouteC = Void Function( - Int64, Pointer, Pointer); typedef _NewCustomPrivateRouteDart = void Function( int, Pointer, Pointer); // fn import_remote_private_route(port: i64, blob: FfiStr) -typedef _ImportRemotePrivateRouteC = Void Function(Int64, Pointer); typedef _ImportRemotePrivateRouteDart = void Function(int, Pointer); // fn release_private_route(port:i64, key: FfiStr) -typedef _ReleasePrivateRouteC = Void Function(Int64, Pointer); typedef _ReleasePrivateRouteDart = void Function(int, Pointer); // fn app_call_reply(port: i64, id: FfiStr, message: FfiStr) -typedef _AppCallReplyC = Void Function(Int64, Pointer, Pointer); typedef _AppCallReplyDart = void Function(int, Pointer, Pointer); // fn open_table_db(port: i64, name: FfiStr, column_count: u32) -typedef _OpenTableDbC = Void Function(Int64, Pointer, Uint32); typedef _OpenTableDbDart = void Function(int, Pointer, int); // fn release_table_db(id: u32) -> i32 -typedef _ReleaseTableDbC = Int32 Function(Uint32); typedef _ReleaseTableDbDart = int Function(int); // fn delete_table_db(port: i64, name: FfiStr) -typedef _DeleteTableDbC = Void Function(Int64, Pointer); typedef _DeleteTableDbDart = void Function(int, Pointer); // fn table_db_get_column_count(id: u32) -> u32 -typedef _TableDbGetColumnCountC = Uint32 Function(Uint32); typedef _TableDbGetColumnCountDart = int Function(int); // fn table_db_get_keys(port: i64, id: u32, col: u32) -typedef _TableDbGetKeysC = Pointer Function(Uint64, Uint32, Uint32); typedef _TableDbGetKeysDart = Pointer Function(int, int, int); // fn table_db_store(port: i64, id: u32, col: u32, key: FfiStr, value: FfiStr) -typedef _TableDbStoreC = Void Function( - Int64, Uint32, Uint32, Pointer, Pointer); typedef _TableDbStoreDart = void Function( int, int, int, Pointer, Pointer); // fn table_db_load(port: i64, id: u32, col: u32, key: FfiStr) -typedef _TableDbLoadC = Void Function(Int64, Uint32, Uint32, Pointer); typedef _TableDbLoadDart = void Function(int, int, int, Pointer); // fn table_db_delete(port: i64, id: u32, col: u32, key: FfiStr) -typedef _TableDbDeleteC = Void Function(Int64, Uint32, Uint32, Pointer); typedef _TableDbDeleteDart = void Function(int, int, int, Pointer); // fn table_db_transact(id: u32) -> u32 -typedef _TableDbTransactC = Uint32 Function(Uint32); typedef _TableDbTransactDart = int Function(int); // fn release_table_db_transaction(id: u32) -> i32 -typedef _ReleaseTableDbTransactionC = Int32 Function(Uint32); typedef _ReleaseTableDbTransactionDart = int Function(int); // fn table_db_transaction_commit(port: i64, id: u32) -typedef _TableDbTransactionCommitC = Void Function(Uint64, Uint32); typedef _TableDbTransactionCommitDart = void Function(int, int); // fn table_db_transaction_rollback(port: i64, id: u32) -typedef _TableDbTransactionRollbackC = Void Function(Uint64, Uint32); typedef _TableDbTransactionRollbackDart = void Function(int, int); // fn table_db_transaction_store(port: i64, id: u32, col: u32, key: FfiStr, value: FfiStr) -typedef _TableDbTransactionStoreC = Void Function( - Int64, Uint32, Uint32, Pointer, Pointer); typedef _TableDbTransactionStoreDart = void Function( int, int, int, Pointer, Pointer); // fn table_db_transaction_delete(port: i64, id: u32, col: u32, key: FfiStr) -typedef _TableDbTransactionDeleteC = Void Function( - Int64, Uint32, Uint32, Pointer); typedef _TableDbTransactionDeleteDart = void Function( int, int, int, Pointer); // fn valid_crypto_kinds() -> *mut c_char -typedef _ValidCryptoKindsC = Pointer Function(); typedef _ValidCryptoKindsDart = Pointer Function(); // fn best_crypto_kind() -> u32 -typedef _BestCryptoKindC = Uint32 Function(); typedef _BestCryptoKindDart = int Function(); // fn verify_signatures(port: i64, node_ids: FfiStr, data: FfiStr, signatures: FfiStr) -typedef _VerifySignaturesC = Void Function( - Int64, Pointer, Pointer, Pointer); typedef _VerifySignaturesDart = void Function( int, Pointer, Pointer, Pointer); // fn generate_signatures(port: i64, data: FfiStr, key_pairs: FfiStr) -typedef _GenerateSignaturesC = Void Function( - Int64, Pointer, Pointer); typedef _GenerateSignaturesDart = void Function( int, Pointer, Pointer); // fn generate_key_pair(port: i64, kind: u32) { -typedef _GenerateKeyPairC = Void Function(Int64, Uint32); typedef _GenerateKeyPairDart = void Function(int, int); // fn crypto_cached_dh(port: i64, kind: u32, key: FfiStr, secret: FfiStr) -typedef _CryptoCachedDHC = Void Function( - Int64, Uint32, Pointer, Pointer); typedef _CryptoCachedDHDart = void Function( int, int, Pointer, Pointer); // fn crypto_compute_dh(port: i64, kind: u32, key: FfiStr, secret: FfiStr) -typedef _CryptoComputeDHC = Void Function( - Int64, Uint32, Pointer, Pointer); typedef _CryptoComputeDHDart = void Function( int, int, Pointer, Pointer); // fn crypto_random_bytes(port: i64, kind: u32, len: u32) -typedef _CryptoRandomBytesC = Void Function(Int64, Uint32, Uint32); typedef _CryptoRandomBytesDart = void Function(int, int, int); // fn crypto_default_salt_length(port: i64, kind: u32) -typedef _CryptoDefaultSaltLengthC = Void Function(Int64, Uint32); typedef _CryptoDefaultSaltLengthDart = void Function(int, int); // fn crypto_hash_password(port: i64, kind: u32, password: FfiStr, salt: FfiStr ) -typedef _CryptoHashPasswordC = Void Function( - Int64, Uint32, Pointer, Pointer); typedef _CryptoHashPasswordDart = void Function( int, int, Pointer, Pointer); // fn crypto_verify_password(port: i64, kind: u32, password: FfiStr, password_hash: FfiStr ) -typedef _CryptoVerifyPasswordC = Void Function( - Int64, Uint32, Pointer, Pointer); typedef _CryptoVerifyPasswordDart = void Function( int, int, Pointer, Pointer); // fn crypto_derive_shared_secret(port: i64, kind: u32, password: FfiStr, salt: FfiStr ) -typedef _CryptoDeriveSharedSecretC = Void Function( - Int64, Uint32, Pointer, Pointer); -typedef _CryptoDeriveSharedSecretDart = void Function( - int, int, Pointer, Pointer); // fn crypto_random_nonce(port: i64, kind: u32) -typedef _CryptoRandomNonceC = Void Function(Int64, Uint32); typedef _CryptoRandomNonceDart = void Function(int, int); // fn crypto_random_shared_secret(port: i64, kind: u32) -typedef _CryptoRandomSharedSecretC = Void Function(Int64, Uint32); typedef _CryptoRandomSharedSecretDart = void Function(int, int); // fn crypto_generate_key_pair(port: i64, kind: u32) -typedef _CryptoGenerateKeyPairC = Void Function(Int64, Uint32); typedef _CryptoGenerateKeyPairDart = void Function(int, int); // fn crypto_generate_hash(port: i64, kind: u32, data: FfiStr) -typedef _CryptoGenerateHashC = Void Function(Int64, Uint32, Pointer); typedef _CryptoGenerateHashDart = void Function(int, int, Pointer); // fn crypto_validate_key_pair(port: i64, kind: u32, key: FfiStr, secret: FfiStr) -typedef _CryptoValidateKeyPairC = Void Function( - Int64, Uint32, Pointer, Pointer); typedef _CryptoValidateKeyPairDart = void Function( int, int, Pointer, Pointer); // fn crypto_validate_hash(port: i64, kind: u32, data: FfiStr, hash: FfiStr) -typedef _CryptoValidateHashC = Void Function( - Int64, Uint32, Pointer, Pointer); typedef _CryptoValidateHashDart = void Function( int, int, Pointer, Pointer); // fn crypto_distance(port: i64, kind: u32, key1: FfiStr, key2: FfiStr) -typedef _CryptoDistanceC = Void Function( - Int64, Uint32, Pointer, Pointer); typedef _CryptoDistanceDart = void Function( int, int, Pointer, Pointer); // fn crypto_sign(port: i64, kind: u32, key: FfiStr, secret: FfiStr, data: FfiStr) -typedef _CryptoSignC = Void Function( - Int64, Uint32, Pointer, Pointer, Pointer); typedef _CryptoSignDart = void Function( int, int, Pointer, Pointer, Pointer); // fn crypto_verify(port: i64, kind: u32, key: FfiStr, data: FfiStr, signature: FfiStr) -typedef _CryptoVerifyC = Void Function( - Int64, Uint32, Pointer, Pointer, Pointer); typedef _CryptoVerifyDart = void Function( int, int, Pointer, Pointer, Pointer); // fn crypto_aead_overhead(port: i64, kind: u32) -typedef _CryptoAeadOverheadC = Void Function(Int64, Uint32); typedef _CryptoAeadOverheadDart = void Function(int, int); // fn crypto_decrypt_aead(port: i64, kind: u32, body: FfiStr, nonce: FfiStr, shared_secret: FfiStr, associated_data: FfiStr) -typedef _CryptoDecryptAeadC = Void Function( - Int64, Uint32, Pointer, Pointer, Pointer, Pointer); typedef _CryptoDecryptAeadDart = void Function( int, int, Pointer, Pointer, Pointer, Pointer); // fn crypto_encrypt_aead(port: i64, kind: u32, body: FfiStr, nonce: FfiStr, shared_secret: FfiStr, associated_data: FfiStr) -typedef _CryptoEncryptAeadC = Void Function( - Int64, Uint32, Pointer, Pointer, Pointer, Pointer); typedef _CryptoEncryptAeadDart = void Function( int, int, Pointer, Pointer, Pointer, Pointer); // fn crypto_crypt_no_auth(port: i64, kind: u32, body: FfiStr, nonce: FfiStr, shared_secret: FfiStr) -typedef _CryptoCryptNoAuthC = Void Function( - Int64, Uint32, Pointer, Pointer, Pointer); typedef _CryptoCryptNoAuthDart = void Function( int, int, Pointer, Pointer, Pointer); // fn now() -> u64 -typedef _NowC = Uint64 Function(); typedef _NowDart = int Function(); // fn debug(port: i64, log_level: FfiStr) -typedef _DebugC = Void Function(Int64, Pointer); typedef _DebugDart = void Function(int, Pointer); // fn shutdown_veilid_core(port: i64) -typedef _ShutdownVeilidCoreC = Void Function(Int64); typedef _ShutdownVeilidCoreDart = void Function(int); // fn veilid_version_string() -> *mut c_char -typedef _VeilidVersionStringC = Pointer Function(); typedef _VeilidVersionStringDart = Pointer Function(); // fn veilid_version() -> VeilidVersion @@ -314,7 +209,6 @@ final class VeilidVersionFFI extends Struct { external int patch; } -typedef _VeilidVersionC = VeilidVersionFFI Function(); typedef _VeilidVersionDart = VeilidVersionFFI Function(); // Async message types @@ -332,21 +226,20 @@ const int messageStreamClose = 8; Veilid getVeilid() => VeilidFFI(_dylib); // Parse handle async returns -Future processFuturePlain(Future future) { - return future.then((value) { +Future processFuturePlain(Future future) async => future.then((value) { final list = value as List; switch (list[0] as int) { case messageOk: { if (list[1] == null && null is! T) { throw const VeilidAPIExceptionInternal( - "Null MESSAGE_OK value on non-nullable type"); + 'Null MESSAGE_OK value on non-nullable type'); } return list[1] as T; } case messageErr: { - throw VeilidAPIExceptionInternal("Internal API Error: ${list[1]}"); + throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); } case messageErrJson: { @@ -355,7 +248,7 @@ Future processFuturePlain(Future future) { default: { throw VeilidAPIExceptionInternal( - "Unexpected async return message type: ${list[0]}"); + 'Unexpected async return message type: ${list[0]}'); } } }).catchError((e) { @@ -365,27 +258,25 @@ Future processFuturePlain(Future future) { // Pass errors that are already VeilidAPIException through without wrapping return e is! VeilidAPIException; }); -} Future processFutureJson( - T Function(dynamic) jsonConstructor, Future future) { - return future.then((value) { + T Function(dynamic) jsonConstructor, Future future) async => future.then((value) { final list = value as List; switch (list[0] as int) { case messageErr: { - throw VeilidAPIExceptionInternal("Internal API Error: ${list[1]}"); + throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); } case messageOkJson: { if (list[1] is! String) { throw const VeilidAPIExceptionInternal( - "Non-string MESSAGE_OK_JSON value"); + 'Non-string MESSAGE_OK_JSON value'); } - var ret = jsonDecode(list[1] as String); + final ret = jsonDecode(list[1] as String); if (ret == null) { throw const VeilidAPIExceptionInternal( - "Null JSON object on non nullable type"); + 'Null JSON object on non nullable type'); } return jsonConstructor(ret); } @@ -396,7 +287,7 @@ Future processFutureJson( default: { throw VeilidAPIExceptionInternal( - "Unexpected async return message type: ${list[0]}"); + 'Unexpected async return message type: ${list[0]}'); } } }).catchError((e) { @@ -406,16 +297,14 @@ Future processFutureJson( // Pass errors that are already VeilidAPIException through without wrapping return e is! VeilidAPIException; }); -} Future processFutureOptJson( - T Function(dynamic) jsonConstructor, Future future) { - return future.then((value) { + T Function(dynamic) jsonConstructor, Future future) async => future.then((value) { final list = value as List; switch (list[0] as int) { case messageErr: { - throw VeilidAPIExceptionInternal("Internal API Error: ${list[1]}"); + throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); } case messageOkJson: { @@ -424,9 +313,9 @@ Future processFutureOptJson( } if (list[1] is! String) { throw const VeilidAPIExceptionInternal( - "Non-string MESSAGE_OK_JSON optional value"); + 'Non-string MESSAGE_OK_JSON optional value'); } - var ret = jsonDecode(list[1] as String); + final ret = jsonDecode(list[1] as String); if (ret == null) { return null; } @@ -439,7 +328,7 @@ Future processFutureOptJson( default: { throw VeilidAPIExceptionInternal( - "Unexpected async return message type: ${list[0]}"); + 'Unexpected async return message type: ${list[0]}'); } } }).catchError((e) { @@ -449,10 +338,8 @@ Future processFutureOptJson( // Pass errors that are already VeilidAPIException through without wrapping return e is! VeilidAPIException; }); -} -Future processFutureVoid(Future future) { - return future.then((value) { +Future processFutureVoid(Future future) async => future.then((value) { final list = value as List; switch (list[0] as int) { case messageOk: @@ -465,11 +352,11 @@ Future processFutureVoid(Future future) { } case messageErr: { - throw VeilidAPIExceptionInternal("Internal API Error: ${list[1]}"); + throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); } case messageOkJson: { - var ret = jsonDecode(list[1] as String); + final ret = jsonDecode(list[1] as String); if (ret != null) { throw VeilidAPIExceptionInternal( "Unexpected MESSAGE_OK_JSON value '$ret' where null expected"); @@ -483,7 +370,7 @@ Future processFutureVoid(Future future) { default: { throw VeilidAPIExceptionInternal( - "Unexpected async return message type: ${list[0]}"); + 'Unexpected async return message type: ${list[0]}'); } } }).catchError((e) { @@ -493,11 +380,9 @@ Future processFutureVoid(Future future) { // Pass errors that are already VeilidAPIException through without wrapping return e is! VeilidAPIException; }); -} Future> processFutureStream( - Stream returnStream, Future future) { - return future.then((value) { + Stream returnStream, Future future) async => future.then((value) { final list = value as List; switch (list[0] as int) { case messageOk: @@ -510,11 +395,11 @@ Future> processFutureStream( } case messageErr: { - throw VeilidAPIExceptionInternal("Internal API Error: ${list[1]}"); + throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); } case messageOkJson: { - var ret = jsonDecode(list[1] as String); + final ret = jsonDecode(list[1] as String); if (ret != null) { throw VeilidAPIExceptionInternal( "Unexpected MESSAGE_OK_JSON value '$ret' where null expected"); @@ -528,7 +413,7 @@ Future> processFutureStream( default: { throw VeilidAPIExceptionInternal( - "Unexpected async return message type: ${list[0]}"); + 'Unexpected async return message type: ${list[0]}'); } } }).catchError((e) { @@ -538,28 +423,27 @@ Future> processFutureStream( // Pass errors that are already VeilidAPIException through without wrapping return e is! VeilidAPIException; }); -} Stream processStreamJson( T Function(dynamic) jsonConstructor, ReceivePort port) async* { try { - await for (var value in port) { + await for (final value in port) { final list = value as List; switch (list[0] as int) { case messageStreamItemJson: { if (list[1] == null) { throw const VeilidAPIExceptionInternal( - "Null MESSAGE_STREAM_ITEM_JSON value"); + 'Null MESSAGE_STREAM_ITEM_JSON value'); } - var ret = jsonDecode(list[1] as String); + final ret = jsonDecode(list[1] as String); yield jsonConstructor(ret); break; } case messageStreamAbort: { port.close(); - throw VeilidAPIExceptionInternal("Internal API Error: ${list[1]}"); + throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); } case messageStreamAbortJson: { @@ -574,21 +458,21 @@ Stream processStreamJson( default: { throw VeilidAPIExceptionInternal( - "Unexpected async return message type: ${list[0]}"); + 'Unexpected async return message type: ${list[0]}'); } } } } catch (e, s) { // Wrap all other errors in VeilidAPIExceptionInternal throw VeilidAPIExceptionInternal( - "${e.toString()}\nStack Trace:\n${s.toString()}"); + '$e\nStack Trace:\n$s'); } } class _Ctx { + _Ctx(int this.id, this.ffi); int? id; final VeilidFFI ffi; - _Ctx(int this.id, this.ffi); void ensureValid() { if (id == null) { @@ -606,12 +490,12 @@ class _Ctx { // FFI implementation of VeilidRoutingContext class VeilidRoutingContextFFI extends VeilidRoutingContext { - final _Ctx _ctx; - static final Finalizer<_Ctx> _finalizer = Finalizer((ctx) => ctx.close()); VeilidRoutingContextFFI._(this._ctx) { _finalizer.attach(this, _ctx, detach: this); } + final _Ctx _ctx; + static final Finalizer<_Ctx> _finalizer = Finalizer((ctx) => ctx.close()); @override void close() { @@ -644,10 +528,10 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { @override Future appCall(String target, Uint8List request) async { _ctx.ensureValid(); - var nativeEncodedTarget = target.toNativeUtf8(); - var nativeEncodedRequest = base64UrlNoPadEncode(request).toNativeUtf8(); + final nativeEncodedTarget = target.toNativeUtf8(); + final nativeEncodedRequest = base64UrlNoPadEncode(request).toNativeUtf8(); - final recvPort = ReceivePort("routing_context_app_call"); + final recvPort = ReceivePort('routing_context_app_call'); final sendPort = recvPort.sendPort; _ctx.ffi._routingContextAppCall(sendPort.nativePort, _ctx.id!, nativeEncodedTarget, nativeEncodedRequest); @@ -656,16 +540,16 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { } @override - Future appMessage(String target, Uint8List message) { + Future appMessage(String target, Uint8List message) async { _ctx.ensureValid(); final nativeEncodedTarget = target.toNativeUtf8(); final nativeEncodedMessage = base64UrlNoPadEncode(message).toNativeUtf8(); - final recvPort = ReceivePort("routing_context_app_message"); + final recvPort = ReceivePort('routing_context_app_message'); final sendPort = recvPort.sendPort; _ctx.ffi._routingContextAppMessage(sendPort.nativePort, _ctx.id!, nativeEncodedTarget, nativeEncodedMessage); - return processFutureVoid(recvPort.first); + return await processFutureVoid(recvPort.first); } @override @@ -673,7 +557,7 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { {CryptoKind kind = 0}) async { _ctx.ensureValid(); final nativeSchema = jsonEncode(schema).toNativeUtf8(); - final recvPort = ReceivePort("routing_context_create_dht_record"); + final recvPort = ReceivePort('routing_context_create_dht_record'); final sendPort = recvPort.sendPort; _ctx.ffi._routingContextCreateDHTRecord( sendPort.nativePort, _ctx.id!, nativeSchema, kind); @@ -689,7 +573,7 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { final nativeKey = jsonEncode(key).toNativeUtf8(); final nativeWriter = writer != null ? jsonEncode(writer).toNativeUtf8() : nullptr; - final recvPort = ReceivePort("routing_context_open_dht_record"); + final recvPort = ReceivePort('routing_context_open_dht_record'); final sendPort = recvPort.sendPort; _ctx.ffi._routingContextOpenDHTRecord( sendPort.nativePort, _ctx.id!, nativeKey, nativeWriter); @@ -699,25 +583,25 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { } @override - Future closeDHTRecord(TypedKey key) { + Future closeDHTRecord(TypedKey key) async { _ctx.ensureValid(); final nativeKey = jsonEncode(key).toNativeUtf8(); - final recvPort = ReceivePort("routing_context_close_dht_record"); + final recvPort = ReceivePort('routing_context_close_dht_record'); final sendPort = recvPort.sendPort; _ctx.ffi._routingContextCloseDHTRecord( sendPort.nativePort, _ctx.id!, nativeKey); - return processFutureVoid(recvPort.first); + return await processFutureVoid(recvPort.first); } @override - Future deleteDHTRecord(TypedKey key) { + Future deleteDHTRecord(TypedKey key) async { _ctx.ensureValid(); final nativeKey = jsonEncode(key).toNativeUtf8(); - final recvPort = ReceivePort("routing_context_delete_dht_record"); + final recvPort = ReceivePort('routing_context_delete_dht_record'); final sendPort = recvPort.sendPort; _ctx.ffi._routingContextDeleteDHTRecord( sendPort.nativePort, _ctx.id!, nativeKey); - return processFutureVoid(recvPort.first); + return await processFutureVoid(recvPort.first); } @override @@ -725,7 +609,7 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { TypedKey key, int subkey, bool forceRefresh) async { _ctx.ensureValid(); final nativeKey = jsonEncode(key).toNativeUtf8(); - final recvPort = ReceivePort("routing_context_get_dht_value"); + final recvPort = ReceivePort('routing_context_get_dht_value'); final sendPort = recvPort.sendPort; _ctx.ffi._routingContextGetDHTValue( sendPort.nativePort, _ctx.id!, nativeKey, subkey, forceRefresh); @@ -741,7 +625,7 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { final nativeKey = jsonEncode(key).toNativeUtf8(); final nativeData = base64UrlNoPadEncode(data).toNativeUtf8(); - final recvPort = ReceivePort("routing_context_set_dht_value"); + final recvPort = ReceivePort('routing_context_set_dht_value'); final sendPort = recvPort.sendPort; _ctx.ffi._routingContextSetDHTValue( sendPort.nativePort, _ctx.id!, nativeKey, subkey, nativeData); @@ -758,7 +642,7 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { final nativeSubkeys = jsonEncode(subkeys).toNativeUtf8(); final nativeExpiration = expiration.value.toInt(); - final recvPort = ReceivePort("routing_context_watch_dht_values"); + final recvPort = ReceivePort('routing_context_watch_dht_values'); final sendPort = recvPort.sendPort; _ctx.ffi._routingContextWatchDHTValues(sendPort.nativePort, _ctx.id!, nativeKey, nativeSubkeys, nativeExpiration, count); @@ -774,7 +658,7 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { final nativeKey = jsonEncode(key).toNativeUtf8(); final nativeSubkeys = jsonEncode(subkeys).toNativeUtf8(); - final recvPort = ReceivePort("routing_context_cancel_dht_watch"); + final recvPort = ReceivePort('routing_context_cancel_dht_watch'); final sendPort = recvPort.sendPort; _ctx.ffi._routingContextCancelDHTWatch( sendPort.nativePort, _ctx.id!, nativeKey, nativeSubkeys); @@ -784,11 +668,11 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { } class _TDBT { + + _TDBT(int this.id, this.tdbffi, this.ffi); int? id; final VeilidTableDBFFI tdbffi; final VeilidFFI ffi; - - _TDBT(int this.id, this.tdbffi, this.ffi); void ensureValid() { if (id == null) { throw VeilidAPIExceptionNotInitialized(); @@ -805,22 +689,20 @@ class _TDBT { // FFI implementation of VeilidTableDBTransaction class VeilidTableDBTransactionFFI extends VeilidTableDBTransaction { - final _TDBT _tdbt; - static final Finalizer<_TDBT> _finalizer = Finalizer((tdbt) => tdbt.close()); VeilidTableDBTransactionFFI._(this._tdbt) { _finalizer.attach(this, _tdbt, detach: this); } + final _TDBT _tdbt; + static final Finalizer<_TDBT> _finalizer = Finalizer((tdbt) => tdbt.close()); @override - bool isDone() { - return _tdbt.id == null; - } + bool isDone() => _tdbt.id == null; @override Future commit() async { _tdbt.ensureValid(); - final recvPort = ReceivePort("veilid_table_db_transaction_commit"); + final recvPort = ReceivePort('veilid_table_db_transaction_commit'); final sendPort = recvPort.sendPort; _tdbt.ffi._tableDbTransactionCommit( sendPort.nativePort, @@ -833,7 +715,7 @@ class VeilidTableDBTransactionFFI extends VeilidTableDBTransaction { @override Future rollback() async { _tdbt.ensureValid(); - final recvPort = ReceivePort("veilid_table_db_transaction_rollback"); + final recvPort = ReceivePort('veilid_table_db_transaction_rollback'); final sendPort = recvPort.sendPort; _tdbt.ffi._tableDbTransactionRollback( sendPort.nativePort, @@ -844,12 +726,12 @@ class VeilidTableDBTransactionFFI extends VeilidTableDBTransaction { } @override - Future store(int col, Uint8List key, Uint8List value) { + Future store(int col, Uint8List key, Uint8List value) async { _tdbt.ensureValid(); final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); final nativeEncodedValue = base64UrlNoPadEncode(value).toNativeUtf8(); - final recvPort = ReceivePort("veilid_table_db_transaction_store"); + final recvPort = ReceivePort('veilid_table_db_transaction_store'); final sendPort = recvPort.sendPort; _tdbt.ffi._tableDbTransactionStore( sendPort.nativePort, @@ -858,15 +740,15 @@ class VeilidTableDBTransactionFFI extends VeilidTableDBTransaction { nativeEncodedKey, nativeEncodedValue, ); - return processFutureVoid(recvPort.first); + return await processFutureVoid(recvPort.first); } @override - Future delete(int col, Uint8List key) { + Future delete(int col, Uint8List key) async { _tdbt.ensureValid(); final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); - final recvPort = ReceivePort("veilid_table_db_transaction_delete"); + final recvPort = ReceivePort('veilid_table_db_transaction_delete'); final sendPort = recvPort.sendPort; _tdbt.ffi._tableDbTransactionDelete( sendPort.nativePort, @@ -874,14 +756,14 @@ class VeilidTableDBTransactionFFI extends VeilidTableDBTransaction { col, nativeEncodedKey, ); - return processFuturePlain(recvPort.first); + return await processFuturePlain(recvPort.first); } } class _TDB { + _TDB(int this.id, this.ffi); int? id; final VeilidFFI ffi; - _TDB(int this.id, this.ffi); void ensureValid() { if (id == null) { throw VeilidAPIExceptionNotInitialized(); @@ -898,12 +780,12 @@ class _TDB { // FFI implementation of VeilidTableDB class VeilidTableDBFFI extends VeilidTableDB { - final _TDB _tdb; - static final Finalizer<_TDB> _finalizer = Finalizer((tdb) => tdb.close()); VeilidTableDBFFI._(this._tdb) { _finalizer.attach(this, _tdb, detach: this); } + final _TDB _tdb; + static final Finalizer<_TDB> _finalizer = Finalizer((tdb) => tdb.close()); @override void close() { @@ -917,15 +799,15 @@ class VeilidTableDBFFI extends VeilidTableDB { } @override - Future> getKeys(int col) { + Future> getKeys(int col) async { _tdb.ensureValid(); - final recvPort = ReceivePort("veilid_table_db_get_keys"); + final recvPort = ReceivePort('veilid_table_db_get_keys'); final sendPort = recvPort.sendPort; _tdb.ffi._tableDbGetKeys(sendPort.nativePort, _tdb.id!, col); - return processFutureJson( + return await processFutureJson( jsonListConstructor(base64UrlNoPadDecodeDynamic), recvPort.first); } @@ -939,13 +821,13 @@ class VeilidTableDBFFI extends VeilidTableDB { } @override - Future store(int col, Uint8List key, Uint8List value) { + Future store(int col, Uint8List key, Uint8List value) async { _tdb.ensureValid(); final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); final nativeEncodedValue = base64UrlNoPadEncode(value).toNativeUtf8(); - final recvPort = ReceivePort("veilid_table_db_store"); + final recvPort = ReceivePort('veilid_table_db_store'); final sendPort = recvPort.sendPort; _tdb.ffi._tableDbStore( sendPort.nativePort, @@ -954,7 +836,7 @@ class VeilidTableDBFFI extends VeilidTableDB { nativeEncodedKey, nativeEncodedValue, ); - return processFutureVoid(recvPort.first); + return await processFutureVoid(recvPort.first); } @override @@ -962,7 +844,7 @@ class VeilidTableDBFFI extends VeilidTableDB { _tdb.ensureValid(); final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); - final recvPort = ReceivePort("veilid_table_db_load"); + final recvPort = ReceivePort('veilid_table_db_load'); final sendPort = recvPort.sendPort; _tdb.ffi._tableDbLoad( sendPort.nativePort, @@ -970,7 +852,7 @@ class VeilidTableDBFFI extends VeilidTableDB { col, nativeEncodedKey, ); - String? out = await processFuturePlain(recvPort.first); + final out = await processFuturePlain(recvPort.first); if (out == null) { return null; } @@ -982,7 +864,7 @@ class VeilidTableDBFFI extends VeilidTableDB { _tdb.ensureValid(); final nativeEncodedKey = base64UrlNoPadEncode(key).toNativeUtf8(); - final recvPort = ReceivePort("veilid_table_db_delete"); + final recvPort = ReceivePort('veilid_table_db_delete'); final sendPort = recvPort.sendPort; _tdb.ffi._tableDbDelete( sendPort.nativePort, @@ -990,7 +872,7 @@ class VeilidTableDBFFI extends VeilidTableDB { col, nativeEncodedKey, ); - String? out = await processFuturePlain(recvPort.first); + final out = await processFuturePlain(recvPort.first); if (out == null) { return null; } @@ -1000,41 +882,39 @@ class VeilidTableDBFFI extends VeilidTableDB { // FFI implementation of VeilidCryptoSystem class VeilidCryptoSystemFFI extends VeilidCryptoSystem { + + VeilidCryptoSystemFFI._(this._ffi, this._kind); final CryptoKind _kind; final VeilidFFI _ffi; - VeilidCryptoSystemFFI._(this._ffi, this._kind); + @override + CryptoKind kind() => _kind; @override - CryptoKind kind() { - return _kind; - } - - @override - Future cachedDH(PublicKey key, SecretKey secret) { + Future cachedDH(PublicKey key, SecretKey secret) async { final nativeKey = jsonEncode(key).toNativeUtf8(); final nativeSecret = jsonEncode(secret).toNativeUtf8(); - final recvPort = ReceivePort("crypto_cached_dh"); + final recvPort = ReceivePort('crypto_cached_dh'); final sendPort = recvPort.sendPort; _ffi._cryptoCachedDH(sendPort.nativePort, _kind, nativeKey, nativeSecret); - return processFutureJson(SharedSecret.fromJson, recvPort.first); + return await processFutureJson(SharedSecret.fromJson, recvPort.first); } @override - Future computeDH(PublicKey key, SecretKey secret) { + Future computeDH(PublicKey key, SecretKey secret) async { final nativeKey = jsonEncode(key).toNativeUtf8(); final nativeSecret = jsonEncode(secret).toNativeUtf8(); - final recvPort = ReceivePort("crypto_compute_dh"); + final recvPort = ReceivePort('crypto_compute_dh'); final sendPort = recvPort.sendPort; _ffi._cryptoComputeDH(sendPort.nativePort, _kind, nativeKey, nativeSecret); - return processFutureJson(SharedSecret.fromJson, recvPort.first); + return await processFutureJson(SharedSecret.fromJson, recvPort.first); } @override Future randomBytes(int len) async { - final recvPort = ReceivePort("crypto_random_bytes"); + final recvPort = ReceivePort('crypto_random_bytes'); final sendPort = recvPort.sendPort; _ffi._cryptoRandomBytes(sendPort.nativePort, _kind, len); final out = await processFuturePlain(recvPort.first); @@ -1042,153 +922,156 @@ class VeilidCryptoSystemFFI extends VeilidCryptoSystem { } @override - Future defaultSaltLength() { - final recvPort = ReceivePort("crypto_default_salt_length"); + Future defaultSaltLength() async { + final recvPort = ReceivePort('crypto_default_salt_length'); final sendPort = recvPort.sendPort; _ffi._cryptoDefaultSaltLength(sendPort.nativePort, _kind); - return processFuturePlain(recvPort.first); + return await processFuturePlain(recvPort.first); } @override - Future hashPassword(Uint8List password, Uint8List salt) { + Future hashPassword(Uint8List password, Uint8List salt) async { final nativeEncodedPassword = base64UrlNoPadEncode(password).toNativeUtf8(); final nativeEncodedSalt = base64UrlNoPadEncode(salt).toNativeUtf8(); - final recvPort = ReceivePort("crypto_hash_password"); + final recvPort = ReceivePort('crypto_hash_password'); final sendPort = recvPort.sendPort; _ffi._cryptoHashPassword( sendPort.nativePort, _kind, nativeEncodedPassword, nativeEncodedSalt); - return processFuturePlain(recvPort.first); + return await processFuturePlain(recvPort.first); } @override - Future verifyPassword(Uint8List password, String passwordHash) { + Future verifyPassword(Uint8List password, String passwordHash) async { final nativeEncodedPassword = base64UrlNoPadEncode(password).toNativeUtf8(); final nativeEncodedPasswordHash = passwordHash.toNativeUtf8(); - final recvPort = ReceivePort("crypto_verify_password"); + final recvPort = ReceivePort('crypto_verify_password'); final sendPort = recvPort.sendPort; _ffi._cryptoVerifyPassword(sendPort.nativePort, _kind, nativeEncodedPassword, nativeEncodedPasswordHash); - return processFuturePlain(recvPort.first); + return await processFuturePlain(recvPort.first); } @override - Future deriveSharedSecret(Uint8List password, Uint8List salt) { + Future deriveSharedSecret( + Uint8List password, Uint8List salt) async { final nativeEncodedPassword = base64UrlNoPadEncode(password).toNativeUtf8(); final nativeEncodedSalt = base64UrlNoPadEncode(salt).toNativeUtf8(); - final recvPort = ReceivePort("crypto_derive_shared_secret"); + final recvPort = ReceivePort('crypto_derive_shared_secret'); final sendPort = recvPort.sendPort; _ffi._cryptoDeriveSharedSecret( sendPort.nativePort, _kind, nativeEncodedPassword, nativeEncodedSalt); - return processFutureJson(SharedSecret.fromJson, recvPort.first); + return await processFutureJson(SharedSecret.fromJson, recvPort.first); } @override - Future randomNonce() { - final recvPort = ReceivePort("crypto_random_nonce"); + Future randomNonce() async { + final recvPort = ReceivePort('crypto_random_nonce'); final sendPort = recvPort.sendPort; _ffi._cryptoRandomNonce(sendPort.nativePort, _kind); - return processFutureJson(Nonce.fromJson, recvPort.first); + return await processFutureJson(Nonce.fromJson, recvPort.first); } @override - Future randomSharedSecret() { - final recvPort = ReceivePort("crypto_random_shared_secret"); + Future randomSharedSecret() async { + final recvPort = ReceivePort('crypto_random_shared_secret'); final sendPort = recvPort.sendPort; _ffi._cryptoRandomSharedSecret(sendPort.nativePort, _kind); - return processFutureJson(SharedSecret.fromJson, recvPort.first); + return await processFutureJson(SharedSecret.fromJson, recvPort.first); } @override - Future generateKeyPair() { - final recvPort = ReceivePort("crypto_generate_key_pair"); + Future generateKeyPair() async { + final recvPort = ReceivePort('crypto_generate_key_pair'); final sendPort = recvPort.sendPort; _ffi._cryptoGenerateKeyPair(sendPort.nativePort, _kind); - return processFutureJson(KeyPair.fromJson, recvPort.first); + return await processFutureJson(KeyPair.fromJson, recvPort.first); } @override - Future generateHash(Uint8List data) { + Future generateHash(Uint8List data) async { final nativeEncodedData = base64UrlNoPadEncode(data).toNativeUtf8(); - final recvPort = ReceivePort("crypto_generate_hash"); + final recvPort = ReceivePort('crypto_generate_hash'); final sendPort = recvPort.sendPort; _ffi._cryptoGenerateHash(sendPort.nativePort, _kind, nativeEncodedData); - return processFutureJson(HashDigest.fromJson, recvPort.first); + return await processFutureJson(HashDigest.fromJson, recvPort.first); } @override - Future validateKeyPair(PublicKey key, SecretKey secret) { + Future validateKeyPair(PublicKey key, SecretKey secret) async { final nativeKey = jsonEncode(key).toNativeUtf8(); final nativeSecret = jsonEncode(secret).toNativeUtf8(); - final recvPort = ReceivePort("crypto_validate_key_pair"); + final recvPort = ReceivePort('crypto_validate_key_pair'); final sendPort = recvPort.sendPort; _ffi._cryptoValidateKeyPair( sendPort.nativePort, _kind, nativeKey, nativeSecret); - return processFuturePlain(recvPort.first); + return await processFuturePlain(recvPort.first); } @override - Future validateHash(Uint8List data, HashDigest hash) { + Future validateHash(Uint8List data, HashDigest hash) async { final nativeEncodedData = base64UrlNoPadEncode(data).toNativeUtf8(); final nativeHash = jsonEncode(hash).toNativeUtf8(); - final recvPort = ReceivePort("crypto_validate_hash"); + final recvPort = ReceivePort('crypto_validate_hash'); final sendPort = recvPort.sendPort; _ffi._cryptoValidateHash( sendPort.nativePort, _kind, nativeEncodedData, nativeHash); - return processFuturePlain(recvPort.first); + return await processFuturePlain(recvPort.first); } @override - Future distance(CryptoKey key1, CryptoKey key2) { + Future distance(CryptoKey key1, CryptoKey key2) async { final nativeKey1 = jsonEncode(key1).toNativeUtf8(); final nativeKey2 = jsonEncode(key2).toNativeUtf8(); - final recvPort = ReceivePort("crypto_distance"); + final recvPort = ReceivePort('crypto_distance'); final sendPort = recvPort.sendPort; _ffi._cryptoDistance(sendPort.nativePort, _kind, nativeKey1, nativeKey2); - return processFutureJson(CryptoKeyDistance.fromJson, recvPort.first); + return await processFutureJson(CryptoKeyDistance.fromJson, recvPort.first); } @override - Future sign(PublicKey key, SecretKey secret, Uint8List data) { + Future sign( + PublicKey key, SecretKey secret, Uint8List data) async { final nativeKey = jsonEncode(key).toNativeUtf8(); final nativeSecret = jsonEncode(secret).toNativeUtf8(); final nativeEncodedData = base64UrlNoPadEncode(data).toNativeUtf8(); - final recvPort = ReceivePort("crypto_sign"); + final recvPort = ReceivePort('crypto_sign'); final sendPort = recvPort.sendPort; _ffi._cryptoSign( sendPort.nativePort, _kind, nativeKey, nativeSecret, nativeEncodedData); - return processFutureJson(Signature.fromJson, recvPort.first); + return await processFutureJson(Signature.fromJson, recvPort.first); } @override - Future verify(PublicKey key, Uint8List data, Signature signature) { + Future verify( + PublicKey key, Uint8List data, Signature signature) async { final nativeKey = jsonEncode(key).toNativeUtf8(); final nativeEncodedData = base64UrlNoPadEncode(data).toNativeUtf8(); final nativeSignature = jsonEncode(signature).toNativeUtf8(); - final recvPort = ReceivePort("crypto_verify"); + final recvPort = ReceivePort('crypto_verify'); final sendPort = recvPort.sendPort; _ffi._cryptoVerify(sendPort.nativePort, _kind, nativeKey, nativeEncodedData, nativeSignature); - return processFutureVoid(recvPort.first); + return await processFutureVoid(recvPort.first); } @override - Future aeadOverhead() { - final recvPort = ReceivePort("crypto_aead_overhead"); + Future aeadOverhead() async { + final recvPort = ReceivePort('crypto_aead_overhead'); final sendPort = recvPort.sendPort; _ffi._cryptoAeadOverhead( sendPort.nativePort, _kind, ); - return processFuturePlain(recvPort.first); + return await processFuturePlain(recvPort.first); } @override @@ -1201,7 +1084,7 @@ class VeilidCryptoSystemFFI extends VeilidCryptoSystem { ? jsonEncode(associatedData).toNativeUtf8() : nullptr; - final recvPort = ReceivePort("crypto_decrypt_aead"); + final recvPort = ReceivePort('crypto_decrypt_aead'); final sendPort = recvPort.sendPort; _ffi._cryptoDecryptAead(sendPort.nativePort, _kind, nativeEncodedBody, nativeNonce, nativeSharedSecret, nativeSignature); @@ -1219,7 +1102,7 @@ class VeilidCryptoSystemFFI extends VeilidCryptoSystem { ? jsonEncode(associatedData).toNativeUtf8() : nullptr; - final recvPort = ReceivePort("crypto_encrypt_aead"); + final recvPort = ReceivePort('crypto_encrypt_aead'); final sendPort = recvPort.sendPort; _ffi._cryptoEncryptAead(sendPort.nativePort, _kind, nativeEncodedBody, nativeNonce, nativeSharedSecret, nativeSignature); @@ -1234,7 +1117,7 @@ class VeilidCryptoSystemFFI extends VeilidCryptoSystem { final nativeNonce = jsonEncode(nonce).toNativeUtf8(); final nativeSharedSecret = jsonEncode(sharedSecret).toNativeUtf8(); - final recvPort = ReceivePort("crypto_crypt_no_auth"); + final recvPort = ReceivePort('crypto_crypt_no_auth'); final sendPort = recvPort.sendPort; _ffi._cryptoCryptNoAuth(sendPort.nativePort, _kind, nativeEncodedBody, nativeNonce, nativeSharedSecret); @@ -1245,6 +1128,211 @@ class VeilidCryptoSystemFFI extends VeilidCryptoSystem { // FFI implementation of high level Veilid API class VeilidFFI extends Veilid { + + VeilidFFI(DynamicLibrary dylib) + : _dylib = dylib, + _freeString = + dylib.lookupFunction), _FreeStringDart>('free_string'), + _initializeVeilidCore = dylib.lookupFunction), + _InitializeVeilidCoreDart>('initialize_veilid_core'), + _changeLogLevel = + dylib.lookupFunction, Pointer), _ChangeLogLevelDart>( + 'change_log_level'), + _startupVeilidCore = + dylib.lookupFunction), _StartupVeilidCoreDart>( + 'startup_veilid_core'), + _getVeilidState = + dylib.lookupFunction( + 'get_veilid_state'), + _attach = dylib.lookupFunction('attach'), + _detach = dylib.lookupFunction('detach'), + _shutdownVeilidCore = + dylib.lookupFunction( + 'shutdown_veilid_core'), + _routingContext = + dylib.lookupFunction( + 'routing_context'), + _releaseRoutingContext = dylib.lookupFunction('release_routing_context'), + _routingContextWithPrivacy = dylib.lookupFunction< + Uint32 Function(Uint32), + _RoutingContextWithPrivacyDart>('routing_context_with_privacy'), + _routingContextWithCustomPrivacy = dylib.lookupFunction< + Uint32 Function(Uint32, Pointer), + _RoutingContextWithCustomPrivacyDart>( + 'routing_context_with_custom_privacy'), + _routingContextWithSequencing = dylib.lookupFunction< + Uint32 Function(Uint32, Pointer), + _RoutingContextWithSequencingDart>( + 'routing_context_with_sequencing'), + _routingContextAppCall = dylib.lookupFunction, Pointer), + _RoutingContextAppCallDart>('routing_context_app_call'), + _routingContextAppMessage = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Pointer), + _RoutingContextAppMessageDart>('routing_context_app_message'), + _routingContextCreateDHTRecord = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Uint32), + _RoutingContextCreateDHTRecordDart>( + 'routing_context_create_dht_record'), + _routingContextOpenDHTRecord = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Pointer), + _RoutingContextOpenDHTRecordDart>( + 'routing_context_open_dht_record'), + _routingContextCloseDHTRecord = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer), + _RoutingContextCloseDHTRecordDart>( + 'routing_context_close_dht_record'), + _routingContextDeleteDHTRecord = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer), + _RoutingContextDeleteDHTRecordDart>( + 'routing_context_delete_dht_record'), + _routingContextGetDHTValue = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Uint32, Bool), + _RoutingContextGetDHTValueDart>('routing_context_get_dht_value'), + _routingContextSetDHTValue = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Uint32, Pointer), + _RoutingContextSetDHTValueDart>('routing_context_set_dht_value'), + _routingContextWatchDHTValues = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Pointer, Uint64, Uint32), + _RoutingContextWatchDHTValuesDart>( + 'routing_context_watch_dht_values'), + _routingContextCancelDHTWatch = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Pointer), + _RoutingContextCancelDHTWatchDart>( + 'routing_context_cancel_dht_watch'), + _newPrivateRoute = + dylib.lookupFunction( + 'new_private_route'), + _newCustomPrivateRoute = dylib.lookupFunction, Pointer), + _NewCustomPrivateRouteDart>('new_custom_private_route'), + _importRemotePrivateRoute = dylib.lookupFunction< + Void Function(Int64, Pointer), + _ImportRemotePrivateRouteDart>('import_remote_private_route'), + _releasePrivateRoute = dylib.lookupFunction), + _ReleasePrivateRouteDart>('release_private_route'), + _appCallReply = dylib.lookupFunction, Pointer), _AppCallReplyDart>( + 'app_call_reply'), + _openTableDb = dylib + .lookupFunction, Uint32), _OpenTableDbDart>('open_table_db'), + _releaseTableDb = + dylib.lookupFunction( + 'release_table_db'), + _deleteTableDb = + dylib.lookupFunction), _DeleteTableDbDart>( + 'delete_table_db'), + _tableDbGetColumnCount = dylib.lookupFunction('table_db_get_column_count'), + _tableDbGetKeys = + dylib.lookupFunction Function(Uint64, Uint32, Uint32), _TableDbGetKeysDart>( + 'table_db_get_keys'), + _tableDbStore = dylib.lookupFunction, Pointer), _TableDbStoreDart>( + 'table_db_store'), + _tableDbLoad = dylib + .lookupFunction), _TableDbLoadDart>('table_db_load'), + _tableDbDelete = + dylib.lookupFunction), _TableDbDeleteDart>( + 'table_db_delete'), + _tableDbTransact = + dylib.lookupFunction( + 'table_db_transact'), + _releaseTableDbTransaction = dylib.lookupFunction< + Int32 Function(Uint32), + _ReleaseTableDbTransactionDart>('release_table_db_transaction'), + _tableDbTransactionCommit = dylib.lookupFunction< + Void Function(Uint64, Uint32), + _TableDbTransactionCommitDart>('table_db_transaction_commit'), + _tableDbTransactionRollback = dylib.lookupFunction< + Void Function(Uint64, Uint32), + _TableDbTransactionRollbackDart>('table_db_transaction_rollback'), + _tableDbTransactionStore = dylib.lookupFunction< + Void Function(Int64, Uint32, Uint32, Pointer, Pointer), + _TableDbTransactionStoreDart>('table_db_transaction_store'), + _tableDbTransactionDelete = dylib.lookupFunction< + Void Function(Int64, Uint32, Uint32, Pointer), + _TableDbTransactionDeleteDart>('table_db_transaction_delete'), + _validCryptoKinds = + dylib.lookupFunction Function(), _ValidCryptoKindsDart>( + 'valid_crypto_kinds'), + _bestCryptoKind = + dylib.lookupFunction( + 'best_crypto_kind'), + _verifySignatures = + dylib.lookupFunction, Pointer, Pointer), _VerifySignaturesDart>( + 'verify_signatures'), + _generateSignatures = + dylib.lookupFunction, Pointer), _GenerateSignaturesDart>( + 'generate_signatures'), + _generateKeyPair = + dylib.lookupFunction( + 'generate_key_pair'), + _cryptoCachedDH = + dylib.lookupFunction, Pointer), _CryptoCachedDHDart>( + 'crypto_cached_dh'), + _cryptoComputeDH = + dylib.lookupFunction, Pointer), _CryptoComputeDHDart>( + 'crypto_compute_dh'), + _cryptoRandomBytes = + dylib.lookupFunction( + 'crypto_random_bytes'), + _cryptoDefaultSaltLength = dylib.lookupFunction< + Void Function(Int64, Uint32), + _CryptoDefaultSaltLengthDart>('crypto_default_salt_length'), + _cryptoHashPassword = + dylib.lookupFunction, Pointer), _CryptoHashPasswordDart>( + 'crypto_hash_password'), + _cryptoVerifyPassword = dylib.lookupFunction, Pointer), + _CryptoVerifyPasswordDart>('crypto_verify_password'), + _cryptoDeriveSharedSecret = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Pointer), + _CryptoVerifyPasswordDart>('crypto_derive_shared_secret'), + _cryptoRandomNonce = + dylib.lookupFunction( + 'crypto_random_nonce'), + _cryptoRandomSharedSecret = dylib.lookupFunction< + Void Function(Int64, Uint32), + _CryptoRandomSharedSecretDart>('crypto_random_shared_secret'), + _cryptoGenerateKeyPair = dylib.lookupFunction('crypto_generate_key_pair'), + _cryptoGenerateHash = + dylib.lookupFunction), _CryptoGenerateHashDart>( + 'crypto_generate_hash'), + _cryptoValidateKeyPair = dylib.lookupFunction, Pointer), + _CryptoValidateKeyPairDart>('crypto_validate_key_pair'), + _cryptoValidateHash = + dylib.lookupFunction, Pointer), _CryptoValidateHashDart>( + 'crypto_validate_hash'), + _cryptoDistance = + dylib.lookupFunction, Pointer), _CryptoDistanceDart>( + 'crypto_distance'), + _cryptoSign = + dylib.lookupFunction, Pointer, Pointer), _CryptoSignDart>('crypto_sign'), + _cryptoVerify = dylib + .lookupFunction, Pointer, Pointer), _CryptoVerifyDart>('crypto_verify'), + _cryptoAeadOverhead = + dylib.lookupFunction( + 'crypto_aead_overhead'), + _cryptoDecryptAead = + dylib.lookupFunction, Pointer, Pointer, Pointer), _CryptoDecryptAeadDart>( + 'crypto_decrypt_aead'), + _cryptoEncryptAead = + dylib.lookupFunction, Pointer, Pointer, Pointer), _CryptoEncryptAeadDart>( + 'crypto_encrypt_aead'), + _cryptoCryptNoAuth = + dylib.lookupFunction, Pointer, Pointer), _CryptoCryptNoAuthDart>( + 'crypto_crypt_no_auth'), + _now = dylib.lookupFunction('now'), + _debug = dylib.lookupFunction), _DebugDart>('debug'), + _veilidVersionString = dylib.lookupFunction Function(), + _VeilidVersionStringDart>('veilid_version_string'), + _veilidVersion = + dylib.lookupFunction( + 'veilid_version') { + // Get veilid_flutter initializer + final initializeVeilidFlutter = _dylib.lookupFunction< + Void Function(Pointer<_DartPostCObject>), + void Function(Pointer<_DartPostCObject>)>('initialize_veilid_flutter'); + initializeVeilidFlutter(NativeApi.postCObject); + } // veilid_core shared library final DynamicLibrary _dylib; @@ -1309,7 +1397,7 @@ class VeilidFFI extends Veilid { final _CryptoDefaultSaltLengthDart _cryptoDefaultSaltLength; final _CryptoHashPasswordDart _cryptoHashPassword; final _CryptoVerifyPasswordDart _cryptoVerifyPassword; - final _CryptoDeriveSharedSecretDart _cryptoDeriveSharedSecret; + final void Function(int, int, Pointer, Pointer) _cryptoDeriveSharedSecret; final _CryptoRandomNonceDart _cryptoRandomNonce; final _CryptoRandomSharedSecretDart _cryptoRandomSharedSecret; @@ -1330,214 +1418,9 @@ class VeilidFFI extends Veilid { final _VeilidVersionStringDart _veilidVersionString; final _VeilidVersionDart _veilidVersion; - VeilidFFI(DynamicLibrary dylib) - : _dylib = dylib, - _freeString = - dylib.lookupFunction<_FreeStringC, _FreeStringDart>('free_string'), - _initializeVeilidCore = dylib.lookupFunction<_InitializeVeilidCoreC, - _InitializeVeilidCoreDart>('initialize_veilid_core'), - _changeLogLevel = - dylib.lookupFunction<_ChangeLogLevelC, _ChangeLogLevelDart>( - 'change_log_level'), - _startupVeilidCore = - dylib.lookupFunction<_StartupVeilidCoreC, _StartupVeilidCoreDart>( - 'startup_veilid_core'), - _getVeilidState = - dylib.lookupFunction<_GetVeilidStateC, _GetVeilidStateDart>( - 'get_veilid_state'), - _attach = dylib.lookupFunction<_AttachC, _AttachDart>('attach'), - _detach = dylib.lookupFunction<_DetachC, _DetachDart>('detach'), - _shutdownVeilidCore = - dylib.lookupFunction<_ShutdownVeilidCoreC, _ShutdownVeilidCoreDart>( - 'shutdown_veilid_core'), - _routingContext = - dylib.lookupFunction<_RoutingContextC, _RoutingContextDart>( - 'routing_context'), - _releaseRoutingContext = dylib.lookupFunction<_ReleaseRoutingContextC, - _ReleaseRoutingContextDart>('release_routing_context'), - _routingContextWithPrivacy = dylib.lookupFunction< - _RoutingContextWithPrivacyC, - _RoutingContextWithPrivacyDart>('routing_context_with_privacy'), - _routingContextWithCustomPrivacy = dylib.lookupFunction< - _RoutingContextWithCustomPrivacyC, - _RoutingContextWithCustomPrivacyDart>( - 'routing_context_with_custom_privacy'), - _routingContextWithSequencing = dylib.lookupFunction< - _RoutingContextWithSequencingC, - _RoutingContextWithSequencingDart>( - 'routing_context_with_sequencing'), - _routingContextAppCall = dylib.lookupFunction<_RoutingContextAppCallC, - _RoutingContextAppCallDart>('routing_context_app_call'), - _routingContextAppMessage = dylib.lookupFunction< - _RoutingContextAppMessageC, - _RoutingContextAppMessageDart>('routing_context_app_message'), - _routingContextCreateDHTRecord = dylib.lookupFunction< - _RoutingContextCreateDHTRecordC, - _RoutingContextCreateDHTRecordDart>( - 'routing_context_create_dht_record'), - _routingContextOpenDHTRecord = dylib.lookupFunction< - _RoutingContextOpenDHTRecordC, - _RoutingContextOpenDHTRecordDart>( - 'routing_context_open_dht_record'), - _routingContextCloseDHTRecord = dylib.lookupFunction< - _RoutingContextCloseDHTRecordC, - _RoutingContextCloseDHTRecordDart>( - 'routing_context_close_dht_record'), - _routingContextDeleteDHTRecord = dylib.lookupFunction< - _RoutingContextDeleteDHTRecordC, - _RoutingContextDeleteDHTRecordDart>( - 'routing_context_delete_dht_record'), - _routingContextGetDHTValue = dylib.lookupFunction< - _RoutingContextGetDHTValueC, - _RoutingContextGetDHTValueDart>('routing_context_get_dht_value'), - _routingContextSetDHTValue = dylib.lookupFunction< - _RoutingContextSetDHTValueC, - _RoutingContextSetDHTValueDart>('routing_context_set_dht_value'), - _routingContextWatchDHTValues = dylib.lookupFunction< - _RoutingContextWatchDHTValuesC, - _RoutingContextWatchDHTValuesDart>( - 'routing_context_watch_dht_values'), - _routingContextCancelDHTWatch = dylib.lookupFunction< - _RoutingContextCancelDHTWatchC, - _RoutingContextCancelDHTWatchDart>( - 'routing_context_cancel_dht_watch'), - _newPrivateRoute = - dylib.lookupFunction<_NewPrivateRouteC, _NewPrivateRouteDart>( - 'new_private_route'), - _newCustomPrivateRoute = dylib.lookupFunction<_NewCustomPrivateRouteC, - _NewCustomPrivateRouteDart>('new_custom_private_route'), - _importRemotePrivateRoute = dylib.lookupFunction< - _ImportRemotePrivateRouteC, - _ImportRemotePrivateRouteDart>('import_remote_private_route'), - _releasePrivateRoute = dylib.lookupFunction<_ReleasePrivateRouteC, - _ReleasePrivateRouteDart>('release_private_route'), - _appCallReply = dylib.lookupFunction<_AppCallReplyC, _AppCallReplyDart>( - 'app_call_reply'), - _openTableDb = dylib - .lookupFunction<_OpenTableDbC, _OpenTableDbDart>('open_table_db'), - _releaseTableDb = - dylib.lookupFunction<_ReleaseTableDbC, _ReleaseTableDbDart>( - 'release_table_db'), - _deleteTableDb = - dylib.lookupFunction<_DeleteTableDbC, _DeleteTableDbDart>( - 'delete_table_db'), - _tableDbGetColumnCount = dylib.lookupFunction<_TableDbGetColumnCountC, - _TableDbGetColumnCountDart>('table_db_get_column_count'), - _tableDbGetKeys = - dylib.lookupFunction<_TableDbGetKeysC, _TableDbGetKeysDart>( - 'table_db_get_keys'), - _tableDbStore = dylib.lookupFunction<_TableDbStoreC, _TableDbStoreDart>( - 'table_db_store'), - _tableDbLoad = dylib - .lookupFunction<_TableDbLoadC, _TableDbLoadDart>('table_db_load'), - _tableDbDelete = - dylib.lookupFunction<_TableDbDeleteC, _TableDbDeleteDart>( - 'table_db_delete'), - _tableDbTransact = - dylib.lookupFunction<_TableDbTransactC, _TableDbTransactDart>( - 'table_db_transact'), - _releaseTableDbTransaction = dylib.lookupFunction< - _ReleaseTableDbTransactionC, - _ReleaseTableDbTransactionDart>('release_table_db_transaction'), - _tableDbTransactionCommit = dylib.lookupFunction< - _TableDbTransactionCommitC, - _TableDbTransactionCommitDart>('table_db_transaction_commit'), - _tableDbTransactionRollback = dylib.lookupFunction< - _TableDbTransactionRollbackC, - _TableDbTransactionRollbackDart>('table_db_transaction_rollback'), - _tableDbTransactionStore = dylib.lookupFunction< - _TableDbTransactionStoreC, - _TableDbTransactionStoreDart>('table_db_transaction_store'), - _tableDbTransactionDelete = dylib.lookupFunction< - _TableDbTransactionDeleteC, - _TableDbTransactionDeleteDart>('table_db_transaction_delete'), - _validCryptoKinds = - dylib.lookupFunction<_ValidCryptoKindsC, _ValidCryptoKindsDart>( - 'valid_crypto_kinds'), - _bestCryptoKind = - dylib.lookupFunction<_BestCryptoKindC, _BestCryptoKindDart>( - 'best_crypto_kind'), - _verifySignatures = - dylib.lookupFunction<_VerifySignaturesC, _VerifySignaturesDart>( - 'verify_signatures'), - _generateSignatures = - dylib.lookupFunction<_GenerateSignaturesC, _GenerateSignaturesDart>( - 'generate_signatures'), - _generateKeyPair = - dylib.lookupFunction<_GenerateKeyPairC, _GenerateKeyPairDart>( - 'generate_key_pair'), - _cryptoCachedDH = - dylib.lookupFunction<_CryptoCachedDHC, _CryptoCachedDHDart>( - 'crypto_cached_dh'), - _cryptoComputeDH = - dylib.lookupFunction<_CryptoComputeDHC, _CryptoComputeDHDart>( - 'crypto_compute_dh'), - _cryptoRandomBytes = - dylib.lookupFunction<_CryptoRandomBytesC, _CryptoRandomBytesDart>( - 'crypto_random_bytes'), - _cryptoDefaultSaltLength = dylib.lookupFunction< - _CryptoDefaultSaltLengthC, - _CryptoDefaultSaltLengthDart>('crypto_default_salt_length'), - _cryptoHashPassword = - dylib.lookupFunction<_CryptoHashPasswordC, _CryptoHashPasswordDart>( - 'crypto_hash_password'), - _cryptoVerifyPassword = dylib.lookupFunction<_CryptoVerifyPasswordC, - _CryptoVerifyPasswordDart>('crypto_verify_password'), - _cryptoDeriveSharedSecret = dylib.lookupFunction< - _CryptoDeriveSharedSecretC, - _CryptoVerifyPasswordDart>('crypto_derive_shared_secret'), - _cryptoRandomNonce = - dylib.lookupFunction<_CryptoRandomNonceC, _CryptoRandomNonceDart>( - 'crypto_random_nonce'), - _cryptoRandomSharedSecret = dylib.lookupFunction< - _CryptoRandomSharedSecretC, - _CryptoRandomSharedSecretDart>('crypto_random_shared_secret'), - _cryptoGenerateKeyPair = dylib.lookupFunction<_CryptoGenerateKeyPairC, - _CryptoGenerateKeyPairDart>('crypto_generate_key_pair'), - _cryptoGenerateHash = - dylib.lookupFunction<_CryptoGenerateHashC, _CryptoGenerateHashDart>( - 'crypto_generate_hash'), - _cryptoValidateKeyPair = dylib.lookupFunction<_CryptoValidateKeyPairC, - _CryptoValidateKeyPairDart>('crypto_validate_key_pair'), - _cryptoValidateHash = - dylib.lookupFunction<_CryptoValidateHashC, _CryptoValidateHashDart>( - 'crypto_validate_hash'), - _cryptoDistance = - dylib.lookupFunction<_CryptoDistanceC, _CryptoDistanceDart>( - 'crypto_distance'), - _cryptoSign = - dylib.lookupFunction<_CryptoSignC, _CryptoSignDart>('crypto_sign'), - _cryptoVerify = dylib - .lookupFunction<_CryptoVerifyC, _CryptoVerifyDart>('crypto_verify'), - _cryptoAeadOverhead = - dylib.lookupFunction<_CryptoAeadOverheadC, _CryptoAeadOverheadDart>( - 'crypto_aead_overhead'), - _cryptoDecryptAead = - dylib.lookupFunction<_CryptoDecryptAeadC, _CryptoDecryptAeadDart>( - 'crypto_decrypt_aead'), - _cryptoEncryptAead = - dylib.lookupFunction<_CryptoEncryptAeadC, _CryptoEncryptAeadDart>( - 'crypto_encrypt_aead'), - _cryptoCryptNoAuth = - dylib.lookupFunction<_CryptoCryptNoAuthC, _CryptoCryptNoAuthDart>( - 'crypto_crypt_no_auth'), - _now = dylib.lookupFunction<_NowC, _NowDart>('now'), - _debug = dylib.lookupFunction<_DebugC, _DebugDart>('debug'), - _veilidVersionString = dylib.lookupFunction<_VeilidVersionStringC, - _VeilidVersionStringDart>('veilid_version_string'), - _veilidVersion = - dylib.lookupFunction<_VeilidVersionC, _VeilidVersionDart>( - 'veilid_version') { - // Get veilid_flutter initializer - var initializeVeilidFlutter = _dylib.lookupFunction< - _InitializeVeilidFlutterC, - _InitializeVeilidFlutterDart>('initialize_veilid_flutter'); - initializeVeilidFlutter(NativeApi.postCObject); - } - @override void initializeVeilidCore(Map platformConfigJson) { - var nativePlatformConfig = jsonEncode(platformConfigJson).toNativeUtf8(); + final nativePlatformConfig = jsonEncode(platformConfigJson).toNativeUtf8(); _initializeVeilidCore(nativePlatformConfig); @@ -1546,63 +1429,63 @@ class VeilidFFI extends Veilid { @override void changeLogLevel(String layer, VeilidConfigLogLevel logLevel) { - var nativeLogLevel = jsonEncode(logLevel).toNativeUtf8(); - var nativeLayer = layer.toNativeUtf8(); + final nativeLogLevel = jsonEncode(logLevel).toNativeUtf8(); + final nativeLayer = layer.toNativeUtf8(); _changeLogLevel(nativeLayer, nativeLogLevel); malloc.free(nativeLayer); malloc.free(nativeLogLevel); } @override - Future> startupVeilidCore(VeilidConfig config) { - var nativeConfig = jsonEncode(config).toNativeUtf8(); - final recvStreamPort = ReceivePort("veilid_api_stream"); + Future> startupVeilidCore(VeilidConfig config) async { + final nativeConfig = jsonEncode(config).toNativeUtf8(); + final recvStreamPort = ReceivePort('veilid_api_stream'); final sendStreamPort = recvStreamPort.sendPort; - final recvPort = ReceivePort("startup_veilid_core"); + final recvPort = ReceivePort('startup_veilid_core'); final sendPort = recvPort.sendPort; _startupVeilidCore( sendPort.nativePort, sendStreamPort.nativePort, nativeConfig); malloc.free(nativeConfig); - return processFutureStream( + return await processFutureStream( processStreamJson(VeilidUpdate.fromJson, recvStreamPort), recvPort.first); } @override - Future getVeilidState() { - final recvPort = ReceivePort("get_veilid_state"); + Future getVeilidState() async { + final recvPort = ReceivePort('get_veilid_state'); final sendPort = recvPort.sendPort; _getVeilidState(sendPort.nativePort); - return processFutureJson(VeilidState.fromJson, recvPort.first); + return await processFutureJson(VeilidState.fromJson, recvPort.first); } @override - Future attach() { - final recvPort = ReceivePort("attach"); + Future attach() async { + final recvPort = ReceivePort('attach'); final sendPort = recvPort.sendPort; _attach(sendPort.nativePort); - return processFutureVoid(recvPort.first); + return await processFutureVoid(recvPort.first); } @override - Future detach() { - final recvPort = ReceivePort("detach"); + Future detach() async { + final recvPort = ReceivePort('detach'); final sendPort = recvPort.sendPort; _detach(sendPort.nativePort); - return processFutureVoid(recvPort.first); + return await processFutureVoid(recvPort.first); } @override - Future shutdownVeilidCore() { - final recvPort = ReceivePort("shutdown_veilid_core"); + Future shutdownVeilidCore() async { + final recvPort = ReceivePort('shutdown_veilid_core'); final sendPort = recvPort.sendPort; _shutdownVeilidCore(sendPort.nativePort); - return processFutureVoid(recvPort.first); + return await processFutureVoid(recvPort.first); } @override Future routingContext() async { - final recvPort = ReceivePort("routing_context"); + final recvPort = ReceivePort('routing_context'); final sendPort = recvPort.sendPort; _routingContext(sendPort.nativePort); final id = await processFuturePlain(recvPort.first); @@ -1610,59 +1493,59 @@ class VeilidFFI extends Veilid { } @override - Future newPrivateRoute() { - final recvPort = ReceivePort("new_private_route"); + Future newPrivateRoute() async { + final recvPort = ReceivePort('new_private_route'); final sendPort = recvPort.sendPort; _newPrivateRoute(sendPort.nativePort); - return processFutureJson(RouteBlob.fromJson, recvPort.first); + return await processFutureJson(RouteBlob.fromJson, recvPort.first); } @override Future newCustomPrivateRoute( - Stability stability, Sequencing sequencing) { - final recvPort = ReceivePort("new_custom_private_route"); + Stability stability, Sequencing sequencing) async { + final recvPort = ReceivePort('new_custom_private_route'); final sendPort = recvPort.sendPort; _newCustomPrivateRoute( sendPort.nativePort, jsonEncode(stability).toNativeUtf8(), jsonEncode(sequencing).toNativeUtf8()); - return processFutureJson(RouteBlob.fromJson, recvPort.first); + return await processFutureJson(RouteBlob.fromJson, recvPort.first); } @override - Future importRemotePrivateRoute(Uint8List blob) { + Future importRemotePrivateRoute(Uint8List blob) async { final nativeEncodedBlob = base64UrlNoPadEncode(blob).toNativeUtf8(); - final recvPort = ReceivePort("import_remote_private_route"); + final recvPort = ReceivePort('import_remote_private_route'); final sendPort = recvPort.sendPort; _importRemotePrivateRoute(sendPort.nativePort, nativeEncodedBlob); - return processFuturePlain(recvPort.first); + return await processFuturePlain(recvPort.first); } @override - Future releasePrivateRoute(String key) { + Future releasePrivateRoute(String key) async { final nativeEncodedKey = key.toNativeUtf8(); - final recvPort = ReceivePort("release_private_route"); + final recvPort = ReceivePort('release_private_route'); final sendPort = recvPort.sendPort; _releasePrivateRoute(sendPort.nativePort, nativeEncodedKey); - return processFutureVoid(recvPort.first); + return await processFutureVoid(recvPort.first); } @override - Future appCallReply(String callId, Uint8List message) { + Future appCallReply(String callId, Uint8List message) async { final nativeCallId = callId.toNativeUtf8(); final nativeEncodedMessage = base64UrlNoPadEncode(message).toNativeUtf8(); - final recvPort = ReceivePort("app_call_reply"); + final recvPort = ReceivePort('app_call_reply'); final sendPort = recvPort.sendPort; _appCallReply(sendPort.nativePort, nativeCallId, nativeEncodedMessage); - return processFutureVoid(recvPort.first); + return await processFutureVoid(recvPort.first); } @override Future openTableDB(String name, int columnCount) async { - final recvPort = ReceivePort("open_table_db"); + final recvPort = ReceivePort('open_table_db'); final sendPort = recvPort.sendPort; _openTableDb(sendPort.nativePort, name.toNativeUtf8(), columnCount); final id = await processFuturePlain(recvPort.first); @@ -1671,7 +1554,7 @@ class VeilidFFI extends Veilid { @override Future deleteTableDB(String name) async { - final recvPort = ReceivePort("delete_table_db"); + final recvPort = ReceivePort('delete_table_db'); final sendPort = recvPort.sendPort; _deleteTableDb(sendPort.nativePort, name.toNativeUtf8()); final deleted = await processFuturePlain(recvPort.first); @@ -1689,41 +1572,39 @@ class VeilidFFI extends Veilid { @override Future getCryptoSystem(CryptoKind kind) async { if (!validCryptoKinds().contains(kind)) { - throw const VeilidAPIExceptionGeneric("unsupported cryptosystem"); + throw const VeilidAPIExceptionGeneric('unsupported cryptosystem'); } return VeilidCryptoSystemFFI._(this, kind); } @override - Future bestCryptoSystem() async { - return VeilidCryptoSystemFFI._(this, _bestCryptoKind()); - } + Future bestCryptoSystem() async => VeilidCryptoSystemFFI._(this, _bestCryptoKind()); @override - Future> verifySignatures( - List nodeIds, Uint8List data, List signatures) { + Future> verifySignatures(List nodeIds, + Uint8List data, List signatures) async { final nativeNodeIds = jsonEncode(nodeIds).toNativeUtf8(); final nativeData = base64UrlNoPadEncode(data).toNativeUtf8(); final nativeSignatures = jsonEncode(signatures).toNativeUtf8(); - final recvPort = ReceivePort("verify_signatures"); + final recvPort = ReceivePort('verify_signatures'); final sendPort = recvPort.sendPort; _verifySignatures( sendPort.nativePort, nativeNodeIds, nativeData, nativeSignatures); - return processFutureJson( + return await processFutureJson( jsonListConstructor(TypedKey.fromJson), recvPort.first); } @override Future> generateSignatures( - Uint8List data, List keyPairs) { + Uint8List data, List keyPairs) async { final nativeData = base64UrlNoPadEncode(data).toNativeUtf8(); final nativeKeyPairs = jsonEncode(keyPairs).toNativeUtf8(); - final recvPort = ReceivePort("generate_signatures"); + final recvPort = ReceivePort('generate_signatures'); final sendPort = recvPort.sendPort; _generateSignatures(sendPort.nativePort, nativeData, nativeKeyPairs); - return processFutureJson( + return await processFutureJson( jsonListConstructor(TypedSignature.fromJson), recvPort.first); } @@ -1735,17 +1616,17 @@ class VeilidFFI extends Veilid { } @override - Future generateKeyPair(CryptoKind kind) { - final recvPort = ReceivePort("generate_key_pair"); + Future generateKeyPair(CryptoKind kind) async { + final recvPort = ReceivePort('generate_key_pair'); final sendPort = recvPort.sendPort; _generateKeyPair(sendPort.nativePort, kind); - return processFutureJson(TypedKeyPair.fromJson, recvPort.first); + return await processFutureJson(TypedKeyPair.fromJson, recvPort.first); } @override Future debug(String command) async { - var nativeCommand = command.toNativeUtf8(); - final recvPort = ReceivePort("debug"); + final nativeCommand = command.toNativeUtf8(); + final recvPort = ReceivePort('debug'); final sendPort = recvPort.sendPort; _debug(sendPort.nativePort, nativeCommand); return processFuturePlain(recvPort.first); @@ -1754,7 +1635,7 @@ class VeilidFFI extends Veilid { @override String veilidVersionString() { final versionString = _veilidVersionString(); - String ret = versionString.toDartString(); + final ret = versionString.toDartString(); _freeString(versionString); return ret; } diff --git a/veilid-flutter/lib/veilid_js.dart b/veilid-flutter/lib/veilid_js.dart index 42045dd1..17ff2161 100644 --- a/veilid-flutter/lib/veilid_js.dart +++ b/veilid-flutter/lib/veilid_js.dart @@ -1,30 +1,26 @@ -import 'veilid.dart'; - +import 'dart:async'; +import 'dart:convert'; import 'dart:html' as html; import 'dart:js' as js; import 'dart:js_util' as js_util; -import 'dart:async'; -import 'dart:convert'; import 'dart:typed_data'; -import 'veilid_encoding.dart'; +import 'veilid.dart'; ////////////////////////////////////////////////////////// Veilid getVeilid() => VeilidJS(); -Object wasm = js_util.getProperty(html.window, "veilid_wasm"); +Object wasm = js_util.getProperty(html.window, 'veilid_wasm'); -Future _wrapApiPromise(Object p) { - return js_util.promiseToFuture(p).then((value) => value as T).catchError( +Future _wrapApiPromise(Object p) => js_util.promiseToFuture(p).then((value) => value as T).catchError( (error) => Future.error( VeilidAPIException.fromJson(jsonDecode(error as String)))); -} class _Ctx { + _Ctx(int this.id, this.js); int? id; final VeilidJS js; - _Ctx(int this.id, this.js); void ensureValid() { if (id == null) { throw VeilidAPIExceptionNotInitialized(); @@ -33,7 +29,7 @@ class _Ctx { void close() { if (id != null) { - js_util.callMethod(wasm, "release_routing_context", [id!]); + js_util.callMethod(wasm, 'release_routing_context', [id!]); id = null; } } @@ -41,12 +37,12 @@ class _Ctx { // JS implementation of VeilidRoutingContext class VeilidRoutingContextJS extends VeilidRoutingContext { - final _Ctx _ctx; - static final Finalizer<_Ctx> _finalizer = Finalizer((ctx) => ctx.close()); VeilidRoutingContextJS._(this._ctx) { _finalizer.attach(this, _ctx, detach: this); } + final _Ctx _ctx; + static final Finalizer<_Ctx> _finalizer = Finalizer((ctx) => ctx.close()); @override void close() { @@ -56,8 +52,8 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override VeilidRoutingContextJS withPrivacy() { _ctx.ensureValid(); - int newId = - js_util.callMethod(wasm, "routing_context_with_privacy", [_ctx.id!]); + final int newId = + js_util.callMethod(wasm, 'routing_context_with_privacy', [_ctx.id!]); return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js)); } @@ -66,7 +62,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { _ctx.ensureValid(); final newId = js_util.callMethod( wasm, - "routing_context_with_custom_privacy", + 'routing_context_with_custom_privacy', [_ctx.id!, jsonEncode(safetySelection)]); return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js)); @@ -75,7 +71,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override VeilidRoutingContextJS withSequencing(Sequencing sequencing) { _ctx.ensureValid(); - final newId = js_util.callMethod(wasm, "routing_context_with_sequencing", + final newId = js_util.callMethod(wasm, 'routing_context_with_sequencing', [_ctx.id!, jsonEncode(sequencing)]); return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js)); } @@ -83,19 +79,19 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override Future appCall(String target, Uint8List request) async { _ctx.ensureValid(); - var encodedRequest = base64UrlNoPadEncode(request); + final encodedRequest = base64UrlNoPadEncode(request); return base64UrlNoPadDecode(await _wrapApiPromise(js_util.callMethod( - wasm, "routing_context_app_call", [_ctx.id!, target, encodedRequest]))); + wasm, 'routing_context_app_call', [_ctx.id!, target, encodedRequest]))); } @override Future appMessage(String target, Uint8List message) { _ctx.ensureValid(); - var encodedMessage = base64UrlNoPadEncode(message); + final encodedMessage = base64UrlNoPadEncode(message); return _wrapApiPromise(js_util.callMethod(wasm, - "routing_context_app_message", [_ctx.id!, target, encodedMessage])); + 'routing_context_app_message', [_ctx.id!, target, encodedMessage])); } @override @@ -103,7 +99,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { {CryptoKind kind = 0}) async { _ctx.ensureValid(); return DHTRecordDescriptor.fromJson(jsonDecode(await _wrapApiPromise(js_util - .callMethod(wasm, "routing_context_create_dht_record", + .callMethod(wasm, 'routing_context_create_dht_record', [_ctx.id!, jsonEncode(schema), kind])))); } @@ -112,10 +108,10 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { TypedKey key, KeyPair? writer) async { _ctx.ensureValid(); return DHTRecordDescriptor.fromJson(jsonDecode(await _wrapApiPromise(js_util - .callMethod(wasm, "routing_context_open_dht_record", [ + .callMethod(wasm, 'routing_context_open_dht_record', [ _ctx.id!, jsonEncode(key), - writer != null ? jsonEncode(writer) : null + if (writer != null) jsonEncode(writer) else null ])))); } @@ -123,14 +119,14 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { Future closeDHTRecord(TypedKey key) { _ctx.ensureValid(); return _wrapApiPromise(js_util.callMethod( - wasm, "routing_context_close_dht_record", [_ctx.id!, jsonEncode(key)])); + wasm, 'routing_context_close_dht_record', [_ctx.id!, jsonEncode(key)])); } @override Future deleteDHTRecord(TypedKey key) { _ctx.ensureValid(); return _wrapApiPromise(js_util.callMethod(wasm, - "routing_context_delete_dht_record", [_ctx.id!, jsonEncode(key)])); + 'routing_context_delete_dht_record', [_ctx.id!, jsonEncode(key)])); } @override @@ -139,7 +135,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { _ctx.ensureValid(); final opt = await _wrapApiPromise(js_util.callMethod( wasm, - "routing_context_get_dht_value", + 'routing_context_get_dht_value', [_ctx.id!, jsonEncode(key), subkey, forceRefresh])); return opt == null ? null : ValueData.fromJson(jsonDecode(opt)); } @@ -150,7 +146,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { _ctx.ensureValid(); final opt = await _wrapApiPromise(js_util.callMethod( wasm, - "routing_context_set_dht_value", + 'routing_context_set_dht_value', [_ctx.id!, jsonEncode(key), subkey, base64UrlNoPadEncode(data)])); return opt == null ? null : ValueData.fromJson(jsonDecode(opt)); } @@ -160,7 +156,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { Timestamp expiration, int count) async { _ctx.ensureValid(); final ts = await _wrapApiPromise(js_util.callMethod( - wasm, "routing_context_watch_dht_values", [ + wasm, 'routing_context_watch_dht_values', [ _ctx.id!, jsonEncode(key), jsonEncode(subkeys), @@ -175,192 +171,150 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { _ctx.ensureValid(); return _wrapApiPromise(js_util.callMethod( wasm, - "routing_context_cancel_dht_watch", + 'routing_context_cancel_dht_watch', [_ctx.id!, jsonEncode(key), jsonEncode(subkeys)])); } } // JS implementation of VeilidCryptoSystem class VeilidCryptoSystemJS extends VeilidCryptoSystem { - final CryptoKind _kind; - final VeilidJS _js; VeilidCryptoSystemJS._(this._js, this._kind) { // Keep the reference _js; } + final CryptoKind _kind; + final VeilidJS _js; @override - CryptoKind kind() { - return _kind; - } + CryptoKind kind() => _kind; @override - Future cachedDH(PublicKey key, SecretKey secret) async { - return SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(js_util - .callMethod(wasm, "crypto_cached_dh", + Future cachedDH(PublicKey key, SecretKey secret) async => SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(js_util + .callMethod(wasm, 'crypto_cached_dh', [_kind, jsonEncode(key), jsonEncode(secret)])))); - } @override - Future computeDH(PublicKey key, SecretKey secret) async { - return SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(js_util - .callMethod(wasm, "crypto_compute_dh", + Future computeDH(PublicKey key, SecretKey secret) async => SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(js_util + .callMethod(wasm, 'crypto_compute_dh', [_kind, jsonEncode(key), jsonEncode(secret)])))); - } @override - Future randomBytes(int len) async { - return base64UrlNoPadDecode(await _wrapApiPromise( - js_util.callMethod(wasm, "crypto_random_bytes", [_kind, len]))); - } + Future randomBytes(int len) async => base64UrlNoPadDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'crypto_random_bytes', [_kind, len]))); @override - Future defaultSaltLength() { - return _wrapApiPromise( - js_util.callMethod(wasm, "crypto_default_salt_length", [_kind])); - } + Future defaultSaltLength() => _wrapApiPromise( + js_util.callMethod(wasm, 'crypto_default_salt_length', [_kind])); @override - Future hashPassword(Uint8List password, Uint8List salt) { - return _wrapApiPromise(js_util.callMethod(wasm, "crypto_hash_password", + Future hashPassword(Uint8List password, Uint8List salt) => _wrapApiPromise(js_util.callMethod(wasm, 'crypto_hash_password', [_kind, base64UrlNoPadEncode(password), base64UrlNoPadEncode(salt)])); - } @override - Future verifyPassword(Uint8List password, String passwordHash) { - return _wrapApiPromise(js_util.callMethod(wasm, "crypto_verify_password", + Future verifyPassword(Uint8List password, String passwordHash) => _wrapApiPromise(js_util.callMethod(wasm, 'crypto_verify_password', [_kind, base64UrlNoPadEncode(password), passwordHash])); - } @override Future deriveSharedSecret( - Uint8List password, Uint8List salt) async { - return SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(js_util - .callMethod(wasm, "crypto_derive_shared_secret", [ + Uint8List password, Uint8List salt) async => SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(js_util + .callMethod(wasm, 'crypto_derive_shared_secret', [ _kind, base64UrlNoPadEncode(password), base64UrlNoPadEncode(salt) ])))); - } @override - Future randomNonce() async { - return Nonce.fromJson(jsonDecode(await _wrapApiPromise( - js_util.callMethod(wasm, "crypto_random_nonce", [_kind])))); - } + Future randomNonce() async => Nonce.fromJson(jsonDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'crypto_random_nonce', [_kind])))); @override - Future randomSharedSecret() async { - return SharedSecret.fromJson(jsonDecode(await _wrapApiPromise( - js_util.callMethod(wasm, "crypto_random_shared_secret", [_kind])))); - } + Future randomSharedSecret() async => SharedSecret.fromJson(jsonDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'crypto_random_shared_secret', [_kind])))); @override - Future generateKeyPair() async { - return KeyPair.fromJson(jsonDecode(await _wrapApiPromise( - js_util.callMethod(wasm, "crypto_generate_key_pair", [_kind])))); - } + Future generateKeyPair() async => KeyPair.fromJson(jsonDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'crypto_generate_key_pair', [_kind])))); @override - Future generateHash(Uint8List data) async { - return HashDigest.fromJson(jsonDecode(await _wrapApiPromise(js_util - .callMethod(wasm, "crypto_generate_hash", + Future generateHash(Uint8List data) async => HashDigest.fromJson(jsonDecode(await _wrapApiPromise(js_util + .callMethod(wasm, 'crypto_generate_hash', [_kind, base64UrlNoPadEncode(data)])))); - } @override - Future validateKeyPair(PublicKey key, SecretKey secret) { - return _wrapApiPromise(js_util.callMethod(wasm, "crypto_validate_key_pair", + Future validateKeyPair(PublicKey key, SecretKey secret) => _wrapApiPromise(js_util.callMethod(wasm, 'crypto_validate_key_pair', [_kind, jsonEncode(key), jsonEncode(secret)])); - } @override - Future validateHash(Uint8List data, HashDigest hash) { - return _wrapApiPromise(js_util.callMethod(wasm, "crypto_validate_hash", + Future validateHash(Uint8List data, HashDigest hash) => _wrapApiPromise(js_util.callMethod(wasm, 'crypto_validate_hash', [_kind, base64UrlNoPadEncode(data), jsonEncode(hash)])); - } @override - Future distance(CryptoKey key1, CryptoKey key2) async { - return CryptoKeyDistance.fromJson(jsonDecode(await _wrapApiPromise(js_util - .callMethod(wasm, "crypto_distance", + Future distance(CryptoKey key1, CryptoKey key2) async => CryptoKeyDistance.fromJson(jsonDecode(await _wrapApiPromise(js_util + .callMethod(wasm, 'crypto_distance', [_kind, jsonEncode(key1), jsonEncode(key2)])))); - } @override Future sign( - PublicKey key, SecretKey secret, Uint8List data) async { - return Signature.fromJson(jsonDecode(await _wrapApiPromise(js_util - .callMethod(wasm, "crypto_sign", [ + PublicKey key, SecretKey secret, Uint8List data) async => Signature.fromJson(jsonDecode(await _wrapApiPromise(js_util + .callMethod(wasm, 'crypto_sign', [ _kind, jsonEncode(key), jsonEncode(secret), base64UrlNoPadEncode(data) ])))); - } @override - Future verify(PublicKey key, Uint8List data, Signature signature) { - return _wrapApiPromise(js_util.callMethod(wasm, "crypto_verify", [ + Future verify(PublicKey key, Uint8List data, Signature signature) => _wrapApiPromise(js_util.callMethod(wasm, 'crypto_verify', [ _kind, jsonEncode(key), base64UrlNoPadEncode(data), jsonEncode(signature), ])); - } @override - Future aeadOverhead() { - return _wrapApiPromise( - js_util.callMethod(wasm, "crypto_aead_overhead", [_kind])); - } + Future aeadOverhead() => _wrapApiPromise( + js_util.callMethod(wasm, 'crypto_aead_overhead', [_kind])); @override Future decryptAead(Uint8List body, Nonce nonce, - SharedSecret sharedSecret, Uint8List? associatedData) async { - return base64UrlNoPadDecode( - await _wrapApiPromise(js_util.callMethod(wasm, "crypto_decrypt_aead", [ + SharedSecret sharedSecret, Uint8List? associatedData) async => base64UrlNoPadDecode( + await _wrapApiPromise(js_util.callMethod(wasm, 'crypto_decrypt_aead', [ _kind, base64UrlNoPadEncode(body), jsonEncode(nonce), jsonEncode(sharedSecret), - associatedData != null ? base64UrlNoPadEncode(associatedData) : null + if (associatedData != null) base64UrlNoPadEncode(associatedData) else null ]))); - } @override Future encryptAead(Uint8List body, Nonce nonce, - SharedSecret sharedSecret, Uint8List? associatedData) async { - return base64UrlNoPadDecode( - await _wrapApiPromise(js_util.callMethod(wasm, "crypto_encrypt_aead", [ + SharedSecret sharedSecret, Uint8List? associatedData) async => base64UrlNoPadDecode( + await _wrapApiPromise(js_util.callMethod(wasm, 'crypto_encrypt_aead', [ _kind, base64UrlNoPadEncode(body), jsonEncode(nonce), jsonEncode(sharedSecret), - associatedData != null ? base64UrlNoPadEncode(associatedData) : null + if (associatedData != null) base64UrlNoPadEncode(associatedData) else null ]))); - } @override Future cryptNoAuth( - Uint8List body, Nonce nonce, SharedSecret sharedSecret) async { - return base64UrlNoPadDecode(await _wrapApiPromise(js_util.callMethod( - wasm, "crypto_crypt_no_auth", [ + Uint8List body, Nonce nonce, SharedSecret sharedSecret) async => base64UrlNoPadDecode(await _wrapApiPromise(js_util.callMethod( + wasm, 'crypto_crypt_no_auth', [ _kind, base64UrlNoPadEncode(body), jsonEncode(nonce), jsonEncode(sharedSecret) ]))); - } } class _TDBT { + + _TDBT(this.id, this.tdbjs, this.js); int? id; final VeilidTableDBJS tdbjs; final VeilidJS js; - - _TDBT(this.id, this.tdbjs, this.js); void ensureValid() { if (id == null) { throw VeilidAPIExceptionNotInitialized(); @@ -369,7 +323,7 @@ class _TDBT { void close() { if (id != null) { - js_util.callMethod(wasm, "release_table_db_transaction", [id!]); + js_util.callMethod(wasm, 'release_table_db_transaction', [id!]); id = null; } } @@ -377,23 +331,21 @@ class _TDBT { // JS implementation of VeilidTableDBTransaction class VeilidTableDBTransactionJS extends VeilidTableDBTransaction { - final _TDBT _tdbt; - static final Finalizer<_TDBT> _finalizer = Finalizer((tdbt) => tdbt.close()); VeilidTableDBTransactionJS._(this._tdbt) { _finalizer.attach(this, _tdbt, detach: this); } + final _TDBT _tdbt; + static final Finalizer<_TDBT> _finalizer = Finalizer((tdbt) => tdbt.close()); @override - bool isDone() { - return _tdbt.id == null; - } + bool isDone() => _tdbt.id == null; @override Future commit() async { _tdbt.ensureValid(); await _wrapApiPromise( - js_util.callMethod(wasm, "table_db_transaction_commit", [_tdbt.id!])); + js_util.callMethod(wasm, 'table_db_transaction_commit', [_tdbt.id!])); _tdbt.close(); } @@ -401,7 +353,7 @@ class VeilidTableDBTransactionJS extends VeilidTableDBTransaction { Future rollback() async { _tdbt.ensureValid(); await _wrapApiPromise( - js_util.callMethod(wasm, "table_db_transaction_rollback", [_tdbt.id!])); + js_util.callMethod(wasm, 'table_db_transaction_rollback', [_tdbt.id!])); _tdbt.close(); } @@ -411,7 +363,7 @@ class VeilidTableDBTransactionJS extends VeilidTableDBTransaction { final encodedKey = base64UrlNoPadEncode(key); final encodedValue = base64UrlNoPadEncode(value); - await _wrapApiPromise(js_util.callMethod(wasm, "table_db_transaction_store", + await _wrapApiPromise(js_util.callMethod(wasm, 'table_db_transaction_store', [_tdbt.id!, col, encodedKey, encodedValue])); } @@ -421,15 +373,15 @@ class VeilidTableDBTransactionJS extends VeilidTableDBTransaction { final encodedKey = base64UrlNoPadEncode(key); await _wrapApiPromise(js_util.callMethod( - wasm, "table_db_transaction_delete", [_tdbt.id!, col, encodedKey])); + wasm, 'table_db_transaction_delete', [_tdbt.id!, col, encodedKey])); } } class _TDB { - int? id; - final VeilidJS js; _TDB(int this.id, this.js); + int? id; + final VeilidJS js; void ensureValid() { if (id == null) { throw VeilidAPIExceptionNotInitialized(); @@ -438,7 +390,7 @@ class _TDB { void close() { if (id != null) { - js_util.callMethod(wasm, "release_table_db", [id!]); + js_util.callMethod(wasm, 'release_table_db', [id!]); id = null; } } @@ -446,12 +398,12 @@ class _TDB { // JS implementation of VeilidTableDB class VeilidTableDBJS extends VeilidTableDB { - final _TDB _tdb; - static final Finalizer<_TDB> _finalizer = Finalizer((tdb) => tdb.close()); VeilidTableDBJS._(this._tdb) { _finalizer.attach(this, _tdb, detach: this); } + final _TDB _tdb; + static final Finalizer<_TDB> _finalizer = Finalizer((tdb) => tdb.close()); @override void close() { @@ -461,20 +413,20 @@ class VeilidTableDBJS extends VeilidTableDB { @override int getColumnCount() { _tdb.ensureValid(); - return js_util.callMethod(wasm, "table_db_get_column_count", [_tdb.id!]); + return js_util.callMethod(wasm, 'table_db_get_column_count', [_tdb.id!]); } @override Future> getKeys(int col) async { _tdb.ensureValid(); return jsonListConstructor(base64UrlNoPadDecodeDynamic)(jsonDecode( - await js_util.callMethod(wasm, "table_db_get_keys", [_tdb.id!, col]))); + await js_util.callMethod(wasm, 'table_db_get_keys', [_tdb.id!, col]))); } @override VeilidTableDBTransaction transact() { _tdb.ensureValid(); - final id = js_util.callMethod(wasm, "table_db_transact", [_tdb.id!]); + final id = js_util.callMethod(wasm, 'table_db_transact', [_tdb.id!]); return VeilidTableDBTransactionJS._(_TDBT(id, this, _tdb.js)); } @@ -486,7 +438,7 @@ class VeilidTableDBJS extends VeilidTableDB { final encodedValue = base64UrlNoPadEncode(value); return _wrapApiPromise(js_util.callMethod( - wasm, "table_db_store", [_tdb.id!, col, encodedKey, encodedValue])); + wasm, 'table_db_store', [_tdb.id!, col, encodedKey, encodedValue])); } @override @@ -494,8 +446,8 @@ class VeilidTableDBJS extends VeilidTableDB { _tdb.ensureValid(); final encodedKey = base64UrlNoPadEncode(key); - String? out = await _wrapApiPromise( - js_util.callMethod(wasm, "table_db_load", [_tdb.id!, col, encodedKey])); + final out = await _wrapApiPromise( + js_util.callMethod(wasm, 'table_db_load', [_tdb.id!, col, encodedKey])); if (out == null) { return null; } @@ -508,7 +460,7 @@ class VeilidTableDBJS extends VeilidTableDB { final encodedKey = base64UrlNoPadEncode(key); return _wrapApiPromise(js_util - .callMethod(wasm, "table_db_delete", [_tdb.id!, col, encodedKey])); + .callMethod(wasm, 'table_db_delete', [_tdb.id!, col, encodedKey])); } } @@ -517,61 +469,53 @@ class VeilidTableDBJS extends VeilidTableDB { class VeilidJS extends Veilid { @override void initializeVeilidCore(Map platformConfigJson) { - var platformConfigJsonString = jsonEncode(platformConfigJson); + final platformConfigJsonString = jsonEncode(platformConfigJson); js_util - .callMethod(wasm, "initialize_veilid_core", [platformConfigJsonString]); + .callMethod(wasm, 'initialize_veilid_core', [platformConfigJsonString]); } @override void changeLogLevel(String layer, VeilidConfigLogLevel logLevel) { - var logLevelJsonString = jsonEncode(logLevel); - js_util.callMethod(wasm, "change_log_level", [layer, logLevelJsonString]); + final logLevelJsonString = jsonEncode(logLevel); + js_util.callMethod(wasm, 'change_log_level', [layer, logLevelJsonString]); } @override Future> startupVeilidCore(VeilidConfig config) async { - var streamController = StreamController(); + final streamController = StreamController(); updateCallback(String update) { - var updateJson = jsonDecode(update); - if (updateJson["kind"] == "Shutdown") { + final updateJson = jsonDecode(update); + if (updateJson['kind'] == 'Shutdown') { streamController.close(); } else { - var update = VeilidUpdate.fromJson(updateJson); + final update = VeilidUpdate.fromJson(updateJson); streamController.add(update); } } - await _wrapApiPromise(js_util.callMethod(wasm, "startup_veilid_core", + await _wrapApiPromise(js_util.callMethod(wasm, 'startup_veilid_core', [js.allowInterop(updateCallback), jsonEncode(config)])); return streamController.stream; } @override - Future getVeilidState() async { - return VeilidState.fromJson(jsonDecode(await _wrapApiPromise( - js_util.callMethod(wasm, "get_veilid_state", [])))); - } + Future getVeilidState() async => VeilidState.fromJson(jsonDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'get_veilid_state', [])))); @override - Future attach() { - return _wrapApiPromise(js_util.callMethod(wasm, "attach", [])); - } + Future attach() => _wrapApiPromise(js_util.callMethod(wasm, 'attach', [])); @override - Future detach() { - return _wrapApiPromise(js_util.callMethod(wasm, "detach", [])); - } + Future detach() => _wrapApiPromise(js_util.callMethod(wasm, 'detach', [])); @override - Future shutdownVeilidCore() { - return _wrapApiPromise( - js_util.callMethod(wasm, "shutdown_veilid_core", [])); - } + Future shutdownVeilidCore() => _wrapApiPromise( + js_util.callMethod(wasm, 'shutdown_veilid_core', [])); @override List validCryptoKinds() { - final vck = 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(); } @@ -579,118 +523,98 @@ class VeilidJS extends Veilid { @override Future getCryptoSystem(CryptoKind kind) async { if (!validCryptoKinds().contains(kind)) { - throw const VeilidAPIExceptionGeneric("unsupported cryptosystem"); + throw const VeilidAPIExceptionGeneric('unsupported cryptosystem'); } return VeilidCryptoSystemJS._(this, kind); } @override - Future bestCryptoSystem() async { - return VeilidCryptoSystemJS._( - this, js_util.callMethod(wasm, "best_crypto_kind", [])); - } + Future bestCryptoSystem() async => VeilidCryptoSystemJS._( + this, js_util.callMethod(wasm, 'best_crypto_kind', [])); @override Future> verifySignatures(List nodeIds, - Uint8List data, List signatures) async { - return jsonListConstructor(TypedKey.fromJson)(jsonDecode( - await _wrapApiPromise(js_util.callMethod(wasm, "verify_signatures", [ + Uint8List data, List signatures) async => jsonListConstructor(TypedKey.fromJson)(jsonDecode( + await _wrapApiPromise(js_util.callMethod(wasm, 'verify_signatures', [ jsonEncode(nodeIds), base64UrlNoPadEncode(data), jsonEncode(signatures) ])))); - } @override Future> generateSignatures( - Uint8List data, List keyPairs) async { - return jsonListConstructor(TypedSignature.fromJson)(jsonDecode( - await _wrapApiPromise(js_util.callMethod(wasm, "generate_signatures", + Uint8List data, List keyPairs) async => jsonListConstructor(TypedSignature.fromJson)(jsonDecode( + await _wrapApiPromise(js_util.callMethod(wasm, 'generate_signatures', [base64UrlNoPadEncode(data), jsonEncode(keyPairs)])))); - } @override - Future generateKeyPair(CryptoKind kind) async { - return TypedKeyPair.fromJson(jsonDecode(await _wrapApiPromise( - js_util.callMethod(wasm, "generate_key_pair", [kind])))); - } + Future generateKeyPair(CryptoKind kind) async => TypedKeyPair.fromJson(jsonDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'generate_key_pair', [kind])))); @override Future routingContext() async { - int id = - await _wrapApiPromise(js_util.callMethod(wasm, "routing_context", [])); + final var id = + await _wrapApiPromise(js_util.callMethod(wasm, 'routing_context', [])); return VeilidRoutingContextJS._(_Ctx(id, this)); } @override - Future newPrivateRoute() async { - return RouteBlob.fromJson(jsonDecode(await _wrapApiPromise( - js_util.callMethod(wasm, "new_private_route", [])))); - } + Future newPrivateRoute() async => RouteBlob.fromJson(jsonDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'new_private_route', [])))); @override Future newCustomPrivateRoute( Stability stability, Sequencing sequencing) async { - var stabilityString = jsonEncode(stability); - var sequencingString = jsonEncode(sequencing); + final stabilityString = jsonEncode(stability); + final sequencingString = jsonEncode(sequencing); return RouteBlob.fromJson(jsonDecode(await _wrapApiPromise(js_util .callMethod( - wasm, "new_private_route", [stabilityString, sequencingString])))); + wasm, 'new_private_route', [stabilityString, sequencingString])))); } @override Future importRemotePrivateRoute(Uint8List blob) { - var encodedBlob = base64UrlNoPadEncode(blob); + final encodedBlob = base64UrlNoPadEncode(blob); return _wrapApiPromise( - js_util.callMethod(wasm, "import_remote_private_route", [encodedBlob])); + js_util.callMethod(wasm, 'import_remote_private_route', [encodedBlob])); } @override - Future releasePrivateRoute(String key) { - return _wrapApiPromise( - js_util.callMethod(wasm, "release_private_route", [key])); - } + Future releasePrivateRoute(String key) => _wrapApiPromise( + js_util.callMethod(wasm, 'release_private_route', [key])); @override Future appCallReply(String callId, Uint8List message) { - var encodedMessage = base64UrlNoPadEncode(message); + final encodedMessage = base64UrlNoPadEncode(message); return _wrapApiPromise( - js_util.callMethod(wasm, "app_call_reply", [callId, encodedMessage])); + js_util.callMethod(wasm, 'app_call_reply', [callId, encodedMessage])); } @override Future openTableDB(String name, int columnCount) async { - int id = await _wrapApiPromise( - js_util.callMethod(wasm, "open_table_db", [name, columnCount])); + final id = await _wrapApiPromise( + js_util.callMethod(wasm, 'open_table_db', [name, columnCount])); return VeilidTableDBJS._(_TDB(id, this)); } @override - Future deleteTableDB(String name) { - return _wrapApiPromise(js_util.callMethod(wasm, "delete_table_db", [name])); - } + Future deleteTableDB(String name) => _wrapApiPromise(js_util.callMethod(wasm, 'delete_table_db', [name])); @override - Timestamp now() { - return Timestamp.fromString(js_util.callMethod(wasm, "now", [])); - } + Timestamp now() => Timestamp.fromString(js_util.callMethod(wasm, 'now', [])); @override - Future debug(String command) async { - return await _wrapApiPromise(js_util.callMethod(wasm, "debug", [command])); - } + Future debug(String command) async => await _wrapApiPromise(js_util.callMethod(wasm, 'debug', [command])); @override - String veilidVersionString() { - return js_util.callMethod(wasm, "veilid_version_string", []); - } + String veilidVersionString() => js_util.callMethod(wasm, 'veilid_version_string', []); @override VeilidVersion veilidVersion() { Map jsonVersion = - jsonDecode(js_util.callMethod(wasm, "veilid_version", [])); + jsonDecode(js_util.callMethod(wasm, 'veilid_version', [])); return VeilidVersion( - jsonVersion["major"], jsonVersion["minor"], jsonVersion["patch"]); + jsonVersion['major'], jsonVersion['minor'], jsonVersion['patch']); } } diff --git a/veilid-flutter/lib/veilid_plugin_stub_web.dart b/veilid-flutter/lib/veilid_plugin_stub_web.dart index 77a8436a..8a7c3022 100644 --- a/veilid-flutter/lib/veilid_plugin_stub_web.dart +++ b/veilid-flutter/lib/veilid_plugin_stub_web.dart @@ -11,7 +11,7 @@ class VeilidPluginStubWeb { Future handleMethodCall(MethodCall call) async { throw PlatformException( code: 'Unimplemented', - details: 'Veilid for Web doesn\'t implement \'${call.method}\'', + details: "Veilid for Web doesn't implement '${call.method}'", ); } } diff --git a/veilid-flutter/lib/veilid_state.dart b/veilid-flutter/lib/veilid_state.dart index 9a7146d5..835a786a 100644 --- a/veilid-flutter/lib/veilid_state.dart +++ b/veilid-flutter/lib/veilid_state.dart @@ -109,8 +109,7 @@ class PeerStats with _$PeerStats { const factory PeerStats({ required Timestamp timeAdded, required RPCStats rpcStats, - LatencyStats? latency, - required TransferStatsDownUp transfer, + required TransferStatsDownUp transfer, LatencyStats? latency, }) = _PeerStats; factory PeerStats.fromJson(dynamic json) => @@ -142,13 +141,10 @@ sealed class VeilidUpdate with _$VeilidUpdate { String? backtrace, }) = VeilidLog; const factory VeilidUpdate.appMessage({ - TypedKey? sender, - @Uint8ListJsonConverter() required Uint8List message, + @Uint8ListJsonConverter() required Uint8List message, TypedKey? sender, }) = VeilidAppMessage; const factory VeilidUpdate.appCall({ - TypedKey? sender, - @Uint8ListJsonConverter() required Uint8List message, - required String callId, + @Uint8ListJsonConverter() required Uint8List message, required String callId, TypedKey? sender, }) = VeilidAppCall; const factory VeilidUpdate.attachment( {required AttachmentState state, diff --git a/veilid-flutter/lib/veilid_table_db.dart b/veilid-flutter/lib/veilid_table_db.dart index 43868c0b..cf7b34c6 100644 --- a/veilid-flutter/lib/veilid_table_db.dart +++ b/veilid-flutter/lib/veilid_table_db.dart @@ -1,6 +1,6 @@ import 'dart:async'; -import 'dart:typed_data'; import 'dart:convert'; +import 'dart:typed_data'; ///////////////////////////////////// /// VeilidTableDB @@ -12,16 +12,12 @@ abstract class VeilidTableDBTransaction { Future delete(int col, Uint8List key); Future storeJson(int col, Uint8List key, Object? object, - {Object? Function(Object? nonEncodable)? toEncodable}) async { - return store(col, key, + {Object? Function(Object? nonEncodable)? toEncodable}) async => store(col, key, utf8.encoder.convert(jsonEncode(object, toEncodable: toEncodable))); - } Future storeStringJson(int col, String key, Object? object, - {Object? Function(Object? nonEncodable)? toEncodable}) { - return storeJson(col, utf8.encoder.convert(key), object, + {Object? Function(Object? nonEncodable)? toEncodable}) => storeJson(col, utf8.encoder.convert(key), object, toEncodable: toEncodable); - } } abstract class VeilidTableDB { @@ -34,20 +30,16 @@ abstract class VeilidTableDB { Future delete(int col, Uint8List key); Future storeJson(int col, Uint8List key, Object? object, - {Object? Function(Object? nonEncodable)? toEncodable}) { - return store(col, key, + {Object? Function(Object? nonEncodable)? toEncodable}) => store(col, key, utf8.encoder.convert(jsonEncode(object, toEncodable: toEncodable))); - } Future storeStringJson(int col, String key, Object? object, - {Object? Function(Object? nonEncodable)? toEncodable}) { - return storeJson(col, utf8.encoder.convert(key), object, + {Object? Function(Object? nonEncodable)? toEncodable}) => storeJson(col, utf8.encoder.convert(key), object, toEncodable: toEncodable); - } Future loadJson(int col, Uint8List key, {Object? Function(Object? key, Object? value)? reviver}) async { - var s = await load(col, key); + final s = await load(col, key); if (s == null) { return null; } @@ -55,13 +47,11 @@ abstract class VeilidTableDB { } Future loadStringJson(int col, String key, - {Object? Function(Object? key, Object? value)? reviver}) { - return loadJson(col, utf8.encoder.convert(key), reviver: reviver); - } + {Object? Function(Object? key, Object? value)? reviver}) => loadJson(col, utf8.encoder.convert(key), reviver: reviver); Future deleteJson(int col, Uint8List key, {Object? Function(Object? key, Object? value)? reviver}) async { - var s = await delete(col, key); + final s = await delete(col, key); if (s == null) { return null; } @@ -69,7 +59,5 @@ abstract class VeilidTableDB { } Future deleteStringJson(int col, String key, - {Object? Function(Object? key, Object? value)? reviver}) { - return deleteJson(col, utf8.encoder.convert(key), reviver: reviver); - } + {Object? Function(Object? key, Object? value)? reviver}) => deleteJson(col, utf8.encoder.convert(key), reviver: reviver); } diff --git a/veilid-flutter/pubspec.yaml b/veilid-flutter/pubspec.yaml index d4638878..ec1052ac 100644 --- a/veilid-flutter/pubspec.yaml +++ b/veilid-flutter/pubspec.yaml @@ -8,28 +8,28 @@ environment: sdk: '>=3.0.0 <4.0.0' dependencies: + change_case: ^1.0.1 + charcode: ^1.3.1 + equatable: ^2.0.5 + ffi: ^2.0.0 flutter: sdk: flutter flutter_web_plugins: sdk: flutter - ffi: ^2.0.0 - change_case: ^1.0.1 + freezed_annotation: ^2.2.0 + json_annotation: ^4.8.1 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 - equatable: ^2.0.5 dev_dependencies: + build_runner: ^2.4.6 flutter_test: sdk: flutter - flutter_lints: ^2.0.1 - build_runner: ^2.4.6 freezed: ^2.3.5 json_serializable: ^6.7.1 + lint_hard: ^4.0.0 # The following section is specific to Flutter. flutter: diff --git a/veilid-flutter/test/veilid_test.dart b/veilid-flutter/test/veilid_test.dart index 82a21982..8e419017 100644 --- a/veilid-flutter/test/veilid_test.dart +++ b/veilid-flutter/test/veilid_test.dart @@ -2,7 +2,7 @@ import 'package:flutter_test/flutter_test.dart'; import 'package:veilid/veilid.dart'; void main() { - Veilid api = Veilid.instance; + final api = Veilid.instance; TestWidgetsFlutterBinding.ensureInitialized(); From d49c631fac1e4a5c73a4a320d185dadfb05a8f24 Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Wed, 26 Jul 2023 15:12:28 -0400 Subject: [PATCH 27/42] lint cleanup --- veilid-flutter/lib/veilid.dart | 17 +- veilid-flutter/lib/veilid_api_exception.dart | 44 +-- veilid-flutter/lib/veilid_js.dart | 381 +++++++++++-------- 3 files changed, 243 insertions(+), 199 deletions(-) diff --git a/veilid-flutter/lib/veilid.dart b/veilid-flutter/lib/veilid.dart index 296f976b..b65ac68d 100644 --- a/veilid-flutter/lib/veilid.dart +++ b/veilid-flutter/lib/veilid.dart @@ -39,26 +39,15 @@ Object? veilidApiToEncodable(Object? value) { throw UnsupportedError('Cannot convert to JSON: $value'); } -T? Function(dynamic) optFromJson( - T Function(Map) jsonConstructor) => (dynamic j) { - if (j == null) { - return null; - } else { - return jsonConstructor(j); - } - }; - List Function(dynamic) jsonListConstructor( - T Function(Map) jsonConstructor) => (dynamic j) => (j as List>) - .map((e) => jsonConstructor(e)) - .toList(); + T Function(dynamic) jsonConstructor) => + (dynamic j) => (j as List).map((e) => jsonConstructor(e)).toList(); ////////////////////////////////////// /// VeilidVersion @immutable class VeilidVersion extends Equatable { - const VeilidVersion(this.major, this.minor, this.patch); final int major; final int minor; @@ -71,7 +60,6 @@ class VeilidVersion extends Equatable { /// Timestamp @immutable class Timestamp extends Equatable { - const Timestamp({required this.value}); factory Timestamp.fromString(String s) => Timestamp(value: BigInt.parse(s)); factory Timestamp.fromJson(dynamic json) => @@ -94,7 +82,6 @@ class Timestamp extends Equatable { @immutable class TimestampDuration extends Equatable { - const TimestampDuration({required this.value}); factory TimestampDuration.fromString(String s) => TimestampDuration(value: BigInt.parse(s)); diff --git a/veilid-flutter/lib/veilid_api_exception.dart b/veilid-flutter/lib/veilid_api_exception.dart index bc0deb7d..b61886d7 100644 --- a/veilid-flutter/lib/veilid_api_exception.dart +++ b/veilid-flutter/lib/veilid_api_exception.dart @@ -5,8 +5,9 @@ import 'package:freezed_annotation/freezed_annotation.dart'; @immutable abstract class VeilidAPIException implements Exception { - factory VeilidAPIException.fromJson(dynamic json) { - switch (json['kind']) { + factory VeilidAPIException.fromJson(dynamic j) { + final json = j as Map; + switch (json['kind']! as String) { case 'NotInitialized': { return VeilidAPIExceptionNotInitialized(); @@ -33,42 +34,44 @@ abstract class VeilidAPIException implements Exception { } case 'NoConnection': { - return VeilidAPIExceptionNoConnection(json['message']); + return VeilidAPIExceptionNoConnection(json['message']! as String); } case 'KeyNotFound': { - return VeilidAPIExceptionKeyNotFound(json['key']); + return VeilidAPIExceptionKeyNotFound(json['key']! as String); } case 'Internal': { - return VeilidAPIExceptionInternal(json['message']); + return VeilidAPIExceptionInternal(json['message']! as String); } case 'Unimplemented': { - return VeilidAPIExceptionUnimplemented(json['unimplemented']); + return VeilidAPIExceptionUnimplemented( + json['unimplemented']! as String); } case 'ParseError': { - return VeilidAPIExceptionParseError(json['message'], json['value']); + return VeilidAPIExceptionParseError( + json['message']! as String, json['value']! as String); } case 'InvalidArgument': { - return VeilidAPIExceptionInvalidArgument( - json['context'], json['argument'], json['value']); + return VeilidAPIExceptionInvalidArgument(json['context']! as String, + json['argument']! as String, json['value']! as String); } case 'MissingArgument': { return VeilidAPIExceptionMissingArgument( - json['context'], json['argument']); + json['context']! as String, json['argument']! as String); } case 'Generic': { - return VeilidAPIExceptionGeneric(json['message']); + return VeilidAPIExceptionGeneric(json['message']! as String); } default: { throw VeilidAPIExceptionInternal( - "Invalid VeilidAPIException type: ${json['kind']}"); + "Invalid VeilidAPIException type: ${json['kind']! as String}"); } } } @@ -132,7 +135,6 @@ class VeilidAPIExceptionInvalidTarget implements VeilidAPIException { @immutable class VeilidAPIExceptionNoConnection implements VeilidAPIException { - // const VeilidAPIExceptionNoConnection(this.message); final String message; @@ -145,7 +147,6 @@ class VeilidAPIExceptionNoConnection implements VeilidAPIException { @immutable class VeilidAPIExceptionKeyNotFound implements VeilidAPIException { - // const VeilidAPIExceptionKeyNotFound(this.key); final String key; @@ -158,7 +159,6 @@ class VeilidAPIExceptionKeyNotFound implements VeilidAPIException { @immutable class VeilidAPIExceptionInternal implements VeilidAPIException { - // const VeilidAPIExceptionInternal(this.message); final String message; @@ -172,7 +172,6 @@ class VeilidAPIExceptionInternal implements VeilidAPIException { @immutable class VeilidAPIExceptionUnimplemented implements VeilidAPIException { - // const VeilidAPIExceptionUnimplemented(this.message); final String message; @@ -186,14 +185,14 @@ class VeilidAPIExceptionUnimplemented implements VeilidAPIException { @immutable class VeilidAPIExceptionParseError implements VeilidAPIException { - // const VeilidAPIExceptionParseError(this.message, this.value); final String message; final String value; @override - String toString() => 'VeilidAPIException: ParseError ($message)\n value: $value'; + String toString() => + 'VeilidAPIException: ParseError ($message)\n value: $value'; @override String toDisplayError() => 'Parse error: $message'; @@ -201,7 +200,6 @@ class VeilidAPIExceptionParseError implements VeilidAPIException { @immutable class VeilidAPIExceptionInvalidArgument implements VeilidAPIException { - // const VeilidAPIExceptionInvalidArgument( this.context, this.argument, this.value); @@ -210,7 +208,8 @@ class VeilidAPIExceptionInvalidArgument implements VeilidAPIException { final String value; @override - String toString() => 'VeilidAPIException: InvalidArgument ($context:$argument)\n value: $value'; + String toString() => + 'VeilidAPIException: InvalidArgument ($context:$argument)\n value: $value'; @override String toDisplayError() => 'Invalid argument for $context: $argument'; @@ -218,14 +217,14 @@ class VeilidAPIExceptionInvalidArgument implements VeilidAPIException { @immutable class VeilidAPIExceptionMissingArgument implements VeilidAPIException { - // const VeilidAPIExceptionMissingArgument(this.context, this.argument); final String context; final String argument; @override - String toString() => 'VeilidAPIException: MissingArgument ($context:$argument)'; + String toString() => + 'VeilidAPIException: MissingArgument ($context:$argument)'; @override String toDisplayError() => 'Missing argument for $context: $argument'; @@ -233,7 +232,6 @@ class VeilidAPIExceptionMissingArgument implements VeilidAPIException { @immutable class VeilidAPIExceptionGeneric implements VeilidAPIException { - // const VeilidAPIExceptionGeneric(this.message); final String message; diff --git a/veilid-flutter/lib/veilid_js.dart b/veilid-flutter/lib/veilid_js.dart index 17ff2161..c50c8a2d 100644 --- a/veilid-flutter/lib/veilid_js.dart +++ b/veilid-flutter/lib/veilid_js.dart @@ -13,9 +13,12 @@ Veilid getVeilid() => VeilidJS(); Object wasm = js_util.getProperty(html.window, 'veilid_wasm'); -Future _wrapApiPromise(Object p) => js_util.promiseToFuture(p).then((value) => value as T).catchError( - (error) => Future.error( - VeilidAPIException.fromJson(jsonDecode(error as String)))); +Future _wrapApiPromise(Object p) => js_util + .promiseToFuture(p) + .then((value) => value) + // ignore: inference_failure_on_untyped_parameter + .catchError((error) => Future.error( + VeilidAPIException.fromJson(jsonDecode(error as String)))); class _Ctx { _Ctx(int this.id, this.js); @@ -29,7 +32,7 @@ class _Ctx { void close() { if (id != null) { - js_util.callMethod(wasm, 'release_routing_context', [id!]); + js_util.callMethod(wasm, 'release_routing_context', [id]); id = null; } } @@ -37,7 +40,6 @@ class _Ctx { // JS implementation of VeilidRoutingContext class VeilidRoutingContextJS extends VeilidRoutingContext { - VeilidRoutingContextJS._(this._ctx) { _finalizer.attach(this, _ctx, detach: this); } @@ -60,10 +62,11 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override VeilidRoutingContextJS withCustomPrivacy(SafetySelection safetySelection) { _ctx.ensureValid(); - final newId = js_util.callMethod( + final id = _ctx.id!; + final newId = js_util.callMethod( wasm, 'routing_context_with_custom_privacy', - [_ctx.id!, jsonEncode(safetySelection)]); + [id, jsonEncode(safetySelection)]); return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js)); } @@ -71,45 +74,50 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override VeilidRoutingContextJS withSequencing(Sequencing sequencing) { _ctx.ensureValid(); - final newId = js_util.callMethod(wasm, 'routing_context_with_sequencing', - [_ctx.id!, jsonEncode(sequencing)]); + final id = _ctx.id!; + final newId = js_util.callMethod( + wasm, 'routing_context_with_sequencing', [id, jsonEncode(sequencing)]); return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js)); } @override Future appCall(String target, Uint8List request) async { _ctx.ensureValid(); + final id = _ctx.id!; final encodedRequest = base64UrlNoPadEncode(request); return base64UrlNoPadDecode(await _wrapApiPromise(js_util.callMethod( - wasm, 'routing_context_app_call', [_ctx.id!, target, encodedRequest]))); + wasm, 'routing_context_app_call', [id, target, encodedRequest]))); } @override Future appMessage(String target, Uint8List message) { _ctx.ensureValid(); + final id = _ctx.id!; final encodedMessage = base64UrlNoPadEncode(message); - return _wrapApiPromise(js_util.callMethod(wasm, - 'routing_context_app_message', [_ctx.id!, target, encodedMessage])); + return _wrapApiPromise(js_util.callMethod( + wasm, 'routing_context_app_message', [id, target, encodedMessage])); } @override Future createDHTRecord(DHTSchema schema, {CryptoKind kind = 0}) async { _ctx.ensureValid(); + final id = _ctx.id!; return DHTRecordDescriptor.fromJson(jsonDecode(await _wrapApiPromise(js_util .callMethod(wasm, 'routing_context_create_dht_record', - [_ctx.id!, jsonEncode(schema), kind])))); + [id, jsonEncode(schema), kind])))); } @override Future openDHTRecord( TypedKey key, KeyPair? writer) async { _ctx.ensureValid(); + final id = _ctx.id!; return DHTRecordDescriptor.fromJson(jsonDecode(await _wrapApiPromise(js_util .callMethod(wasm, 'routing_context_open_dht_record', [ - _ctx.id!, + id, jsonEncode(key), if (writer != null) jsonEncode(writer) else null ])))); @@ -118,25 +126,28 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override Future closeDHTRecord(TypedKey key) { _ctx.ensureValid(); + final id = _ctx.id!; return _wrapApiPromise(js_util.callMethod( - wasm, 'routing_context_close_dht_record', [_ctx.id!, jsonEncode(key)])); + wasm, 'routing_context_close_dht_record', [id, jsonEncode(key)])); } @override Future deleteDHTRecord(TypedKey key) { _ctx.ensureValid(); - return _wrapApiPromise(js_util.callMethod(wasm, - 'routing_context_delete_dht_record', [_ctx.id!, jsonEncode(key)])); + final id = _ctx.id!; + return _wrapApiPromise(js_util.callMethod( + wasm, 'routing_context_delete_dht_record', [id, jsonEncode(key)])); } @override Future getDHTValue( TypedKey key, int subkey, bool forceRefresh) async { _ctx.ensureValid(); - final opt = await _wrapApiPromise(js_util.callMethod( + final id = _ctx.id!; + final opt = await _wrapApiPromise(js_util.callMethod( wasm, 'routing_context_get_dht_value', - [_ctx.id!, jsonEncode(key), subkey, forceRefresh])); + [id, jsonEncode(key), subkey, forceRefresh])); return opt == null ? null : ValueData.fromJson(jsonDecode(opt)); } @@ -144,10 +155,11 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { Future setDHTValue( TypedKey key, int subkey, Uint8List data) async { _ctx.ensureValid(); - final opt = await _wrapApiPromise(js_util.callMethod( + final id = _ctx.id!; + final opt = await _wrapApiPromise(js_util.callMethod( wasm, 'routing_context_set_dht_value', - [_ctx.id!, jsonEncode(key), subkey, base64UrlNoPadEncode(data)])); + [id, jsonEncode(key), subkey, base64UrlNoPadEncode(data)])); return opt == null ? null : ValueData.fromJson(jsonDecode(opt)); } @@ -155,9 +167,10 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { Future watchDHTValues(TypedKey key, List subkeys, Timestamp expiration, int count) async { _ctx.ensureValid(); - final ts = await _wrapApiPromise(js_util.callMethod( + final id = _ctx.id!; + final ts = await _wrapApiPromise(js_util.callMethod( wasm, 'routing_context_watch_dht_values', [ - _ctx.id!, + id, jsonEncode(key), jsonEncode(subkeys), expiration.toString(), @@ -169,16 +182,16 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override Future cancelDHTWatch(TypedKey key, List subkeys) { _ctx.ensureValid(); + final id = _ctx.id!; return _wrapApiPromise(js_util.callMethod( wasm, 'routing_context_cancel_dht_watch', - [_ctx.id!, jsonEncode(key), jsonEncode(subkeys)])); + [id, jsonEncode(key), jsonEncode(subkeys)])); } } // JS implementation of VeilidCryptoSystem class VeilidCryptoSystemJS extends VeilidCryptoSystem { - VeilidCryptoSystemJS._(this._js, this._kind) { // Keep the reference _js; @@ -190,127 +203,151 @@ class VeilidCryptoSystemJS extends VeilidCryptoSystem { CryptoKind kind() => _kind; @override - Future cachedDH(PublicKey key, SecretKey secret) async => SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(js_util - .callMethod(wasm, 'crypto_cached_dh', - [_kind, jsonEncode(key), jsonEncode(secret)])))); + Future cachedDH(PublicKey key, SecretKey secret) async => + SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(js_util.callMethod( + wasm, + 'crypto_cached_dh', + [_kind, jsonEncode(key), jsonEncode(secret)])))); @override - Future computeDH(PublicKey key, SecretKey secret) async => SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(js_util - .callMethod(wasm, 'crypto_compute_dh', - [_kind, jsonEncode(key), jsonEncode(secret)])))); + Future computeDH(PublicKey key, SecretKey secret) async => + SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(js_util.callMethod( + wasm, + 'crypto_compute_dh', + [_kind, jsonEncode(key), jsonEncode(secret)])))); @override - Future randomBytes(int len) async => base64UrlNoPadDecode(await _wrapApiPromise( - js_util.callMethod(wasm, 'crypto_random_bytes', [_kind, len]))); + Future randomBytes(int len) async => + base64UrlNoPadDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'crypto_random_bytes', [_kind, len]))); @override Future defaultSaltLength() => _wrapApiPromise( - js_util.callMethod(wasm, 'crypto_default_salt_length', [_kind])); + js_util.callMethod(wasm, 'crypto_default_salt_length', [_kind])); @override - Future hashPassword(Uint8List password, Uint8List salt) => _wrapApiPromise(js_util.callMethod(wasm, 'crypto_hash_password', - [_kind, base64UrlNoPadEncode(password), base64UrlNoPadEncode(salt)])); + Future hashPassword(Uint8List password, Uint8List salt) => + _wrapApiPromise(js_util.callMethod(wasm, 'crypto_hash_password', + [_kind, base64UrlNoPadEncode(password), base64UrlNoPadEncode(salt)])); @override - Future verifyPassword(Uint8List password, String passwordHash) => _wrapApiPromise(js_util.callMethod(wasm, 'crypto_verify_password', - [_kind, base64UrlNoPadEncode(password), passwordHash])); + Future verifyPassword(Uint8List password, String passwordHash) => + _wrapApiPromise(js_util.callMethod(wasm, 'crypto_verify_password', + [_kind, base64UrlNoPadEncode(password), passwordHash])); @override Future deriveSharedSecret( - Uint8List password, Uint8List salt) async => SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(js_util - .callMethod(wasm, 'crypto_derive_shared_secret', [ - _kind, - base64UrlNoPadEncode(password), - base64UrlNoPadEncode(salt) - ])))); + Uint8List password, Uint8List salt) async => + SharedSecret.fromJson(jsonDecode(await _wrapApiPromise(js_util.callMethod( + wasm, 'crypto_derive_shared_secret', [ + _kind, + base64UrlNoPadEncode(password), + base64UrlNoPadEncode(salt) + ])))); @override - Future randomNonce() async => Nonce.fromJson(jsonDecode(await _wrapApiPromise( - js_util.callMethod(wasm, 'crypto_random_nonce', [_kind])))); + Future randomNonce() async => + Nonce.fromJson(jsonDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'crypto_random_nonce', [_kind])))); @override - Future randomSharedSecret() async => SharedSecret.fromJson(jsonDecode(await _wrapApiPromise( - js_util.callMethod(wasm, 'crypto_random_shared_secret', [_kind])))); + Future randomSharedSecret() async => + SharedSecret.fromJson(jsonDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'crypto_random_shared_secret', [_kind])))); @override - Future generateKeyPair() async => KeyPair.fromJson(jsonDecode(await _wrapApiPromise( - js_util.callMethod(wasm, 'crypto_generate_key_pair', [_kind])))); + Future generateKeyPair() async => + KeyPair.fromJson(jsonDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'crypto_generate_key_pair', [_kind])))); @override - Future generateHash(Uint8List data) async => HashDigest.fromJson(jsonDecode(await _wrapApiPromise(js_util - .callMethod(wasm, 'crypto_generate_hash', - [_kind, base64UrlNoPadEncode(data)])))); + Future generateHash(Uint8List data) async => + HashDigest.fromJson(jsonDecode(await _wrapApiPromise(js_util.callMethod( + wasm, 'crypto_generate_hash', [_kind, base64UrlNoPadEncode(data)])))); @override - Future validateKeyPair(PublicKey key, SecretKey secret) => _wrapApiPromise(js_util.callMethod(wasm, 'crypto_validate_key_pair', - [_kind, jsonEncode(key), jsonEncode(secret)])); + Future validateKeyPair(PublicKey key, SecretKey secret) => + _wrapApiPromise(js_util.callMethod(wasm, 'crypto_validate_key_pair', + [_kind, jsonEncode(key), jsonEncode(secret)])); @override - Future validateHash(Uint8List data, HashDigest hash) => _wrapApiPromise(js_util.callMethod(wasm, 'crypto_validate_hash', - [_kind, base64UrlNoPadEncode(data), jsonEncode(hash)])); + Future validateHash(Uint8List data, HashDigest hash) => + _wrapApiPromise(js_util.callMethod(wasm, 'crypto_validate_hash', + [_kind, base64UrlNoPadEncode(data), jsonEncode(hash)])); @override - Future distance(CryptoKey key1, CryptoKey key2) async => CryptoKeyDistance.fromJson(jsonDecode(await _wrapApiPromise(js_util - .callMethod(wasm, 'crypto_distance', - [_kind, jsonEncode(key1), jsonEncode(key2)])))); + Future distance(CryptoKey key1, CryptoKey key2) async => + CryptoKeyDistance.fromJson(jsonDecode(await _wrapApiPromise(js_util + .callMethod(wasm, 'crypto_distance', + [_kind, jsonEncode(key1), jsonEncode(key2)])))); @override Future sign( - PublicKey key, SecretKey secret, Uint8List data) async => Signature.fromJson(jsonDecode(await _wrapApiPromise(js_util - .callMethod(wasm, 'crypto_sign', [ - _kind, - jsonEncode(key), - jsonEncode(secret), - base64UrlNoPadEncode(data) - ])))); + PublicKey key, SecretKey secret, Uint8List data) async => + Signature.fromJson(jsonDecode(await _wrapApiPromise(js_util.callMethod( + wasm, 'crypto_sign', [ + _kind, + jsonEncode(key), + jsonEncode(secret), + base64UrlNoPadEncode(data) + ])))); @override - Future verify(PublicKey key, Uint8List data, Signature signature) => _wrapApiPromise(js_util.callMethod(wasm, 'crypto_verify', [ - _kind, - jsonEncode(key), - base64UrlNoPadEncode(data), - jsonEncode(signature), - ])); + Future verify(PublicKey key, Uint8List data, Signature signature) => + _wrapApiPromise(js_util.callMethod(wasm, 'crypto_verify', [ + _kind, + jsonEncode(key), + base64UrlNoPadEncode(data), + jsonEncode(signature), + ])); @override Future aeadOverhead() => _wrapApiPromise( - js_util.callMethod(wasm, 'crypto_aead_overhead', [_kind])); + js_util.callMethod(wasm, 'crypto_aead_overhead', [_kind])); @override Future decryptAead(Uint8List body, Nonce nonce, - SharedSecret sharedSecret, Uint8List? associatedData) async => base64UrlNoPadDecode( - await _wrapApiPromise(js_util.callMethod(wasm, 'crypto_decrypt_aead', [ - _kind, - base64UrlNoPadEncode(body), - jsonEncode(nonce), - jsonEncode(sharedSecret), - if (associatedData != null) base64UrlNoPadEncode(associatedData) else null - ]))); + SharedSecret sharedSecret, Uint8List? associatedData) async => + base64UrlNoPadDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'crypto_decrypt_aead', [ + _kind, + base64UrlNoPadEncode(body), + jsonEncode(nonce), + jsonEncode(sharedSecret), + if (associatedData != null) + base64UrlNoPadEncode(associatedData) + else + null + ]))); @override Future encryptAead(Uint8List body, Nonce nonce, - SharedSecret sharedSecret, Uint8List? associatedData) async => base64UrlNoPadDecode( - await _wrapApiPromise(js_util.callMethod(wasm, 'crypto_encrypt_aead', [ - _kind, - base64UrlNoPadEncode(body), - jsonEncode(nonce), - jsonEncode(sharedSecret), - if (associatedData != null) base64UrlNoPadEncode(associatedData) else null - ]))); + SharedSecret sharedSecret, Uint8List? associatedData) async => + base64UrlNoPadDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'crypto_encrypt_aead', [ + _kind, + base64UrlNoPadEncode(body), + jsonEncode(nonce), + jsonEncode(sharedSecret), + if (associatedData != null) + base64UrlNoPadEncode(associatedData) + else + null + ]))); @override Future cryptNoAuth( - Uint8List body, Nonce nonce, SharedSecret sharedSecret) async => base64UrlNoPadDecode(await _wrapApiPromise(js_util.callMethod( - wasm, 'crypto_crypt_no_auth', [ - _kind, - base64UrlNoPadEncode(body), - jsonEncode(nonce), - jsonEncode(sharedSecret) - ]))); + Uint8List body, Nonce nonce, SharedSecret sharedSecret) async => + base64UrlNoPadDecode(await _wrapApiPromise(js_util.callMethod( + wasm, 'crypto_crypt_no_auth', [ + _kind, + base64UrlNoPadEncode(body), + jsonEncode(nonce), + jsonEncode(sharedSecret) + ]))); } class _TDBT { - _TDBT(this.id, this.tdbjs, this.js); int? id; final VeilidTableDBJS tdbjs; @@ -323,7 +360,7 @@ class _TDBT { void close() { if (id != null) { - js_util.callMethod(wasm, 'release_table_db_transaction', [id!]); + js_util.callMethod(wasm, 'release_table_db_transaction', [id]); id = null; } } @@ -331,7 +368,6 @@ class _TDBT { // JS implementation of VeilidTableDBTransaction class VeilidTableDBTransactionJS extends VeilidTableDBTransaction { - VeilidTableDBTransactionJS._(this._tdbt) { _finalizer.attach(this, _tdbt, detach: this); } @@ -344,41 +380,44 @@ class VeilidTableDBTransactionJS extends VeilidTableDBTransaction { @override Future commit() async { _tdbt.ensureValid(); - await _wrapApiPromise( - js_util.callMethod(wasm, 'table_db_transaction_commit', [_tdbt.id!])); + final id = _tdbt.id!; + await _wrapApiPromise( + js_util.callMethod(wasm, 'table_db_transaction_commit', [id])); _tdbt.close(); } @override Future rollback() async { _tdbt.ensureValid(); - await _wrapApiPromise( - js_util.callMethod(wasm, 'table_db_transaction_rollback', [_tdbt.id!])); + final id = _tdbt.id!; + await _wrapApiPromise( + js_util.callMethod(wasm, 'table_db_transaction_rollback', [id])); _tdbt.close(); } @override Future store(int col, Uint8List key, Uint8List value) async { _tdbt.ensureValid(); + final id = _tdbt.id!; final encodedKey = base64UrlNoPadEncode(key); final encodedValue = base64UrlNoPadEncode(value); - await _wrapApiPromise(js_util.callMethod(wasm, 'table_db_transaction_store', - [_tdbt.id!, col, encodedKey, encodedValue])); + await _wrapApiPromise(js_util.callMethod(wasm, + 'table_db_transaction_store', [id, col, encodedKey, encodedValue])); } @override Future delete(int col, Uint8List key) async { _tdbt.ensureValid(); + final id = _tdbt.id!; final encodedKey = base64UrlNoPadEncode(key); - await _wrapApiPromise(js_util.callMethod( - wasm, 'table_db_transaction_delete', [_tdbt.id!, col, encodedKey])); + await _wrapApiPromise(js_util.callMethod( + wasm, 'table_db_transaction_delete', [id, col, encodedKey])); } } class _TDB { - _TDB(int this.id, this.js); int? id; final VeilidJS js; @@ -390,7 +429,7 @@ class _TDB { void close() { if (id != null) { - js_util.callMethod(wasm, 'release_table_db', [id!]); + js_util.callMethod(wasm, 'release_table_db', [id]); id = null; } } @@ -398,7 +437,6 @@ class _TDB { // JS implementation of VeilidTableDB class VeilidTableDBJS extends VeilidTableDB { - VeilidTableDBJS._(this._tdb) { _finalizer.attach(this, _tdb, detach: this); } @@ -419,35 +457,39 @@ class VeilidTableDBJS extends VeilidTableDB { @override Future> getKeys(int col) async { _tdb.ensureValid(); + final id = _tdb.id!; return jsonListConstructor(base64UrlNoPadDecodeDynamic)(jsonDecode( - await js_util.callMethod(wasm, 'table_db_get_keys', [_tdb.id!, col]))); + await js_util.callMethod(wasm, 'table_db_get_keys', [id, col]))); } @override VeilidTableDBTransaction transact() { _tdb.ensureValid(); - final id = js_util.callMethod(wasm, 'table_db_transact', [_tdb.id!]); + final id = _tdb.id!; + final xid = js_util.callMethod(wasm, 'table_db_transact', [id]); - return VeilidTableDBTransactionJS._(_TDBT(id, this, _tdb.js)); + return VeilidTableDBTransactionJS._(_TDBT(xid, this, _tdb.js)); } @override Future store(int col, Uint8List key, Uint8List value) { _tdb.ensureValid(); + final id = _tdb.id!; final encodedKey = base64UrlNoPadEncode(key); final encodedValue = base64UrlNoPadEncode(value); return _wrapApiPromise(js_util.callMethod( - wasm, 'table_db_store', [_tdb.id!, col, encodedKey, encodedValue])); + wasm, 'table_db_store', [id, col, encodedKey, encodedValue])); } @override Future load(int col, Uint8List key) async { _tdb.ensureValid(); + final id = _tdb.id!; final encodedKey = base64UrlNoPadEncode(key); - final out = await _wrapApiPromise( - js_util.callMethod(wasm, 'table_db_load', [_tdb.id!, col, encodedKey])); + final out = await _wrapApiPromise( + js_util.callMethod(wasm, 'table_db_load', [id, col, encodedKey])); if (out == null) { return null; } @@ -455,12 +497,17 @@ class VeilidTableDBJS extends VeilidTableDB { } @override - Future delete(int col, Uint8List key) { + Future delete(int col, Uint8List key) async { _tdb.ensureValid(); + final id = _tdb.id!; final encodedKey = base64UrlNoPadEncode(key); - return _wrapApiPromise(js_util - .callMethod(wasm, 'table_db_delete', [_tdb.id!, col, encodedKey])); + final out = await _wrapApiPromise( + js_util.callMethod(wasm, 'table_db_delete', [id, col, encodedKey])); + if (out == null) { + return null; + } + return base64UrlNoPadDecode(out); } } @@ -470,48 +517,52 @@ class VeilidJS extends Veilid { @override void initializeVeilidCore(Map platformConfigJson) { final platformConfigJsonString = jsonEncode(platformConfigJson); - js_util - .callMethod(wasm, 'initialize_veilid_core', [platformConfigJsonString]); + js_util.callMethod( + wasm, 'initialize_veilid_core', [platformConfigJsonString]); } @override void changeLogLevel(String layer, VeilidConfigLogLevel logLevel) { final logLevelJsonString = jsonEncode(logLevel); - js_util.callMethod(wasm, 'change_log_level', [layer, logLevelJsonString]); + js_util.callMethod( + wasm, 'change_log_level', [layer, logLevelJsonString]); } @override Future> startupVeilidCore(VeilidConfig config) async { final streamController = StreamController(); - updateCallback(String update) { - final updateJson = jsonDecode(update); + void updateCallback(String update) { + final updateJson = jsonDecode(update) as Map; if (updateJson['kind'] == 'Shutdown') { - streamController.close(); + unawaited(streamController.close()); } else { final update = VeilidUpdate.fromJson(updateJson); streamController.add(update); } } - await _wrapApiPromise(js_util.callMethod(wasm, 'startup_veilid_core', + await _wrapApiPromise(js_util.callMethod(wasm, 'startup_veilid_core', [js.allowInterop(updateCallback), jsonEncode(config)])); return streamController.stream; } @override - Future getVeilidState() async => VeilidState.fromJson(jsonDecode(await _wrapApiPromise( - js_util.callMethod(wasm, 'get_veilid_state', [])))); + Future getVeilidState() async => + VeilidState.fromJson(jsonDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'get_veilid_state', [])))); @override - Future attach() => _wrapApiPromise(js_util.callMethod(wasm, 'attach', [])); + Future attach() => + _wrapApiPromise(js_util.callMethod(wasm, 'attach', [])); @override - Future detach() => _wrapApiPromise(js_util.callMethod(wasm, 'detach', [])); + Future detach() => + _wrapApiPromise(js_util.callMethod(wasm, 'detach', [])); @override - Future shutdownVeilidCore() => _wrapApiPromise( - js_util.callMethod(wasm, 'shutdown_veilid_core', [])); + Future shutdownVeilidCore() => + _wrapApiPromise(js_util.callMethod(wasm, 'shutdown_veilid_core', [])); @override List validCryptoKinds() { @@ -530,37 +581,41 @@ class VeilidJS extends Veilid { @override Future bestCryptoSystem() async => VeilidCryptoSystemJS._( - this, js_util.callMethod(wasm, 'best_crypto_kind', [])); + this, js_util.callMethod(wasm, 'best_crypto_kind', [])); @override Future> verifySignatures(List nodeIds, - Uint8List data, List signatures) async => jsonListConstructor(TypedKey.fromJson)(jsonDecode( - await _wrapApiPromise(js_util.callMethod(wasm, 'verify_signatures', [ - jsonEncode(nodeIds), - base64UrlNoPadEncode(data), - jsonEncode(signatures) - ])))); + Uint8List data, List signatures) async => + jsonListConstructor(TypedKey.fromJson)(jsonDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'verify_signatures', [ + jsonEncode(nodeIds), + base64UrlNoPadEncode(data), + jsonEncode(signatures) + ])))); @override Future> generateSignatures( - Uint8List data, List keyPairs) async => jsonListConstructor(TypedSignature.fromJson)(jsonDecode( - await _wrapApiPromise(js_util.callMethod(wasm, 'generate_signatures', - [base64UrlNoPadEncode(data), jsonEncode(keyPairs)])))); + Uint8List data, List keyPairs) async => + jsonListConstructor(TypedSignature.fromJson)(jsonDecode( + await _wrapApiPromise(js_util.callMethod(wasm, 'generate_signatures', + [base64UrlNoPadEncode(data), jsonEncode(keyPairs)])))); @override - Future generateKeyPair(CryptoKind kind) async => TypedKeyPair.fromJson(jsonDecode(await _wrapApiPromise( - js_util.callMethod(wasm, 'generate_key_pair', [kind])))); + Future generateKeyPair(CryptoKind kind) async => + TypedKeyPair.fromJson(jsonDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'generate_key_pair', [kind])))); @override Future routingContext() async { - final var id = - await _wrapApiPromise(js_util.callMethod(wasm, 'routing_context', [])); - return VeilidRoutingContextJS._(_Ctx(id, this)); + final rcid = await _wrapApiPromise( + js_util.callMethod(wasm, 'routing_context', [])); + return VeilidRoutingContextJS._(_Ctx(rcid, this)); } @override - Future newPrivateRoute() async => RouteBlob.fromJson(jsonDecode(await _wrapApiPromise( - js_util.callMethod(wasm, 'new_private_route', [])))); + Future newPrivateRoute() async => + RouteBlob.fromJson(jsonDecode(await _wrapApiPromise( + js_util.callMethod(wasm, 'new_private_route', [])))); @override Future newCustomPrivateRoute( @@ -581,8 +636,8 @@ class VeilidJS extends Veilid { } @override - Future releasePrivateRoute(String key) => _wrapApiPromise( - js_util.callMethod(wasm, 'release_private_route', [key])); + Future releasePrivateRoute(String key) => + _wrapApiPromise(js_util.callMethod(wasm, 'release_private_route', [key])); @override Future appCallReply(String callId, Uint8List message) { @@ -593,28 +648,32 @@ class VeilidJS extends Veilid { @override Future openTableDB(String name, int columnCount) async { - final id = await _wrapApiPromise( + final dbid = await _wrapApiPromise( js_util.callMethod(wasm, 'open_table_db', [name, columnCount])); - return VeilidTableDBJS._(_TDB(id, this)); + return VeilidTableDBJS._(_TDB(dbid, this)); } @override - Future deleteTableDB(String name) => _wrapApiPromise(js_util.callMethod(wasm, 'delete_table_db', [name])); + Future deleteTableDB(String name) => + _wrapApiPromise(js_util.callMethod(wasm, 'delete_table_db', [name])); @override Timestamp now() => Timestamp.fromString(js_util.callMethod(wasm, 'now', [])); @override - Future debug(String command) async => await _wrapApiPromise(js_util.callMethod(wasm, 'debug', [command])); + Future debug(String command) async => + _wrapApiPromise(js_util.callMethod(wasm, 'debug', [command])); @override - String veilidVersionString() => js_util.callMethod(wasm, 'veilid_version_string', []); + String veilidVersionString() => + js_util.callMethod(wasm, 'veilid_version_string', []); @override VeilidVersion veilidVersion() { - Map jsonVersion = - jsonDecode(js_util.callMethod(wasm, 'veilid_version', [])); - return VeilidVersion( - jsonVersion['major'], jsonVersion['minor'], jsonVersion['patch']); + final jsonVersion = + jsonDecode(js_util.callMethod(wasm, 'veilid_version', [])) + as Map; + return VeilidVersion(jsonVersion['major'] as int, + jsonVersion['minor'] as int, jsonVersion['patch'] as int); } } From a589dbf100dcd78595a39820e343877a1bbbf508 Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Wed, 26 Jul 2023 15:30:00 -0400 Subject: [PATCH 28/42] lint cleanup --- veilid-flutter/lib/veilid_ffi.dart | 613 +++++++++++++++-------------- veilid-flutter/lib/veilid_js.dart | 12 +- 2 files changed, 318 insertions(+), 307 deletions(-) diff --git a/veilid-flutter/lib/veilid_ffi.dart b/veilid-flutter/lib/veilid_ffi.dart index 83a1fe69..0db5b9a1 100644 --- a/veilid-flutter/lib/veilid_ffi.dart +++ b/veilid-flutter/lib/veilid_ffi.dart @@ -226,203 +226,198 @@ const int messageStreamClose = 8; Veilid getVeilid() => VeilidFFI(_dylib); // Parse handle async returns -Future processFuturePlain(Future future) async => future.then((value) { - final list = value as List; - switch (list[0] as int) { - case messageOk: - { - if (list[1] == null && null is! T) { - throw const VeilidAPIExceptionInternal( - 'Null MESSAGE_OK value on non-nullable type'); +Future processFuturePlain(Future future) async => + future.then((value) { + final list = value as List; + switch (list[0] as int) { + case messageOk: + { + if (list[1] == null && null is! T) { + throw const VeilidAPIExceptionInternal( + 'Null MESSAGE_OK value on non-nullable type'); + } + return list[1] as T; } - return list[1] as T; - } - case messageErr: - { - throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); - } - case messageErrJson: - { - throw VeilidAPIException.fromJson(jsonDecode(list[1])); - } - default: - { - throw VeilidAPIExceptionInternal( - 'Unexpected async return message type: ${list[0]}'); - } - } - }).catchError((e) { - // Wrap all other errors in VeilidAPIExceptionInternal - throw VeilidAPIExceptionInternal(e.toString()); - }, test: (e) { - // Pass errors that are already VeilidAPIException through without wrapping - return e is! VeilidAPIException; - }); + case messageErr: + { + throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); + } + case messageErrJson: + { + throw VeilidAPIException.fromJson(jsonDecode(list[1] as String)); + } + default: + { + throw VeilidAPIExceptionInternal( + 'Unexpected async return message type: ${list[0]}'); + } + } + // ignore: inference_failure_on_untyped_parameter + }).catchError((e) { + // Wrap all other errors in VeilidAPIExceptionInternal + throw VeilidAPIExceptionInternal(e.toString()); + }, test: (e) => e is! VeilidAPIException); Future processFutureJson( - T Function(dynamic) jsonConstructor, Future future) async => future.then((value) { - final list = value as List; - switch (list[0] as int) { - case messageErr: - { - throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); - } - case messageOkJson: - { - if (list[1] is! String) { - throw const VeilidAPIExceptionInternal( - 'Non-string MESSAGE_OK_JSON value'); + T Function(dynamic) jsonConstructor, Future future) async => + future.then((value) { + final list = value as List; + switch (list[0] as int) { + case messageErr: + { + throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); } - final ret = jsonDecode(list[1] as String); - if (ret == null) { - throw const VeilidAPIExceptionInternal( - 'Null JSON object on non nullable type'); + case messageOkJson: + { + if (list[1] is! String) { + throw const VeilidAPIExceptionInternal( + 'Non-string MESSAGE_OK_JSON value'); + } + final ret = jsonDecode(list[1] as String); + if (ret == null) { + throw const VeilidAPIExceptionInternal( + 'Null JSON object on non nullable type'); + } + return jsonConstructor(ret); } - return jsonConstructor(ret); - } - case messageErrJson: - { - throw VeilidAPIException.fromJson(jsonDecode(list[1])); - } - default: - { - throw VeilidAPIExceptionInternal( - 'Unexpected async return message type: ${list[0]}'); - } - } - }).catchError((e) { - // Wrap all other errors in VeilidAPIExceptionInternal - throw VeilidAPIExceptionInternal(e.toString()); - }, test: (e) { - // Pass errors that are already VeilidAPIException through without wrapping - return e is! VeilidAPIException; - }); + case messageErrJson: + { + throw VeilidAPIException.fromJson(jsonDecode(list[1] as String)); + } + default: + { + throw VeilidAPIExceptionInternal( + 'Unexpected async return message type: ${list[0]}'); + } + } + // ignore: inference_failure_on_untyped_parameter + }).catchError((e) { + // Wrap all other errors in VeilidAPIExceptionInternal + throw VeilidAPIExceptionInternal(e.toString()); + }, test: (e) => e is! VeilidAPIException); Future processFutureOptJson( - T Function(dynamic) jsonConstructor, Future future) async => future.then((value) { - final list = value as List; - switch (list[0] as int) { - case messageErr: - { - throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); - } - case messageOkJson: - { - if (list[1] == null) { - return null; + T Function(dynamic) jsonConstructor, Future future) async => + future.then((value) { + final list = value as List; + switch (list[0] as int) { + case messageErr: + { + throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); } - if (list[1] is! String) { - throw const VeilidAPIExceptionInternal( - 'Non-string MESSAGE_OK_JSON optional value'); + case messageOkJson: + { + if (list[1] == null) { + return null; + } + if (list[1] is! String) { + throw const VeilidAPIExceptionInternal( + 'Non-string MESSAGE_OK_JSON optional value'); + } + final ret = jsonDecode(list[1] as String); + if (ret == null) { + return null; + } + return jsonConstructor(ret); } - final ret = jsonDecode(list[1] as String); - if (ret == null) { - return null; + case messageErrJson: + { + throw VeilidAPIException.fromJson(jsonDecode(list[1] as String)); } - return jsonConstructor(ret); - } - case messageErrJson: - { - throw VeilidAPIException.fromJson(jsonDecode(list[1])); - } - default: - { - throw VeilidAPIExceptionInternal( - 'Unexpected async return message type: ${list[0]}'); - } - } - }).catchError((e) { - // Wrap all other errors in VeilidAPIExceptionInternal - throw VeilidAPIExceptionInternal(e.toString()); - }, test: (e) { - // Pass errors that are already VeilidAPIException through without wrapping - return e is! VeilidAPIException; - }); + default: + { + throw VeilidAPIExceptionInternal( + 'Unexpected async return message type: ${list[0]}'); + } + } + // ignore: inference_failure_on_untyped_parameter + }).catchError((e) { + // Wrap all other errors in VeilidAPIExceptionInternal + throw VeilidAPIExceptionInternal(e.toString()); + }, test: (e) => e is! VeilidAPIException); -Future processFutureVoid(Future future) async => future.then((value) { - final list = value as List; - switch (list[0] as int) { - case messageOk: - { - if (list[1] != null) { - throw VeilidAPIExceptionInternal( - "Unexpected MESSAGE_OK value '${list[1]}' where null expected"); +Future processFutureVoid(Future future) async => + future.then((value) { + final list = value as List; + switch (list[0] as int) { + case messageOk: + { + if (list[1] != null) { + throw VeilidAPIExceptionInternal( + "Unexpected MESSAGE_OK value '${list[1]}' where null expected"); + } + return; } - return; - } - case messageErr: - { - throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); - } - case messageOkJson: - { - final ret = jsonDecode(list[1] as String); - if (ret != null) { - throw VeilidAPIExceptionInternal( - "Unexpected MESSAGE_OK_JSON value '$ret' where null expected"); + case messageErr: + { + throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); } - return; - } - case messageErrJson: - { - throw VeilidAPIException.fromJson(jsonDecode(list[1] as String)); - } - default: - { - throw VeilidAPIExceptionInternal( - 'Unexpected async return message type: ${list[0]}'); - } - } - }).catchError((e) { - // Wrap all other errors in VeilidAPIExceptionInternal - throw VeilidAPIExceptionInternal(e.toString()); - }, test: (e) { - // Pass errors that are already VeilidAPIException through without wrapping - return e is! VeilidAPIException; - }); + case messageOkJson: + { + final ret = jsonDecode(list[1] as String); + if (ret != null) { + throw VeilidAPIExceptionInternal( + "Unexpected MESSAGE_OK_JSON value '$ret' where null expected"); + } + return; + } + case messageErrJson: + { + throw VeilidAPIException.fromJson(jsonDecode(list[1] as String)); + } + default: + { + throw VeilidAPIExceptionInternal( + 'Unexpected async return message type: ${list[0]}'); + } + } + // ignore: inference_failure_on_untyped_parameter + }).catchError((e) { + // Wrap all other errors in VeilidAPIExceptionInternal + throw VeilidAPIExceptionInternal(e.toString()); + }, test: (e) => e is! VeilidAPIException); Future> processFutureStream( - Stream returnStream, Future future) async => future.then((value) { - final list = value as List; - switch (list[0] as int) { - case messageOk: - { - if (list[1] != null) { - throw VeilidAPIExceptionInternal( - "Unexpected MESSAGE_OK value '${list[1]}' where null expected"); + Stream returnStream, Future future) async => + future.then((value) { + final list = value as List; + switch (list[0] as int) { + case messageOk: + { + if (list[1] != null) { + throw VeilidAPIExceptionInternal( + "Unexpected MESSAGE_OK value '${list[1]}' where null expected"); + } + return returnStream; } - return returnStream; - } - case messageErr: - { - throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); - } - case messageOkJson: - { - final ret = jsonDecode(list[1] as String); - if (ret != null) { - throw VeilidAPIExceptionInternal( - "Unexpected MESSAGE_OK_JSON value '$ret' where null expected"); + case messageErr: + { + throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); } - return returnStream; - } - case messageErrJson: - { - throw VeilidAPIException.fromJson(jsonDecode(list[1] as String)); - } - default: - { - throw VeilidAPIExceptionInternal( - 'Unexpected async return message type: ${list[0]}'); - } - } - }).catchError((e) { - // Wrap all other errors in VeilidAPIExceptionInternal - throw VeilidAPIExceptionInternal(e.toString()); - }, test: (e) { - // Pass errors that are already VeilidAPIException through without wrapping - return e is! VeilidAPIException; - }); + case messageOkJson: + { + final ret = jsonDecode(list[1] as String); + if (ret != null) { + throw VeilidAPIExceptionInternal( + "Unexpected MESSAGE_OK_JSON value '$ret' where null expected"); + } + return returnStream; + } + case messageErrJson: + { + throw VeilidAPIException.fromJson(jsonDecode(list[1] as String)); + } + default: + { + throw VeilidAPIExceptionInternal( + 'Unexpected async return message type: ${list[0]}'); + } + } + // ignore: inference_failure_on_untyped_parameter + }).catchError((e) { + // Wrap all other errors in VeilidAPIExceptionInternal + throw VeilidAPIExceptionInternal(e.toString()); + }, test: (e) => e is! VeilidAPIException); Stream processStreamJson( T Function(dynamic) jsonConstructor, ReceivePort port) async* { @@ -442,17 +437,14 @@ Stream processStreamJson( } case messageStreamAbort: { - port.close(); throw VeilidAPIExceptionInternal('Internal API Error: ${list[1]}'); } case messageStreamAbortJson: { - port.close(); - throw VeilidAPIException.fromJson(jsonDecode(list[1])); + throw VeilidAPIException.fromJson(jsonDecode(list[1] as String)); } case messageStreamClose: { - port.close(); break; } default: @@ -462,10 +454,13 @@ Stream processStreamJson( } } } - } catch (e, s) { + } on VeilidAPIException catch (_) { + rethrow; + } on Exception catch (e, s) { // Wrap all other errors in VeilidAPIExceptionInternal - throw VeilidAPIExceptionInternal( - '$e\nStack Trace:\n$s'); + throw VeilidAPIExceptionInternal('$e\nStack Trace:\n$s'); + } finally { + port.close(); } } @@ -490,7 +485,6 @@ class _Ctx { // FFI implementation of VeilidRoutingContext class VeilidRoutingContextFFI extends VeilidRoutingContext { - VeilidRoutingContextFFI._(this._ctx) { _finalizer.attach(this, _ctx, detach: this); } @@ -535,7 +529,7 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { final sendPort = recvPort.sendPort; _ctx.ffi._routingContextAppCall(sendPort.nativePort, _ctx.id!, nativeEncodedTarget, nativeEncodedRequest); - final out = await processFuturePlain(recvPort.first); + final out = await processFuturePlain(recvPort.first); return base64UrlNoPadDecode(out); } @@ -613,8 +607,8 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { final sendPort = recvPort.sendPort; _ctx.ffi._routingContextGetDHTValue( sendPort.nativePort, _ctx.id!, nativeKey, subkey, forceRefresh); - final valueData = await processFutureOptJson( - optFromJson(ValueData.fromJson), recvPort.first); + final valueData = + await processFutureOptJson(ValueData.fromJson, recvPort.first); return valueData; } @@ -629,8 +623,8 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { final sendPort = recvPort.sendPort; _ctx.ffi._routingContextSetDHTValue( sendPort.nativePort, _ctx.id!, nativeKey, subkey, nativeData); - final valueData = await processFutureOptJson( - optFromJson(ValueData.fromJson), recvPort.first); + final valueData = + await processFutureOptJson(ValueData.fromJson, recvPort.first); return valueData; } @@ -668,7 +662,6 @@ class VeilidRoutingContextFFI extends VeilidRoutingContext { } class _TDBT { - _TDBT(int this.id, this.tdbffi, this.ffi); int? id; final VeilidTableDBFFI tdbffi; @@ -689,7 +682,6 @@ class _TDBT { // FFI implementation of VeilidTableDBTransaction class VeilidTableDBTransactionFFI extends VeilidTableDBTransaction { - VeilidTableDBTransactionFFI._(this._tdbt) { _finalizer.attach(this, _tdbt, detach: this); } @@ -780,7 +772,6 @@ class _TDB { // FFI implementation of VeilidTableDB class VeilidTableDBFFI extends VeilidTableDB { - VeilidTableDBFFI._(this._tdb) { _finalizer.attach(this, _tdb, detach: this); } @@ -852,7 +843,7 @@ class VeilidTableDBFFI extends VeilidTableDB { col, nativeEncodedKey, ); - final out = await processFuturePlain(recvPort.first); + final out = await processFuturePlain(recvPort.first); if (out == null) { return null; } @@ -872,7 +863,7 @@ class VeilidTableDBFFI extends VeilidTableDB { col, nativeEncodedKey, ); - final out = await processFuturePlain(recvPort.first); + final out = await processFuturePlain(recvPort.first); if (out == null) { return null; } @@ -882,7 +873,6 @@ class VeilidTableDBFFI extends VeilidTableDB { // FFI implementation of VeilidCryptoSystem class VeilidCryptoSystemFFI extends VeilidCryptoSystem { - VeilidCryptoSystemFFI._(this._ffi, this._kind); final CryptoKind _kind; final VeilidFFI _ffi; @@ -917,7 +907,7 @@ class VeilidCryptoSystemFFI extends VeilidCryptoSystem { final recvPort = ReceivePort('crypto_random_bytes'); final sendPort = recvPort.sendPort; _ffi._cryptoRandomBytes(sendPort.nativePort, _kind, len); - final out = await processFuturePlain(recvPort.first); + final out = await processFuturePlain(recvPort.first); return base64UrlNoPadDecode(out); } @@ -1088,7 +1078,7 @@ class VeilidCryptoSystemFFI extends VeilidCryptoSystem { final sendPort = recvPort.sendPort; _ffi._cryptoDecryptAead(sendPort.nativePort, _kind, nativeEncodedBody, nativeNonce, nativeSharedSecret, nativeSignature); - final out = await processFuturePlain(recvPort.first); + final out = await processFuturePlain(recvPort.first); return base64UrlNoPadDecode(out); } @@ -1106,7 +1096,7 @@ class VeilidCryptoSystemFFI extends VeilidCryptoSystem { final sendPort = recvPort.sendPort; _ffi._cryptoEncryptAead(sendPort.nativePort, _kind, nativeEncodedBody, nativeNonce, nativeSharedSecret, nativeSignature); - final out = await processFuturePlain(recvPort.first); + final out = await processFuturePlain(recvPort.first); return base64UrlNoPadDecode(out); } @@ -1121,31 +1111,34 @@ class VeilidCryptoSystemFFI extends VeilidCryptoSystem { final sendPort = recvPort.sendPort; _ffi._cryptoCryptNoAuth(sendPort.nativePort, _kind, nativeEncodedBody, nativeNonce, nativeSharedSecret); - final out = await processFuturePlain(recvPort.first); + final out = await processFuturePlain(recvPort.first); return base64UrlNoPadDecode(out); } } // FFI implementation of high level Veilid API class VeilidFFI extends Veilid { - VeilidFFI(DynamicLibrary dylib) : _dylib = dylib, _freeString = - dylib.lookupFunction), _FreeStringDart>('free_string'), - _initializeVeilidCore = dylib.lookupFunction), + dylib.lookupFunction), _FreeStringDart>( + 'free_string'), + _initializeVeilidCore = dylib.lookupFunction< + Void Function(Pointer), _InitializeVeilidCoreDart>('initialize_veilid_core'), - _changeLogLevel = - dylib.lookupFunction, Pointer), _ChangeLogLevelDart>( - 'change_log_level'), - _startupVeilidCore = - dylib.lookupFunction), _StartupVeilidCoreDart>( - 'startup_veilid_core'), + _changeLogLevel = dylib.lookupFunction< + Void Function(Pointer, Pointer), + _ChangeLogLevelDart>('change_log_level'), + _startupVeilidCore = dylib.lookupFunction< + Void Function(Int64, Int64, Pointer), + _StartupVeilidCoreDart>('startup_veilid_core'), _getVeilidState = dylib.lookupFunction( 'get_veilid_state'), - _attach = dylib.lookupFunction('attach'), - _detach = dylib.lookupFunction('detach'), + _attach = + dylib.lookupFunction('attach'), + _detach = + dylib.lookupFunction('detach'), _shutdownVeilidCore = dylib.lookupFunction( 'shutdown_veilid_core'), @@ -1165,7 +1158,8 @@ class VeilidFFI extends Veilid { Uint32 Function(Uint32, Pointer), _RoutingContextWithSequencingDart>( 'routing_context_with_sequencing'), - _routingContextAppCall = dylib.lookupFunction, Pointer), + _routingContextAppCall = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Pointer), _RoutingContextAppCallDart>('routing_context_app_call'), _routingContextAppMessage = dylib.lookupFunction< Void Function(Int64, Uint32, Pointer, Pointer), @@ -1193,7 +1187,8 @@ class VeilidFFI extends Veilid { Void Function(Int64, Uint32, Pointer, Uint32, Pointer), _RoutingContextSetDHTValueDart>('routing_context_set_dht_value'), _routingContextWatchDHTValues = dylib.lookupFunction< - Void Function(Int64, Uint32, Pointer, Pointer, Uint64, Uint32), + Void Function(Int64, Uint32, Pointer, Pointer, + Uint64, Uint32), _RoutingContextWatchDHTValuesDart>( 'routing_context_watch_dht_values'), _routingContextCancelDHTWatch = dylib.lookupFunction< @@ -1203,35 +1198,41 @@ class VeilidFFI extends Veilid { _newPrivateRoute = dylib.lookupFunction( 'new_private_route'), - _newCustomPrivateRoute = dylib.lookupFunction, Pointer), + _newCustomPrivateRoute = dylib.lookupFunction< + Void Function(Int64, Pointer, Pointer), _NewCustomPrivateRouteDart>('new_custom_private_route'), _importRemotePrivateRoute = dylib.lookupFunction< Void Function(Int64, Pointer), _ImportRemotePrivateRouteDart>('import_remote_private_route'), - _releasePrivateRoute = dylib.lookupFunction), + _releasePrivateRoute = dylib.lookupFunction< + Void Function(Int64, Pointer), _ReleasePrivateRouteDart>('release_private_route'), - _appCallReply = dylib.lookupFunction, Pointer), _AppCallReplyDart>( - 'app_call_reply'), - _openTableDb = dylib - .lookupFunction, Uint32), _OpenTableDbDart>('open_table_db'), + _appCallReply = dylib.lookupFunction< + Void Function(Int64, Pointer, Pointer), + _AppCallReplyDart>('app_call_reply'), + _openTableDb = dylib.lookupFunction< + Void Function(Int64, Pointer, Uint32), + _OpenTableDbDart>('open_table_db'), _releaseTableDb = dylib.lookupFunction( 'release_table_db'), - _deleteTableDb = - dylib.lookupFunction), _DeleteTableDbDart>( - 'delete_table_db'), + _deleteTableDb = dylib.lookupFunction< + Void Function(Int64, Pointer), + _DeleteTableDbDart>('delete_table_db'), _tableDbGetColumnCount = dylib.lookupFunction('table_db_get_column_count'), - _tableDbGetKeys = - dylib.lookupFunction Function(Uint64, Uint32, Uint32), _TableDbGetKeysDart>( - 'table_db_get_keys'), - _tableDbStore = dylib.lookupFunction, Pointer), _TableDbStoreDart>( - 'table_db_store'), - _tableDbLoad = dylib - .lookupFunction), _TableDbLoadDart>('table_db_load'), - _tableDbDelete = - dylib.lookupFunction), _TableDbDeleteDart>( - 'table_db_delete'), + _tableDbGetKeys = dylib.lookupFunction< + Pointer Function(Uint64, Uint32, Uint32), + _TableDbGetKeysDart>('table_db_get_keys'), + _tableDbStore = dylib.lookupFunction< + Void Function(Int64, Uint32, Uint32, Pointer, Pointer), + _TableDbStoreDart>('table_db_store'), + _tableDbLoad = dylib.lookupFunction< + Void Function(Int64, Uint32, Uint32, Pointer), + _TableDbLoadDart>('table_db_load'), + _tableDbDelete = dylib.lookupFunction< + Void Function(Int64, Uint32, Uint32, Pointer), + _TableDbDeleteDart>('table_db_delete'), _tableDbTransact = dylib.lookupFunction( 'table_db_transact'), @@ -1250,83 +1251,89 @@ class VeilidFFI extends Veilid { _tableDbTransactionDelete = dylib.lookupFunction< Void Function(Int64, Uint32, Uint32, Pointer), _TableDbTransactionDeleteDart>('table_db_transaction_delete'), - _validCryptoKinds = - dylib.lookupFunction Function(), _ValidCryptoKindsDart>( - 'valid_crypto_kinds'), + _validCryptoKinds = dylib.lookupFunction Function(), + _ValidCryptoKindsDart>('valid_crypto_kinds'), _bestCryptoKind = dylib.lookupFunction( 'best_crypto_kind'), - _verifySignatures = - dylib.lookupFunction, Pointer, Pointer), _VerifySignaturesDart>( - 'verify_signatures'), - _generateSignatures = - dylib.lookupFunction, Pointer), _GenerateSignaturesDart>( - 'generate_signatures'), - _generateKeyPair = - dylib.lookupFunction( - 'generate_key_pair'), - _cryptoCachedDH = - dylib.lookupFunction, Pointer), _CryptoCachedDHDart>( - 'crypto_cached_dh'), - _cryptoComputeDH = - dylib.lookupFunction, Pointer), _CryptoComputeDHDart>( - 'crypto_compute_dh'), - _cryptoRandomBytes = - dylib.lookupFunction( - 'crypto_random_bytes'), + _verifySignatures = dylib.lookupFunction< + Void Function(Int64, Pointer, Pointer, Pointer), + _VerifySignaturesDart>('verify_signatures'), + _generateSignatures = dylib.lookupFunction< + Void Function(Int64, Pointer, Pointer), + _GenerateSignaturesDart>('generate_signatures'), + _generateKeyPair = dylib.lookupFunction('generate_key_pair'), + _cryptoCachedDH = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Pointer), + _CryptoCachedDHDart>('crypto_cached_dh'), + _cryptoComputeDH = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Pointer), + _CryptoComputeDHDart>('crypto_compute_dh'), + _cryptoRandomBytes = dylib.lookupFunction< + Void Function(Int64, Uint32, Uint32), + _CryptoRandomBytesDart>('crypto_random_bytes'), _cryptoDefaultSaltLength = dylib.lookupFunction< Void Function(Int64, Uint32), _CryptoDefaultSaltLengthDart>('crypto_default_salt_length'), - _cryptoHashPassword = - dylib.lookupFunction, Pointer), _CryptoHashPasswordDart>( - 'crypto_hash_password'), - _cryptoVerifyPassword = dylib.lookupFunction, Pointer), + _cryptoHashPassword = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Pointer), + _CryptoHashPasswordDart>('crypto_hash_password'), + _cryptoVerifyPassword = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Pointer), _CryptoVerifyPasswordDart>('crypto_verify_password'), _cryptoDeriveSharedSecret = dylib.lookupFunction< Void Function(Int64, Uint32, Pointer, Pointer), _CryptoVerifyPasswordDart>('crypto_derive_shared_secret'), - _cryptoRandomNonce = - dylib.lookupFunction( - 'crypto_random_nonce'), + _cryptoRandomNonce = dylib.lookupFunction('crypto_random_nonce'), _cryptoRandomSharedSecret = dylib.lookupFunction< Void Function(Int64, Uint32), _CryptoRandomSharedSecretDart>('crypto_random_shared_secret'), - _cryptoGenerateKeyPair = dylib.lookupFunction('crypto_generate_key_pair'), - _cryptoGenerateHash = - dylib.lookupFunction), _CryptoGenerateHashDart>( - 'crypto_generate_hash'), - _cryptoValidateKeyPair = dylib.lookupFunction, Pointer), + _cryptoGenerateHash = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer), + _CryptoGenerateHashDart>('crypto_generate_hash'), + _cryptoValidateKeyPair = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Pointer), _CryptoValidateKeyPairDart>('crypto_validate_key_pair'), - _cryptoValidateHash = - dylib.lookupFunction, Pointer), _CryptoValidateHashDart>( - 'crypto_validate_hash'), - _cryptoDistance = - dylib.lookupFunction, Pointer), _CryptoDistanceDart>( - 'crypto_distance'), - _cryptoSign = - dylib.lookupFunction, Pointer, Pointer), _CryptoSignDart>('crypto_sign'), - _cryptoVerify = dylib - .lookupFunction, Pointer, Pointer), _CryptoVerifyDart>('crypto_verify'), - _cryptoAeadOverhead = - dylib.lookupFunction( - 'crypto_aead_overhead'), - _cryptoDecryptAead = - dylib.lookupFunction, Pointer, Pointer, Pointer), _CryptoDecryptAeadDart>( - 'crypto_decrypt_aead'), - _cryptoEncryptAead = - dylib.lookupFunction, Pointer, Pointer, Pointer), _CryptoEncryptAeadDart>( - 'crypto_encrypt_aead'), - _cryptoCryptNoAuth = - dylib.lookupFunction, Pointer, Pointer), _CryptoCryptNoAuthDart>( - 'crypto_crypt_no_auth'), + _cryptoValidateHash = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Pointer), + _CryptoValidateHashDart>('crypto_validate_hash'), + _cryptoDistance = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Pointer), + _CryptoDistanceDart>('crypto_distance'), + _cryptoSign = dylib.lookupFunction< + Void Function( + Int64, Uint32, Pointer, Pointer, Pointer), + _CryptoSignDart>('crypto_sign'), + _cryptoVerify = dylib.lookupFunction< + Void Function( + Int64, Uint32, Pointer, Pointer, Pointer), + _CryptoVerifyDart>('crypto_verify'), + _cryptoAeadOverhead = dylib.lookupFunction('crypto_aead_overhead'), + _cryptoDecryptAead = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Pointer, + Pointer, Pointer), + _CryptoDecryptAeadDart>('crypto_decrypt_aead'), + _cryptoEncryptAead = dylib.lookupFunction< + Void Function(Int64, Uint32, Pointer, Pointer, + Pointer, Pointer), + _CryptoEncryptAeadDart>('crypto_encrypt_aead'), + _cryptoCryptNoAuth = dylib.lookupFunction< + Void Function( + Int64, Uint32, Pointer, Pointer, Pointer), + _CryptoCryptNoAuthDart>('crypto_crypt_no_auth'), _now = dylib.lookupFunction('now'), - _debug = dylib.lookupFunction), _DebugDart>('debug'), + _debug = dylib.lookupFunction), + _DebugDart>('debug'), _veilidVersionString = dylib.lookupFunction Function(), _VeilidVersionStringDart>('veilid_version_string'), - _veilidVersion = - dylib.lookupFunction( - 'veilid_version') { + _veilidVersion = dylib.lookupFunction('veilid_version') { // Get veilid_flutter initializer final initializeVeilidFlutter = _dylib.lookupFunction< Void Function(Pointer<_DartPostCObject>), @@ -1397,7 +1404,8 @@ class VeilidFFI extends Veilid { final _CryptoDefaultSaltLengthDart _cryptoDefaultSaltLength; final _CryptoHashPasswordDart _cryptoHashPassword; final _CryptoVerifyPasswordDart _cryptoVerifyPassword; - final void Function(int, int, Pointer, Pointer) _cryptoDeriveSharedSecret; + final void Function(int, int, Pointer, Pointer) + _cryptoDeriveSharedSecret; final _CryptoRandomNonceDart _cryptoRandomNonce; final _CryptoRandomSharedSecretDart _cryptoRandomSharedSecret; @@ -1432,8 +1440,9 @@ class VeilidFFI extends Veilid { final nativeLogLevel = jsonEncode(logLevel).toNativeUtf8(); final nativeLayer = layer.toNativeUtf8(); _changeLogLevel(nativeLayer, nativeLogLevel); - malloc.free(nativeLayer); - malloc.free(nativeLogLevel); + malloc + ..free(nativeLayer) + ..free(nativeLogLevel); } @override @@ -1488,7 +1497,7 @@ class VeilidFFI extends Veilid { final recvPort = ReceivePort('routing_context'); final sendPort = recvPort.sendPort; _routingContext(sendPort.nativePort); - final id = await processFuturePlain(recvPort.first); + final id = await processFuturePlain(recvPort.first); return VeilidRoutingContextFFI._(_Ctx(id, this)); } @@ -1548,7 +1557,7 @@ class VeilidFFI extends Veilid { final recvPort = ReceivePort('open_table_db'); final sendPort = recvPort.sendPort; _openTableDb(sendPort.nativePort, name.toNativeUtf8(), columnCount); - final id = await processFuturePlain(recvPort.first); + final id = await processFuturePlain(recvPort.first); return VeilidTableDBFFI._(_TDB(id, this)); } @@ -1557,8 +1566,7 @@ class VeilidFFI extends Veilid { final recvPort = ReceivePort('delete_table_db'); final sendPort = recvPort.sendPort; _deleteTableDb(sendPort.nativePort, name.toNativeUtf8()); - final deleted = await processFuturePlain(recvPort.first); - return deleted; + return await processFuturePlain(recvPort.first); } @override @@ -1578,7 +1586,8 @@ class VeilidFFI extends Veilid { } @override - Future bestCryptoSystem() async => VeilidCryptoSystemFFI._(this, _bestCryptoKind()); + Future bestCryptoSystem() async => + VeilidCryptoSystemFFI._(this, _bestCryptoKind()); @override Future> verifySignatures(List nodeIds, diff --git a/veilid-flutter/lib/veilid_js.dart b/veilid-flutter/lib/veilid_js.dart index c50c8a2d..0beb71c3 100644 --- a/veilid-flutter/lib/veilid_js.dart +++ b/veilid-flutter/lib/veilid_js.dart @@ -14,11 +14,13 @@ Veilid getVeilid() => VeilidJS(); Object wasm = js_util.getProperty(html.window, 'veilid_wasm'); Future _wrapApiPromise(Object p) => js_util - .promiseToFuture(p) - .then((value) => value) - // ignore: inference_failure_on_untyped_parameter - .catchError((error) => Future.error( - VeilidAPIException.fromJson(jsonDecode(error as String)))); + .promiseToFuture(p) + .then((value) => value) + // ignore: inference_failure_on_untyped_parameter + .catchError((e) { + // Wrap all other errors in VeilidAPIExceptionInternal + throw VeilidAPIExceptionInternal(e.toString()); + }, test: (e) => e is! VeilidAPIException); class _Ctx { _Ctx(int this.id, this.js); From efd3fa4cb2dee916c31869a9b7d332b7c267a4d4 Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Sun, 30 Jul 2023 15:57:06 -0400 Subject: [PATCH 29/42] fixes --- veilid-flutter/analysis_options.yaml | 7 + veilid-flutter/lib/default_config.dart | 234 ++++++++++--------- veilid-flutter/lib/routing_context.dart | 20 +- veilid-flutter/lib/veilid_api_exception.dart | 4 +- veilid-flutter/lib/veilid_config.dart | 20 +- veilid-flutter/lib/veilid_crypto.dart | 25 +- veilid-flutter/lib/veilid_ffi.dart | 75 +++--- veilid-flutter/lib/veilid_js.dart | 99 ++++---- veilid-flutter/lib/veilid_state.dart | 16 +- veilid-flutter/lib/veilid_table_db.dart | 26 ++- veilid-flutter/pubspec.yaml | 4 +- 11 files changed, 287 insertions(+), 243 deletions(-) diff --git a/veilid-flutter/analysis_options.yaml b/veilid-flutter/analysis_options.yaml index adefccd2..d262dc94 100644 --- a/veilid-flutter/analysis_options.yaml +++ b/veilid-flutter/analysis_options.yaml @@ -2,3 +2,10 @@ include: package:lint_hard/all.yaml analyzer: errors: invalid_annotation_target: ignore + exclude: + - '**/*.g.dart' + - '**/*.freezed.dart' + +linter: + rules: + avoid_positional_boolean_parameters: false \ No newline at end of file diff --git a/veilid-flutter/lib/default_config.dart b/veilid-flutter/lib/default_config.dart index ad27e415..9b877dbc 100644 --- a/veilid-flutter/lib/default_config.dart +++ b/veilid-flutter/lib/default_config.dart @@ -58,127 +58,129 @@ int getRemoteMaxStorageSpaceMb() { return 256; } -Future getDefaultVeilidConfig(String programName) async => VeilidConfig( - programName: programName, - namespace: '', - capabilities: const VeilidConfigCapabilities(disable: []), - protectedStore: const VeilidConfigProtectedStore( - allowInsecureFallback: false, - alwaysUseInsecureStorage: false, - directory: '', - delete: false, - deviceEncryptionKeyPassword: '', - ), - tableStore: VeilidConfigTableStore( - directory: kIsWeb - ? '' - : p.join((await getApplicationSupportDirectory()).absolute.path, - 'table_store'), - delete: false, - ), - blockStore: VeilidConfigBlockStore( - directory: kIsWeb - ? '' - : p.join((await getApplicationSupportDirectory()).absolute.path, - 'block_store'), - delete: false, - ), - network: VeilidConfigNetwork( - connectionInitialTimeoutMs: 2000, - connectionInactivityTimeoutMs: 60000, - maxConnectionsPerIp4: 32, - maxConnectionsPerIp6Prefix: 32, - maxConnectionsPerIp6PrefixSize: 56, - maxConnectionFrequencyPerMin: 128, - clientWhitelistTimeoutMs: 300000, - reverseConnectionReceiptTimeMs: 5000, - holePunchReceiptTimeMs: 5000, - routingTable: const VeilidConfigRoutingTable( - nodeId: [], - nodeIdSecret: [], - bootstrap: kIsWeb - ? ['ws://bootstrap.veilid.net:5150/ws'] - : ['bootstrap.veilid.net'], - limitOverAttached: 64, - limitFullyAttached: 32, - limitAttachedStrong: 16, - limitAttachedGood: 8, - limitAttachedWeak: 4, +Future getDefaultVeilidConfig(String programName) async => + VeilidConfig( + programName: programName, + namespace: '', + capabilities: const VeilidConfigCapabilities(disable: []), + protectedStore: const VeilidConfigProtectedStore( + allowInsecureFallback: false, + alwaysUseInsecureStorage: false, + directory: '', + delete: false, + deviceEncryptionKeyPassword: '', ), - rpc: const VeilidConfigRPC( - concurrency: 0, - queueSize: 1024, - maxTimestampBehindMs: 10000, - maxTimestampAheadMs: 10000, - timeoutMs: 5000, - maxRouteHopCount: 4, - defaultRouteHopCount: 1, + tableStore: VeilidConfigTableStore( + directory: kIsWeb + ? '' + : p.join((await getApplicationSupportDirectory()).absolute.path, + 'table_store'), + delete: false, ), - dht: VeilidConfigDHT( - resolveNodeTimeoutMs: 10000, - resolveNodeCount: 20, - resolveNodeFanout: 3, - maxFindNodeCount: 20, - getValueTimeoutMs: 10000, - getValueCount: 20, - getValueFanout: 3, - setValueTimeoutMs: 10000, - setValueCount: 20, - setValueFanout: 5, - minPeerCount: 20, - minPeerRefreshTimeMs: 60000, - validateDialInfoReceiptTimeMs: 2000, - localSubkeyCacheSize: getLocalSubkeyCacheSize(), - localMaxSubkeyCacheMemoryMb: await getLocalMaxSubkeyCacheMemoryMb(), - remoteSubkeyCacheSize: getRemoteSubkeyCacheSize(), - remoteMaxRecords: getRemoteMaxRecords(), - remoteMaxSubkeyCacheMemoryMb: await getRemoteMaxSubkeyCacheMemoryMb(), - remoteMaxStorageSpaceMb: getRemoteMaxStorageSpaceMb()), - upnp: true, - detectAddressChanges: true, - restrictedNatRetries: 0, - tls: const VeilidConfigTLS( - certificatePath: '', - privateKeyPath: '', + blockStore: VeilidConfigBlockStore( + directory: kIsWeb + ? '' + : p.join((await getApplicationSupportDirectory()).absolute.path, + 'block_store'), + delete: false, + ), + network: VeilidConfigNetwork( connectionInitialTimeoutMs: 2000, - ), - application: const VeilidConfigApplication( - https: VeilidConfigHTTPS( - enabled: false, + connectionInactivityTimeoutMs: 60000, + maxConnectionsPerIp4: 32, + maxConnectionsPerIp6Prefix: 32, + maxConnectionsPerIp6PrefixSize: 56, + maxConnectionFrequencyPerMin: 128, + clientWhitelistTimeoutMs: 300000, + reverseConnectionReceiptTimeMs: 5000, + holePunchReceiptTimeMs: 5000, + routingTable: const VeilidConfigRoutingTable( + nodeId: [], + nodeIdSecret: [], + bootstrap: kIsWeb + ? ['ws://bootstrap.veilid.net:5150/ws'] + : ['bootstrap.veilid.net'], + limitOverAttached: 64, + limitFullyAttached: 32, + limitAttachedStrong: 16, + limitAttachedGood: 8, + limitAttachedWeak: 4, + ), + rpc: const VeilidConfigRPC( + concurrency: 0, + queueSize: 1024, + maxTimestampBehindMs: 10000, + maxTimestampAheadMs: 10000, + timeoutMs: 5000, + maxRouteHopCount: 4, + defaultRouteHopCount: 1, + ), + dht: VeilidConfigDHT( + resolveNodeTimeoutMs: 10000, + resolveNodeCount: 20, + resolveNodeFanout: 3, + maxFindNodeCount: 20, + getValueTimeoutMs: 10000, + getValueCount: 20, + getValueFanout: 3, + setValueTimeoutMs: 10000, + setValueCount: 20, + setValueFanout: 5, + minPeerCount: 20, + minPeerRefreshTimeMs: 60000, + validateDialInfoReceiptTimeMs: 2000, + localSubkeyCacheSize: getLocalSubkeyCacheSize(), + localMaxSubkeyCacheMemoryMb: await getLocalMaxSubkeyCacheMemoryMb(), + remoteSubkeyCacheSize: getRemoteSubkeyCacheSize(), + remoteMaxRecords: getRemoteMaxRecords(), + remoteMaxSubkeyCacheMemoryMb: + await getRemoteMaxSubkeyCacheMemoryMb(), + remoteMaxStorageSpaceMb: getRemoteMaxStorageSpaceMb()), + upnp: true, + detectAddressChanges: true, + restrictedNatRetries: 0, + tls: const VeilidConfigTLS( + certificatePath: '', + privateKeyPath: '', + connectionInitialTimeoutMs: 2000, + ), + application: const VeilidConfigApplication( + https: VeilidConfigHTTPS( + enabled: false, + listenAddress: '', + path: '', + ), + http: VeilidConfigHTTP( + enabled: false, + listenAddress: '', + path: '', + )), + protocol: const VeilidConfigProtocol( + udp: VeilidConfigUDP( + enabled: !kIsWeb, + socketPoolSize: 0, listenAddress: '', - path: '', ), - http: VeilidConfigHTTP( - enabled: false, + tcp: VeilidConfigTCP( + connect: !kIsWeb, + listen: !kIsWeb, + maxConnections: 32, listenAddress: '', - path: '', - )), - protocol: const VeilidConfigProtocol( - udp: VeilidConfigUDP( - enabled: !kIsWeb, - socketPoolSize: 0, - listenAddress: '', - ), - tcp: VeilidConfigTCP( - connect: !kIsWeb, - listen: !kIsWeb, - maxConnections: 32, - listenAddress: '', - ), - ws: VeilidConfigWS( - connect: true, - listen: !kIsWeb, - maxConnections: 16, - listenAddress: '', - path: 'ws', - ), - wss: VeilidConfigWSS( - connect: true, - listen: false, - maxConnections: 16, - listenAddress: '', - path: 'ws', + ), + ws: VeilidConfigWS( + connect: true, + listen: !kIsWeb, + maxConnections: 16, + listenAddress: '', + path: 'ws', + ), + wss: VeilidConfigWSS( + connect: true, + listen: false, + maxConnections: 16, + listenAddress: '', + path: 'ws', + ), ), ), - ), - ); + ); diff --git a/veilid-flutter/lib/routing_context.dart b/veilid-flutter/lib/routing_context.dart index eda071bb..5060e98e 100644 --- a/veilid-flutter/lib/routing_context.dart +++ b/veilid-flutter/lib/routing_context.dart @@ -76,7 +76,8 @@ class DHTRecordDescriptor with _$DHTRecordDescriptor { const factory DHTRecordDescriptor({ required TypedKey key, required PublicKey owner, - required DHTSchema schema, PublicKey? ownerSecret, + required DHTSchema schema, + PublicKey? ownerSecret, }) = _DHTRecordDescriptor; factory DHTRecordDescriptor.fromJson(dynamic json) => _$DHTRecordDescriptorFromJson(json as Map); @@ -137,9 +138,9 @@ enum Stability { lowLatency, reliable; - String toJson() => name.toPascalCase(); factory Stability.fromJson(dynamic j) => Stability.values.byName((j as String).toCamelCase()); + String toJson() => name.toPascalCase(); } ////////////////////////////////////// @@ -150,16 +151,16 @@ enum Sequencing { preferOrdered, ensureOrdered; - String toJson() => name.toPascalCase(); factory Sequencing.fromJson(dynamic j) => Sequencing.values.byName((j as String).toCamelCase()); + String toJson() => name.toPascalCase(); } ////////////////////////////////////// /// SafetySelection @immutable -abstract class SafetySelection extends Equatable { +abstract class SafetySelection { factory SafetySelection.fromJson(dynamic jsond) { final json = jsond as Map; if (json.containsKey('Unsafe')) { @@ -175,8 +176,7 @@ abstract class SafetySelection extends Equatable { } @immutable -class SafetySelectionUnsafe implements SafetySelection { - +class SafetySelectionUnsafe extends Equatable implements SafetySelection { // const SafetySelectionUnsafe({ required this.sequencing, @@ -192,8 +192,7 @@ class SafetySelectionUnsafe implements SafetySelection { } @immutable -class SafetySelectionSafe implements SafetySelection { - +class SafetySelectionSafe extends Equatable implements SafetySelection { // const SafetySelectionSafe({ required this.safetySpec, @@ -212,7 +211,10 @@ class SafetySelectionSafe implements SafetySelection { @freezed class SafetySpec with _$SafetySpec { const factory SafetySpec({ - required int hopCount, required Stability stability, required Sequencing sequencing, String? preferredRoute, + required int hopCount, + required Stability stability, + required Sequencing sequencing, + String? preferredRoute, }) = _SafetySpec; factory SafetySpec.fromJson(dynamic json) => diff --git a/veilid-flutter/lib/veilid_api_exception.dart b/veilid-flutter/lib/veilid_api_exception.dart index b61886d7..433114e9 100644 --- a/veilid-flutter/lib/veilid_api_exception.dart +++ b/veilid-flutter/lib/veilid_api_exception.dart @@ -208,8 +208,8 @@ class VeilidAPIExceptionInvalidArgument implements VeilidAPIException { final String value; @override - String toString() => - 'VeilidAPIException: InvalidArgument ($context:$argument)\n value: $value'; + String toString() => 'VeilidAPIException: InvalidArgument' + ' ($context:$argument)\n value: $value'; @override String toDisplayError() => 'Invalid argument for $context: $argument'; diff --git a/veilid-flutter/lib/veilid_config.dart b/veilid-flutter/lib/veilid_config.dart index ab6b2ba2..09de9ac6 100644 --- a/veilid-flutter/lib/veilid_config.dart +++ b/veilid-flutter/lib/veilid_config.dart @@ -125,9 +125,9 @@ enum VeilidConfigLogLevel { debug, trace; + factory VeilidConfigLogLevel.fromJson(dynamic j) => + VeilidConfigLogLevel.values.byName((j as String).toCamelCase()); String toJson() => name.toPascalCase(); - - factory VeilidConfigLogLevel.fromJson(dynamic j) => VeilidConfigLogLevel.values.byName((j as String).toCamelCase()); } ////////////////////////////////////// @@ -295,7 +295,10 @@ class VeilidConfigRPC with _$VeilidConfigRPC { const factory VeilidConfigRPC( {required int concurrency, required int queueSize, - required int timeoutMs, required int maxRouteHopCount, required int defaultRouteHopCount, int? maxTimestampBehindMs, + required int timeoutMs, + required int maxRouteHopCount, + required int defaultRouteHopCount, + int? maxTimestampBehindMs, int? maxTimestampAheadMs}) = _VeilidConfigRPC; factory VeilidConfigRPC.fromJson(dynamic json) => @@ -335,7 +338,16 @@ class VeilidConfigNetwork with _$VeilidConfigNetwork { required int clientWhitelistTimeoutMs, required int reverseConnectionReceiptTimeMs, required int holePunchReceiptTimeMs, - required VeilidConfigRoutingTable routingTable, required VeilidConfigRPC rpc, required VeilidConfigDHT dht, required bool upnp, required bool detectAddressChanges, required int restrictedNatRetries, required VeilidConfigTLS tls, required VeilidConfigApplication application, required VeilidConfigProtocol protocol, String? networkKeyPassword, + required VeilidConfigRoutingTable routingTable, + required VeilidConfigRPC rpc, + required VeilidConfigDHT dht, + required bool upnp, + required bool detectAddressChanges, + required int restrictedNatRetries, + required VeilidConfigTLS tls, + required VeilidConfigApplication application, + required VeilidConfigProtocol protocol, + String? networkKeyPassword, }) = _VeilidConfigNetwork; factory VeilidConfigNetwork.fromJson(dynamic json) => diff --git a/veilid-flutter/lib/veilid_crypto.dart b/veilid-flutter/lib/veilid_crypto.dart index 8a267b42..97686ec7 100644 --- a/veilid-flutter/lib/veilid_crypto.dart +++ b/veilid-flutter/lib/veilid_crypto.dart @@ -16,7 +16,8 @@ const CryptoKind cryptoKindVLD0 = const CryptoKind cryptoKindNONE = $N << 0 | $O << 8 | $N << 16 | $E << 24; // "NONE" -String cryptoKindToString(CryptoKind kind) => cryptoKindToBytes(kind).map(String.fromCharCode).join(); +String cryptoKindToString(CryptoKind kind) => + cryptoKindToBytes(kind).map(String.fromCharCode).join(); const CryptoKind bestCryptoKind = cryptoKindVLD0; @@ -30,8 +31,8 @@ CryptoKind cryptoKindFromString(String s) { if (s.codeUnits.length != 4) { throw const FormatException('malformed string'); } - final kind = ByteData.sublistView(Uint8List.fromList(s.codeUnits)) - .getUint32(0); + final kind = + ByteData.sublistView(Uint8List.fromList(s.codeUnits)).getUint32(0); return kind; } @@ -40,7 +41,6 @@ CryptoKind cryptoKindFromString(String s) { @immutable class Typed extends Equatable { - const Typed({required this.kind, required this.value}); factory Typed.fromString(String s) { @@ -62,9 +62,9 @@ class Typed extends Equatable { String toString() => '${cryptoKindToString(kind)}:$value'; Uint8List decode() { - final b = BytesBuilder(); - b.add(cryptoKindToBytes(kind)); - b.add(value.decode()); + final b = BytesBuilder() + ..add(cryptoKindToBytes(kind)) + ..add(value.decode()); return b.toBytes(); } @@ -73,7 +73,6 @@ class Typed extends Equatable { @immutable class KeyPair extends Equatable { - const KeyPair({required this.key, required this.secret}); factory KeyPair.fromString(String s) { @@ -101,7 +100,6 @@ class KeyPair extends Equatable { @immutable class TypedKeyPair extends Equatable { - const TypedKeyPair( {required this.kind, required this.key, required this.secret}); @@ -129,8 +127,7 @@ class TypedKeyPair extends Equatable { List get props => [kind, key, secret]; @override - String toString() => - '${cryptoKindToString(kind)}:$key:$secret'; + String toString() => '${cryptoKindToString(kind)}:$key:$secret'; String toJson() => toString(); } @@ -169,13 +166,15 @@ abstract class VeilidCryptoSystem { Future generateHash(Uint8List data); //Future generateHashReader(Stream> reader); Future validateKeyPair(PublicKey key, SecretKey secret); - Future validateKeyPairWithKeyPair(KeyPair keyPair) => validateKeyPair(keyPair.key, keyPair.secret); + Future validateKeyPairWithKeyPair(KeyPair keyPair) => + validateKeyPair(keyPair.key, keyPair.secret); Future validateHash(Uint8List data, HashDigest hash); //Future validateHashReader(Stream> reader, HashDigest hash); Future distance(CryptoKey key1, CryptoKey key2); Future sign(PublicKey key, SecretKey secret, Uint8List data); - Future signWithKeyPair(KeyPair keyPair, Uint8List data) => sign(keyPair.key, keyPair.secret, data); + Future signWithKeyPair(KeyPair keyPair, Uint8List data) => + sign(keyPair.key, keyPair.secret, data); Future verify(PublicKey key, Uint8List data, Signature signature); Future aeadOverhead(); diff --git a/veilid-flutter/lib/veilid_ffi.dart b/veilid-flutter/lib/veilid_ffi.dart index 0db5b9a1..cf899832 100644 --- a/veilid-flutter/lib/veilid_ffi.dart +++ b/veilid-flutter/lib/veilid_ffi.dart @@ -26,7 +26,8 @@ typedef _DartPostCObject = NativeFunction)>; // fn free_string(s: *mut std::os::raw::c_char) typedef _FreeStringDart = void Function(Pointer); -// fn initialize_veilid_flutter(dart_post_c_object_ptr: ffi::DartPostCObjectFnType) +// fn initialize_veilid_flutter( +// dart_post_c_object_ptr: ffi::DartPostCObjectFnType) // fn initialize_veilid_core(platform_config: FfiStr) typedef _InitializeVeilidCoreDart = void Function(Pointer); // fn change_log_level(layer: FfiStr, log_level: FfiStr) @@ -50,16 +51,20 @@ typedef _RoutingContextWithPrivacyDart = int Function(int); typedef _RoutingContextWithCustomPrivacyDart = int Function(int, Pointer); // fn routing_context_with_sequencing(id: u32, sequencing: FfiStr) typedef _RoutingContextWithSequencingDart = int Function(int, Pointer); -// fn routing_context_app_call(port: i64, id: u32, target: FfiStr, request: FfiStr) +// fn routing_context_app_call(port: i64, +// id: u32, target: FfiStr, request: FfiStr) typedef _RoutingContextAppCallDart = void Function( int, int, Pointer, Pointer); -// fn routing_context_app_message(port: i64, id: u32, target: FfiStr, request: FfiStr) +// fn routing_context_app_message(port: i64, +// id: u32, target: FfiStr, request: FfiStr) typedef _RoutingContextAppMessageDart = void Function( int, int, Pointer, Pointer); -// fn routing_context_create_dht_record(port: i64, id: u32, kind: u32, schema: FfiStr) +// fn routing_context_create_dht_record(port: i64, +// id: u32, kind: u32, schema: FfiStr) typedef _RoutingContextCreateDHTRecordDart = void Function( int, int, Pointer, int); -// fn routing_context_open_dht_record(port: i64, id: u32, key: FfiStr, writer: FfiStr) +// fn routing_context_open_dht_record(port: i64, +// id: u32, key: FfiStr, writer: FfiStr) typedef _RoutingContextOpenDHTRecordDart = void Function( int, int, Pointer, Pointer); // fn routing_context_close_dht_record(port: i64, id: u32, key: FfiStr) @@ -68,16 +73,20 @@ typedef _RoutingContextCloseDHTRecordDart = void Function( // fn routing_context_delete_dht_record(port: i64, id: u32, key: FfiStr) typedef _RoutingContextDeleteDHTRecordDart = void Function( int, int, Pointer); -// fn routing_context_get_dht_value(port: i64, id: u32, key: FfiStr, subkey: u32, force_refresh: bool) +// fn routing_context_get_dht_value(port: i64, +// id: u32, key: FfiStr, subkey: u32, force_refresh: bool) typedef _RoutingContextGetDHTValueDart = void Function( int, int, Pointer, int, bool); -// fn routing_context_set_dht_value(port: i64, id: u32, key: FfiStr, subkey: u32, data: FfiStr) +// fn routing_context_set_dht_value(port: i64, +// id: u32, key: FfiStr, subkey: u32, data: FfiStr) typedef _RoutingContextSetDHTValueDart = void Function( int, int, Pointer, int, Pointer); -// fn routing_context_watch_dht_values(port: i64, id: u32, key: FfiStr, subkeys: FfiStr, expiration: FfiStr, count: u32) +// fn routing_context_watch_dht_values(port: i64, +// id: u32, key: FfiStr, subkeys: FfiStr, expiration: FfiStr, count: u32) typedef _RoutingContextWatchDHTValuesDart = void Function( int, int, Pointer, Pointer, int, int); -// fn routing_context_cancel_dht_watch(port: i64, id: u32, key: FfiStr, subkeys: FfiStr) +// fn routing_context_cancel_dht_watch(port: i64, +// id: u32, key: FfiStr, subkeys: FfiStr) typedef _RoutingContextCancelDHTWatchDart = void Function( int, int, Pointer, Pointer); @@ -119,7 +128,8 @@ typedef _ReleaseTableDbTransactionDart = int Function(int); typedef _TableDbTransactionCommitDart = void Function(int, int); // fn table_db_transaction_rollback(port: i64, id: u32) typedef _TableDbTransactionRollbackDart = void Function(int, int); -// fn table_db_transaction_store(port: i64, id: u32, col: u32, key: FfiStr, value: FfiStr) +// fn table_db_transaction_store(port: i64, +// id: u32, col: u32, key: FfiStr, value: FfiStr) typedef _TableDbTransactionStoreDart = void Function( int, int, int, Pointer, Pointer); // fn table_db_transaction_delete(port: i64, id: u32, col: u32, key: FfiStr) @@ -129,7 +139,8 @@ typedef _TableDbTransactionDeleteDart = void Function( typedef _ValidCryptoKindsDart = Pointer Function(); // fn best_crypto_kind() -> u32 typedef _BestCryptoKindDart = int Function(); -// fn verify_signatures(port: i64, node_ids: FfiStr, data: FfiStr, signatures: FfiStr) +// fn verify_signatures(port: i64, +// node_ids: FfiStr, data: FfiStr, signatures: FfiStr) typedef _VerifySignaturesDart = void Function( int, Pointer, Pointer, Pointer); // fn generate_signatures(port: i64, data: FfiStr, key_pairs: FfiStr) @@ -147,13 +158,15 @@ typedef _CryptoComputeDHDart = void Function( typedef _CryptoRandomBytesDart = void Function(int, int, int); // fn crypto_default_salt_length(port: i64, kind: u32) typedef _CryptoDefaultSaltLengthDart = void Function(int, int); -// fn crypto_hash_password(port: i64, kind: u32, password: FfiStr, salt: FfiStr ) +// fn crypto_hash_password(port: i64, kind: u32, password: FfiStr, salt: FfiStr) typedef _CryptoHashPasswordDart = void Function( int, int, Pointer, Pointer); -// fn crypto_verify_password(port: i64, kind: u32, password: FfiStr, password_hash: FfiStr ) +// fn crypto_verify_password(port: i64, +// kind: u32, password: FfiStr, password_hash: FfiStr ) typedef _CryptoVerifyPasswordDart = void Function( int, int, Pointer, Pointer); -// fn crypto_derive_shared_secret(port: i64, kind: u32, password: FfiStr, salt: FfiStr ) +// fn crypto_derive_shared_secret(port: i64, +// kind: u32, password: FfiStr, salt: FfiStr ) // fn crypto_random_nonce(port: i64, kind: u32) typedef _CryptoRandomNonceDart = void Function(int, int); @@ -163,7 +176,8 @@ typedef _CryptoRandomSharedSecretDart = void Function(int, int); typedef _CryptoGenerateKeyPairDart = void Function(int, int); // fn crypto_generate_hash(port: i64, kind: u32, data: FfiStr) typedef _CryptoGenerateHashDart = void Function(int, int, Pointer); -// fn crypto_validate_key_pair(port: i64, kind: u32, key: FfiStr, secret: FfiStr) +// fn crypto_validate_key_pair(port: i64, +// kind: u32, key: FfiStr, secret: FfiStr) typedef _CryptoValidateKeyPairDart = void Function( int, int, Pointer, Pointer); // fn crypto_validate_hash(port: i64, kind: u32, data: FfiStr, hash: FfiStr) @@ -172,21 +186,28 @@ typedef _CryptoValidateHashDart = void Function( // fn crypto_distance(port: i64, kind: u32, key1: FfiStr, key2: FfiStr) typedef _CryptoDistanceDart = void Function( int, int, Pointer, Pointer); -// fn crypto_sign(port: i64, kind: u32, key: FfiStr, secret: FfiStr, data: FfiStr) +// fn crypto_sign(port: i64, +// kind: u32, key: FfiStr, secret: FfiStr, data: FfiStr) typedef _CryptoSignDart = void Function( int, int, Pointer, Pointer, Pointer); -// fn crypto_verify(port: i64, kind: u32, key: FfiStr, data: FfiStr, signature: FfiStr) +// fn crypto_verify(port: i64, +// kind: u32, key: FfiStr, data: FfiStr, signature: FfiStr) typedef _CryptoVerifyDart = void Function( int, int, Pointer, Pointer, Pointer); // fn crypto_aead_overhead(port: i64, kind: u32) typedef _CryptoAeadOverheadDart = void Function(int, int); -// fn crypto_decrypt_aead(port: i64, kind: u32, body: FfiStr, nonce: FfiStr, shared_secret: FfiStr, associated_data: FfiStr) +// fn crypto_decrypt_aead(port: i64, +// kind: u32, body: FfiStr, nonce: FfiStr, +// shared_secret: FfiStr, associated_data: FfiStr) typedef _CryptoDecryptAeadDart = void Function( int, int, Pointer, Pointer, Pointer, Pointer); -// fn crypto_encrypt_aead(port: i64, kind: u32, body: FfiStr, nonce: FfiStr, shared_secret: FfiStr, associated_data: FfiStr) +// fn crypto_encrypt_aead(port: i64, +// kind: u32, body: FfiStr, nonce: FfiStr, +// shared_secret: FfiStr, associated_data: FfiStr) typedef _CryptoEncryptAeadDart = void Function( int, int, Pointer, Pointer, Pointer, Pointer); -// fn crypto_crypt_no_auth(port: i64, kind: u32, body: FfiStr, nonce: FfiStr, shared_secret: FfiStr) +// fn crypto_crypt_no_auth(port: i64, +// kind: u32, body: FfiStr, nonce: FfiStr, shared_secret: FfiStr) typedef _CryptoCryptNoAuthDart = void Function( int, int, Pointer, Pointer, Pointer); @@ -343,8 +364,8 @@ Future processFutureVoid(Future future) async => case messageOk: { if (list[1] != null) { - throw VeilidAPIExceptionInternal( - "Unexpected MESSAGE_OK value '${list[1]}' where null expected"); + throw VeilidAPIExceptionInternal('Unexpected MESSAGE_OK value' + ' "${list[1]}" where null expected'); } return; } @@ -357,7 +378,8 @@ Future processFutureVoid(Future future) async => final ret = jsonDecode(list[1] as String); if (ret != null) { throw VeilidAPIExceptionInternal( - "Unexpected MESSAGE_OK_JSON value '$ret' where null expected"); + 'Unexpected MESSAGE_OK_JSON value' + ' "$ret" where null expected'); } return; } @@ -385,8 +407,8 @@ Future> processFutureStream( case messageOk: { if (list[1] != null) { - throw VeilidAPIExceptionInternal( - "Unexpected MESSAGE_OK value '${list[1]}' where null expected"); + throw VeilidAPIExceptionInternal('Unexpected MESSAGE_OK value' + ' "${list[1]}" where null expected'); } return returnStream; } @@ -399,7 +421,8 @@ Future> processFutureStream( final ret = jsonDecode(list[1] as String); if (ret != null) { throw VeilidAPIExceptionInternal( - "Unexpected MESSAGE_OK_JSON value '$ret' where null expected"); + 'Unexpected MESSAGE_OK_JSON value' + ' "$ret" where null expected'); } return returnStream; } diff --git a/veilid-flutter/lib/veilid_js.dart b/veilid-flutter/lib/veilid_js.dart index 0beb71c3..3ef1f283 100644 --- a/veilid-flutter/lib/veilid_js.dart +++ b/veilid-flutter/lib/veilid_js.dart @@ -23,19 +23,20 @@ Future _wrapApiPromise(Object p) => js_util }, test: (e) => e is! VeilidAPIException); class _Ctx { - _Ctx(int this.id, this.js); - int? id; + _Ctx(int id, this.js) : _id = id; + int? _id; final VeilidJS js; - void ensureValid() { - if (id == null) { + int requireId() { + if (_id == null) { throw VeilidAPIExceptionNotInitialized(); } + return _id!; } void close() { - if (id != null) { - js_util.callMethod(wasm, 'release_routing_context', [id]); - id = null; + if (_id != null) { + js_util.callMethod(wasm, 'release_routing_context', [_id]); + _id = null; } } } @@ -55,16 +56,15 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override VeilidRoutingContextJS withPrivacy() { - _ctx.ensureValid(); + final id = _ctx.requireId(); final int newId = - js_util.callMethod(wasm, 'routing_context_with_privacy', [_ctx.id!]); + js_util.callMethod(wasm, 'routing_context_with_privacy', [id]); return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js)); } @override VeilidRoutingContextJS withCustomPrivacy(SafetySelection safetySelection) { - _ctx.ensureValid(); - final id = _ctx.id!; + final id = _ctx.requireId(); final newId = js_util.callMethod( wasm, 'routing_context_with_custom_privacy', @@ -75,8 +75,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override VeilidRoutingContextJS withSequencing(Sequencing sequencing) { - _ctx.ensureValid(); - final id = _ctx.id!; + final id = _ctx.requireId(); final newId = js_util.callMethod( wasm, 'routing_context_with_sequencing', [id, jsonEncode(sequencing)]); return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js)); @@ -84,8 +83,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override Future appCall(String target, Uint8List request) async { - _ctx.ensureValid(); - final id = _ctx.id!; + final id = _ctx.requireId(); final encodedRequest = base64UrlNoPadEncode(request); return base64UrlNoPadDecode(await _wrapApiPromise(js_util.callMethod( @@ -94,8 +92,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override Future appMessage(String target, Uint8List message) { - _ctx.ensureValid(); - final id = _ctx.id!; + final id = _ctx.requireId(); final encodedMessage = base64UrlNoPadEncode(message); return _wrapApiPromise(js_util.callMethod( @@ -105,8 +102,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override Future createDHTRecord(DHTSchema schema, {CryptoKind kind = 0}) async { - _ctx.ensureValid(); - final id = _ctx.id!; + final id = _ctx.requireId(); return DHTRecordDescriptor.fromJson(jsonDecode(await _wrapApiPromise(js_util .callMethod(wasm, 'routing_context_create_dht_record', [id, jsonEncode(schema), kind])))); @@ -115,8 +111,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override Future openDHTRecord( TypedKey key, KeyPair? writer) async { - _ctx.ensureValid(); - final id = _ctx.id!; + final id = _ctx.requireId(); return DHTRecordDescriptor.fromJson(jsonDecode(await _wrapApiPromise(js_util .callMethod(wasm, 'routing_context_open_dht_record', [ id, @@ -127,16 +122,14 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override Future closeDHTRecord(TypedKey key) { - _ctx.ensureValid(); - final id = _ctx.id!; + final id = _ctx.requireId(); return _wrapApiPromise(js_util.callMethod( wasm, 'routing_context_close_dht_record', [id, jsonEncode(key)])); } @override Future deleteDHTRecord(TypedKey key) { - _ctx.ensureValid(); - final id = _ctx.id!; + final id = _ctx.requireId(); return _wrapApiPromise(js_util.callMethod( wasm, 'routing_context_delete_dht_record', [id, jsonEncode(key)])); } @@ -144,8 +137,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override Future getDHTValue( TypedKey key, int subkey, bool forceRefresh) async { - _ctx.ensureValid(); - final id = _ctx.id!; + final id = _ctx.requireId(); final opt = await _wrapApiPromise(js_util.callMethod( wasm, 'routing_context_get_dht_value', @@ -156,8 +148,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override Future setDHTValue( TypedKey key, int subkey, Uint8List data) async { - _ctx.ensureValid(); - final id = _ctx.id!; + final id = _ctx.requireId(); final opt = await _wrapApiPromise(js_util.callMethod( wasm, 'routing_context_set_dht_value', @@ -168,8 +159,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override Future watchDHTValues(TypedKey key, List subkeys, Timestamp expiration, int count) async { - _ctx.ensureValid(); - final id = _ctx.id!; + final id = _ctx.requireId(); final ts = await _wrapApiPromise(js_util.callMethod( wasm, 'routing_context_watch_dht_values', [ id, @@ -183,8 +173,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { @override Future cancelDHTWatch(TypedKey key, List subkeys) { - _ctx.ensureValid(); - final id = _ctx.id!; + final id = _ctx.requireId(); return _wrapApiPromise(js_util.callMethod( wasm, 'routing_context_cancel_dht_watch', @@ -194,11 +183,11 @@ class VeilidRoutingContextJS extends VeilidRoutingContext { // JS implementation of VeilidCryptoSystem class VeilidCryptoSystemJS extends VeilidCryptoSystem { - VeilidCryptoSystemJS._(this._js, this._kind) { - // Keep the reference - _js; - } + VeilidCryptoSystemJS._(this._js, this._kind); + final CryptoKind _kind; + // Keep the reference + // ignore: unused_field final VeilidJS _js; @override @@ -420,19 +409,22 @@ class VeilidTableDBTransactionJS extends VeilidTableDBTransaction { } class _TDB { - _TDB(int this.id, this.js); - int? id; + _TDB(int id, this.js) : _id = id; + + int? _id; + final VeilidJS js; - void ensureValid() { - if (id == null) { + int requireId() { + if (_id == null) { throw VeilidAPIExceptionNotInitialized(); } + return _id!; } void close() { - if (id != null) { - js_util.callMethod(wasm, 'release_table_db', [id]); - id = null; + if (_id != null) { + js_util.callMethod(wasm, 'release_table_db', [_id]); + _id = null; } } } @@ -452,22 +444,20 @@ class VeilidTableDBJS extends VeilidTableDB { @override int getColumnCount() { - _tdb.ensureValid(); - return js_util.callMethod(wasm, 'table_db_get_column_count', [_tdb.id!]); + final id = _tdb.requireId(); + return js_util.callMethod(wasm, 'table_db_get_column_count', [id]); } @override Future> getKeys(int col) async { - _tdb.ensureValid(); - final id = _tdb.id!; + final id = _tdb.requireId(); return jsonListConstructor(base64UrlNoPadDecodeDynamic)(jsonDecode( await js_util.callMethod(wasm, 'table_db_get_keys', [id, col]))); } @override VeilidTableDBTransaction transact() { - _tdb.ensureValid(); - final id = _tdb.id!; + final id = _tdb.requireId(); final xid = js_util.callMethod(wasm, 'table_db_transact', [id]); return VeilidTableDBTransactionJS._(_TDBT(xid, this, _tdb.js)); @@ -475,8 +465,7 @@ class VeilidTableDBJS extends VeilidTableDB { @override Future store(int col, Uint8List key, Uint8List value) { - _tdb.ensureValid(); - final id = _tdb.id!; + final id = _tdb.requireId(); final encodedKey = base64UrlNoPadEncode(key); final encodedValue = base64UrlNoPadEncode(value); @@ -486,8 +475,7 @@ class VeilidTableDBJS extends VeilidTableDB { @override Future load(int col, Uint8List key) async { - _tdb.ensureValid(); - final id = _tdb.id!; + final id = _tdb.requireId(); final encodedKey = base64UrlNoPadEncode(key); final out = await _wrapApiPromise( @@ -500,8 +488,7 @@ class VeilidTableDBJS extends VeilidTableDB { @override Future delete(int col, Uint8List key) async { - _tdb.ensureValid(); - final id = _tdb.id!; + final id = _tdb.requireId(); final encodedKey = base64UrlNoPadEncode(key); final out = await _wrapApiPromise( diff --git a/veilid-flutter/lib/veilid_state.dart b/veilid-flutter/lib/veilid_state.dart index 835a786a..9ed85a5d 100644 --- a/veilid-flutter/lib/veilid_state.dart +++ b/veilid-flutter/lib/veilid_state.dart @@ -21,9 +21,10 @@ enum AttachmentState { overAttached, detaching; - String toJson() => name.toPascalCase(); factory AttachmentState.fromJson(dynamic j) => AttachmentState.values.byName((j as String).toCamelCase()); + + String toJson() => name.toPascalCase(); } ////////////////////////////////////// @@ -36,9 +37,10 @@ enum VeilidLogLevel { debug, trace; - String toJson() => name.toPascalCase(); factory VeilidLogLevel.fromJson(dynamic j) => VeilidLogLevel.values.byName((j as String).toCamelCase()); + + String toJson() => name.toPascalCase(); } //////////// @@ -109,7 +111,8 @@ class PeerStats with _$PeerStats { const factory PeerStats({ required Timestamp timeAdded, required RPCStats rpcStats, - required TransferStatsDownUp transfer, LatencyStats? latency, + required TransferStatsDownUp transfer, + LatencyStats? latency, }) = _PeerStats; factory PeerStats.fromJson(dynamic json) => @@ -141,10 +144,13 @@ sealed class VeilidUpdate with _$VeilidUpdate { String? backtrace, }) = VeilidLog; const factory VeilidUpdate.appMessage({ - @Uint8ListJsonConverter() required Uint8List message, TypedKey? sender, + @Uint8ListJsonConverter() required Uint8List message, + TypedKey? sender, }) = VeilidAppMessage; const factory VeilidUpdate.appCall({ - @Uint8ListJsonConverter() required Uint8List message, required String callId, TypedKey? sender, + @Uint8ListJsonConverter() required Uint8List message, + required String callId, + TypedKey? sender, }) = VeilidAppCall; const factory VeilidUpdate.attachment( {required AttachmentState state, diff --git a/veilid-flutter/lib/veilid_table_db.dart b/veilid-flutter/lib/veilid_table_db.dart index cf7b34c6..a78569bb 100644 --- a/veilid-flutter/lib/veilid_table_db.dart +++ b/veilid-flutter/lib/veilid_table_db.dart @@ -12,12 +12,14 @@ abstract class VeilidTableDBTransaction { Future delete(int col, Uint8List key); Future storeJson(int col, Uint8List key, Object? object, - {Object? Function(Object? nonEncodable)? toEncodable}) async => store(col, key, - utf8.encoder.convert(jsonEncode(object, toEncodable: toEncodable))); + {Object? Function(Object? nonEncodable)? toEncodable}) async => + store(col, key, + utf8.encoder.convert(jsonEncode(object, toEncodable: toEncodable))); Future storeStringJson(int col, String key, Object? object, - {Object? Function(Object? nonEncodable)? toEncodable}) => storeJson(col, utf8.encoder.convert(key), object, - toEncodable: toEncodable); + {Object? Function(Object? nonEncodable)? toEncodable}) => + storeJson(col, utf8.encoder.convert(key), object, + toEncodable: toEncodable); } abstract class VeilidTableDB { @@ -30,12 +32,14 @@ abstract class VeilidTableDB { Future delete(int col, Uint8List key); Future storeJson(int col, Uint8List key, Object? object, - {Object? Function(Object? nonEncodable)? toEncodable}) => store(col, key, - utf8.encoder.convert(jsonEncode(object, toEncodable: toEncodable))); + {Object? Function(Object? nonEncodable)? toEncodable}) => + store(col, key, + utf8.encoder.convert(jsonEncode(object, toEncodable: toEncodable))); Future storeStringJson(int col, String key, Object? object, - {Object? Function(Object? nonEncodable)? toEncodable}) => storeJson(col, utf8.encoder.convert(key), object, - toEncodable: toEncodable); + {Object? Function(Object? nonEncodable)? toEncodable}) => + storeJson(col, utf8.encoder.convert(key), object, + toEncodable: toEncodable); Future loadJson(int col, Uint8List key, {Object? Function(Object? key, Object? value)? reviver}) async { @@ -47,7 +51,8 @@ abstract class VeilidTableDB { } Future loadStringJson(int col, String key, - {Object? Function(Object? key, Object? value)? reviver}) => loadJson(col, utf8.encoder.convert(key), reviver: reviver); + {Object? Function(Object? key, Object? value)? reviver}) => + loadJson(col, utf8.encoder.convert(key), reviver: reviver); Future deleteJson(int col, Uint8List key, {Object? Function(Object? key, Object? value)? reviver}) async { @@ -59,5 +64,6 @@ abstract class VeilidTableDB { } Future deleteStringJson(int col, String key, - {Object? Function(Object? key, Object? value)? reviver}) => deleteJson(col, utf8.encoder.convert(key), reviver: reviver); + {Object? Function(Object? key, Object? value)? reviver}) => + deleteJson(col, utf8.encoder.convert(key), reviver: reviver); } diff --git a/veilid-flutter/pubspec.yaml b/veilid-flutter/pubspec.yaml index ec1052ac..daea35ca 100644 --- a/veilid-flutter/pubspec.yaml +++ b/veilid-flutter/pubspec.yaml @@ -18,11 +18,11 @@ dependencies: sdk: flutter freezed_annotation: ^2.2.0 json_annotation: ^4.8.1 - path_provider: ^2.0.9 path: ^1.8.0 + path_provider: ^2.0.9 system_info2: ^3.0.2 system_info_plus: ^0.0.5 - + dev_dependencies: build_runner: ^2.4.6 flutter_test: From 079211890b0e177a9f00f21f56f57dda05b448ce Mon Sep 17 00:00:00 2001 From: John Smith Date: Sun, 30 Jul 2023 16:15:57 -0400 Subject: [PATCH 30/42] fix lru bug in connection table --- veilid-core/src/network_manager/connection_table.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/veilid-core/src/network_manager/connection_table.rs b/veilid-core/src/network_manager/connection_table.rs index 2ece5f05..eab8606e 100644 --- a/veilid-core/src/network_manager/connection_table.rs +++ b/veilid-core/src/network_manager/connection_table.rs @@ -177,10 +177,10 @@ impl ConnectionTable { // then drop the least recently used connection let mut out_conn = None; if inner.conn_by_id[protocol_index].len() > inner.max_connections[protocol_index] { - if let Some((lruk, lru_conn)) = inner.conn_by_id[protocol_index].remove_lru() { + if let Some((lruk, lru_conn)) = inner.conn_by_id[protocol_index].peek_lru() { log_net!(debug "connection lru out: {:?}", lru_conn); out_conn = Some(lru_conn); - Self::remove_connection_records(&mut *inner, lruk); + Self::remove_connection_records(&mut *inner, *lruk); } } From 9551ecd9a385848bc65bc2ca259bfaeee85be80d Mon Sep 17 00:00:00 2001 From: John Smith Date: Sun, 30 Jul 2023 16:25:17 -0400 Subject: [PATCH 31/42] fix compilation error --- veilid-core/src/network_manager/connection_table.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/veilid-core/src/network_manager/connection_table.rs b/veilid-core/src/network_manager/connection_table.rs index eab8606e..a6d60ec6 100644 --- a/veilid-core/src/network_manager/connection_table.rs +++ b/veilid-core/src/network_manager/connection_table.rs @@ -178,9 +178,9 @@ impl ConnectionTable { let mut out_conn = None; if inner.conn_by_id[protocol_index].len() > inner.max_connections[protocol_index] { if let Some((lruk, lru_conn)) = inner.conn_by_id[protocol_index].peek_lru() { + let lruk = *lruk; log_net!(debug "connection lru out: {:?}", lru_conn); - out_conn = Some(lru_conn); - Self::remove_connection_records(&mut *inner, *lruk); + out_conn = Some(Self::remove_connection_records(&mut *inner, lruk)); } } From 422a64570868b93cb0d5a88efef839f452f17db3 Mon Sep 17 00:00:00 2001 From: John Smith Date: Sun, 30 Jul 2023 16:45:20 -0400 Subject: [PATCH 32/42] python and api fixes --- veilid-core/src/storage_manager/mod.rs | 5 ++- veilid-python/tests/test_dht.py | 54 +++++++++++--------------- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/veilid-core/src/storage_manager/mod.rs b/veilid-core/src/storage_manager/mod.rs index b6dad7a2..4b21abb7 100644 --- a/veilid-core/src/storage_manager/mod.rs +++ b/veilid-core/src/storage_manager/mod.rs @@ -347,8 +347,9 @@ impl StorageManager { if last_signed_value_data.value_data().data() == &data && last_signed_value_data.value_data().writer() == &writer.key { - // Data and writer is the name, nothing is changing, just return the same ValueData - return Ok(Some(last_signed_value_data.into_value_data())); + // Data and writer is the same, nothing is changing, + // just return that we set it, but no network activity needs to happen + return Ok(None); } let seq = last_signed_value_data.value_data().seq(); ValueData::new_with_seq(seq + 1, data, writer.key) diff --git a/veilid-python/tests/test_dht.py b/veilid-python/tests/test_dht.py index 8faae302..713f5675 100644 --- a/veilid-python/tests/test_dht.py +++ b/veilid-python/tests/test_dht.py @@ -7,7 +7,9 @@ import json from . import * ################################################################## -BOGUS_KEY = veilid.TypedKey.from_value(veilid.CryptoKind.CRYPTO_KIND_VLD0, veilid.PublicKey.from_bytes(b' ')) +BOGUS_KEY = veilid.TypedKey.from_value( + veilid.CryptoKind.CRYPTO_KIND_VLD0, veilid.PublicKey.from_bytes(b' ')) + @pytest.mark.asyncio async def test_get_dht_value_unopened(api_connection: veilid.VeilidAPI): @@ -24,6 +26,7 @@ async def test_open_dht_record_nonexistent_no_writer(api_connection: veilid.Veil with pytest.raises(veilid.VeilidAPIError): out = await rc.open_dht_record(BOGUS_KEY, None) + @pytest.mark.asyncio async def test_close_dht_record_nonexistent(api_connection: veilid.VeilidAPI): rc = await api_connection.new_routing_context() @@ -31,13 +34,15 @@ async def test_close_dht_record_nonexistent(api_connection: veilid.VeilidAPI): with pytest.raises(veilid.VeilidAPIError): await rc.close_dht_record(BOGUS_KEY) + @pytest.mark.asyncio async def test_delete_dht_record_nonexistent(api_connection: veilid.VeilidAPI): rc = await api_connection.new_routing_context() async with rc: with pytest.raises(veilid.VeilidAPIError): await rc.delete_dht_record(BOGUS_KEY) - + + @pytest.mark.asyncio async def test_create_delete_dht_record_simple(api_connection: veilid.VeilidAPI): rc = await api_connection.new_routing_context() @@ -46,6 +51,7 @@ async def test_create_delete_dht_record_simple(api_connection: veilid.VeilidAPI) await rc.close_dht_record(rec.key) await rc.delete_dht_record(rec.key) + @pytest.mark.asyncio async def test_get_dht_value_nonexistent(api_connection: veilid.VeilidAPI): rc = await api_connection.new_routing_context() @@ -55,34 +61,34 @@ async def test_get_dht_value_nonexistent(api_connection: veilid.VeilidAPI): await rc.close_dht_record(rec.key) await rc.delete_dht_record(rec.key) + @pytest.mark.asyncio async def test_set_get_dht_value(api_connection: veilid.VeilidAPI): rc = await api_connection.new_routing_context() async with rc: rec = await rc.create_dht_record(veilid.DHTSchema.dflt(2)) - + vd = await rc.set_dht_value(rec.key, 0, b"BLAH BLAH BLAH") - assert vd != None - + assert vd == None + vd2 = await rc.get_dht_value(rec.key, 0, False) assert vd2 != None - + vd3 = await rc.get_dht_value(rec.key, 0, True) assert vd3 != None vd4 = await rc.get_dht_value(rec.key, 1, False) assert vd4 == None - print("vd: {}", vd.__dict__) print("vd2: {}", vd2.__dict__) print("vd3: {}", vd3.__dict__) - assert vd == vd2 assert vd2 == vd3 await rc.close_dht_record(rec.key) await rc.delete_dht_record(rec.key) + @pytest.mark.asyncio async def test_open_writer_dht_value(api_connection: veilid.VeilidAPI): rc = await api_connection.new_routing_context() @@ -104,10 +110,7 @@ async def test_open_writer_dht_value(api_connection: veilid.VeilidAPI): # Test subkey writes vdtemp = await rc.set_dht_value(key, 1, va) - assert vdtemp != None - assert vdtemp.data == va - assert vdtemp.seq == 0 - assert vdtemp.writer == owner + assert vdtemp == None vdtemp = await rc.get_dht_value(key, 1, False) assert vdtemp.data == va @@ -118,8 +121,7 @@ async def test_open_writer_dht_value(api_connection: veilid.VeilidAPI): assert vdtemp == None vdtemp = await rc.set_dht_value(key, 0, vb) - assert vdtemp.data == vb - assert vdtemp.seq == 0 + assert vdtemp == None vdtemp = await rc.get_dht_value(key, 0, True) assert vdtemp.data == vb @@ -129,17 +131,11 @@ async def test_open_writer_dht_value(api_connection: veilid.VeilidAPI): # Equal value should not trigger sequence number update vdtemp = await rc.set_dht_value(key, 1, va) - assert vdtemp != None - assert vdtemp.data == va - assert vdtemp.seq == 0 - assert vdtemp.writer == owner + assert vdtemp == None # Different value should trigger sequence number update vdtemp = await rc.set_dht_value(key, 1, vb) - assert vdtemp != None - assert vdtemp.data == vb - assert vdtemp.seq == 1 - assert vdtemp.writer == owner + assert vdtemp == None # Now that we initialized some subkeys # and verified they stored correctly @@ -166,11 +162,8 @@ async def test_open_writer_dht_value(api_connection: veilid.VeilidAPI): # Verify subkey 1 can be set a second time and it updates because seq is newer vdtemp = await rc.set_dht_value(key, 1, vc) - assert vdtemp != None - assert vdtemp.data == vc - assert vdtemp.seq == 2 - assert vdtemp.writer == owner - + assert vdtemp == None + # Verify the network got the subkey update with a refresh check vdtemp = await rc.get_dht_value(key, 1, True) assert vdtemp != None @@ -183,7 +176,7 @@ async def test_open_writer_dht_value(api_connection: veilid.VeilidAPI): await rc.close_dht_record(key) await rc.delete_dht_record(key) - + rec = await rc.open_dht_record(key, other_keypair) assert rec != None assert rec.key == key @@ -195,12 +188,11 @@ async def test_open_writer_dht_value(api_connection: veilid.VeilidAPI): # Verify subkey 1 can NOT be set because we have the wrong writer with pytest.raises(veilid.VeilidAPIError): vdtemp = await rc.set_dht_value(key, 1, va) - + # Verify subkey 0 can NOT be set because we have the wrong writer with pytest.raises(veilid.VeilidAPIError): vdtemp = await rc.set_dht_value(key, 0, va) - + # Clean up await rc.close_dht_record(key) await rc.delete_dht_record(key) - From ffdd885aa4b911ff4b84ba87d78e25fbfb0562e5 Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Sun, 30 Jul 2023 17:20:36 -0400 Subject: [PATCH 33/42] test --- veilid-core/src/tests/files/flag.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 veilid-core/src/tests/files/flag.txt diff --git a/veilid-core/src/tests/files/flag.txt b/veilid-core/src/tests/files/flag.txt new file mode 100644 index 00000000..f7ab4830 --- /dev/null +++ b/veilid-core/src/tests/files/flag.txt @@ -0,0 +1 @@ +Ray says "Go SubSix!" \ No newline at end of file From 6a9b644463c1f7690e461c428dba36209c678d9a Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Sun, 30 Jul 2023 17:22:47 -0400 Subject: [PATCH 34/42] 0.1.7 release notes --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f169520..84628f07 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +**Changes in Veilid 0.1.7** + +- Fix for connection table crash +- Fix for incorrect set_dht_value return value +- Python test updates +- Various VeilidChat-prompted veilid-flutter updates + **Changes in Veilid 0.1.6** - Fix for 'find_node' too many nodes returned issue From 8a287d13efb146094eb8bbac06a22641afaa97a7 Mon Sep 17 00:00:00 2001 From: John Smith Date: Sun, 30 Jul 2023 19:33:58 -0400 Subject: [PATCH 35/42] 0.1.7 --- .bumpversion.cfg | 2 +- Cargo.lock | 104 ++++++++++++++++----------------- veilid-cli/Cargo.toml | 2 +- veilid-core/Cargo.toml | 2 +- veilid-flutter/pubspec.yaml | 2 +- veilid-flutter/rust/Cargo.toml | 2 +- veilid-python/pyproject.toml | 2 +- veilid-server/Cargo.toml | 2 +- veilid-tools/Cargo.toml | 2 +- veilid-wasm/Cargo.toml | 2 +- 10 files changed, 58 insertions(+), 64 deletions(-) diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 65e96b9b..bd23adfd 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.1.6 +current_version = 0.1.7 [bumpversion:file:veilid-server/Cargo.toml] search = name = "veilid-server" diff --git a/Cargo.lock b/Cargo.lock index ed055337..c8247f0e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1294,7 +1294,7 @@ dependencies = [ "flexi_logger", "lazy_static", "log", - "time 0.3.23", + "time 0.3.24", "unicode-width", ] @@ -1336,7 +1336,7 @@ dependencies = [ "owning_ref", "serde_json", "serde_yaml", - "time 0.3.23", + "time 0.3.24", "tokio", "toml 0.7.6", "unicode-segmentation", @@ -1463,6 +1463,12 @@ version = "2.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2e66c9d817f1720209181c316d28635c050fa304f9c79e47a520882661b7308" +[[package]] +name = "deranged" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8810e7e2cf385b1e9b50d68264908ec367ba642c96d02edfe61c39e88e2a3c01" + [[package]] name = "derivative" version = "2.2.0" @@ -1676,9 +1682,9 @@ checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] name = "errno" -version = "0.3.1" +version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" +checksum = "6b30f669a7961ef1631673d2766cc92f52d64f7ef354d4fe0ddfd30ed52f0f4f" dependencies = [ "errno-dragonfly", "libc", @@ -1820,7 +1826,7 @@ dependencies = [ "regex", "rustversion", "thiserror", - "time 0.3.23", + "time 0.3.24", ] [[package]] @@ -2758,9 +2764,9 @@ dependencies = [ [[package]] name = "libz-sys" -version = "1.1.9" +version = "1.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56ee889ecc9568871456d42f603d6a0ce59ff328d291063a45cbdf0036baf6db" +checksum = "d97137b25e321a73eef1418d1d5d2eda4d77e12813f8e6dead84bc52c5870a7b" dependencies = [ "cc", "libc", @@ -2852,9 +2858,9 @@ checksum = "2532096657941c2fea9c289d370a250971c689d4f143798ff67113ec042024a5" [[package]] name = "matchit" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b87248edafb776e59e6ee64a79086f65890d3510f2c656c000bf2a7e8a0aea40" +checksum = "67827e6ea8ee8a7c4a72227ef4fc08957040acffdb5f122733b24fa12daff41b" [[package]] name = "memchr" @@ -3858,9 +3864,9 @@ checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" [[package]] name = "quote" -version = "1.0.31" +version = "1.0.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5fe8a65d69dd0808184ebb5f836ab526bb259db23c657efa38711b1072ee47f0" +checksum = "50f3b39ccfb720540debaa0164757101c08ecb8d326b15358ce76a62c7e85965" dependencies = [ "proc-macro2", ] @@ -3991,7 +3997,7 @@ checksum = "b2eae68fc220f7cf2532e4494aded17545fce192d59cd996e0fe7887f4ceb575" dependencies = [ "aho-corasick", "memchr", - "regex-automata 0.3.3", + "regex-automata 0.3.4", "regex-syntax 0.7.4", ] @@ -4006,9 +4012,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.3" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39354c10dd07468c2e73926b23bb9c2caca74c5501e38a35da70406f1d923310" +checksum = "b7b6d6190b7594385f61bd3911cd1be99dfddcfc365a4160cc2ab5bff4aed294" dependencies = [ "aho-corasick", "memchr", @@ -4237,16 +4243,6 @@ dependencies = [ "base64 0.21.2", ] -[[package]] -name = "rustls-webpki" -version = "0.101.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15f36a6828982f422756984e47912a7a51dcbc2a197aa791158f8ca61cd8204e" -dependencies = [ - "ring", - "untrusted", -] - [[package]] name = "rustversion" version = "1.0.14" @@ -4405,9 +4401,9 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.174" +version = "1.0.178" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3b88756493a5bd5e5395d53baa70b194b05764ab85b59e43e4b8f4e1192fa9b1" +checksum = "60363bdd39a7be0266a520dab25fdc9241d2f987b08a01e01f0ec6d06a981348" dependencies = [ "serde_derive", ] @@ -4433,9 +4429,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.174" +version = "1.0.178" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e5c3a298c7f978e53536f95a63bdc4c4a64550582f31a0359a9afda6aede62e" +checksum = "f28482318d6641454cb273da158647922d1be6b5a2fcc6165cd89ebdd7ed576b" dependencies = [ "proc-macro2", "quote", @@ -4455,9 +4451,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.103" +version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d03b412469450d4404fe8499a268edd7f8b79fecb074b0d812ad64ca21f4031b" +checksum = "076066c5f1078eac5b722a31827a8832fe108bed65dfa75e233c89f8206e976c" dependencies = [ "itoa", "ryu", @@ -4466,9 +4462,9 @@ dependencies = [ [[package]] name = "serde_repr" -version = "0.1.15" +version = "0.1.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e168eaaf71e8f9bd6037feb05190485708e019f4fd87d161b3c0a0d37daf85e5" +checksum = "8725e1dfadb3a50f7e5ce0b1a540466f6ed3fe7a0fca2ac2b8b831d31316bd00" dependencies = [ "proc-macro2", "quote", @@ -4662,9 +4658,9 @@ checksum = "74233d3b3b2f6d4b006dc19dee745e73e2a6bfb6f93607cd3b02bd5b00797d7c" [[package]] name = "simd-adler32" -version = "0.3.5" +version = "0.3.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "238abfbb77c1915110ad968465608b68e869e0772622c9656714e73e5a1a522f" +checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe" [[package]] name = "simplelog" @@ -4674,7 +4670,7 @@ checksum = "acee08041c5de3d5048c8b3f6f13fafb3026b24ba43c6a695a0c76179b844369" dependencies = [ "log", "termcolor", - "time 0.3.23", + "time 0.3.24", ] [[package]] @@ -5003,16 +4999,17 @@ dependencies = [ [[package]] name = "time" -version = "0.3.23" +version = "0.3.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59e399c068f43a5d116fedaf73b203fa4f9c519f17e2b34f63221d3792f81446" +checksum = "b79eabcd964882a646b3584543ccabeae7869e9ac32a46f6f22b7a5bd405308b" dependencies = [ + "deranged", "itoa", "libc", "num_threads", "serde", "time-core", - "time-macros 0.2.10", + "time-macros 0.2.11", ] [[package]] @@ -5033,9 +5030,9 @@ dependencies = [ [[package]] name = "time-macros" -version = "0.2.10" +version = "0.2.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "96ba15a897f3c86766b757e5ac7221554c6750054d74d5b28844fce5fb36a6c4" +checksum = "eb71511c991639bb078fd5bf97757e03914361c48100d52878b8e52b46fb92cd" dependencies = [ "time-core", ] @@ -5304,7 +5301,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09d48f71a791638519505cefafe162606f706c25592e4bde4d97600c0195312e" dependencies = [ "crossbeam-channel", - "time 0.3.23", + "time 0.3.24", "tracing-subscriber", ] @@ -5651,7 +5648,7 @@ checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" [[package]] name = "veilid-cli" -version = "0.1.6" +version = "0.1.7" dependencies = [ "arboard", "async-std", @@ -5687,7 +5684,7 @@ dependencies = [ [[package]] name = "veilid-core" -version = "0.1.6" +version = "0.1.7" dependencies = [ "argon2", "async-io", @@ -5779,7 +5776,7 @@ dependencies = [ "weak-table", "web-sys", "webpki 0.22.0", - "webpki-roots 0.24.0", + "webpki-roots 0.25.1", "wee_alloc", "winapi", "windows 0.38.0", @@ -5790,7 +5787,7 @@ dependencies = [ [[package]] name = "veilid-flutter" -version = "0.1.6" +version = "0.1.7" dependencies = [ "allo-isolate", "async-std", @@ -5819,7 +5816,7 @@ dependencies = [ [[package]] name = "veilid-server" -version = "0.1.6" +version = "0.1.7" dependencies = [ "ansi_term", "async-std", @@ -5869,7 +5866,7 @@ dependencies = [ [[package]] name = "veilid-tools" -version = "0.1.6" +version = "0.1.7" dependencies = [ "android-logd-logger", "async-lock", @@ -5920,7 +5917,7 @@ dependencies = [ [[package]] name = "veilid-wasm" -version = "0.1.6" +version = "0.1.7" dependencies = [ "cfg-if 1.0.0", "console_error_panic_hook", @@ -6156,12 +6153,9 @@ dependencies = [ [[package]] name = "webpki-roots" -version = "0.24.0" +version = "0.25.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b291546d5d9d1eab74f069c77749f2cb8504a12caa20f0f2de93ddbf6f411888" -dependencies = [ - "rustls-webpki", -] +checksum = "c9c6eda1c830a36f361e7721c87fd79ea84293b54f8c48c959f85ec636f0f196" [[package]] name = "wee_alloc" @@ -6462,9 +6456,9 @@ checksum = "1a515f5799fe4961cb532f983ce2b23082366b898e52ffbce459c86f67c8378a" [[package]] name = "winnow" -version = "0.5.0" +version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81fac9742fd1ad1bd9643b991319f72dd031016d44b77039a26977eb667141e7" +checksum = "8bd122eb777186e60c3fdf765a58ac76e41c582f1f535fbf3314434c6b58f3f7" dependencies = [ "memchr", ] diff --git a/veilid-cli/Cargo.toml b/veilid-cli/Cargo.toml index 82b45638..6bebace4 100644 --- a/veilid-cli/Cargo.toml +++ b/veilid-cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-cli" -version = "0.1.6" +version = "0.1.7" authors = ["Veilid Team "] edition = "2021" license = "MPL-2.0" diff --git a/veilid-core/Cargo.toml b/veilid-core/Cargo.toml index da028eb2..9309923c 100644 --- a/veilid-core/Cargo.toml +++ b/veilid-core/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-core" -version = "0.1.6" +version = "0.1.7" authors = ["Veilid Team "] edition = "2021" build = "build.rs" diff --git a/veilid-flutter/pubspec.yaml b/veilid-flutter/pubspec.yaml index daea35ca..efe8114f 100644 --- a/veilid-flutter/pubspec.yaml +++ b/veilid-flutter/pubspec.yaml @@ -1,5 +1,5 @@ name: veilid -version: 0.1.6 +version: 0.1.7 description: Veilid Framework homepage: https://veilid.com publish_to: "none" # Remove this line if you wish to publish to pub.dev diff --git a/veilid-flutter/rust/Cargo.toml b/veilid-flutter/rust/Cargo.toml index 61f4d495..b623fa93 100644 --- a/veilid-flutter/rust/Cargo.toml +++ b/veilid-flutter/rust/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-flutter" -version = "0.1.6" +version = "0.1.7" authors = ["Veilid Team "] license = "MPL-2.0" edition = "2021" diff --git a/veilid-python/pyproject.toml b/veilid-python/pyproject.toml index 4209cfba..345fa903 100644 --- a/veilid-python/pyproject.toml +++ b/veilid-python/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "veilid" -version = "0.1.6" +version = "0.1.7" description = "" authors = ["Veilid Team "] readme = "README.md" diff --git a/veilid-server/Cargo.toml b/veilid-server/Cargo.toml index a2502104..2a1a355f 100644 --- a/veilid-server/Cargo.toml +++ b/veilid-server/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-server" -version = "0.1.6" +version = "0.1.7" authors = ["Veilid Team "] license = "MPL-2.0" edition = "2021" diff --git a/veilid-tools/Cargo.toml b/veilid-tools/Cargo.toml index 4bba6dde..71e6dfb9 100644 --- a/veilid-tools/Cargo.toml +++ b/veilid-tools/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-tools" -version = "0.1.6" +version = "0.1.7" authors = ["Veilid Team "] license = "MPL-2.0" edition = "2021" diff --git a/veilid-wasm/Cargo.toml b/veilid-wasm/Cargo.toml index b656bd78..53d7e285 100644 --- a/veilid-wasm/Cargo.toml +++ b/veilid-wasm/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "veilid-wasm" -version = "0.1.6" +version = "0.1.7" authors = ["Veilid Team "] license = "MPL-2.0" edition = "2021" From 9fc114c21d35fbbc3ad150c500f369d5703bcef5 Mon Sep 17 00:00:00 2001 From: TC Date: Tue, 1 Aug 2023 00:59:23 +0000 Subject: [PATCH 36/42] Adding release stage triggered by existence of a tag that matches the v+semver format --- .gitlab-ci.yml | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ff740a31..7658a260 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,6 +7,7 @@ variables: stages: - test - build_packages + - release - distribute #before_script: @@ -51,6 +52,8 @@ delete_test_machine: create_build_machines: stage: build_packages + rules: + - if: '$CI_COMMIT_BRANCH =~ /v\d.+/' only: - stable tags: @@ -62,6 +65,8 @@ create_build_machines: package_amd64_deb: stage: build_packages + rules: + - if: '$CI_COMMIT_BRANCH =~ /v\d.+/' only: - stable needs: @@ -75,6 +80,8 @@ package_amd64_deb: package_arm64_deb: stage: build_packages + rules: + - if: '$CI_COMMIT_BRANCH =~ /v\d.+/' only: - stable needs: @@ -88,6 +95,8 @@ package_arm64_deb: package_amd64_rpm: stage: build_packages + rules: + - if: '$CI_COMMIT_BRANCH =~ /v\d.+/' only: - stable needs: @@ -99,8 +108,21 @@ package_amd64_rpm: - earthly +package-linux-amd64-rpm - /home/gitlab-runner/scp-to-orchestrator.sh +release_job: + stage: release + image: registry.gitlab.com/gitlab-org/release-cli:latest + rules: + - if: '$CI_COMMIT_BRANCH =~ /v\d.+/' # Run this job when a tag is created + script: + - echo "running release_job" + release: # See https://docs.gitlab.com/ee/ci/yaml/#release for available properties + tag_name: '$CI_COMMIT_TAG' + description: '$CI_COMMIT_TAG' + build_repositories: stage: distribute + rules: + - if: '$CI_COMMIT_BRANCH =~ /v\d.+/' only: - stable tags: @@ -110,6 +132,8 @@ build_repositories: deploy_repos: stage: distribute + rules: + - if: '$CI_COMMIT_BRANCH =~ /v\d.+/' only: - stable needs: @@ -121,6 +145,8 @@ deploy_repos: delete_build_machines: stage: distribute + rules: + - if: '$CI_COMMIT_BRANCH =~ /v\d.+/' only: - stable tags: From f0f60318432b7a1e5de5e3e97b5f6ca9ad9e0832 Mon Sep 17 00:00:00 2001 From: TC Date: Tue, 1 Aug 2023 01:08:41 +0000 Subject: [PATCH 37/42] Adding release stage triggered by existence of a tag that matches the v+semver format --- .gitlab-ci.yml | 50 ++++++++++++++++++-------------------------------- 1 file changed, 18 insertions(+), 32 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7658a260..bafff791 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -52,23 +52,17 @@ delete_test_machine: create_build_machines: stage: build_packages - rules: - - if: '$CI_COMMIT_BRANCH =~ /v\d.+/' - only: - - stable tags: - build-orchestration script: - /home/gitlab-runner/build-machine-ctl.sh create amd64-deb - /home/gitlab-runner/build-machine-ctl.sh create arm64-deb - /home/gitlab-runner/build-machine-ctl.sh create amd64-rpm - + rules: + - if: '$CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' + package_amd64_deb: stage: build_packages - rules: - - if: '$CI_COMMIT_BRANCH =~ /v\d.+/' - only: - - stable needs: - create_build_machines tags: @@ -77,13 +71,11 @@ package_amd64_deb: - earthly bootstrap - earthly +package-linux-amd64-deb - /home/gitlab-runner/scp-to-orchestrator.sh + rules: + - if: '$CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' package_arm64_deb: stage: build_packages - rules: - - if: '$CI_COMMIT_BRANCH =~ /v\d.+/' - only: - - stable needs: - create_build_machines tags: @@ -92,13 +84,11 @@ package_arm64_deb: - earthly bootstrap - earthly +package-linux-arm64-deb - /home/gitlab-runner/scp-to-orchestrator.sh + rules: + - if: '$CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' package_amd64_rpm: stage: build_packages - rules: - - if: '$CI_COMMIT_BRANCH =~ /v\d.+/' - only: - - stable needs: - create_build_machines tags: @@ -107,51 +97,47 @@ package_amd64_rpm: - earthly bootstrap - earthly +package-linux-amd64-rpm - /home/gitlab-runner/scp-to-orchestrator.sh + rules: + - if: '$CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' release_job: stage: release - image: registry.gitlab.com/gitlab-org/release-cli:latest - rules: - - if: '$CI_COMMIT_BRANCH =~ /v\d.+/' # Run this job when a tag is created + image: registry.gitlab.com/gitlab-org/release-cli:latest script: - echo "running release_job" release: # See https://docs.gitlab.com/ee/ci/yaml/#release for available properties tag_name: '$CI_COMMIT_TAG' description: '$CI_COMMIT_TAG' + rules: + - if: '$CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' build_repositories: stage: distribute - rules: - - if: '$CI_COMMIT_BRANCH =~ /v\d.+/' - only: - - stable tags: - build-orchestration script: - /home/gitlab-runner/distribute-packages.sh + rules: + - if: '$CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' deploy_repos: stage: distribute - rules: - - if: '$CI_COMMIT_BRANCH =~ /v\d.+/' - only: - - stable needs: - build_repositories tags: - repo-server script: - /home/gitlab-runner/deploy-repo.sh + rules: + - if: '$CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' delete_build_machines: stage: distribute - rules: - - if: '$CI_COMMIT_BRANCH =~ /v\d.+/' - only: - - stable tags: - build-orchestration script: - /home/gitlab-runner/build-machine-ctl.sh delete amd64-deb - /home/gitlab-runner/build-machine-ctl.sh delete arm64-deb - /home/gitlab-runner/build-machine-ctl.sh delete amd64-rpm + rules: + - if: '$CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' \ No newline at end of file From e36434fa3696656f23d31f19cdc2c99cf7aad0e9 Mon Sep 17 00:00:00 2001 From: TC Date: Tue, 1 Aug 2023 01:19:47 +0000 Subject: [PATCH 38/42] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index bafff791..2770c3c6 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -59,7 +59,7 @@ create_build_machines: - /home/gitlab-runner/build-machine-ctl.sh create arm64-deb - /home/gitlab-runner/build-machine-ctl.sh create amd64-rpm rules: - - if: '$CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' + - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' package_amd64_deb: stage: build_packages @@ -72,7 +72,7 @@ package_amd64_deb: - earthly +package-linux-amd64-deb - /home/gitlab-runner/scp-to-orchestrator.sh rules: - - if: '$CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' + - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' package_arm64_deb: stage: build_packages @@ -85,7 +85,7 @@ package_arm64_deb: - earthly +package-linux-arm64-deb - /home/gitlab-runner/scp-to-orchestrator.sh rules: - - if: '$CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' + - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' package_amd64_rpm: stage: build_packages @@ -98,7 +98,7 @@ package_amd64_rpm: - earthly +package-linux-amd64-rpm - /home/gitlab-runner/scp-to-orchestrator.sh rules: - - if: '$CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' + - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' release_job: stage: release @@ -109,7 +109,7 @@ release_job: tag_name: '$CI_COMMIT_TAG' description: '$CI_COMMIT_TAG' rules: - - if: '$CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' + - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' build_repositories: stage: distribute @@ -118,7 +118,7 @@ build_repositories: script: - /home/gitlab-runner/distribute-packages.sh rules: - - if: '$CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' + - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' deploy_repos: stage: distribute @@ -129,7 +129,7 @@ deploy_repos: script: - /home/gitlab-runner/deploy-repo.sh rules: - - if: '$CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' + - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' delete_build_machines: stage: distribute @@ -140,4 +140,4 @@ delete_build_machines: - /home/gitlab-runner/build-machine-ctl.sh delete arm64-deb - /home/gitlab-runner/build-machine-ctl.sh delete amd64-rpm rules: - - if: '$CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' \ No newline at end of file + - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' \ No newline at end of file From 3c68899b754ea2705893836f3fd0042ceacbe128 Mon Sep 17 00:00:00 2001 From: TC Date: Tue, 1 Aug 2023 01:23:36 +0000 Subject: [PATCH 39/42] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 2770c3c6..a277a335 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -50,6 +50,17 @@ delete_test_machine: script: - /home/gitlab-runner/build-machine-ctl.sh delete amd64-deb +release_job: + stage: release + image: registry.gitlab.com/gitlab-org/release-cli:latest + script: + - echo "running release_job" + release: # See https://docs.gitlab.com/ee/ci/yaml/#release for available properties + tag_name: '$CI_COMMIT_TAG' + description: '$CI_COMMIT_TAG' + rules: + - if: '$CI_COMMIT_TAG =~ /v\d.+/' + create_build_machines: stage: build_packages tags: @@ -59,7 +70,7 @@ create_build_machines: - /home/gitlab-runner/build-machine-ctl.sh create arm64-deb - /home/gitlab-runner/build-machine-ctl.sh create amd64-rpm rules: - - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' + - if: '$CI_COMMIT_TAG =~ /v\d.+/' package_amd64_deb: stage: build_packages @@ -72,7 +83,7 @@ package_amd64_deb: - earthly +package-linux-amd64-deb - /home/gitlab-runner/scp-to-orchestrator.sh rules: - - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' + - if: '$CI_COMMIT_TAG =~ /v\d.+/' package_arm64_deb: stage: build_packages @@ -85,7 +96,7 @@ package_arm64_deb: - earthly +package-linux-arm64-deb - /home/gitlab-runner/scp-to-orchestrator.sh rules: - - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' + - if: '$CI_COMMIT_TAG =~ /v\d.+/' package_amd64_rpm: stage: build_packages @@ -98,18 +109,7 @@ package_amd64_rpm: - earthly +package-linux-amd64-rpm - /home/gitlab-runner/scp-to-orchestrator.sh rules: - - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' - -release_job: - stage: release - image: registry.gitlab.com/gitlab-org/release-cli:latest - script: - - echo "running release_job" - release: # See https://docs.gitlab.com/ee/ci/yaml/#release for available properties - tag_name: '$CI_COMMIT_TAG' - description: '$CI_COMMIT_TAG' - rules: - - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' + - if: '$CI_COMMIT_TAG =~ /v\d.+/' build_repositories: stage: distribute @@ -118,7 +118,7 @@ build_repositories: script: - /home/gitlab-runner/distribute-packages.sh rules: - - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' + - if: '$CI_COMMIT_TAG =~ /v\d.+/' deploy_repos: stage: distribute @@ -129,7 +129,7 @@ deploy_repos: script: - /home/gitlab-runner/deploy-repo.sh rules: - - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' + - if: '$CCI_COMMIT_TAG =~ /v\d.+/' delete_build_machines: stage: distribute @@ -140,4 +140,4 @@ delete_build_machines: - /home/gitlab-runner/build-machine-ctl.sh delete arm64-deb - /home/gitlab-runner/build-machine-ctl.sh delete amd64-rpm rules: - - if: '$CI_COMMIT_BRANCH == "main" && $CI_COMMIT_BRANCH =~ /v\d.+/' \ No newline at end of file + - if: '$CI_COMMIT_TAG =~ /v\d.+/' \ No newline at end of file From aedafaebf660325c079acc7050e21c764acbf18b Mon Sep 17 00:00:00 2001 From: TC Date: Tue, 1 Aug 2023 02:44:28 +0000 Subject: [PATCH 40/42] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a277a335..a61ba4de 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -52,7 +52,9 @@ delete_test_machine: release_job: stage: release - image: registry.gitlab.com/gitlab-org/release-cli:latest + image: registry.gitlab.com/gitlab-org/release-cli:latest + tags: + - build-orchestration script: - echo "running release_job" release: # See https://docs.gitlab.com/ee/ci/yaml/#release for available properties From 1227579048a4652845b660dfea931b9b774399db Mon Sep 17 00:00:00 2001 From: TC Date: Tue, 1 Aug 2023 03:25:58 +0000 Subject: [PATCH 41/42] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a61ba4de..4a90a1f4 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -52,9 +52,7 @@ delete_test_machine: release_job: stage: release - image: registry.gitlab.com/gitlab-org/release-cli:latest - tags: - - build-orchestration + image: registry.gitlab.com/gitlab-org/release-cli:latest script: - echo "running release_job" release: # See https://docs.gitlab.com/ee/ci/yaml/#release for available properties @@ -86,6 +84,15 @@ package_amd64_deb: - /home/gitlab-runner/scp-to-orchestrator.sh rules: - if: '$CI_COMMIT_TAG =~ /v\d.+/' + +delete_amd64_deb_build_machine: + stage: build_packages + needs: + - package_amd64_deb + tags: + - build-orchestration + script: + - /home/gitlab-runner/build-machine-ctl.sh delete amd64-deb package_arm64_deb: stage: build_packages @@ -100,6 +107,15 @@ package_arm64_deb: rules: - if: '$CI_COMMIT_TAG =~ /v\d.+/' +delete_arm64_deb_build_machine: + stage: build_packages + needs: + - package_arm64_deb + tags: + - build-orchestration + script: + - /home/gitlab-runner/build-machine-ctl.sh delete arm64-deb + package_amd64_rpm: stage: build_packages needs: @@ -113,6 +129,15 @@ package_amd64_rpm: rules: - if: '$CI_COMMIT_TAG =~ /v\d.+/' +delete_amd64_rpm_build_machine: + stage: build_packages + needs: + - package_amd64_rpm + tags: + - build-orchestration + script: + - /home/gitlab-runner/build-machine-ctl.sh delete amd64-rpm + build_repositories: stage: distribute tags: @@ -131,15 +156,4 @@ deploy_repos: script: - /home/gitlab-runner/deploy-repo.sh rules: - - if: '$CCI_COMMIT_TAG =~ /v\d.+/' - -delete_build_machines: - stage: distribute - tags: - - build-orchestration - script: - - /home/gitlab-runner/build-machine-ctl.sh delete amd64-deb - - /home/gitlab-runner/build-machine-ctl.sh delete arm64-deb - - /home/gitlab-runner/build-machine-ctl.sh delete amd64-rpm - rules: - - if: '$CI_COMMIT_TAG =~ /v\d.+/' \ No newline at end of file + - if: '$CCI_COMMIT_TAG =~ /v\d.+/' \ No newline at end of file From 1f96a5317fa0ad5a3ccefdf02e7c085cf5b4650f Mon Sep 17 00:00:00 2001 From: TC Date: Tue, 1 Aug 2023 03:30:11 +0000 Subject: [PATCH 42/42] Update .gitlab-ci.yml file --- .gitlab-ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 4a90a1f4..bcb6f652 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -156,4 +156,4 @@ deploy_repos: script: - /home/gitlab-runner/deploy-repo.sh rules: - - if: '$CCI_COMMIT_TAG =~ /v\d.+/' \ No newline at end of file + - if: '$CI_COMMIT_TAG =~ /v\d.+/' \ No newline at end of file