veilid/veilid-flutter/lib/veilid.dart

171 lines
4.5 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';
import 'package:freezed_annotation/freezed_annotation.dart';
2022-02-06 21:18:42 -05:00
import 'veilid_stub.dart'
2022-02-09 09:47:36 -05:00
if (dart.library.io) 'veilid_ffi.dart'
if (dart.library.js) 'veilid_js.dart';
2022-02-06 21:18:42 -05: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_table_db.dart';
import 'veilid_state.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';
2023-07-07 19:32:59 -04:00
export 'veilid_encoding.dart';
2023-05-29 15:24:57 -04:00
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-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-07-06 11:41:38 -04:00
T? Function(dynamic) optFromJson<T>(
T Function(Map<String, dynamic>) jsonConstructor) {
2023-05-29 15:24:57 -04:00
return (dynamic j) {
if (j == null) {
return null;
} else {
return jsonConstructor(j);
2022-02-09 09:47:36 -05:00
}
2023-05-29 15:24:57 -04:00
};
2022-02-09 09:47:36 -05:00
}
2023-05-29 15:24:57 -04:00
List<T> Function(dynamic) jsonListConstructor<T>(
2023-07-06 11:41:38 -04:00
T Function(Map<String, dynamic>) jsonConstructor) {
2023-05-29 15:24:57 -04:00
return (dynamic j) {
2023-07-06 11:41:38 -04:00
return (j as List<Map<String, dynamic>>)
.map((e) => jsonConstructor(e))
.toList();
2023-05-29 15:24:57 -04:00
};
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 {
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
2023-07-05 18:48:06 -04:00
const VeilidVersion(this.major, this.minor, this.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
class Timestamp extends Equatable {
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-07-05 18:48:06 -04:00
const Timestamp({required this.value});
2022-11-26 14:16:02 -05:00
2023-07-05 18:48:06 -04:00
@override
String toString() => value.toString();
factory Timestamp.fromString(String s) => Timestamp(value: BigInt.parse(s));
2022-11-26 14:16:02 -05:00
2023-07-05 18:48:06 -04:00
String toJson() => toString();
factory Timestamp.fromJson(dynamic json) =>
Timestamp.fromString(json as String);
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
class TimestampDuration extends Equatable {
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-07-05 18:48:06 -04:00
const TimestampDuration({required this.value});
2023-05-29 15:24:57 -04:00
2023-07-05 18:48:06 -04:00
@override
String toString() => value.toString();
factory TimestampDuration.fromString(String s) =>
TimestampDuration(value: BigInt.parse(s));
2023-01-03 09:13:18 -05:00
2023-07-05 18:48:06 -04:00
String toJson() => toString();
factory TimestampDuration.fromJson(dynamic json) =>
TimestampDuration.fromString(json as String);
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
2022-09-30 22:37:55 -04:00
Future<void> appCallReply(String id, 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
}