Merge branch veilid:main into main

This commit is contained in:
Debanjan Basu 2024-05-17 07:06:48 +00:00
commit 29d4de05ea
3 changed files with 63 additions and 2 deletions

View File

@ -228,10 +228,11 @@ impl RoutingContext {
///////////////////////////////////
/// DHT Records
/// Creates a new DHT record a specified crypto kind and schema.
/// Creates a new DHT record
///
/// The record is considered 'open' after the create operation succeeds.
///
/// * 'schema' - the schema to use when creating the DHT record
/// * 'kind' - specify a cryptosystem kind to use. Normally you will leave this as None to choose the 'best' cryptosystem available.
/// Returns the newly allocated DHT record's key if successful.
#[instrument(target = "veilid_api", level = "debug", ret, err)]
pub async fn create_dht_record(

View File

@ -28,6 +28,9 @@ Uint8List cryptoKindToBytes(CryptoKind kind) {
return b;
}
CryptoKind cryptoKindFromBytes(Uint8List b) =>
ByteData.sublistView(b).getUint32(0);
CryptoKind cryptoKindFromString(String s) {
if (s.codeUnits.length != 4) {
throw const FormatException('malformed string');
@ -53,6 +56,11 @@ class Typed<V extends EncodedString> extends Equatable {
final value = EncodedString.fromString<V>(parts.sublist(1).join(':'));
return Typed(kind: kind, value: value);
}
factory Typed.fromBytes(Uint8List b) {
final kind = cryptoKindFromBytes(b);
final value = EncodedString.fromBytes<V>(b.sublist(4));
return Typed(kind: kind, value: value);
}
factory Typed.fromJson(dynamic json) => Typed.fromString(json as String);
final CryptoKind kind;
final V value;
@ -69,6 +77,32 @@ class Typed<V extends EncodedString> extends Equatable {
return b.toBytes();
}
static int encodedLength<X>() {
switch (X) {
case const (Typed<FixedEncodedString32>):
return FixedEncodedString32.encodedLength() + 5;
case const (Typed<FixedEncodedString43>):
return FixedEncodedString43.encodedLength() + 5;
case const (Typed<FixedEncodedString86>):
return FixedEncodedString86.encodedLength() + 5;
default:
throw UnimplementedError();
}
}
static int decodedLength<X>() {
switch (X) {
case const (Typed<FixedEncodedString32>):
return FixedEncodedString32.decodedLength() + 4;
case const (Typed<FixedEncodedString43>):
return FixedEncodedString43.decodedLength() + 4;
case const (Typed<FixedEncodedString86>):
return FixedEncodedString86.decodedLength() + 4;
default:
throw UnimplementedError();
}
}
String toJson() => toString();
}

View File

@ -52,6 +52,32 @@ abstract class EncodedString extends Equatable {
@override
String toString() => contents;
static int encodedLength<T extends EncodedString>() {
switch (T) {
case FixedEncodedString32:
return FixedEncodedString32.encodedLength();
case FixedEncodedString43:
return FixedEncodedString43.encodedLength();
case FixedEncodedString86:
return FixedEncodedString86.encodedLength();
default:
throw UnimplementedError();
}
}
static int decodedLength<T extends EncodedString>() {
switch (T) {
case FixedEncodedString32:
return FixedEncodedString32.decodedLength();
case FixedEncodedString43:
return FixedEncodedString43.decodedLength();
case FixedEncodedString86:
return FixedEncodedString86.decodedLength();
default:
throw UnimplementedError();
}
}
static T fromBytes<T extends EncodedString>(Uint8List bytes) {
switch (T) {
case FixedEncodedString32: