veilidchat/lib/providers/contact.dart

218 lines
8.2 KiB
Dart
Raw Normal View History

2023-08-02 21:09:28 -04:00
import 'dart:typed_data';
2023-08-04 01:00:38 -04:00
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
2023-08-03 00:49:48 -04:00
import 'package:fixnum/fixnum.dart';
2023-08-04 01:00:38 -04:00
import 'package:riverpod_annotation/riverpod_annotation.dart';
2023-08-02 21:09:28 -04:00
import '../entities/local_account.dart';
import '../entities/proto.dart' as proto;
2023-08-04 01:00:38 -04:00
import '../entities/proto.dart'
show
Contact,
ContactInvitation,
ContactInvitationRecord,
ContactRequest,
ContactRequestPrivate,
SignedContactInvitation;
2023-08-02 21:09:28 -04:00
import '../tools/tools.dart';
import '../veilid_support/veilid_support.dart';
import 'account.dart';
2023-08-04 01:00:38 -04:00
part 'contact.g.dart';
2023-08-05 01:00:46 -04:00
Future<void> deleteContactInvitation(
{required ActiveAccountInfo activeAccountInfo,
required ContactInvitationRecord contactInvitationRecord}) async {
final pool = await DHTRecordPool.instance();
final accountRecordKey =
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
// Remove ContactInvitationRecord from account's list
await (await DHTShortArray.openOwned(
proto.OwnedDHTRecordPointerProto.fromProto(
activeAccountInfo.account.contactInvitationRecords),
parent: accountRecordKey))
.scope((cirList) async {
for (var i = 0; i < cirList.length; i++) {
final item = await cirList.getItemProtobuf(
proto.ContactInvitationRecord.fromBuffer, i);
if (item == null) {
throw StateError('Failed to get contact invitation record');
}
if (item.contactRequestInbox.recordKey ==
contactInvitationRecord.contactRequestInbox.recordKey) {
await cirList.tryRemoveItem(i);
break;
}
}
await (await pool.openOwned(
proto.OwnedDHTRecordPointerProto.fromProto(
contactInvitationRecord.contactRequestInbox),
parent: accountRecordKey))
.delete();
await (await pool.openOwned(
proto.OwnedDHTRecordPointerProto.fromProto(
contactInvitationRecord.localConversation),
parent: accountRecordKey))
.delete();
});
}
2023-08-02 21:09:28 -04:00
Future<Uint8List> createContactInvitation(
2023-08-03 00:49:48 -04:00
{required ActiveAccountInfo activeAccountInfo,
required EncryptionKeyType encryptionKeyType,
required String encryptionKey,
2023-08-04 01:00:38 -04:00
required String message,
2023-08-03 00:49:48 -04:00
required Timestamp? expiration}) async {
2023-08-02 21:09:28 -04:00
final pool = await DHTRecordPool.instance();
final accountRecordKey =
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
final identityKey =
activeAccountInfo.localAccount.identityMaster.identityPublicKey;
final identitySecret = activeAccountInfo.userLogin.identitySecret.value;
// Generate writer keypair to share with new contact
final cs = await pool.veilid.bestCryptoSystem();
final writer = await cs.generateKeyPair();
// Encrypt the writer secret with the encryption key
final encryptedSecret = await encryptSecretToBytes(
secret: writer.secret,
cryptoKind: cs.kind(),
encryptionKey: encryptionKey,
encryptionKeyType: encryptionKeyType);
// Create local chat DHT record with the account record key as its parent
// Do not set the encryption of this key yet as it will not yet be written
// to and it will be eventually encrypted with the DH of the contact's
// identity key
late final Uint8List signedContactInvitationBytes;
await (await pool.create(parent: accountRecordKey))
2023-08-05 01:00:46 -04:00
.deleteScope((localConversation) async {
2023-08-02 21:09:28 -04:00
// Make ContactRequestPrivate and encrypt with the writer secret
2023-08-04 01:00:38 -04:00
final crpriv = ContactRequestPrivate()
2023-08-02 21:09:28 -04:00
..writerKey = writer.key.toProto()
..profile = activeAccountInfo.account.profile
2023-08-05 01:15:08 -04:00
..identityMasterRecordKey =
2023-08-02 21:09:28 -04:00
activeAccountInfo.userLogin.accountMasterRecordKey.toProto()
2023-08-05 01:00:46 -04:00
..chatRecordKey = localConversation.key.toProto()
2023-08-03 00:49:48 -04:00
..expiration = expiration?.toInt64() ?? Int64.ZERO;
2023-08-02 21:09:28 -04:00
final crprivbytes = crpriv.writeToBuffer();
final encryptedContactRequestPrivate =
await cs.encryptNoAuthWithNonce(crprivbytes, writer.secret);
// Create ContactRequest and embed contactrequestprivate
2023-08-04 01:00:38 -04:00
final creq = ContactRequest()
2023-08-02 21:09:28 -04:00
..encryptionKeyType = encryptionKeyType.toProto()
..private = encryptedContactRequestPrivate;
// Create DHT unicast inbox for ContactRequest
await (await pool.create(
parent: accountRecordKey,
schema: DHTSchema.smpl(
oCnt: 1, members: [DHTSchemaMember(mCnt: 1, mKey: writer.key)]),
crypto: const DHTRecordCryptoPublic()))
2023-08-05 01:00:46 -04:00
.deleteScope((contactRequestInbox) async {
2023-08-02 21:09:28 -04:00
// Store ContactRequest in owner subkey
2023-08-05 01:00:46 -04:00
await contactRequestInbox.eventualWriteProtobuf(creq);
2023-08-02 21:09:28 -04:00
// Create ContactInvitation and SignedContactInvitation
2023-08-04 01:00:38 -04:00
final cinv = ContactInvitation()
2023-08-05 01:00:46 -04:00
..contactRequestInboxKey = contactRequestInbox.key.toProto()
2023-08-02 21:09:28 -04:00
..writerSecret = encryptedSecret;
final cinvbytes = cinv.writeToBuffer();
2023-08-04 01:00:38 -04:00
final scinv = SignedContactInvitation()
2023-08-02 21:09:28 -04:00
..contactInvitation = cinvbytes
..identitySignature =
(await cs.sign(identityKey, identitySecret, cinvbytes)).toProto();
signedContactInvitationBytes = scinv.writeToBuffer();
// Create ContactInvitationRecord
2023-08-04 01:00:38 -04:00
final cinvrec = ContactInvitationRecord()
2023-08-05 01:00:46 -04:00
..contactRequestInbox =
contactRequestInbox.ownedDHTRecordPointer.toProto()
2023-08-02 21:09:28 -04:00
..writerKey = writer.key.toProto()
..writerSecret = writer.secret.toProto()
2023-08-05 01:00:46 -04:00
..localConversation = localConversation.ownedDHTRecordPointer.toProto()
2023-08-03 00:49:48 -04:00
..expiration = expiration?.toInt64() ?? Int64.ZERO
2023-08-04 01:00:38 -04:00
..invitation = signedContactInvitationBytes
..message = message;
2023-08-02 21:09:28 -04:00
2023-08-05 01:00:46 -04:00
// Add ContactInvitationRecord to account's list
2023-08-03 00:49:48 -04:00
// if this fails, don't keep retrying, user can try again later
2023-08-02 21:09:28 -04:00
await (await DHTShortArray.openOwned(
proto.OwnedDHTRecordPointerProto.fromProto(
activeAccountInfo.account.contactInvitationRecords),
parent: accountRecordKey))
.scope((cirList) async {
if (await cirList.tryAddItem(cinvrec.writeToBuffer()) == false) {
throw StateError('Failed to add contact invitation record');
}
});
});
});
return signedContactInvitationBytes;
}
2023-08-04 01:00:38 -04:00
/// Get the active account contact invitation list
@riverpod
Future<IList<ContactInvitationRecord>?> fetchContactInvitationRecords(
FetchContactInvitationRecordsRef ref) async {
// See if we've logged into this account or if it is locked
final activeAccountInfo = await ref.watch(fetchActiveAccountProvider.future);
if (activeAccountInfo == null) {
return null;
}
final accountRecordKey =
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
// Decode the contact invitation list from the DHT
IList<ContactInvitationRecord> out = const IListConst([]);
await (await DHTShortArray.openOwned(
proto.OwnedDHTRecordPointerProto.fromProto(
activeAccountInfo.account.contactInvitationRecords),
parent: accountRecordKey))
.scope((cirList) async {
for (var i = 0; i < cirList.length; i++) {
final cir = await cirList.getItem(i);
if (cir == null) {
throw StateError('Failed to get contact invitation record');
}
out = out.add(ContactInvitationRecord.fromBuffer(cir));
}
});
return out;
}
/// Get the active account contact list
@riverpod
Future<IList<Contact>?> fetchContactList(FetchContactListRef ref) async {
// See if we've logged into this account or if it is locked
final activeAccountInfo = await ref.watch(fetchActiveAccountProvider.future);
if (activeAccountInfo == null) {
return null;
}
final accountRecordKey =
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
// Decode the contact list from the DHT
IList<Contact> out = const IListConst([]);
await (await DHTShortArray.openOwned(
proto.OwnedDHTRecordPointerProto.fromProto(
activeAccountInfo.account.contactList),
parent: accountRecordKey))
.scope((cList) async {
for (var i = 0; i < cList.length; i++) {
final cir = await cList.getItem(i);
if (cir == null) {
throw StateError('Failed to get contact');
}
out = out.add(Contact.fromBuffer(cir));
}
});
return out;
}