veilid/veilid-flutter/lib/veilid.dart

169 lines
4.5 KiB
Dart
Raw Normal View History

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