veilidchat/lib/providers/local_accounts.dart

156 lines
5.3 KiB
Dart
Raw Normal View History

2023-07-20 19:48:55 +00:00
import 'dart:async';
2023-07-19 04:09:57 +00:00
import 'dart:convert';
import 'dart:typed_data';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:veilid/veilid.dart';
2023-07-20 19:48:55 +00:00
import 'package:riverpod_annotation/riverpod_annotation.dart';
2023-07-19 04:09:57 +00:00
import '../tools/tools.dart';
2023-07-22 01:25:27 +00:00
import '../veilid_support/veilid_support.dart';
2023-07-19 04:09:57 +00:00
import '../entities/entities.dart';
import '../entities/proto.dart' as proto;
2023-07-23 03:29:10 +00:00
import 'logins.dart';
2023-07-19 04:09:57 +00:00
2023-07-20 19:48:55 +00:00
part 'local_accounts.g.dart';
2023-07-19 04:09:57 +00:00
// Local account manager
2023-07-20 19:48:55 +00:00
@riverpod
2023-07-22 01:25:27 +00:00
class LocalAccounts extends _$LocalAccounts
with AsyncTableDBBacked<IList<LocalAccount>> {
//////////////////////////////////////////////////////////////
/// AsyncTableDBBacked
@override
String tableName() => "local_account_manager";
@override
String tableKeyName() => "local_accounts";
@override
2023-07-26 14:06:54 +00:00
IList<LocalAccount> valueFromJson(Object? obj) => obj != null
2023-07-22 01:25:27 +00:00
? IList<LocalAccount>.fromJson(
obj, genericFromJson(LocalAccount.fromJson))
: IList<LocalAccount>();
2023-07-26 14:06:54 +00:00
@override
Object? valueToJson(IList<LocalAccount> val) =>
val.toJson((la) => la.toJson());
2023-07-19 04:09:57 +00:00
2023-07-20 19:48:55 +00:00
/// Get all local account information
@override
FutureOr<IList<LocalAccount>> build() async {
2023-07-22 01:25:27 +00:00
return await load();
2023-07-19 04:09:57 +00:00
}
2023-07-22 01:25:27 +00:00
//////////////////////////////////////////////////////////////
/// Mutators and Selectors
2023-07-19 04:09:57 +00:00
2023-07-23 03:29:10 +00:00
/// Reorder accounts
Future<void> reorderAccount(int oldIndex, int newIndex) async {
final localAccounts = state.requireValue;
var removedItem = Output<LocalAccount>();
final updated = localAccounts
.removeAt(oldIndex, removedItem)
.insert(newIndex, removedItem.value!);
await store(updated);
state = AsyncValue.data(updated);
2023-07-19 04:09:57 +00:00
}
/// Creates a new account associated with master identity
Future<LocalAccount> newAccount(
2023-07-25 05:04:34 +00:00
{required IdentityMaster identityMaster,
required SecretKey identitySecret,
EncryptionKeyType encryptionKeyType = EncryptionKeyType.none,
String encryptionKey = "",
required proto.Account account}) async {
2023-07-23 03:29:10 +00:00
final veilid = await eventualVeilid.future;
2023-07-20 19:48:55 +00:00
final localAccounts = state.requireValue;
2023-07-19 04:09:57 +00:00
// Encrypt identitySecret with key
2023-07-22 01:25:27 +00:00
late final Uint8List identitySecretBytes;
late final Uint8List identitySecretSaltBytes;
switch (encryptionKeyType) {
case EncryptionKeyType.none:
identitySecretBytes = identitySecret.decode();
identitySecretSaltBytes = Uint8List(0);
case EncryptionKeyType.pin:
case EncryptionKeyType.password:
2023-07-23 03:29:10 +00:00
final cs =
await veilid.getCryptoSystem(identityMaster.identityRecordKey.kind);
2023-07-22 01:25:27 +00:00
final ekbytes = Uint8List.fromList(utf8.encode(encryptionKey));
final nonce = await cs.randomNonce();
identitySecretSaltBytes = nonce.decode();
SharedSecret sharedSecret =
await cs.deriveSharedSecret(ekbytes, identitySecretSaltBytes);
identitySecretBytes =
await cs.cryptNoAuth(identitySecret.decode(), nonce, sharedSecret);
}
2023-07-19 04:09:57 +00:00
// Create local account object
final localAccount = LocalAccount(
identityMaster: identityMaster,
identitySecretKeyBytes: identitySecretBytes,
2023-07-22 01:25:27 +00:00
identitySecretSaltBytes: identitySecretSaltBytes,
2023-07-19 04:09:57 +00:00
encryptionKeyType: encryptionKeyType,
biometricsEnabled: false,
hiddenAccount: false,
);
/////// Add account with profile to DHT
// Create private routing context
2023-07-23 03:29:10 +00:00
final dhtctx = (await veilid.routingContext())
2023-07-19 04:09:57 +00:00
.withPrivacy()
.withSequencing(Sequencing.ensureOrdered);
// Open identity key for writing
2023-07-22 01:25:27 +00:00
(await DHTRecord.openWrite(dhtctx, identityMaster.identityRecordKey,
2023-07-19 04:09:57 +00:00
identityMaster.identityWriter(identitySecret)))
.scope((identityRec) async {
// Create new account to insert into identity
(await DHTRecord.create(dhtctx)).deleteScope((accountRec) async {
// Write account key
await accountRec.eventualWriteProtobuf(account);
// Update identity key to include account
final newAccountRecordInfo = AccountRecordInfo(
2023-07-22 01:25:27 +00:00
key: accountRec.key(), owner: accountRec.ownerKeyPair()!);
2023-07-19 04:09:57 +00:00
await identityRec.eventualUpdateJson(Identity.fromJson,
(oldIdentity) async {
final accountRecords = IMapOfSets.from(oldIdentity.accountRecords)
2023-07-25 05:04:34 +00:00
.add("com.veilid.veilidchat", newAccountRecordInfo)
2023-07-19 04:09:57 +00:00
.asIMap();
return oldIdentity.copyWith(accountRecords: accountRecords);
});
});
});
// Add local account object to internal store
2023-07-20 19:48:55 +00:00
final newLocalAccounts = localAccounts.add(localAccount);
2023-07-22 01:25:27 +00:00
await store(newLocalAccounts);
2023-07-20 19:48:55 +00:00
state = AsyncValue.data(newLocalAccounts);
2023-07-19 04:09:57 +00:00
// Return local account object
return localAccount;
}
2023-07-20 19:48:55 +00:00
2023-07-23 03:29:10 +00:00
/// Remove an account and wipe the messages for this account from this device
Future<bool> deleteAccount(TypedKey accountMasterRecordKey) async {
final logins = ref.read(loginsProvider.notifier);
await logins.logout(accountMasterRecordKey);
final localAccounts = state.requireValue;
final updated = localAccounts.removeWhere(
(la) => la.identityMaster.masterRecordKey == accountMasterRecordKey);
await store(updated);
state = AsyncValue.data(updated);
// xxx todo: wipe messages
return true;
}
2023-07-20 19:48:55 +00:00
/// Import an account from another VeilidChat instance
/// Recover an account with the master identity secret
2023-07-19 04:09:57 +00:00
}