This commit is contained in:
Christien Rioux 2023-07-20 15:48:55 -04:00
parent 20bdf07f24
commit fb93e07ef2
6 changed files with 86 additions and 58 deletions

View File

@ -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<IdentityMasterWithSecrets> newIdentityMaster();
/// Creates a new account associated with master identity
Future<LocalAccount> newAccount(
IdentityMaster identityMaster,
SecretKey identitySecret,
EncryptionKeyType encryptionKeyType,
String encryptionKey,
proto.Account account);
}
@riverpod
Future<LocalAccountRepository> localAccountManager(
LocalAccountManagerRef ref) async {
return await LocalAccountRepositoryImpl.open();
}

View File

@ -1,27 +1,26 @@
import 'dart:async';
import 'dart:convert'; import 'dart:convert';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:fast_immutable_collections/fast_immutable_collections.dart'; import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:veilid/veilid.dart'; import 'package:veilid/veilid.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../tools/tools.dart'; import '../tools/tools.dart';
import '../entities/entities.dart'; import '../entities/entities.dart';
import '../entities/proto.dart' as proto; import '../entities/proto.dart' as proto;
import 'local_account_repository.dart'; part 'local_accounts.g.dart';
// Local account manager // Local account manager
class LocalAccountRepositoryImpl extends LocalAccountRepository { @riverpod
IList<LocalAccount> _localAccounts; abstract class LocalAccounts extends _$LocalAccounts {
static const localAccountManagerTable = "local_account_manager"; static const localAccountManagerTable = "local_account_manager";
static const localAccountsKey = "local_accounts"; static const localAccountsKey = "local_accounts";
LocalAccountRepositoryImpl._({required IList<LocalAccount> localAccounts}) /// Get all local account information
: _localAccounts = localAccounts; @override
FutureOr<IList<LocalAccount>> build() async {
/// Gets or creates a local account manager
static Future<LocalAccountRepository> open() async {
// Load accounts from tabledb // Load accounts from tabledb
final localAccounts = final localAccounts =
await tableScope(localAccountManagerTable, (tdb) async { await tableScope(localAccountManagerTable, (tdb) async {
@ -31,19 +30,17 @@ class LocalAccountRepositoryImpl extends LocalAccountRepository {
localAccountsJson, genericFromJson(LocalAccount.fromJson)) localAccountsJson, genericFromJson(LocalAccount.fromJson))
: IList<LocalAccount>(); : IList<LocalAccount>();
}); });
return localAccounts;
return LocalAccountRepositoryImpl._(localAccounts: localAccounts);
} }
/// Store things back to storage /// Store things back to storage
Future<void> flush() async { Future<void> flush(IList<LocalAccount> localAccounts) async {
await tableScope(localAccountManagerTable, (tdb) 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 /// Creates a new master identity and returns it with its secrets
@override
Future<IdentityMasterWithSecrets> newIdentityMaster() async { Future<IdentityMasterWithSecrets> newIdentityMaster() async {
final crypto = await Veilid.instance.bestCryptoSystem(); final crypto = await Veilid.instance.bestCryptoSystem();
final dhtctx = (await Veilid.instance.routingContext()) final dhtctx = (await Veilid.instance.routingContext())
@ -94,13 +91,14 @@ class LocalAccountRepositoryImpl extends LocalAccountRepository {
} }
/// Creates a new account associated with master identity /// Creates a new account associated with master identity
@override
Future<LocalAccount> newAccount( Future<LocalAccount> newAccount(
IdentityMaster identityMaster, IdentityMaster identityMaster,
SecretKey identitySecret, SecretKey identitySecret,
EncryptionKeyType encryptionKeyType, EncryptionKeyType encryptionKeyType,
String encryptionKey, String encryptionKey,
proto.Account account) async { proto.Account account) async {
final localAccounts = state.requireValue;
// Encrypt identitySecret with key // Encrypt identitySecret with key
final cs = await Veilid.instance.bestCryptoSystem(); final cs = await Veilid.instance.bestCryptoSystem();
final ekbytes = Uint8List.fromList(utf8.encode(encryptionKey)); final ekbytes = Uint8List.fromList(utf8.encode(encryptionKey));
@ -152,8 +150,15 @@ class LocalAccountRepositoryImpl extends LocalAccountRepository {
}); });
// Add local account object to internal store // Add local account object to internal store
final newLocalAccounts = localAccounts.add(localAccount);
await flush(newLocalAccounts);
state = AsyncValue.data(newLocalAccounts);
// Return local account object // Return local account object
return localAccount; return localAccount;
} }
/// Import an account from another VeilidChat instance
/// Recover an account with the master identity secret
} }

View File

@ -1,27 +1,25 @@
// GENERATED CODE - DO NOT MODIFY BY HAND // GENERATED CODE - DO NOT MODIFY BY HAND
part of 'local_account_repository.dart'; part of 'local_accounts.dart';
// ************************************************************************** // **************************************************************************
// RiverpodGenerator // RiverpodGenerator
// ************************************************************************** // **************************************************************************
String _$localAccountManagerHash() => String _$localAccountsHash() => r'a4ad015e192c5db8e59ba2b5e922109c34572095';
r'0ecdc84f868edb33f6e62bfe116f18568b424267';
/// See also [localAccountManager]. /// See also [LocalAccounts].
@ProviderFor(localAccountManager) @ProviderFor(LocalAccounts)
final localAccountManagerProvider = final localAccountsProvider = AutoDisposeAsyncNotifierProvider<LocalAccounts,
AutoDisposeFutureProvider<LocalAccountRepository>.internal( IList<LocalAccount>>.internal(
localAccountManager, LocalAccounts.new,
name: r'localAccountManagerProvider', name: r'localAccountsProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product') debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
? null ? null
: _$localAccountManagerHash, : _$localAccountsHash,
dependencies: null, dependencies: null,
allTransitiveDependencies: null, allTransitiveDependencies: null,
); );
typedef LocalAccountManagerRef typedef _$LocalAccounts = AutoDisposeAsyncNotifier<IList<LocalAccount>>;
= AutoDisposeFutureProviderRef<LocalAccountRepository>;
// 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 // 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

View File

@ -1 +1,3 @@
export 'local_account_repository.dart'; export 'local_accounts.dart';
// xxx rename this probably

View File

@ -10,7 +10,8 @@ class DHTRecord {
static Future<DHTRecord> create(VeilidRoutingContext dhtctx, static Future<DHTRecord> create(VeilidRoutingContext dhtctx,
{DHTSchema schema = const DHTSchema.dflt(oCnt: 1), {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); DHTRecordDescriptor recordDescriptor = await dhtctx.createDHTRecord(schema);
return DHTRecord( return DHTRecord(
dhtctx: dhtctx, dhtctx: dhtctx,
@ -20,7 +21,8 @@ class DHTRecord {
static Future<DHTRecord> open( static Future<DHTRecord> open(
VeilidRoutingContext dhtctx, TypedKey recordKey, KeyPair? writer, VeilidRoutingContext dhtctx, TypedKey recordKey, KeyPair? writer,
{int defaultSubkey = 0}) async { {int defaultSubkey = 0,
DHTRecordEncryption encrypt = DHTRecordEncryption.private}) async {
DHTRecordDescriptor recordDescriptor = DHTRecordDescriptor recordDescriptor =
await dhtctx.openDHTRecord(recordKey, writer); await dhtctx.openDHTRecord(recordKey, writer);
return DHTRecord( return DHTRecord(

View File

@ -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<Uint8List> encrypt(Uint8List data);
FutureOr<Uint8List> decrypt(Uint8List data);
}
class DHTRecordEncryptionPrivate implements DHTRecordEncryption {
DHTRecordEncryptionPrivate() {
//
}
@override
FutureOr<Uint8List> encrypt(Uint8List data) {
//
}
@override
FutureOr<Uint8List> decrypt(Uint8List data) {
//
}
}
class DHTRecordEncryptionPublic implements DHTRecordEncryption {
DHTRecordEncryptionPublic() {
//
}
@override
FutureOr<Uint8List> encrypt(Uint8List data) {
return data;
}
@override
FutureOr<Uint8List> decrypt(Uint8List data) {
return data;
}
}