This commit is contained in:
Christien Rioux 2023-07-30 15:57:06 -04:00
parent a589dbf100
commit efd3fa4cb2
11 changed files with 287 additions and 243 deletions

View File

@ -2,3 +2,10 @@ include: package:lint_hard/all.yaml
analyzer:
errors:
invalid_annotation_target: ignore
exclude:
- '**/*.g.dart'
- '**/*.freezed.dart'
linter:
rules:
avoid_positional_boolean_parameters: false

View File

@ -58,127 +58,129 @@ int getRemoteMaxStorageSpaceMb() {
return 256;
}
Future<VeilidConfig> getDefaultVeilidConfig(String programName) async => VeilidConfig(
programName: programName,
namespace: '',
capabilities: const VeilidConfigCapabilities(disable: []),
protectedStore: const VeilidConfigProtectedStore(
allowInsecureFallback: false,
alwaysUseInsecureStorage: false,
directory: '',
delete: false,
deviceEncryptionKeyPassword: '',
),
tableStore: VeilidConfigTableStore(
directory: kIsWeb
? ''
: p.join((await getApplicationSupportDirectory()).absolute.path,
'table_store'),
delete: false,
),
blockStore: VeilidConfigBlockStore(
directory: kIsWeb
? ''
: p.join((await getApplicationSupportDirectory()).absolute.path,
'block_store'),
delete: false,
),
network: VeilidConfigNetwork(
connectionInitialTimeoutMs: 2000,
connectionInactivityTimeoutMs: 60000,
maxConnectionsPerIp4: 32,
maxConnectionsPerIp6Prefix: 32,
maxConnectionsPerIp6PrefixSize: 56,
maxConnectionFrequencyPerMin: 128,
clientWhitelistTimeoutMs: 300000,
reverseConnectionReceiptTimeMs: 5000,
holePunchReceiptTimeMs: 5000,
routingTable: const VeilidConfigRoutingTable(
nodeId: [],
nodeIdSecret: [],
bootstrap: kIsWeb
? ['ws://bootstrap.veilid.net:5150/ws']
: ['bootstrap.veilid.net'],
limitOverAttached: 64,
limitFullyAttached: 32,
limitAttachedStrong: 16,
limitAttachedGood: 8,
limitAttachedWeak: 4,
Future<VeilidConfig> getDefaultVeilidConfig(String programName) async =>
VeilidConfig(
programName: programName,
namespace: '',
capabilities: const VeilidConfigCapabilities(disable: []),
protectedStore: const VeilidConfigProtectedStore(
allowInsecureFallback: false,
alwaysUseInsecureStorage: false,
directory: '',
delete: false,
deviceEncryptionKeyPassword: '',
),
rpc: const VeilidConfigRPC(
concurrency: 0,
queueSize: 1024,
maxTimestampBehindMs: 10000,
maxTimestampAheadMs: 10000,
timeoutMs: 5000,
maxRouteHopCount: 4,
defaultRouteHopCount: 1,
tableStore: VeilidConfigTableStore(
directory: kIsWeb
? ''
: p.join((await getApplicationSupportDirectory()).absolute.path,
'table_store'),
delete: false,
),
dht: VeilidConfigDHT(
resolveNodeTimeoutMs: 10000,
resolveNodeCount: 20,
resolveNodeFanout: 3,
maxFindNodeCount: 20,
getValueTimeoutMs: 10000,
getValueCount: 20,
getValueFanout: 3,
setValueTimeoutMs: 10000,
setValueCount: 20,
setValueFanout: 5,
minPeerCount: 20,
minPeerRefreshTimeMs: 60000,
validateDialInfoReceiptTimeMs: 2000,
localSubkeyCacheSize: getLocalSubkeyCacheSize(),
localMaxSubkeyCacheMemoryMb: await getLocalMaxSubkeyCacheMemoryMb(),
remoteSubkeyCacheSize: getRemoteSubkeyCacheSize(),
remoteMaxRecords: getRemoteMaxRecords(),
remoteMaxSubkeyCacheMemoryMb: await getRemoteMaxSubkeyCacheMemoryMb(),
remoteMaxStorageSpaceMb: getRemoteMaxStorageSpaceMb()),
upnp: true,
detectAddressChanges: true,
restrictedNatRetries: 0,
tls: const VeilidConfigTLS(
certificatePath: '',
privateKeyPath: '',
blockStore: VeilidConfigBlockStore(
directory: kIsWeb
? ''
: p.join((await getApplicationSupportDirectory()).absolute.path,
'block_store'),
delete: false,
),
network: VeilidConfigNetwork(
connectionInitialTimeoutMs: 2000,
),
application: const VeilidConfigApplication(
https: VeilidConfigHTTPS(
enabled: false,
connectionInactivityTimeoutMs: 60000,
maxConnectionsPerIp4: 32,
maxConnectionsPerIp6Prefix: 32,
maxConnectionsPerIp6PrefixSize: 56,
maxConnectionFrequencyPerMin: 128,
clientWhitelistTimeoutMs: 300000,
reverseConnectionReceiptTimeMs: 5000,
holePunchReceiptTimeMs: 5000,
routingTable: const VeilidConfigRoutingTable(
nodeId: [],
nodeIdSecret: [],
bootstrap: kIsWeb
? ['ws://bootstrap.veilid.net:5150/ws']
: ['bootstrap.veilid.net'],
limitOverAttached: 64,
limitFullyAttached: 32,
limitAttachedStrong: 16,
limitAttachedGood: 8,
limitAttachedWeak: 4,
),
rpc: const VeilidConfigRPC(
concurrency: 0,
queueSize: 1024,
maxTimestampBehindMs: 10000,
maxTimestampAheadMs: 10000,
timeoutMs: 5000,
maxRouteHopCount: 4,
defaultRouteHopCount: 1,
),
dht: VeilidConfigDHT(
resolveNodeTimeoutMs: 10000,
resolveNodeCount: 20,
resolveNodeFanout: 3,
maxFindNodeCount: 20,
getValueTimeoutMs: 10000,
getValueCount: 20,
getValueFanout: 3,
setValueTimeoutMs: 10000,
setValueCount: 20,
setValueFanout: 5,
minPeerCount: 20,
minPeerRefreshTimeMs: 60000,
validateDialInfoReceiptTimeMs: 2000,
localSubkeyCacheSize: getLocalSubkeyCacheSize(),
localMaxSubkeyCacheMemoryMb: await getLocalMaxSubkeyCacheMemoryMb(),
remoteSubkeyCacheSize: getRemoteSubkeyCacheSize(),
remoteMaxRecords: getRemoteMaxRecords(),
remoteMaxSubkeyCacheMemoryMb:
await getRemoteMaxSubkeyCacheMemoryMb(),
remoteMaxStorageSpaceMb: getRemoteMaxStorageSpaceMb()),
upnp: true,
detectAddressChanges: true,
restrictedNatRetries: 0,
tls: const VeilidConfigTLS(
certificatePath: '',
privateKeyPath: '',
connectionInitialTimeoutMs: 2000,
),
application: const VeilidConfigApplication(
https: VeilidConfigHTTPS(
enabled: false,
listenAddress: '',
path: '',
),
http: VeilidConfigHTTP(
enabled: false,
listenAddress: '',
path: '',
)),
protocol: const VeilidConfigProtocol(
udp: VeilidConfigUDP(
enabled: !kIsWeb,
socketPoolSize: 0,
listenAddress: '',
path: '',
),
http: VeilidConfigHTTP(
enabled: false,
tcp: VeilidConfigTCP(
connect: !kIsWeb,
listen: !kIsWeb,
maxConnections: 32,
listenAddress: '',
path: '',
)),
protocol: const VeilidConfigProtocol(
udp: VeilidConfigUDP(
enabled: !kIsWeb,
socketPoolSize: 0,
listenAddress: '',
),
tcp: VeilidConfigTCP(
connect: !kIsWeb,
listen: !kIsWeb,
maxConnections: 32,
listenAddress: '',
),
ws: VeilidConfigWS(
connect: true,
listen: !kIsWeb,
maxConnections: 16,
listenAddress: '',
path: 'ws',
),
wss: VeilidConfigWSS(
connect: true,
listen: false,
maxConnections: 16,
listenAddress: '',
path: 'ws',
),
ws: VeilidConfigWS(
connect: true,
listen: !kIsWeb,
maxConnections: 16,
listenAddress: '',
path: 'ws',
),
wss: VeilidConfigWSS(
connect: true,
listen: false,
maxConnections: 16,
listenAddress: '',
path: 'ws',
),
),
),
),
);
);

View File

@ -76,7 +76,8 @@ class DHTRecordDescriptor with _$DHTRecordDescriptor {
const factory DHTRecordDescriptor({
required TypedKey key,
required PublicKey owner,
required DHTSchema schema, PublicKey? ownerSecret,
required DHTSchema schema,
PublicKey? ownerSecret,
}) = _DHTRecordDescriptor;
factory DHTRecordDescriptor.fromJson(dynamic json) =>
_$DHTRecordDescriptorFromJson(json as Map<String, dynamic>);
@ -137,9 +138,9 @@ enum Stability {
lowLatency,
reliable;
String toJson() => name.toPascalCase();
factory Stability.fromJson(dynamic j) =>
Stability.values.byName((j as String).toCamelCase());
String toJson() => name.toPascalCase();
}
//////////////////////////////////////
@ -150,16 +151,16 @@ enum Sequencing {
preferOrdered,
ensureOrdered;
String toJson() => name.toPascalCase();
factory Sequencing.fromJson(dynamic j) =>
Sequencing.values.byName((j as String).toCamelCase());
String toJson() => name.toPascalCase();
}
//////////////////////////////////////
/// SafetySelection
@immutable
abstract class SafetySelection extends Equatable {
abstract class SafetySelection {
factory SafetySelection.fromJson(dynamic jsond) {
final json = jsond as Map<String, dynamic>;
if (json.containsKey('Unsafe')) {
@ -175,8 +176,7 @@ abstract class SafetySelection extends Equatable {
}
@immutable
class SafetySelectionUnsafe implements SafetySelection {
class SafetySelectionUnsafe extends Equatable implements SafetySelection {
//
const SafetySelectionUnsafe({
required this.sequencing,
@ -192,8 +192,7 @@ class SafetySelectionUnsafe implements SafetySelection {
}
@immutable
class SafetySelectionSafe implements SafetySelection {
class SafetySelectionSafe extends Equatable implements SafetySelection {
//
const SafetySelectionSafe({
required this.safetySpec,
@ -212,7 +211,10 @@ class SafetySelectionSafe implements SafetySelection {
@freezed
class SafetySpec with _$SafetySpec {
const factory SafetySpec({
required int hopCount, required Stability stability, required Sequencing sequencing, String? preferredRoute,
required int hopCount,
required Stability stability,
required Sequencing sequencing,
String? preferredRoute,
}) = _SafetySpec;
factory SafetySpec.fromJson(dynamic json) =>

View File

@ -208,8 +208,8 @@ class VeilidAPIExceptionInvalidArgument implements VeilidAPIException {
final String value;
@override
String toString() =>
'VeilidAPIException: InvalidArgument ($context:$argument)\n value: $value';
String toString() => 'VeilidAPIException: InvalidArgument'
' ($context:$argument)\n value: $value';
@override
String toDisplayError() => 'Invalid argument for $context: $argument';

View File

@ -125,9 +125,9 @@ enum VeilidConfigLogLevel {
debug,
trace;
factory VeilidConfigLogLevel.fromJson(dynamic j) =>
VeilidConfigLogLevel.values.byName((j as String).toCamelCase());
String toJson() => name.toPascalCase();
factory VeilidConfigLogLevel.fromJson(dynamic j) => VeilidConfigLogLevel.values.byName((j as String).toCamelCase());
}
//////////////////////////////////////
@ -295,7 +295,10 @@ class VeilidConfigRPC with _$VeilidConfigRPC {
const factory VeilidConfigRPC(
{required int concurrency,
required int queueSize,
required int timeoutMs, required int maxRouteHopCount, required int defaultRouteHopCount, int? maxTimestampBehindMs,
required int timeoutMs,
required int maxRouteHopCount,
required int defaultRouteHopCount,
int? maxTimestampBehindMs,
int? maxTimestampAheadMs}) = _VeilidConfigRPC;
factory VeilidConfigRPC.fromJson(dynamic json) =>
@ -335,7 +338,16 @@ class VeilidConfigNetwork with _$VeilidConfigNetwork {
required int clientWhitelistTimeoutMs,
required int reverseConnectionReceiptTimeMs,
required int holePunchReceiptTimeMs,
required VeilidConfigRoutingTable routingTable, required VeilidConfigRPC rpc, required VeilidConfigDHT dht, required bool upnp, required bool detectAddressChanges, required int restrictedNatRetries, required VeilidConfigTLS tls, required VeilidConfigApplication application, required VeilidConfigProtocol protocol, String? networkKeyPassword,
required VeilidConfigRoutingTable routingTable,
required VeilidConfigRPC rpc,
required VeilidConfigDHT dht,
required bool upnp,
required bool detectAddressChanges,
required int restrictedNatRetries,
required VeilidConfigTLS tls,
required VeilidConfigApplication application,
required VeilidConfigProtocol protocol,
String? networkKeyPassword,
}) = _VeilidConfigNetwork;
factory VeilidConfigNetwork.fromJson(dynamic json) =>

View File

@ -16,7 +16,8 @@ const CryptoKind cryptoKindVLD0 =
const CryptoKind cryptoKindNONE =
$N << 0 | $O << 8 | $N << 16 | $E << 24; // "NONE"
String cryptoKindToString(CryptoKind kind) => cryptoKindToBytes(kind).map(String.fromCharCode).join();
String cryptoKindToString(CryptoKind kind) =>
cryptoKindToBytes(kind).map(String.fromCharCode).join();
const CryptoKind bestCryptoKind = cryptoKindVLD0;
@ -30,8 +31,8 @@ CryptoKind cryptoKindFromString(String s) {
if (s.codeUnits.length != 4) {
throw const FormatException('malformed string');
}
final kind = ByteData.sublistView(Uint8List.fromList(s.codeUnits))
.getUint32(0);
final kind =
ByteData.sublistView(Uint8List.fromList(s.codeUnits)).getUint32(0);
return kind;
}
@ -40,7 +41,6 @@ CryptoKind cryptoKindFromString(String s) {
@immutable
class Typed<V extends EncodedString> extends Equatable {
const Typed({required this.kind, required this.value});
factory Typed.fromString(String s) {
@ -62,9 +62,9 @@ class Typed<V extends EncodedString> extends Equatable {
String toString() => '${cryptoKindToString(kind)}:$value';
Uint8List decode() {
final b = BytesBuilder();
b.add(cryptoKindToBytes(kind));
b.add(value.decode());
final b = BytesBuilder()
..add(cryptoKindToBytes(kind))
..add(value.decode());
return b.toBytes();
}
@ -73,7 +73,6 @@ class Typed<V extends EncodedString> extends Equatable {
@immutable
class KeyPair extends Equatable {
const KeyPair({required this.key, required this.secret});
factory KeyPair.fromString(String s) {
@ -101,7 +100,6 @@ class KeyPair extends Equatable {
@immutable
class TypedKeyPair extends Equatable {
const TypedKeyPair(
{required this.kind, required this.key, required this.secret});
@ -129,8 +127,7 @@ class TypedKeyPair extends Equatable {
List<Object> get props => [kind, key, secret];
@override
String toString() =>
'${cryptoKindToString(kind)}:$key:$secret';
String toString() => '${cryptoKindToString(kind)}:$key:$secret';
String toJson() => toString();
}
@ -169,13 +166,15 @@ abstract class VeilidCryptoSystem {
Future<HashDigest> generateHash(Uint8List data);
//Future<HashDigest> generateHashReader(Stream<List<int>> reader);
Future<bool> validateKeyPair(PublicKey key, SecretKey secret);
Future<bool> validateKeyPairWithKeyPair(KeyPair keyPair) => validateKeyPair(keyPair.key, keyPair.secret);
Future<bool> validateKeyPairWithKeyPair(KeyPair keyPair) =>
validateKeyPair(keyPair.key, keyPair.secret);
Future<bool> validateHash(Uint8List data, HashDigest hash);
//Future<bool> validateHashReader(Stream<List<int>> reader, HashDigest hash);
Future<CryptoKeyDistance> distance(CryptoKey key1, CryptoKey key2);
Future<Signature> sign(PublicKey key, SecretKey secret, Uint8List data);
Future<Signature> signWithKeyPair(KeyPair keyPair, Uint8List data) => sign(keyPair.key, keyPair.secret, data);
Future<Signature> signWithKeyPair(KeyPair keyPair, Uint8List data) =>
sign(keyPair.key, keyPair.secret, data);
Future<void> verify(PublicKey key, Uint8List data, Signature signature);
Future<int> aeadOverhead();

View File

@ -26,7 +26,8 @@ typedef _DartPostCObject
= NativeFunction<Int8 Function(Int64, Pointer<Dart_CObject>)>;
// fn free_string(s: *mut std::os::raw::c_char)
typedef _FreeStringDart = void Function(Pointer<Utf8>);
// fn initialize_veilid_flutter(dart_post_c_object_ptr: ffi::DartPostCObjectFnType)
// fn initialize_veilid_flutter(
// dart_post_c_object_ptr: ffi::DartPostCObjectFnType)
// fn initialize_veilid_core(platform_config: FfiStr)
typedef _InitializeVeilidCoreDart = void Function(Pointer<Utf8>);
// fn change_log_level(layer: FfiStr, log_level: FfiStr)
@ -50,16 +51,20 @@ typedef _RoutingContextWithPrivacyDart = int Function(int);
typedef _RoutingContextWithCustomPrivacyDart = int Function(int, Pointer<Utf8>);
// fn routing_context_with_sequencing(id: u32, sequencing: FfiStr)
typedef _RoutingContextWithSequencingDart = int Function(int, Pointer<Utf8>);
// fn routing_context_app_call(port: i64, id: u32, target: FfiStr, request: FfiStr)
// fn routing_context_app_call(port: i64,
// id: u32, target: FfiStr, request: FfiStr)
typedef _RoutingContextAppCallDart = void Function(
int, int, Pointer<Utf8>, Pointer<Utf8>);
// fn routing_context_app_message(port: i64, id: u32, target: FfiStr, request: FfiStr)
// fn routing_context_app_message(port: i64,
// id: u32, target: FfiStr, request: FfiStr)
typedef _RoutingContextAppMessageDart = void Function(
int, int, Pointer<Utf8>, Pointer<Utf8>);
// fn routing_context_create_dht_record(port: i64, id: u32, kind: u32, schema: FfiStr)
// fn routing_context_create_dht_record(port: i64,
// id: u32, kind: u32, schema: FfiStr)
typedef _RoutingContextCreateDHTRecordDart = void Function(
int, int, Pointer<Utf8>, int);
// fn routing_context_open_dht_record(port: i64, id: u32, key: FfiStr, writer: FfiStr)
// fn routing_context_open_dht_record(port: i64,
// id: u32, key: FfiStr, writer: FfiStr)
typedef _RoutingContextOpenDHTRecordDart = void Function(
int, int, Pointer<Utf8>, Pointer<Utf8>);
// fn routing_context_close_dht_record(port: i64, id: u32, key: FfiStr)
@ -68,16 +73,20 @@ typedef _RoutingContextCloseDHTRecordDart = void Function(
// fn routing_context_delete_dht_record(port: i64, id: u32, key: FfiStr)
typedef _RoutingContextDeleteDHTRecordDart = void Function(
int, int, Pointer<Utf8>);
// fn routing_context_get_dht_value(port: i64, id: u32, key: FfiStr, subkey: u32, force_refresh: bool)
// fn routing_context_get_dht_value(port: i64,
// id: u32, key: FfiStr, subkey: u32, force_refresh: bool)
typedef _RoutingContextGetDHTValueDart = void Function(
int, int, Pointer<Utf8>, int, bool);
// fn routing_context_set_dht_value(port: i64, id: u32, key: FfiStr, subkey: u32, data: FfiStr)
// fn routing_context_set_dht_value(port: i64,
// id: u32, key: FfiStr, subkey: u32, data: FfiStr)
typedef _RoutingContextSetDHTValueDart = void Function(
int, int, Pointer<Utf8>, int, Pointer<Utf8>);
// fn routing_context_watch_dht_values(port: i64, id: u32, key: FfiStr, subkeys: FfiStr, expiration: FfiStr, count: u32)
// fn routing_context_watch_dht_values(port: i64,
// id: u32, key: FfiStr, subkeys: FfiStr, expiration: FfiStr, count: u32)
typedef _RoutingContextWatchDHTValuesDart = void Function(
int, int, Pointer<Utf8>, Pointer<Utf8>, int, int);
// fn routing_context_cancel_dht_watch(port: i64, id: u32, key: FfiStr, subkeys: FfiStr)
// fn routing_context_cancel_dht_watch(port: i64,
// id: u32, key: FfiStr, subkeys: FfiStr)
typedef _RoutingContextCancelDHTWatchDart = void Function(
int, int, Pointer<Utf8>, Pointer<Utf8>);
@ -119,7 +128,8 @@ typedef _ReleaseTableDbTransactionDart = int Function(int);
typedef _TableDbTransactionCommitDart = void Function(int, int);
// fn table_db_transaction_rollback(port: i64, id: u32)
typedef _TableDbTransactionRollbackDart = void Function(int, int);
// fn table_db_transaction_store(port: i64, id: u32, col: u32, key: FfiStr, value: FfiStr)
// fn table_db_transaction_store(port: i64,
// id: u32, col: u32, key: FfiStr, value: FfiStr)
typedef _TableDbTransactionStoreDart = void Function(
int, int, int, Pointer<Utf8>, Pointer<Utf8>);
// fn table_db_transaction_delete(port: i64, id: u32, col: u32, key: FfiStr)
@ -129,7 +139,8 @@ typedef _TableDbTransactionDeleteDart = void Function(
typedef _ValidCryptoKindsDart = Pointer<Utf8> Function();
// fn best_crypto_kind() -> u32
typedef _BestCryptoKindDart = int Function();
// fn verify_signatures(port: i64, node_ids: FfiStr, data: FfiStr, signatures: FfiStr)
// fn verify_signatures(port: i64,
// node_ids: FfiStr, data: FfiStr, signatures: FfiStr)
typedef _VerifySignaturesDart = void Function(
int, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>);
// fn generate_signatures(port: i64, data: FfiStr, key_pairs: FfiStr)
@ -147,13 +158,15 @@ typedef _CryptoComputeDHDart = void Function(
typedef _CryptoRandomBytesDart = void Function(int, int, int);
// fn crypto_default_salt_length(port: i64, kind: u32)
typedef _CryptoDefaultSaltLengthDart = void Function(int, int);
// fn crypto_hash_password(port: i64, kind: u32, password: FfiStr, salt: FfiStr )
// fn crypto_hash_password(port: i64, kind: u32, password: FfiStr, salt: FfiStr)
typedef _CryptoHashPasswordDart = void Function(
int, int, Pointer<Utf8>, Pointer<Utf8>);
// fn crypto_verify_password(port: i64, kind: u32, password: FfiStr, password_hash: FfiStr )
// fn crypto_verify_password(port: i64,
// kind: u32, password: FfiStr, password_hash: FfiStr )
typedef _CryptoVerifyPasswordDart = void Function(
int, int, Pointer<Utf8>, Pointer<Utf8>);
// fn crypto_derive_shared_secret(port: i64, kind: u32, password: FfiStr, salt: FfiStr )
// fn crypto_derive_shared_secret(port: i64,
// kind: u32, password: FfiStr, salt: FfiStr )
// fn crypto_random_nonce(port: i64, kind: u32)
typedef _CryptoRandomNonceDart = void Function(int, int);
@ -163,7 +176,8 @@ typedef _CryptoRandomSharedSecretDart = void Function(int, int);
typedef _CryptoGenerateKeyPairDart = void Function(int, int);
// fn crypto_generate_hash(port: i64, kind: u32, data: FfiStr)
typedef _CryptoGenerateHashDart = void Function(int, int, Pointer<Utf8>);
// fn crypto_validate_key_pair(port: i64, kind: u32, key: FfiStr, secret: FfiStr)
// fn crypto_validate_key_pair(port: i64,
// kind: u32, key: FfiStr, secret: FfiStr)
typedef _CryptoValidateKeyPairDart = void Function(
int, int, Pointer<Utf8>, Pointer<Utf8>);
// fn crypto_validate_hash(port: i64, kind: u32, data: FfiStr, hash: FfiStr)
@ -172,21 +186,28 @@ typedef _CryptoValidateHashDart = void Function(
// fn crypto_distance(port: i64, kind: u32, key1: FfiStr, key2: FfiStr)
typedef _CryptoDistanceDart = void Function(
int, int, Pointer<Utf8>, Pointer<Utf8>);
// fn crypto_sign(port: i64, kind: u32, key: FfiStr, secret: FfiStr, data: FfiStr)
// fn crypto_sign(port: i64,
// kind: u32, key: FfiStr, secret: FfiStr, data: FfiStr)
typedef _CryptoSignDart = void Function(
int, int, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>);
// fn crypto_verify(port: i64, kind: u32, key: FfiStr, data: FfiStr, signature: FfiStr)
// fn crypto_verify(port: i64,
// kind: u32, key: FfiStr, data: FfiStr, signature: FfiStr)
typedef _CryptoVerifyDart = void Function(
int, int, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>);
// fn crypto_aead_overhead(port: i64, kind: u32)
typedef _CryptoAeadOverheadDart = void Function(int, int);
// fn crypto_decrypt_aead(port: i64, kind: u32, body: FfiStr, nonce: FfiStr, shared_secret: FfiStr, associated_data: FfiStr)
// fn crypto_decrypt_aead(port: i64,
// kind: u32, body: FfiStr, nonce: FfiStr,
// shared_secret: FfiStr, associated_data: FfiStr)
typedef _CryptoDecryptAeadDart = void Function(
int, int, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>);
// fn crypto_encrypt_aead(port: i64, kind: u32, body: FfiStr, nonce: FfiStr, shared_secret: FfiStr, associated_data: FfiStr)
// fn crypto_encrypt_aead(port: i64,
// kind: u32, body: FfiStr, nonce: FfiStr,
// shared_secret: FfiStr, associated_data: FfiStr)
typedef _CryptoEncryptAeadDart = void Function(
int, int, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>);
// fn crypto_crypt_no_auth(port: i64, kind: u32, body: FfiStr, nonce: FfiStr, shared_secret: FfiStr)
// fn crypto_crypt_no_auth(port: i64,
// kind: u32, body: FfiStr, nonce: FfiStr, shared_secret: FfiStr)
typedef _CryptoCryptNoAuthDart = void Function(
int, int, Pointer<Utf8>, Pointer<Utf8>, Pointer<Utf8>);
@ -343,8 +364,8 @@ Future<void> processFutureVoid(Future<dynamic> future) async =>
case messageOk:
{
if (list[1] != null) {
throw VeilidAPIExceptionInternal(
"Unexpected MESSAGE_OK value '${list[1]}' where null expected");
throw VeilidAPIExceptionInternal('Unexpected MESSAGE_OK value'
' "${list[1]}" where null expected');
}
return;
}
@ -357,7 +378,8 @@ Future<void> processFutureVoid(Future<dynamic> future) async =>
final ret = jsonDecode(list[1] as String);
if (ret != null) {
throw VeilidAPIExceptionInternal(
"Unexpected MESSAGE_OK_JSON value '$ret' where null expected");
'Unexpected MESSAGE_OK_JSON value'
' "$ret" where null expected');
}
return;
}
@ -385,8 +407,8 @@ Future<Stream<T>> processFutureStream<T>(
case messageOk:
{
if (list[1] != null) {
throw VeilidAPIExceptionInternal(
"Unexpected MESSAGE_OK value '${list[1]}' where null expected");
throw VeilidAPIExceptionInternal('Unexpected MESSAGE_OK value'
' "${list[1]}" where null expected');
}
return returnStream;
}
@ -399,7 +421,8 @@ Future<Stream<T>> processFutureStream<T>(
final ret = jsonDecode(list[1] as String);
if (ret != null) {
throw VeilidAPIExceptionInternal(
"Unexpected MESSAGE_OK_JSON value '$ret' where null expected");
'Unexpected MESSAGE_OK_JSON value'
' "$ret" where null expected');
}
return returnStream;
}

View File

@ -23,19 +23,20 @@ Future<T> _wrapApiPromise<T>(Object p) => js_util
}, test: (e) => e is! VeilidAPIException);
class _Ctx {
_Ctx(int this.id, this.js);
int? id;
_Ctx(int id, this.js) : _id = id;
int? _id;
final VeilidJS js;
void ensureValid() {
if (id == null) {
int requireId() {
if (_id == null) {
throw VeilidAPIExceptionNotInitialized();
}
return _id!;
}
void close() {
if (id != null) {
js_util.callMethod<void>(wasm, 'release_routing_context', [id]);
id = null;
if (_id != null) {
js_util.callMethod<void>(wasm, 'release_routing_context', [_id]);
_id = null;
}
}
}
@ -55,16 +56,15 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
@override
VeilidRoutingContextJS withPrivacy() {
_ctx.ensureValid();
final id = _ctx.requireId();
final int newId =
js_util.callMethod(wasm, 'routing_context_with_privacy', [_ctx.id!]);
js_util.callMethod(wasm, 'routing_context_with_privacy', [id]);
return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js));
}
@override
VeilidRoutingContextJS withCustomPrivacy(SafetySelection safetySelection) {
_ctx.ensureValid();
final id = _ctx.id!;
final id = _ctx.requireId();
final newId = js_util.callMethod<int>(
wasm,
'routing_context_with_custom_privacy',
@ -75,8 +75,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
@override
VeilidRoutingContextJS withSequencing(Sequencing sequencing) {
_ctx.ensureValid();
final id = _ctx.id!;
final id = _ctx.requireId();
final newId = js_util.callMethod<int>(
wasm, 'routing_context_with_sequencing', [id, jsonEncode(sequencing)]);
return VeilidRoutingContextJS._(_Ctx(newId, _ctx.js));
@ -84,8 +83,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
@override
Future<Uint8List> appCall(String target, Uint8List request) async {
_ctx.ensureValid();
final id = _ctx.id!;
final id = _ctx.requireId();
final encodedRequest = base64UrlNoPadEncode(request);
return base64UrlNoPadDecode(await _wrapApiPromise(js_util.callMethod(
@ -94,8 +92,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
@override
Future<void> appMessage(String target, Uint8List message) {
_ctx.ensureValid();
final id = _ctx.id!;
final id = _ctx.requireId();
final encodedMessage = base64UrlNoPadEncode(message);
return _wrapApiPromise(js_util.callMethod(
@ -105,8 +102,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
@override
Future<DHTRecordDescriptor> createDHTRecord(DHTSchema schema,
{CryptoKind kind = 0}) async {
_ctx.ensureValid();
final id = _ctx.id!;
final id = _ctx.requireId();
return DHTRecordDescriptor.fromJson(jsonDecode(await _wrapApiPromise(js_util
.callMethod(wasm, 'routing_context_create_dht_record',
[id, jsonEncode(schema), kind]))));
@ -115,8 +111,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
@override
Future<DHTRecordDescriptor> openDHTRecord(
TypedKey key, KeyPair? writer) async {
_ctx.ensureValid();
final id = _ctx.id!;
final id = _ctx.requireId();
return DHTRecordDescriptor.fromJson(jsonDecode(await _wrapApiPromise(js_util
.callMethod(wasm, 'routing_context_open_dht_record', [
id,
@ -127,16 +122,14 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
@override
Future<void> closeDHTRecord(TypedKey key) {
_ctx.ensureValid();
final id = _ctx.id!;
final id = _ctx.requireId();
return _wrapApiPromise(js_util.callMethod(
wasm, 'routing_context_close_dht_record', [id, jsonEncode(key)]));
}
@override
Future<void> deleteDHTRecord(TypedKey key) {
_ctx.ensureValid();
final id = _ctx.id!;
final id = _ctx.requireId();
return _wrapApiPromise(js_util.callMethod(
wasm, 'routing_context_delete_dht_record', [id, jsonEncode(key)]));
}
@ -144,8 +137,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
@override
Future<ValueData?> getDHTValue(
TypedKey key, int subkey, bool forceRefresh) async {
_ctx.ensureValid();
final id = _ctx.id!;
final id = _ctx.requireId();
final opt = await _wrapApiPromise<String?>(js_util.callMethod(
wasm,
'routing_context_get_dht_value',
@ -156,8 +148,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
@override
Future<ValueData?> setDHTValue(
TypedKey key, int subkey, Uint8List data) async {
_ctx.ensureValid();
final id = _ctx.id!;
final id = _ctx.requireId();
final opt = await _wrapApiPromise<String?>(js_util.callMethod(
wasm,
'routing_context_set_dht_value',
@ -168,8 +159,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
@override
Future<Timestamp> watchDHTValues(TypedKey key, List<ValueSubkeyRange> subkeys,
Timestamp expiration, int count) async {
_ctx.ensureValid();
final id = _ctx.id!;
final id = _ctx.requireId();
final ts = await _wrapApiPromise<String>(js_util.callMethod(
wasm, 'routing_context_watch_dht_values', [
id,
@ -183,8 +173,7 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
@override
Future<bool> cancelDHTWatch(TypedKey key, List<ValueSubkeyRange> subkeys) {
_ctx.ensureValid();
final id = _ctx.id!;
final id = _ctx.requireId();
return _wrapApiPromise(js_util.callMethod(
wasm,
'routing_context_cancel_dht_watch',
@ -194,11 +183,11 @@ class VeilidRoutingContextJS extends VeilidRoutingContext {
// JS implementation of VeilidCryptoSystem
class VeilidCryptoSystemJS extends VeilidCryptoSystem {
VeilidCryptoSystemJS._(this._js, this._kind) {
// Keep the reference
_js;
}
VeilidCryptoSystemJS._(this._js, this._kind);
final CryptoKind _kind;
// Keep the reference
// ignore: unused_field
final VeilidJS _js;
@override
@ -420,19 +409,22 @@ class VeilidTableDBTransactionJS extends VeilidTableDBTransaction {
}
class _TDB {
_TDB(int this.id, this.js);
int? id;
_TDB(int id, this.js) : _id = id;
int? _id;
final VeilidJS js;
void ensureValid() {
if (id == null) {
int requireId() {
if (_id == null) {
throw VeilidAPIExceptionNotInitialized();
}
return _id!;
}
void close() {
if (id != null) {
js_util.callMethod<void>(wasm, 'release_table_db', [id]);
id = null;
if (_id != null) {
js_util.callMethod<void>(wasm, 'release_table_db', [_id]);
_id = null;
}
}
}
@ -452,22 +444,20 @@ class VeilidTableDBJS extends VeilidTableDB {
@override
int getColumnCount() {
_tdb.ensureValid();
return js_util.callMethod(wasm, 'table_db_get_column_count', [_tdb.id!]);
final id = _tdb.requireId();
return js_util.callMethod(wasm, 'table_db_get_column_count', [id]);
}
@override
Future<List<Uint8List>> getKeys(int col) async {
_tdb.ensureValid();
final id = _tdb.id!;
final id = _tdb.requireId();
return jsonListConstructor(base64UrlNoPadDecodeDynamic)(jsonDecode(
await js_util.callMethod(wasm, 'table_db_get_keys', [id, col])));
}
@override
VeilidTableDBTransaction transact() {
_tdb.ensureValid();
final id = _tdb.id!;
final id = _tdb.requireId();
final xid = js_util.callMethod<int>(wasm, 'table_db_transact', [id]);
return VeilidTableDBTransactionJS._(_TDBT(xid, this, _tdb.js));
@ -475,8 +465,7 @@ class VeilidTableDBJS extends VeilidTableDB {
@override
Future<void> store(int col, Uint8List key, Uint8List value) {
_tdb.ensureValid();
final id = _tdb.id!;
final id = _tdb.requireId();
final encodedKey = base64UrlNoPadEncode(key);
final encodedValue = base64UrlNoPadEncode(value);
@ -486,8 +475,7 @@ class VeilidTableDBJS extends VeilidTableDB {
@override
Future<Uint8List?> load(int col, Uint8List key) async {
_tdb.ensureValid();
final id = _tdb.id!;
final id = _tdb.requireId();
final encodedKey = base64UrlNoPadEncode(key);
final out = await _wrapApiPromise<String?>(
@ -500,8 +488,7 @@ class VeilidTableDBJS extends VeilidTableDB {
@override
Future<Uint8List?> delete(int col, Uint8List key) async {
_tdb.ensureValid();
final id = _tdb.id!;
final id = _tdb.requireId();
final encodedKey = base64UrlNoPadEncode(key);
final out = await _wrapApiPromise<String?>(

View File

@ -21,9 +21,10 @@ enum AttachmentState {
overAttached,
detaching;
String toJson() => name.toPascalCase();
factory AttachmentState.fromJson(dynamic j) =>
AttachmentState.values.byName((j as String).toCamelCase());
String toJson() => name.toPascalCase();
}
//////////////////////////////////////
@ -36,9 +37,10 @@ enum VeilidLogLevel {
debug,
trace;
String toJson() => name.toPascalCase();
factory VeilidLogLevel.fromJson(dynamic j) =>
VeilidLogLevel.values.byName((j as String).toCamelCase());
String toJson() => name.toPascalCase();
}
////////////
@ -109,7 +111,8 @@ class PeerStats with _$PeerStats {
const factory PeerStats({
required Timestamp timeAdded,
required RPCStats rpcStats,
required TransferStatsDownUp transfer, LatencyStats? latency,
required TransferStatsDownUp transfer,
LatencyStats? latency,
}) = _PeerStats;
factory PeerStats.fromJson(dynamic json) =>
@ -141,10 +144,13 @@ sealed class VeilidUpdate with _$VeilidUpdate {
String? backtrace,
}) = VeilidLog;
const factory VeilidUpdate.appMessage({
@Uint8ListJsonConverter() required Uint8List message, TypedKey? sender,
@Uint8ListJsonConverter() required Uint8List message,
TypedKey? sender,
}) = VeilidAppMessage;
const factory VeilidUpdate.appCall({
@Uint8ListJsonConverter() required Uint8List message, required String callId, TypedKey? sender,
@Uint8ListJsonConverter() required Uint8List message,
required String callId,
TypedKey? sender,
}) = VeilidAppCall;
const factory VeilidUpdate.attachment(
{required AttachmentState state,

View File

@ -12,12 +12,14 @@ abstract class VeilidTableDBTransaction {
Future<void> delete(int col, Uint8List key);
Future<void> storeJson(int col, Uint8List key, Object? object,
{Object? Function(Object? nonEncodable)? toEncodable}) async => store(col, key,
utf8.encoder.convert(jsonEncode(object, toEncodable: toEncodable)));
{Object? Function(Object? nonEncodable)? toEncodable}) async =>
store(col, key,
utf8.encoder.convert(jsonEncode(object, toEncodable: toEncodable)));
Future<void> storeStringJson(int col, String key, Object? object,
{Object? Function(Object? nonEncodable)? toEncodable}) => storeJson(col, utf8.encoder.convert(key), object,
toEncodable: toEncodable);
{Object? Function(Object? nonEncodable)? toEncodable}) =>
storeJson(col, utf8.encoder.convert(key), object,
toEncodable: toEncodable);
}
abstract class VeilidTableDB {
@ -30,12 +32,14 @@ abstract class VeilidTableDB {
Future<Uint8List?> delete(int col, Uint8List key);
Future<void> storeJson(int col, Uint8List key, Object? object,
{Object? Function(Object? nonEncodable)? toEncodable}) => store(col, key,
utf8.encoder.convert(jsonEncode(object, toEncodable: toEncodable)));
{Object? Function(Object? nonEncodable)? toEncodable}) =>
store(col, key,
utf8.encoder.convert(jsonEncode(object, toEncodable: toEncodable)));
Future<void> storeStringJson(int col, String key, Object? object,
{Object? Function(Object? nonEncodable)? toEncodable}) => storeJson(col, utf8.encoder.convert(key), object,
toEncodable: toEncodable);
{Object? Function(Object? nonEncodable)? toEncodable}) =>
storeJson(col, utf8.encoder.convert(key), object,
toEncodable: toEncodable);
Future<Object?> loadJson(int col, Uint8List key,
{Object? Function(Object? key, Object? value)? reviver}) async {
@ -47,7 +51,8 @@ abstract class VeilidTableDB {
}
Future<Object?> loadStringJson(int col, String key,
{Object? Function(Object? key, Object? value)? reviver}) => loadJson(col, utf8.encoder.convert(key), reviver: reviver);
{Object? Function(Object? key, Object? value)? reviver}) =>
loadJson(col, utf8.encoder.convert(key), reviver: reviver);
Future<Object?> deleteJson(int col, Uint8List key,
{Object? Function(Object? key, Object? value)? reviver}) async {
@ -59,5 +64,6 @@ abstract class VeilidTableDB {
}
Future<Object?> deleteStringJson(int col, String key,
{Object? Function(Object? key, Object? value)? reviver}) => deleteJson(col, utf8.encoder.convert(key), reviver: reviver);
{Object? Function(Object? key, Object? value)? reviver}) =>
deleteJson(col, utf8.encoder.convert(key), reviver: reviver);
}

View File

@ -18,11 +18,11 @@ dependencies:
sdk: flutter
freezed_annotation: ^2.2.0
json_annotation: ^4.8.1
path_provider: ^2.0.9
path: ^1.8.0
path_provider: ^2.0.9
system_info2: ^3.0.2
system_info_plus: ^0.0.5
dev_dependencies:
build_runner: ^2.4.6
flutter_test: