2023-05-29 15:24:57 -04:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
|
|
import 'package:change_case/change_case.dart';
|
2023-07-06 11:41:38 -04:00
|
|
|
import 'package:equatable/equatable.dart';
|
2023-07-05 23:53:08 -04:00
|
|
|
import 'package:freezed_annotation/freezed_annotation.dart';
|
2023-05-29 15:24:57 -04:00
|
|
|
|
|
|
|
import 'veilid.dart';
|
|
|
|
|
2023-07-05 23:53:08 -04:00
|
|
|
part 'routing_context.freezed.dart';
|
|
|
|
part 'routing_context.g.dart';
|
|
|
|
|
2023-05-29 15:24:57 -04:00
|
|
|
//////////////////////////////////////
|
|
|
|
|
2023-07-06 11:41:38 -04:00
|
|
|
extension ValidateDFLT on DHTSchemaDFLT {
|
|
|
|
bool validate() {
|
|
|
|
if (oCnt > 65535) {
|
|
|
|
return false;
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
2023-07-06 11:41:38 -04:00
|
|
|
if (oCnt <= 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
2024-01-06 20:46:52 -05:00
|
|
|
|
|
|
|
int subkeyCount() => oCnt;
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
|
|
|
|
2023-07-06 11:41:38 -04:00
|
|
|
extension ValidateSMPL on DHTSchemaSMPL {
|
|
|
|
bool validate() {
|
2024-01-06 20:46:52 -05:00
|
|
|
final totalsv = subkeyCount();
|
2023-07-06 11:41:38 -04:00
|
|
|
if (totalsv > 65535) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (totalsv <= 0) {
|
|
|
|
return false;
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
2023-07-06 11:41:38 -04:00
|
|
|
return true;
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
2024-01-06 20:46:52 -05:00
|
|
|
|
2024-03-09 22:22:25 -05:00
|
|
|
int subkeyCount() => members.fold(oCnt, (acc, v) => acc + v.mCnt);
|
2024-01-06 20:46:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extension Validate on DHTSchema {
|
|
|
|
bool validate() {
|
|
|
|
if (this is DHTSchemaDFLT) {
|
|
|
|
return (this as DHTSchemaDFLT).validate();
|
|
|
|
} else if (this is DHTSchemaSMPL) {
|
|
|
|
return (this as DHTSchemaSMPL).validate();
|
|
|
|
}
|
|
|
|
throw TypeError();
|
|
|
|
}
|
|
|
|
|
|
|
|
int subkeyCount() {
|
|
|
|
if (this is DHTSchemaDFLT) {
|
|
|
|
return (this as DHTSchemaDFLT).subkeyCount();
|
|
|
|
} else if (this is DHTSchemaSMPL) {
|
|
|
|
return (this as DHTSchemaSMPL).subkeyCount();
|
|
|
|
}
|
|
|
|
throw TypeError();
|
|
|
|
}
|
2023-07-06 11:41:38 -04:00
|
|
|
}
|
2023-05-29 15:24:57 -04:00
|
|
|
|
2023-07-06 11:41:38 -04:00
|
|
|
//////////////////////////////////////
|
|
|
|
/// DHT Schema
|
|
|
|
|
|
|
|
@Freezed(unionKey: 'kind', unionValueCase: FreezedUnionCase.pascal)
|
|
|
|
sealed class DHTSchema with _$DHTSchema {
|
|
|
|
@FreezedUnionValue('DFLT')
|
|
|
|
const factory DHTSchema.dflt({required int oCnt}) = DHTSchemaDFLT;
|
|
|
|
|
|
|
|
@FreezedUnionValue('SMPL')
|
|
|
|
const factory DHTSchema.smpl(
|
|
|
|
{required int oCnt,
|
|
|
|
required List<DHTSchemaMember> members}) = DHTSchemaSMPL;
|
|
|
|
|
2023-07-25 01:04:22 -04:00
|
|
|
factory DHTSchema.fromJson(dynamic json) =>
|
|
|
|
_$DHTSchemaFromJson(json as Map<String, dynamic>);
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
|
|
|
|
2023-07-12 21:28:00 -04:00
|
|
|
const DHTSchema defaultDHTSchema = DHTSchema.dflt(oCnt: 1);
|
|
|
|
|
2023-07-05 23:53:08 -04:00
|
|
|
@freezed
|
|
|
|
class DHTSchemaMember with _$DHTSchemaMember {
|
2023-07-06 11:41:38 -04:00
|
|
|
@Assert('mCnt > 0 && mCnt <= 65535', 'value out of range')
|
2023-07-05 23:53:08 -04:00
|
|
|
const factory DHTSchemaMember({
|
|
|
|
required PublicKey mKey,
|
|
|
|
required int mCnt,
|
|
|
|
}) = _DHTSchemaMember;
|
2023-05-29 15:24:57 -04:00
|
|
|
|
2023-07-25 01:04:22 -04:00
|
|
|
factory DHTSchemaMember.fromJson(dynamic json) =>
|
|
|
|
_$DHTSchemaMemberFromJson(json as Map<String, dynamic>);
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////
|
|
|
|
/// DHTRecordDescriptor
|
|
|
|
|
2023-07-06 11:41:38 -04:00
|
|
|
@freezed
|
|
|
|
class DHTRecordDescriptor with _$DHTRecordDescriptor {
|
|
|
|
const factory DHTRecordDescriptor({
|
|
|
|
required TypedKey key,
|
|
|
|
required PublicKey owner,
|
2023-07-30 15:57:06 -04:00
|
|
|
required DHTSchema schema,
|
|
|
|
PublicKey? ownerSecret,
|
2023-07-06 11:41:38 -04:00
|
|
|
}) = _DHTRecordDescriptor;
|
2023-07-25 01:04:22 -04:00
|
|
|
factory DHTRecordDescriptor.fromJson(dynamic json) =>
|
|
|
|
_$DHTRecordDescriptorFromJson(json as Map<String, dynamic>);
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
|
|
|
|
2023-07-21 09:48:08 -04:00
|
|
|
extension DHTRecordDescriptorExt on DHTRecordDescriptor {
|
|
|
|
KeyPair? ownerKeyPair() {
|
|
|
|
if (ownerSecret == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return KeyPair(key: owner, secret: ownerSecret!);
|
|
|
|
}
|
2023-07-21 14:30:10 -04:00
|
|
|
|
2024-05-31 16:20:58 -04:00
|
|
|
TypedKey? ownerTypedSecret() {
|
|
|
|
if (ownerSecret == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return TypedKey(kind: key.kind, value: ownerSecret!);
|
|
|
|
}
|
|
|
|
|
2023-07-21 14:30:10 -04:00
|
|
|
TypedKeyPair? ownerTypedKeyPair() {
|
|
|
|
if (ownerSecret == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return TypedKeyPair(kind: key.kind, key: owner, secret: ownerSecret!);
|
|
|
|
}
|
2023-07-21 09:48:08 -04:00
|
|
|
}
|
|
|
|
|
2023-05-29 15:24:57 -04:00
|
|
|
//////////////////////////////////////
|
|
|
|
/// ValueData
|
|
|
|
|
2023-07-06 11:41:38 -04:00
|
|
|
@freezed
|
|
|
|
class ValueData with _$ValueData {
|
|
|
|
@Assert('seq >= 0', 'seq out of range')
|
|
|
|
const factory ValueData({
|
|
|
|
required int seq,
|
2023-10-14 20:18:37 -04:00
|
|
|
@Uint8ListJsonConverter.jsIsArray() required Uint8List data,
|
2023-07-06 11:41:38 -04:00
|
|
|
required PublicKey writer,
|
|
|
|
}) = _ValueData;
|
|
|
|
|
2023-07-25 01:04:22 -04:00
|
|
|
factory ValueData.fromJson(dynamic json) =>
|
|
|
|
_$ValueDataFromJson(json as Map<String, dynamic>);
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
|
|
|
|
2023-06-28 11:40:02 -04:00
|
|
|
//////////////////////////////////////
|
2023-05-29 15:24:57 -04:00
|
|
|
/// Stability
|
|
|
|
|
|
|
|
enum Stability {
|
|
|
|
lowLatency,
|
|
|
|
reliable;
|
|
|
|
|
2023-07-25 01:04:22 -04:00
|
|
|
factory Stability.fromJson(dynamic j) =>
|
|
|
|
Stability.values.byName((j as String).toCamelCase());
|
2023-07-30 15:57:06 -04:00
|
|
|
String toJson() => name.toPascalCase();
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////
|
|
|
|
/// Sequencing
|
|
|
|
|
|
|
|
enum Sequencing {
|
|
|
|
noPreference,
|
|
|
|
preferOrdered,
|
|
|
|
ensureOrdered;
|
|
|
|
|
2023-07-25 01:04:22 -04:00
|
|
|
factory Sequencing.fromJson(dynamic j) =>
|
|
|
|
Sequencing.values.byName((j as String).toCamelCase());
|
2023-07-30 15:57:06 -04:00
|
|
|
String toJson() => name.toPascalCase();
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
|
|
|
|
2023-06-28 11:40:02 -04:00
|
|
|
//////////////////////////////////////
|
|
|
|
/// SafetySelection
|
|
|
|
|
2023-07-06 11:41:38 -04:00
|
|
|
@immutable
|
2023-07-30 15:57:06 -04:00
|
|
|
abstract class SafetySelection {
|
2023-07-25 01:04:22 -04:00
|
|
|
factory SafetySelection.fromJson(dynamic jsond) {
|
|
|
|
final json = jsond as Map<String, dynamic>;
|
2023-07-26 14:20:17 -04:00
|
|
|
if (json.containsKey('Unsafe')) {
|
2023-06-28 11:40:02 -04:00
|
|
|
return SafetySelectionUnsafe(
|
2023-07-26 14:20:17 -04:00
|
|
|
sequencing: Sequencing.fromJson(json['Unsafe']));
|
|
|
|
} else if (json.containsKey('Safe')) {
|
|
|
|
return SafetySelectionSafe(safetySpec: SafetySpec.fromJson(json['Safe']));
|
2023-06-28 11:40:02 -04:00
|
|
|
} else {
|
2023-07-26 14:20:17 -04:00
|
|
|
throw const VeilidAPIExceptionInternal('Invalid SafetySelection');
|
2023-06-28 11:40:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Map<String, dynamic> toJson();
|
|
|
|
}
|
|
|
|
|
2023-07-06 11:41:38 -04:00
|
|
|
@immutable
|
2023-07-30 15:57:06 -04:00
|
|
|
class SafetySelectionUnsafe extends Equatable implements SafetySelection {
|
2023-06-28 11:40:02 -04:00
|
|
|
//
|
2023-07-06 11:41:38 -04:00
|
|
|
const SafetySelectionUnsafe({
|
2023-06-28 11:40:02 -04:00
|
|
|
required this.sequencing,
|
|
|
|
});
|
2023-07-26 14:20:17 -04:00
|
|
|
final Sequencing sequencing;
|
|
|
|
@override
|
|
|
|
List<Object> get props => [sequencing];
|
|
|
|
@override
|
|
|
|
bool? get stringify => null;
|
2023-06-28 11:40:02 -04:00
|
|
|
|
|
|
|
@override
|
2023-07-26 14:20:17 -04:00
|
|
|
Map<String, dynamic> toJson() => {'Unsafe': sequencing.toJson()};
|
2023-06-28 11:40:02 -04:00
|
|
|
}
|
|
|
|
|
2023-07-06 11:41:38 -04:00
|
|
|
@immutable
|
2023-07-30 15:57:06 -04:00
|
|
|
class SafetySelectionSafe extends Equatable implements SafetySelection {
|
2023-06-28 11:40:02 -04:00
|
|
|
//
|
2023-07-06 11:41:38 -04:00
|
|
|
const SafetySelectionSafe({
|
2023-06-28 11:40:02 -04:00
|
|
|
required this.safetySpec,
|
|
|
|
});
|
2023-07-26 14:20:17 -04:00
|
|
|
final SafetySpec safetySpec;
|
|
|
|
@override
|
|
|
|
List<Object> get props => [safetySpec];
|
|
|
|
@override
|
|
|
|
bool? get stringify => null;
|
2023-06-28 11:40:02 -04:00
|
|
|
|
|
|
|
@override
|
2023-07-26 14:20:17 -04:00
|
|
|
Map<String, dynamic> toJson() => {'Safe': safetySpec.toJson()};
|
2023-06-28 11:40:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Options for safety routes (sender privacy)
|
2023-07-06 11:41:38 -04:00
|
|
|
@freezed
|
|
|
|
class SafetySpec with _$SafetySpec {
|
|
|
|
const factory SafetySpec({
|
2023-07-30 15:57:06 -04:00
|
|
|
required int hopCount,
|
|
|
|
required Stability stability,
|
|
|
|
required Sequencing sequencing,
|
|
|
|
String? preferredRoute,
|
2023-07-06 11:41:38 -04:00
|
|
|
}) = _SafetySpec;
|
|
|
|
|
2023-07-25 01:04:22 -04:00
|
|
|
factory SafetySpec.fromJson(dynamic json) =>
|
|
|
|
_$SafetySpecFromJson(json as Map<String, dynamic>);
|
2023-06-28 11:40:02 -04:00
|
|
|
}
|
|
|
|
|
2023-05-29 15:24:57 -04:00
|
|
|
//////////////////////////////////////
|
|
|
|
/// RouteBlob
|
2023-07-06 11:41:38 -04:00
|
|
|
@freezed
|
|
|
|
class RouteBlob with _$RouteBlob {
|
|
|
|
const factory RouteBlob(
|
|
|
|
{required String routeId,
|
|
|
|
@Uint8ListJsonConverter() required Uint8List blob}) = _RouteBlob;
|
2023-07-25 01:04:22 -04:00
|
|
|
factory RouteBlob.fromJson(dynamic json) =>
|
|
|
|
_$RouteBlobFromJson(json as Map<String, dynamic>);
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|
|
|
|
|
2024-03-16 23:57:46 -04:00
|
|
|
//////////////////////////////////////
|
|
|
|
/// Inspect
|
|
|
|
@freezed
|
|
|
|
class DHTRecordReport with _$DHTRecordReport {
|
|
|
|
const factory DHTRecordReport({
|
|
|
|
required List<ValueSubkeyRange> subkeys,
|
2024-04-13 14:15:09 -04:00
|
|
|
required List<ValueSubkeyRange> offlineSubkeys,
|
2024-03-16 23:57:46 -04:00
|
|
|
required List<int> localSeqs,
|
|
|
|
required List<int> networkSeqs,
|
|
|
|
}) = _DHTRecordReport;
|
|
|
|
factory DHTRecordReport.fromJson(dynamic json) =>
|
|
|
|
_$DHTRecordReportFromJson(json as Map<String, dynamic>);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum DHTReportScope {
|
|
|
|
local,
|
|
|
|
syncGet,
|
|
|
|
syncSet,
|
|
|
|
updateGet,
|
|
|
|
updateSet;
|
|
|
|
|
|
|
|
factory DHTReportScope.fromJson(dynamic j) =>
|
|
|
|
DHTReportScope.values.byName((j as String).toCamelCase());
|
|
|
|
String toJson() => name.toPascalCase();
|
|
|
|
}
|
|
|
|
|
2023-05-29 15:24:57 -04:00
|
|
|
//////////////////////////////////////
|
|
|
|
/// VeilidRoutingContext
|
|
|
|
|
|
|
|
abstract class VeilidRoutingContext {
|
2023-07-19 10:07:51 -04:00
|
|
|
void close();
|
|
|
|
|
2023-05-29 15:24:57 -04:00
|
|
|
// Modifiers
|
2024-03-16 23:57:46 -04:00
|
|
|
VeilidRoutingContext withDefaultSafety({bool closeSelf = false});
|
|
|
|
VeilidRoutingContext withSafety(SafetySelection safetySelection,
|
|
|
|
{bool closeSelf = false});
|
|
|
|
VeilidRoutingContext withSequencing(Sequencing sequencing,
|
|
|
|
{bool closeSelf = false});
|
2023-11-05 18:38:05 -05:00
|
|
|
Future<SafetySelection> safety();
|
2023-05-29 15:24:57 -04:00
|
|
|
|
|
|
|
// App call/message
|
|
|
|
Future<Uint8List> appCall(String target, Uint8List request);
|
|
|
|
Future<void> appMessage(String target, Uint8List message);
|
|
|
|
|
|
|
|
// DHT Operations
|
2023-07-08 22:50:44 -04:00
|
|
|
Future<DHTRecordDescriptor> createDHTRecord(DHTSchema schema,
|
|
|
|
{CryptoKind kind = 0});
|
2024-02-21 20:18:07 -05:00
|
|
|
Future<DHTRecordDescriptor> openDHTRecord(TypedKey key, {KeyPair? writer});
|
2023-05-29 15:24:57 -04:00
|
|
|
Future<void> closeDHTRecord(TypedKey key);
|
|
|
|
Future<void> deleteDHTRecord(TypedKey key);
|
2024-02-21 20:18:07 -05:00
|
|
|
Future<ValueData?> getDHTValue(TypedKey key, int subkey,
|
|
|
|
{bool forceRefresh = false});
|
|
|
|
Future<ValueData?> setDHTValue(TypedKey key, int subkey, Uint8List data,
|
|
|
|
{KeyPair? writer});
|
2024-01-04 22:29:31 -05:00
|
|
|
Future<Timestamp> watchDHTValues(TypedKey key,
|
|
|
|
{List<ValueSubkeyRange>? subkeys, Timestamp? expiration, int? count});
|
|
|
|
Future<bool> cancelDHTWatch(TypedKey key, {List<ValueSubkeyRange>? subkeys});
|
2024-03-16 23:57:46 -04:00
|
|
|
Future<DHTRecordReport> inspectDHTRecord(TypedKey key,
|
|
|
|
{List<ValueSubkeyRange>? subkeys,
|
|
|
|
DHTReportScope scope = DHTReportScope.local});
|
2023-05-29 15:24:57 -04:00
|
|
|
}
|