veilid/veilid-flutter/lib/veilid.dart

166 lines
4.1 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
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
//////////////////////////////////////////////////////////
2023-05-14 00:36:52 +00:00
import 'routing_context.dart';
import 'veilid_config.dart';
import 'veilid_crypto.dart';
import 'veilid_table_db.dart';
import 'veilid_state.dart';
2022-09-06 20:49:43 +00:00
2023-05-14 00:36:52 +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-02-07 02:18:42 +00:00
2022-02-09 14:47:36 +00:00
//////////////////////////////////////
2023-05-14 00:36:52 +00:00
/// JSON Encode Helper
2022-12-03 23:08:53 +00:00
2023-05-14 00:36:52 +00:00
Object? veilidApiToEncodable(Object? value) {
if (value == null) {
return value;
2022-12-03 23:08:53 +00:00
}
2023-05-14 00:36:52 +00:00
switch (value.runtimeType) {
2023-05-15 15:33:32 +00:00
// case KeyPair:
// return (value as KeyPair).json;
2022-12-14 21:50:33 +00:00
}
2023-05-14 00:36:52 +00:00
throw UnsupportedError('Cannot convert to JSON: $value');
2022-12-03 23:08:53 +00:00
}
2023-05-15 15:33:32 +00:00
T? Function(dynamic) optFromJson<T>(T Function(dynamic) jsonConstructor) {
return (dynamic j) {
if (j == null) {
return null;
} else {
return jsonConstructor(j);
}
};
}
List<T> Function(dynamic) jsonListConstructor<T>(
T Function(dynamic) jsonConstructor) {
return (dynamic j) {
return (j as List<dynamic>).map((e) => jsonConstructor(e)).toList();
};
}
2022-02-09 14:47:36 +00:00
//////////////////////////////////////
/// VeilidVersion
class VeilidVersion {
final int major;
final int minor;
final int patch;
VeilidVersion(this.major, this.minor, this.patch);
}
2022-11-26 19:16:02 +00:00
//////////////////////////////////////
2023-05-14 00:36:52 +00:00
/// Timestamp
class Timestamp {
final BigInt value;
Timestamp({required this.value});
2022-11-26 19:16:02 +00:00
2023-05-14 00:36:52 +00:00
@override
String toString() {
return value.toString();
2022-11-26 19:16:02 +00:00
}
2023-05-14 00:36:52 +00:00
Timestamp.fromString(String s) : value = BigInt.parse(s);
2022-11-26 19:16:02 +00:00
2023-05-14 00:36:52 +00:00
Timestamp.fromJson(dynamic json) : this.fromString(json as String);
2023-05-15 15:33:32 +00:00
String toJson() {
2023-05-14 00:36:52 +00:00
return toString();
2022-11-26 19:16:02 +00:00
}
2023-05-14 00:36:52 +00:00
TimestampDuration diff(Timestamp other) {
return TimestampDuration(value: value - other.value);
2022-11-26 19:16:02 +00:00
}
2023-05-13 00:13:04 +00:00
2023-05-14 00:36:52 +00:00
Timestamp offset(TimestampDuration dur) {
return Timestamp(value: value + dur.value);
}
2022-11-26 19:16:02 +00:00
}
2023-05-14 00:36:52 +00:00
class TimestampDuration {
final BigInt value;
TimestampDuration({required this.value});
2023-01-04 19:51:13 +00:00
2023-05-14 00:36:52 +00:00
@override
String toString() {
return value.toString();
2023-01-04 19:51:13 +00:00
}
2022-12-28 17:12:04 +00:00
2023-05-14 00:36:52 +00:00
TimestampDuration.fromString(String s) : value = BigInt.parse(s);
2022-12-29 03:53:58 +00:00
2023-05-14 00:36:52 +00:00
TimestampDuration.fromJson(dynamic json) : this.fromString(json as String);
2023-05-15 15:33:32 +00:00
String toJson() {
2023-05-14 00:36:52 +00:00
return toString();
2023-01-03 14:13:18 +00:00
}
2023-05-14 00:36:52 +00:00
int toMillis() {
return (value ~/ BigInt.from(1000)).toInt();
2023-01-03 14:13:18 +00:00
}
2023-05-14 00:36:52 +00:00
BigInt toMicros(Timestamp other) {
return value;
2022-12-29 03:53:58 +00:00
}
2022-12-28 14:48:25 +00:00
}
2022-02-09 14:47:36 +00:00
//////////////////////////////////////
/// Veilid singleton factory
abstract class Veilid {
2023-05-17 17:38:36 +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-14 00:36:52 +00:00
// Crypto
List<CryptoKind> validCryptoKinds();
2023-05-15 00:50:28 +00:00
Future<VeilidCryptoSystem> getCryptoSystem(CryptoKind kind);
Future<VeilidCryptoSystem> bestCryptoSystem();
Future<List<TypedKey>> verifySignatures(
2023-05-14 00:36:52 +00:00
List<TypedKey> nodeIds, Uint8List data, List<TypedSignature> signatures);
2023-05-15 00:50:28 +00:00
Future<List<TypedSignature>> generateSignatures(
2023-05-14 00:36:52 +00:00
Uint8List data, List<TypedKeyPair> keyPairs);
2023-05-15 00:50:28 +00:00
Future<TypedKeyPair> generateKeyPair(CryptoKind kind);
2023-05-14 00:36:52 +00:00
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-14 00:36:52 +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
}