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: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<LocalAccount> _localAccounts;
@riverpod
abstract class LocalAccounts extends _$LocalAccounts {
static const localAccountManagerTable = "local_account_manager";
static const localAccountsKey = "local_accounts";
LocalAccountRepositoryImpl._({required IList<LocalAccount> localAccounts})
: _localAccounts = localAccounts;
/// Gets or creates a local account manager
static Future<LocalAccountRepository> open() async {
/// Get all local account information
@override
FutureOr<IList<LocalAccount>> 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<LocalAccount>();
});
return LocalAccountRepositoryImpl._(localAccounts: localAccounts);
return localAccounts;
}
/// Store things back to storage
Future<void> flush() async {
Future<void> flush(IList<LocalAccount> 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<IdentityMasterWithSecrets> 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<LocalAccount> 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
}

View File

@ -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<LocalAccountRepository>.internal(
localAccountManager,
name: r'localAccountManagerProvider',
/// See also [LocalAccounts].
@ProviderFor(LocalAccounts)
final localAccountsProvider = AutoDisposeAsyncNotifierProvider<LocalAccounts,
IList<LocalAccount>>.internal(
LocalAccounts.new,
name: r'localAccountsProvider',
debugGetCreateSourceHash: const bool.fromEnvironment('dart.vm.product')
? null
: _$localAccountManagerHash,
: _$localAccountsHash,
dependencies: null,
allTransitiveDependencies: null,
);
typedef LocalAccountManagerRef
= AutoDisposeFutureProviderRef<LocalAccountRepository>;
typedef _$LocalAccounts = AutoDisposeAsyncNotifier<IList<LocalAccount>>;
// 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,
{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<DHTRecord> 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(

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;
}
}