veilid/veilid-flutter/lib/veilid.dart

167 lines
5.1 KiB
Dart
Raw Normal View History

2022-01-16 11:19:01 -05:00
import 'dart:async';
2022-09-30 22:37:55 -04:00
import 'dart:typed_data';
2022-02-06 21:18:42 -05:00
2023-07-05 18:48:06 -04:00
import 'package:equatable/equatable.dart';
2023-08-02 21:09:47 -04:00
import 'package:fixnum/fixnum.dart';
2023-10-06 16:41:25 -04:00
import 'package:freezed_annotation/freezed_annotation.dart';
2023-07-05 18:48:06 -04:00
2022-06-15 21:51:38 -04:00
//////////////////////////////////////////////////////////
2023-05-29 15:24:57 -04:00
import 'routing_context.dart';
import 'veilid_config.dart';
import 'veilid_crypto.dart';
import 'veilid_state.dart';
2023-07-26 14:20:17 -04:00
import 'veilid_stub.dart'
if (dart.library.io) 'veilid_ffi.dart'
if (dart.library.js) 'veilid_js.dart';
import 'veilid_table_db.dart';
2022-06-15 21:51:38 -04:00
2023-05-29 15:24:57 -04:00
export 'default_config.dart';
export 'routing_context.dart';
export 'value_subkey_range.dart';
2023-07-26 14:20:17 -04:00
export 'veilid.dart';
export 'veilid_api_exception.dart';
2023-05-29 15:24:57 -04:00
export 'veilid_config.dart';
export 'veilid_crypto.dart';
2023-07-26 14:20:17 -04:00
export 'veilid_encoding.dart';
2023-05-29 15:24:57 -04:00
export 'veilid_state.dart';
2023-07-26 14:20:17 -04:00
export 'veilid_table_db.dart';
2022-06-15 21:51:38 -04:00
2022-02-09 09:47:36 -05:00
//////////////////////////////////////
/// JSON Encode Helper
2022-06-15 21:51:38 -04:00
2022-02-09 09:47:36 -05:00
Object? veilidApiToEncodable(Object? value) {
if (value == null) {
return value;
}
switch (value.runtimeType) {
2023-05-29 15:24:57 -04:00
// case KeyPair:
// return (value as KeyPair).json;
2022-02-09 09:47:36 -05:00
}
throw UnsupportedError('Cannot convert to JSON: $value');
}
2023-05-29 15:24:57 -04:00
List<T> Function(dynamic) jsonListConstructor<T>(
2023-07-26 15:12:28 -04:00
T Function(dynamic) jsonConstructor) =>
2024-02-27 22:12:34 -05:00
(dynamic j) => (j as List<dynamic>).map(jsonConstructor).toList();
2022-12-03 18:08:53 -05:00
2022-02-09 09:47:36 -05:00
//////////////////////////////////////
/// VeilidVersion
2023-07-05 18:48:06 -04:00
@immutable
class VeilidVersion extends Equatable {
2023-07-26 14:20:17 -04:00
const VeilidVersion(this.major, this.minor, this.patch);
2022-02-09 09:47:36 -05:00
final int major;
final int minor;
final int patch;
2023-07-05 18:48:06 -04:00
@override
List<Object> get props => [major, minor, patch];
2022-02-09 09:47:36 -05:00
}
2022-11-26 14:16:02 -05:00
//////////////////////////////////////
2023-05-29 15:24:57 -04:00
/// Timestamp
2023-07-05 18:48:06 -04:00
@immutable
2023-08-07 17:26:58 -04:00
class Timestamp extends Equatable implements Comparable<Timestamp> {
2023-07-26 14:20:17 -04:00
const Timestamp({required this.value});
2023-08-07 17:26:58 -04:00
factory Timestamp.fromInt64(Int64 i64) => Timestamp(
value: (BigInt.from((i64 >> 32).toUnsigned(32).toInt()) << 32) |
BigInt.from(i64.toUnsigned(32).toInt()));
2023-07-26 14:20:17 -04:00
factory Timestamp.fromString(String s) => Timestamp(value: BigInt.parse(s));
factory Timestamp.fromJson(dynamic json) =>
Timestamp.fromString(json as String);
2023-05-29 15:24:57 -04:00
final BigInt value;
@override
2023-07-05 18:48:06 -04:00
List<Object> get props => [value];
2022-11-26 14:16:02 -05:00
2023-08-07 17:26:58 -04:00
@override
int compareTo(Timestamp other) => value.compareTo(other.value);
2023-07-05 18:48:06 -04:00
@override
String toString() => value.toString();
String toJson() => toString();
2023-08-07 17:26:58 -04:00
Int64 toInt64() => Int64.fromInts(
(value >> 32).toUnsigned(32).toInt(), value.toUnsigned(32).toInt());
2023-01-04 14:51:13 -05:00
2023-07-05 18:48:06 -04:00
TimestampDuration diff(Timestamp other) =>
TimestampDuration(value: value - other.value);
Timestamp offset(TimestampDuration dur) =>
Timestamp(value: value + dur.value);
2022-12-28 12:12:04 -05:00
}
2023-07-05 18:48:06 -04:00
@immutable
2023-08-07 17:26:58 -04:00
class TimestampDuration extends Equatable
implements Comparable<TimestampDuration> {
2023-07-26 14:20:17 -04:00
const TimestampDuration({required this.value});
2023-08-07 17:26:58 -04:00
factory TimestampDuration.fromInt64(Int64 i64) => TimestampDuration(
value: (BigInt.from((i64 >> 32).toUnsigned(32).toInt()) << 32) |
BigInt.from(i64.toUnsigned(32).toInt()));
2023-07-26 14:20:17 -04:00
factory TimestampDuration.fromString(String s) =>
TimestampDuration(value: BigInt.parse(s));
factory TimestampDuration.fromJson(dynamic json) =>
TimestampDuration.fromString(json as String);
2023-05-29 15:24:57 -04:00
final BigInt value;
@override
2023-07-05 18:48:06 -04:00
List<Object> get props => [value];
2022-12-28 22:53:58 -05:00
2023-08-07 17:26:58 -04:00
@override
int compareTo(TimestampDuration other) => value.compareTo(other.value);
2023-07-05 18:48:06 -04:00
@override
String toString() => value.toString();
String toJson() => toString();
2023-08-07 17:26:58 -04:00
Int64 toInt64() => Int64.fromInts(
(value >> 32).toUnsigned(32).toInt(), value.toUnsigned(32).toInt());
2023-01-03 09:13:18 -05:00
2023-07-05 18:48:06 -04:00
int toMillis() => (value ~/ BigInt.from(1000)).toInt();
BigInt toMicros() => value;
2022-12-28 09:48:25 -05:00
}
2022-02-09 09:47:36 -05:00
//////////////////////////////////////
/// Veilid singleton factory
abstract class Veilid {
2023-05-29 15:24:57 -04:00
static Veilid instance = getVeilid();
2022-07-01 16:20:43 -04:00
void initializeVeilidCore(Map<String, dynamic> platformConfigJson);
2022-07-01 12:13:52 -04:00
void changeLogLevel(String layer, VeilidConfigLogLevel logLevel);
2022-09-09 16:27:13 -04:00
Future<Stream<VeilidUpdate>> startupVeilidCore(VeilidConfig config);
2022-02-09 09:47:36 -05:00
Future<VeilidState> getVeilidState();
2022-09-06 18:59:41 -04:00
Future<void> attach();
Future<void> detach();
2022-02-09 09:47:36 -05:00
Future<void> shutdownVeilidCore();
2022-11-26 14:16:02 -05:00
2023-05-29 15:24:57 -04:00
// Crypto
List<CryptoKind> validCryptoKinds();
Future<VeilidCryptoSystem> getCryptoSystem(CryptoKind kind);
Future<VeilidCryptoSystem> bestCryptoSystem();
Future<List<TypedKey>> verifySignatures(
List<TypedKey> nodeIds, Uint8List data, List<TypedSignature> signatures);
Future<List<TypedSignature>> generateSignatures(
Uint8List data, List<TypedKeyPair> keyPairs);
Future<TypedKeyPair> generateKeyPair(CryptoKind kind);
2022-11-26 14:16:02 -05:00
// Routing context
Future<VeilidRoutingContext> routingContext();
// Private route allocation
2023-03-01 15:50:30 -05:00
Future<RouteBlob> newPrivateRoute();
Future<RouteBlob> newCustomPrivateRoute(
2022-11-26 14:16:02 -05:00
Stability stability, Sequencing sequencing);
Future<String> importRemotePrivateRoute(Uint8List blob);
Future<void> releasePrivateRoute(String key);
// App calls
2023-07-25 01:04:22 -04:00
Future<void> appCallReply(String callId, Uint8List message);
2022-11-26 14:16:02 -05:00
2022-12-28 09:48:25 -05:00
// TableStore
Future<VeilidTableDB> openTableDB(String name, int columnCount);
Future<bool> deleteTableDB(String name);
2022-11-26 14:16:02 -05:00
// Misc
2023-05-29 15:24:57 -04:00
Timestamp now();
2022-03-17 10:31:10 -04:00
String veilidVersionString();
VeilidVersion veilidVersion();
2022-11-26 14:16:02 -05:00
Future<String> debug(String command);
2022-01-16 11:19:01 -05:00
}