From 6282bfd1c556e7e4ccfc50f68b0f8d1cb5c41a5e Mon Sep 17 00:00:00 2001 From: Christien Rioux Date: Wed, 15 May 2024 21:20:39 -0400 Subject: [PATCH] add lengths to typedkey --- veilid-flutter/lib/veilid_crypto.dart | 34 +++++++++++++++++++++++++ veilid-flutter/lib/veilid_encoding.dart | 26 +++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/veilid-flutter/lib/veilid_crypto.dart b/veilid-flutter/lib/veilid_crypto.dart index 17ee4b5f..186d8e01 100644 --- a/veilid-flutter/lib/veilid_crypto.dart +++ b/veilid-flutter/lib/veilid_crypto.dart @@ -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 extends Equatable { final value = EncodedString.fromString(parts.sublist(1).join(':')); return Typed(kind: kind, value: value); } + factory Typed.fromBytes(Uint8List b) { + final kind = cryptoKindFromBytes(b); + final value = EncodedString.fromBytes(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 extends Equatable { return b.toBytes(); } + static int encodedLength() { + switch (X) { + case const (Typed): + return FixedEncodedString32.encodedLength() + 5; + case const (Typed): + return FixedEncodedString43.encodedLength() + 5; + case const (Typed): + return FixedEncodedString86.encodedLength() + 5; + default: + throw UnimplementedError(); + } + } + + static int decodedLength() { + switch (X) { + case const (Typed): + return FixedEncodedString32.decodedLength() + 4; + case const (Typed): + return FixedEncodedString43.decodedLength() + 4; + case const (Typed): + return FixedEncodedString86.decodedLength() + 4; + default: + throw UnimplementedError(); + } + } + String toJson() => toString(); } diff --git a/veilid-flutter/lib/veilid_encoding.dart b/veilid-flutter/lib/veilid_encoding.dart index 5022bd84..4cde365a 100644 --- a/veilid-flutter/lib/veilid_encoding.dart +++ b/veilid-flutter/lib/veilid_encoding.dart @@ -52,6 +52,32 @@ abstract class EncodedString extends Equatable { @override String toString() => contents; + static int encodedLength() { + switch (T) { + case FixedEncodedString32: + return FixedEncodedString32.encodedLength(); + case FixedEncodedString43: + return FixedEncodedString43.encodedLength(); + case FixedEncodedString86: + return FixedEncodedString86.encodedLength(); + default: + throw UnimplementedError(); + } + } + + static int decodedLength() { + switch (T) { + case FixedEncodedString32: + return FixedEncodedString32.decodedLength(); + case FixedEncodedString43: + return FixedEncodedString43.decodedLength(); + case FixedEncodedString86: + return FixedEncodedString86.decodedLength(); + default: + throw UnimplementedError(); + } + } + static T fromBytes(Uint8List bytes) { switch (T) { case FixedEncodedString32: