diff --git a/lib/repositories/local_account_repository.dart b/lib/repositories/local_account_repository.dart deleted file mode 100644 index 07ced9f..0000000 --- a/lib/repositories/local_account_repository.dart +++ /dev/null @@ -1,28 +0,0 @@ -import 'package:veilid/veilid.dart'; -import 'package:riverpod_annotation/riverpod_annotation.dart'; -import '../entities/entities.dart'; -import '../entities/proto.dart' as proto; - -import 'local_account_repository_impl.dart'; - -part 'local_account_repository.g.dart'; - -// Local account manager -abstract class LocalAccountRepository { - /// Creates a new master identity and returns it with its secrets - Future newIdentityMaster(); - - /// Creates a new account associated with master identity - Future newAccount( - IdentityMaster identityMaster, - SecretKey identitySecret, - EncryptionKeyType encryptionKeyType, - String encryptionKey, - proto.Account account); -} - -@riverpod -Future localAccountManager( - LocalAccountManagerRef ref) async { - return await LocalAccountRepositoryImpl.open(); -} diff --git a/lib/repositories/local_account_repository_impl.dart b/lib/repositories/local_accounts.dart similarity index 87% rename from lib/repositories/local_account_repository_impl.dart rename to lib/repositories/local_accounts.dart index 7b2dee8..dd090c1 100644 --- a/lib/repositories/local_account_repository_impl.dart +++ b/lib/repositories/local_accounts.dart @@ -1,27 +1,26 @@ +import 'dart:async'; import 'dart:convert'; import 'dart:typed_data'; import 'package:fast_immutable_collections/fast_immutable_collections.dart'; import 'package:veilid/veilid.dart'; +import 'package:riverpod_annotation/riverpod_annotation.dart'; import '../tools/tools.dart'; import '../entities/entities.dart'; import '../entities/proto.dart' as proto; -import 'local_account_repository.dart'; +part 'local_accounts.g.dart'; // Local account manager -class LocalAccountRepositoryImpl extends LocalAccountRepository { - IList _localAccounts; - +@riverpod +abstract class LocalAccounts extends _$LocalAccounts { static const localAccountManagerTable = "local_account_manager"; static const localAccountsKey = "local_accounts"; - LocalAccountRepositoryImpl._({required IList localAccounts}) - : _localAccounts = localAccounts; - - /// Gets or creates a local account manager - static Future open() async { + /// Get all local account information + @override + FutureOr> build() async { // Load accounts from tabledb final localAccounts = await tableScope(localAccountManagerTable, (tdb) async { @@ -31,19 +30,17 @@ class LocalAccountRepositoryImpl extends LocalAccountRepository { localAccountsJson, genericFromJson(LocalAccount.fromJson)) : IList(); }); - - return LocalAccountRepositoryImpl._(localAccounts: localAccounts); + return localAccounts; } /// Store things back to storage - Future flush() async { + Future flush(IList localAccounts) async { await tableScope(localAccountManagerTable, (tdb) async { - await tdb.storeStringJson(0, localAccountsKey, _localAccounts); + await tdb.storeStringJson(0, localAccountsKey, localAccounts); }); } /// Creates a new master identity and returns it with its secrets - @override Future newIdentityMaster() async { final crypto = await Veilid.instance.bestCryptoSystem(); final dhtctx = (await Veilid.instance.routingContext()) @@ -94,13 +91,14 @@ class LocalAccountRepositoryImpl extends LocalAccountRepository { } /// Creates a new account associated with master identity - @override Future newAccount( IdentityMaster identityMaster, SecretKey identitySecret, EncryptionKeyType encryptionKeyType, String encryptionKey, proto.Account account) async { + final localAccounts = state.requireValue; + // Encrypt identitySecret with key final cs = await Veilid.instance.bestCryptoSystem(); final ekbytes = Uint8List.fromList(utf8.encode(encryptionKey)); @@ -152,8 +150,15 @@ class LocalAccountRepositoryImpl extends LocalAccountRepository { }); // Add local account object to internal store + final newLocalAccounts = localAccounts.add(localAccount); + await flush(newLocalAccounts); + state = AsyncValue.data(newLocalAccounts); // Return local account object return localAccount; } + + /// Import an account from another VeilidChat instance + + /// Recover an account with the master identity secret } diff --git a/lib/repositories/local_account_repository.g.dart b/lib/repositories/local_accounts.g.dart similarity index 54% rename from lib/repositories/local_account_repository.g.dart rename to lib/repositories/local_accounts.g.dart index b837ac2..921ae8a 100644 --- a/lib/repositories/local_account_repository.g.dart +++ b/lib/repositories/local_accounts.g.dart @@ -1,27 +1,25 @@ // GENERATED CODE - DO NOT MODIFY BY HAND -part of 'local_account_repository.dart'; +part of 'local_accounts.dart'; // ************************************************************************** // RiverpodGenerator // ************************************************************************** -String _$localAccountManagerHash() => - r'0ecdc84f868edb33f6e62bfe116f18568b424267'; +String _$localAccountsHash() => r'a4ad015e192c5db8e59ba2b5e922109c34572095'; -/// See also [localAccountManager]. -@ProviderFor(localAccountManager) -final localAccountManagerProvider = - AutoDisposeFutureProvider.internal( - localAccountManager, - name: r'localAccountManagerProvider', +/// See also [LocalAccounts]. +@ProviderFor(LocalAccounts) +final localAccountsProvider = AutoDisposeAsyncNotifierProvider>.internal( + LocalAccounts.new, + name: r'localAccountsProvider', debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') ? null - : _$localAccountManagerHash, + : _$localAccountsHash, dependencies: null, allTransitiveDependencies: null, ); -typedef LocalAccountManagerRef - = AutoDisposeFutureProviderRef; +typedef _$LocalAccounts = AutoDisposeAsyncNotifier>; // ignore_for_file: unnecessary_raw_strings, subtype_of_sealed_class, invalid_use_of_internal_member, do_not_use_environment, prefer_const_constructors, public_member_api_docs, avoid_private_typedef_functions diff --git a/lib/repositories/repositories.dart b/lib/repositories/repositories.dart index c20fa81..3c533cd 100644 --- a/lib/repositories/repositories.dart +++ b/lib/repositories/repositories.dart @@ -1 +1,3 @@ -export 'local_account_repository.dart'; +export 'local_accounts.dart'; + +// xxx rename this probably \ No newline at end of file diff --git a/lib/tools/dht_record.dart b/lib/tools/dht_record.dart index 9faf15a..0f7093f 100644 --- a/lib/tools/dht_record.dart +++ b/lib/tools/dht_record.dart @@ -10,7 +10,8 @@ class DHTRecord { static Future create(VeilidRoutingContext dhtctx, {DHTSchema schema = const DHTSchema.dflt(oCnt: 1), - int defaultSubkey = 0}) async { + int defaultSubkey = 0, + DHTRecordEncryption encrypt = DHTRecordEncryption.private}) async { DHTRecordDescriptor recordDescriptor = await dhtctx.createDHTRecord(schema); return DHTRecord( dhtctx: dhtctx, @@ -20,7 +21,8 @@ class DHTRecord { static Future open( VeilidRoutingContext dhtctx, TypedKey recordKey, KeyPair? writer, - {int defaultSubkey = 0}) async { + {int defaultSubkey = 0, + DHTRecordEncryption encrypt = DHTRecordEncryption.private}) async { DHTRecordDescriptor recordDescriptor = await dhtctx.openDHTRecord(recordKey, writer); return DHTRecord( diff --git a/lib/tools/dht_record_encryption.dart b/lib/tools/dht_record_encryption.dart new file mode 100644 index 0000000..a76c458 --- /dev/null +++ b/lib/tools/dht_record_encryption.dart @@ -0,0 +1,49 @@ +import 'dart:async'; + +import 'package:veilid/veilid.dart'; +import 'dart:typed_data'; +import 'tools.dart'; + +abstract class DHTRecordEncryption { + factory DHTRecordEncryption.private() { + return DHTRecordEncryptionPrivate(); + } + factory DHTRecordEncryption.public() { + return DHTRecordEncryptionPublic(); + } + + FutureOr encrypt(Uint8List data); + FutureOr decrypt(Uint8List data); +} + +class DHTRecordEncryptionPrivate implements DHTRecordEncryption { + DHTRecordEncryptionPrivate() { + // + } + + @override + FutureOr encrypt(Uint8List data) { + // + } + + @override + FutureOr decrypt(Uint8List data) { + // + } +} + +class DHTRecordEncryptionPublic implements DHTRecordEncryption { + DHTRecordEncryptionPublic() { + // + } + + @override + FutureOr encrypt(Uint8List data) { + return data; + } + + @override + FutureOr decrypt(Uint8List data) { + return data; + } +}