import 'dart:async'; import 'dart:typed_data'; import 'package:equatable/equatable.dart'; import 'package:fixnum/fixnum.dart'; import 'package:freezed_annotation/freezed_annotation.dart'; ////////////////////////////////////////////////////////// import 'routing_context.dart'; import 'veilid_config.dart'; import 'veilid_crypto.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 'value_subkey_range.dart'; export 'veilid.dart'; export 'veilid_api_exception.dart'; export 'veilid_config.dart'; export 'veilid_crypto.dart'; export 'veilid_encoding.dart'; export 'veilid_state.dart'; export 'veilid_table_db.dart'; ////////////////////////////////////// /// JSON Encode Helper List Function(dynamic) jsonListConstructor( T Function(dynamic) jsonConstructor) => (dynamic j) => (j as List).map(jsonConstructor).toList(); List? Function(dynamic) optJsonListConstructor( T Function(dynamic) jsonConstructor) => (dynamic j) => j == null ? null : (j as List).map(jsonConstructor).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]; } ////////////////////////////////////// /// Timestamp @immutable class Timestamp extends Equatable implements Comparable { const Timestamp({required this.value}); factory Timestamp.zero() => Timestamp(value: BigInt.zero); factory Timestamp.fromInt64(Int64 i64) => Timestamp( value: (BigInt.from((i64 >> 32).toUnsigned(32).toInt()) << 32) | BigInt.from(i64.toUnsigned(32).toInt())); 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]; @override int compareTo(Timestamp other) => value.compareTo(other.value); bool operator <(Timestamp other) => compareTo(other) < 0; bool operator <=(Timestamp other) => compareTo(other) <= 0; bool operator >(Timestamp other) => compareTo(other) > 0; bool operator >=(Timestamp other) => compareTo(other) >= 0; @override String toString() => value.toString(); String toJson() => toString(); Int64 toInt64() => Int64.fromInts( (value >> 32).toUnsigned(32).toInt(), value.toUnsigned(32).toInt()); TimestampDuration diff(Timestamp other) => TimestampDuration(value: value - other.value); Timestamp offset(TimestampDuration dur) => Timestamp(value: value + dur.value); } @immutable class TimestampDuration extends Equatable implements Comparable { const TimestampDuration({required this.value}); factory TimestampDuration.fromInt64(Int64 i64) => TimestampDuration( value: (BigInt.from((i64 >> 32).toUnsigned(32).toInt()) << 32) | BigInt.from(i64.toUnsigned(32).toInt())); factory TimestampDuration.fromMillis(int millis) => TimestampDuration(value: BigInt.from(millis) * BigInt.from(1000)); factory TimestampDuration.fromDuration(Duration d) => TimestampDuration( value: BigInt.from(d.inSeconds) * BigInt.from(1000000) + BigInt.from(d.inMicroseconds % 1000000)); 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]; @override int compareTo(TimestampDuration other) => value.compareTo(other.value); bool operator <(TimestampDuration other) => compareTo(other) < 0; bool operator <=(TimestampDuration other) => compareTo(other) <= 0; bool operator >(TimestampDuration other) => compareTo(other) > 0; bool operator >=(TimestampDuration other) => compareTo(other) >= 0; @override String toString() { final biDay = BigInt.from(1000000) * BigInt.from(60 * 60 * 24); final biHour = BigInt.from(1000000) * BigInt.from(60 * 60); final biMin = BigInt.from(1000000) * BigInt.from(60); final biSec = BigInt.from(1000000); final biMsec = BigInt.from(1000); final days = (value ~/ biDay).toInt(); final dvalue = value % biDay; final hours = (dvalue ~/ biHour).toInt(); final hvalue = dvalue % biHour; final mins = (hvalue ~/ biMin).toInt(); final mvalue = hvalue % biMin; final secs = (mvalue ~/ biSec).toInt(); final svalue = mvalue % biSec; final msecs = (svalue ~/ biMsec).toInt(); final uvalue = svalue % biMsec; if (days == 0 && hours == 0 && mins == 0 && secs == 0) { // microseconds format return '$msecs.${uvalue.toString().padLeft(3, '0')}ms'; } var out = ''; if (days != 0) { out += '${days}d'; } if (hours != 0) { out += '${hours}h'; } if (mins != 0) { out += '${mins}m'; } return '$out$secs.${msecs.toString().padLeft(3, '0')}s'; } String toJson() => value.toString(); Int64 toInt64() => Int64.fromInts( (value >> 32).toUnsigned(32).toInt(), value.toUnsigned(32).toInt()); double toMillis() => value / BigInt.from(1000); double toSecs() => value / BigInt.from(1000000); BigInt toMicros() => value; } ////////////////////////////////////// /// Veilid singleton factory abstract class Veilid { static Veilid instance = getVeilid(); void initializeVeilidCore(Map platformConfigJson); void changeLogLevel(String layer, VeilidConfigLogLevel logLevel); void changeLogIgnore(String layer, List changes); Future> startupVeilidCore(VeilidConfig config); Future getVeilidState(); Future isShutdown(); Future attach(); Future detach(); Future shutdownVeilidCore(); // Crypto List validCryptoKinds(); Future getCryptoSystem(CryptoKind kind); Future bestCryptoSystem(); Future?> verifySignatures(List publicKeys, Uint8List data, List signatures); Future> generateSignatures( Uint8List data, List keyPairs); Future generateKeyPair(CryptoKind kind); // Routing context Future routingContext(); Future safeRoutingContext( {Stability stability = Stability.lowLatency, Sequencing sequencing = Sequencing.preferOrdered}) async { final rc = await routingContext(); final originalSafety = await rc.safety() as SafetySelectionSafe; final safetySpec = originalSafety.safetySpec .copyWith(stability: stability, sequencing: sequencing); return rc.withSafety(SafetySelectionSafe(safetySpec: safetySpec), closeSelf: true); } Future unsafeRoutingContext( {Sequencing sequencing = Sequencing.preferOrdered}) async => (await routingContext()) .withSafety(SafetySelectionUnsafe(sequencing: sequencing)); // Private route allocation Future newPrivateRoute(); Future newCustomPrivateRoute( Stability stability, Sequencing sequencing); Future importRemotePrivateRoute(Uint8List blob); Future releasePrivateRoute(String routeId); // App calls Future appCallReply(String callId, Uint8List message); // TableStore Future openTableDB(String name, int columnCount); Future deleteTableDB(String name); // Misc Timestamp now(); String veilidVersionString(); VeilidVersion veilidVersion(); String defaultVeilidConfig(); Future debug(String command); /// Return the features that were enabled when veilid-core was built. List veilidFeatures(); }