2023-08-05 21:01:27 -04:00
|
|
|
import 'dart:convert';
|
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
|
|
|
|
2023-08-05 12:38:03 -04:00
|
|
|
import '../entities/identity.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,
|
2023-08-05 13:50:31 -04:00
|
|
|
ContactResponse,
|
2023-08-05 19:34:00 -04:00
|
|
|
SignedContactInvitation,
|
2023-08-05 13:50:31 -04:00
|
|
|
SignedContactResponse;
|
|
|
|
import '../log/loggy.dart';
|
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 19:34:00 -04:00
|
|
|
Future<bool?> checkAcceptRejectContact(
|
2023-08-05 01:00:46 -04:00
|
|
|
{required ActiveAccountInfo activeAccountInfo,
|
|
|
|
required ContactInvitationRecord contactInvitationRecord}) async {
|
2023-08-05 19:34:00 -04:00
|
|
|
// Open the contact request inbox
|
|
|
|
try {
|
|
|
|
final pool = await DHTRecordPool.instance();
|
|
|
|
final accountRecordKey =
|
|
|
|
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
|
|
|
|
final writerKey =
|
|
|
|
proto.CryptoKeyProto.fromProto(contactInvitationRecord.writerKey);
|
|
|
|
final writerSecret =
|
|
|
|
proto.CryptoKeyProto.fromProto(contactInvitationRecord.writerSecret);
|
|
|
|
final writer = TypedKeyPair(
|
|
|
|
kind: contactInvitationRecord.contactRequestInbox.recordKey.kind,
|
|
|
|
key: writerKey,
|
|
|
|
secret: writerSecret);
|
|
|
|
final acceptReject = await (await pool.openRead(
|
|
|
|
proto.TypedKeyProto.fromProto(
|
|
|
|
contactInvitationRecord.contactRequestInbox.recordKey),
|
|
|
|
crypto: await DHTRecordCryptoPrivate.fromTypedKeyPair(writer),
|
|
|
|
parent: accountRecordKey,
|
|
|
|
defaultSubkey: 1))
|
|
|
|
.scope((contactRequestInbox) async {
|
|
|
|
//
|
|
|
|
final signedContactResponse = await contactRequestInbox
|
|
|
|
.getProtobuf(SignedContactResponse.fromBuffer, forceRefresh: true);
|
|
|
|
if (signedContactResponse == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
final contactResponseBytes =
|
|
|
|
Uint8List.fromList(signedContactResponse.contactResponse);
|
|
|
|
final contactResponse = ContactResponse.fromBuffer(contactResponseBytes);
|
|
|
|
final contactIdentityMasterRecordKey = proto.TypedKeyProto.fromProto(
|
|
|
|
contactResponse.identityMasterRecordKey);
|
|
|
|
final cs = await pool.veilid.getCryptoSystem(
|
|
|
|
contactInvitationRecord.contactRequestInbox.recordKey.kind);
|
|
|
|
|
|
|
|
// Fetch the remote contact's account master
|
|
|
|
final contactIdentityMaster = await openIdentityMaster(
|
|
|
|
identityMasterRecordKey: contactIdentityMasterRecordKey);
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
final signature = proto.SignatureProto.fromProto(
|
|
|
|
signedContactResponse.identitySignature);
|
|
|
|
try {
|
|
|
|
await cs.verify(contactIdentityMaster.identityPublicKey,
|
|
|
|
contactResponseBytes, signature);
|
|
|
|
} on Exception catch (e) {
|
|
|
|
log.error('Bad identity used, failed to verify: $e');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return contactResponse.accept;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (acceptReject == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add contact if accepted
|
|
|
|
if (acceptReject) {
|
|
|
|
//
|
|
|
|
await deleteContactInvitation(
|
|
|
|
accepted: true,
|
|
|
|
activeAccountInfo: activeAccountInfo,
|
|
|
|
contactInvitationRecord: contactInvitationRecord);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
await deleteContactInvitation(
|
|
|
|
accepted: false,
|
|
|
|
activeAccountInfo: activeAccountInfo,
|
|
|
|
contactInvitationRecord: contactInvitationRecord);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} on Exception catch (e) {
|
|
|
|
log.error('Exception in checkAcceptRejectContact: $e');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> deleteContactInvitation(
|
|
|
|
{required bool accepted,
|
|
|
|
required ActiveAccountInfo activeAccountInfo,
|
|
|
|
required ContactInvitationRecord contactInvitationRecord}) async {
|
2023-08-05 01:00:46 -04:00
|
|
|
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))
|
2023-08-05 19:34:00 -04:00
|
|
|
.scope((contactRequestInbox) async {
|
|
|
|
// Wipe out old invitation so it shows up as invalid
|
|
|
|
await contactRequestInbox.tryWriteBytes(Uint8List(0));
|
|
|
|
await contactRequestInbox.delete();
|
|
|
|
});
|
|
|
|
if (!accepted) {
|
|
|
|
await (await pool.openOwned(
|
|
|
|
proto.OwnedDHTRecordPointerProto.fromProto(
|
|
|
|
contactInvitationRecord.localConversation),
|
|
|
|
parent: accountRecordKey))
|
|
|
|
.delete();
|
|
|
|
}
|
2023-08-05 01:00:46 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2023-08-05 12:38:03 -04:00
|
|
|
class ValidContactInvitation {
|
|
|
|
ValidContactInvitation(
|
|
|
|
{required this.signedContactInvitation,
|
|
|
|
required this.contactInvitation,
|
|
|
|
required this.contactRequestInboxKey,
|
|
|
|
required this.contactRequest,
|
|
|
|
required this.contactRequestPrivate,
|
2023-08-05 13:50:31 -04:00
|
|
|
required this.contactIdentityMaster,
|
|
|
|
required this.writer});
|
2023-08-05 12:38:03 -04:00
|
|
|
|
|
|
|
SignedContactInvitation signedContactInvitation;
|
|
|
|
ContactInvitation contactInvitation;
|
|
|
|
TypedKey contactRequestInboxKey;
|
|
|
|
ContactRequest contactRequest;
|
|
|
|
ContactRequestPrivate contactRequestPrivate;
|
|
|
|
IdentityMaster contactIdentityMaster;
|
2023-08-05 13:50:31 -04:00
|
|
|
KeyPair writer;
|
2023-08-05 12:38:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef GetEncryptionKeyCallback = Future<SecretKey> Function(
|
|
|
|
EncryptionKeyType encryptionKeyType, Uint8List encryptedSecret);
|
|
|
|
|
|
|
|
Future<ValidContactInvitation> validateContactInvitation(Uint8List inviteData,
|
|
|
|
GetEncryptionKeyCallback getEncryptionKeyCallback) async {
|
|
|
|
final signedContactInvitation =
|
|
|
|
proto.SignedContactInvitation.fromBuffer(inviteData);
|
|
|
|
|
|
|
|
final contactInvitationBytes =
|
|
|
|
Uint8List.fromList(signedContactInvitation.contactInvitation);
|
|
|
|
final contactInvitation =
|
|
|
|
proto.ContactInvitation.fromBuffer(contactInvitationBytes);
|
|
|
|
|
|
|
|
final contactRequestInboxKey =
|
|
|
|
proto.TypedKeyProto.fromProto(contactInvitation.contactRequestInboxKey);
|
|
|
|
|
|
|
|
late final ValidContactInvitation out;
|
|
|
|
|
|
|
|
final pool = await DHTRecordPool.instance();
|
|
|
|
await (await pool.openRead(contactRequestInboxKey))
|
|
|
|
.deleteScope((contactRequestInbox) async {
|
|
|
|
//
|
|
|
|
final contactRequest =
|
|
|
|
await contactRequestInbox.getProtobuf(proto.ContactRequest.fromBuffer);
|
|
|
|
// Decrypt contact request private
|
|
|
|
final encryptionKeyType =
|
|
|
|
EncryptionKeyType.fromProto(contactRequest!.encryptionKeyType);
|
|
|
|
final writerSecret = await getEncryptionKeyCallback(
|
|
|
|
encryptionKeyType, Uint8List.fromList(contactInvitation.writerSecret));
|
|
|
|
|
|
|
|
final cs = await pool.veilid.getCryptoSystem(contactRequestInboxKey.kind);
|
|
|
|
final contactRequestPrivateBytes = await cs.decryptNoAuthWithNonce(
|
|
|
|
Uint8List.fromList(contactRequest.private), writerSecret);
|
|
|
|
final contactRequestPrivate =
|
|
|
|
proto.ContactRequestPrivate.fromBuffer(contactRequestPrivateBytes);
|
|
|
|
final contactIdentityMasterRecordKey = proto.TypedKeyProto.fromProto(
|
|
|
|
contactRequestPrivate.identityMasterRecordKey);
|
|
|
|
|
|
|
|
// Fetch the account master
|
|
|
|
final contactIdentityMaster = await openIdentityMaster(
|
|
|
|
identityMasterRecordKey: contactIdentityMasterRecordKey);
|
|
|
|
|
|
|
|
// Verify
|
|
|
|
final signature = proto.SignatureProto.fromProto(
|
|
|
|
signedContactInvitation.identitySignature);
|
|
|
|
await cs.verify(contactIdentityMaster.identityPublicKey,
|
|
|
|
contactInvitationBytes, signature);
|
|
|
|
|
2023-08-05 13:50:31 -04:00
|
|
|
final writer = KeyPair(
|
|
|
|
key: proto.CryptoKeyProto.fromProto(contactRequestPrivate.writerKey),
|
|
|
|
secret: writerSecret);
|
|
|
|
|
2023-08-05 12:38:03 -04:00
|
|
|
out = ValidContactInvitation(
|
|
|
|
signedContactInvitation: signedContactInvitation,
|
|
|
|
contactInvitation: contactInvitation,
|
|
|
|
contactRequestInboxKey: contactRequestInboxKey,
|
|
|
|
contactRequest: contactRequest,
|
|
|
|
contactRequestPrivate: contactRequestPrivate,
|
2023-08-05 13:50:31 -04:00
|
|
|
contactIdentityMaster: contactIdentityMaster,
|
|
|
|
writer: writer);
|
2023-08-05 12:38:03 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2023-08-05 21:01:27 -04:00
|
|
|
class AcceptedContact {
|
|
|
|
AcceptedContact({
|
|
|
|
required this.profile,
|
|
|
|
required this.remoteIdentity,
|
|
|
|
required this.remoteConversation,
|
|
|
|
required this.localConversation,
|
|
|
|
});
|
|
|
|
|
|
|
|
proto.Profile profile;
|
|
|
|
IdentityMaster remoteIdentity;
|
|
|
|
TypedKey remoteConversation;
|
|
|
|
OwnedDHTRecordPointer localConversation;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<AcceptedContact?> acceptContactInvitation(
|
|
|
|
ActiveAccountInfo activeAccountInfo,
|
2023-08-05 12:38:03 -04:00
|
|
|
ValidContactInvitation validContactInvitation) async {
|
2023-08-05 13:50:31 -04:00
|
|
|
final pool = await DHTRecordPool.instance();
|
2023-08-05 21:01:27 -04:00
|
|
|
final accountRecordKey =
|
|
|
|
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
|
|
|
|
|
|
|
|
return (await pool.openWrite(validContactInvitation.contactRequestInboxKey,
|
2023-08-05 13:50:31 -04:00
|
|
|
validContactInvitation.writer))
|
|
|
|
.deleteScope((contactRequestInbox) async {
|
|
|
|
final cs = await pool.veilid
|
|
|
|
.getCryptoSystem(validContactInvitation.contactRequestInboxKey.kind);
|
|
|
|
|
2023-08-05 21:01:27 -04:00
|
|
|
// Create local conversation key for this
|
|
|
|
// contact and send via contact response
|
|
|
|
return (await pool.create(parent: accountRecordKey))
|
|
|
|
.deleteScope((localConversation) async {
|
|
|
|
final contactResponse = ContactResponse()
|
|
|
|
..accept = true
|
|
|
|
..remoteConversationKey = localConversation.key.toProto()
|
|
|
|
..identityMasterRecordKey = activeAccountInfo
|
|
|
|
.localAccount.identityMaster.masterRecordKey
|
|
|
|
.toProto();
|
|
|
|
final contactResponseBytes = contactResponse.writeToBuffer();
|
|
|
|
|
|
|
|
final identitySignature = await cs.sign(
|
|
|
|
activeAccountInfo.localAccount.identityMaster.identityPublicKey,
|
|
|
|
activeAccountInfo.userLogin.identitySecret.value,
|
|
|
|
contactResponseBytes);
|
|
|
|
|
|
|
|
final signedContactResponse = SignedContactResponse()
|
|
|
|
..contactResponse = contactResponseBytes
|
|
|
|
..identitySignature = identitySignature.toProto();
|
|
|
|
|
|
|
|
// Write the acceptance to the inbox
|
|
|
|
if (await contactRequestInbox.tryWriteProtobuf(
|
|
|
|
SignedContactResponse.fromBuffer, signedContactResponse,
|
|
|
|
subkey: 1) !=
|
|
|
|
null) {
|
|
|
|
log.error('failed to accept contact invitation');
|
|
|
|
await localConversation.delete();
|
|
|
|
await contactRequestInbox.delete();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return AcceptedContact(
|
|
|
|
profile: validContactInvitation.contactRequestPrivate.profile,
|
|
|
|
remoteIdentity: validContactInvitation.contactIdentityMaster,
|
|
|
|
remoteConversation: proto.TypedKeyProto.fromProto(
|
|
|
|
validContactInvitation.contactRequestPrivate.chatRecordKey),
|
|
|
|
localConversation: localConversation.ownedDHTRecordPointer,
|
|
|
|
);
|
|
|
|
});
|
2023-08-05 13:50:31 -04:00
|
|
|
});
|
2023-08-05 12:38:03 -04:00
|
|
|
}
|
|
|
|
|
2023-08-05 21:01:27 -04:00
|
|
|
Future<bool> rejectContactInvitation(ActiveAccountInfo activeAccountInfo,
|
2023-08-05 12:38:03 -04:00
|
|
|
ValidContactInvitation validContactInvitation) async {
|
2023-08-05 13:50:31 -04:00
|
|
|
final pool = await DHTRecordPool.instance();
|
2023-08-05 21:01:27 -04:00
|
|
|
return (await pool.openWrite(validContactInvitation.contactRequestInboxKey,
|
2023-08-05 13:50:31 -04:00
|
|
|
validContactInvitation.writer))
|
|
|
|
.deleteScope((contactRequestInbox) async {
|
|
|
|
final cs = await pool.veilid
|
|
|
|
.getCryptoSystem(validContactInvitation.contactRequestInboxKey.kind);
|
|
|
|
|
|
|
|
final contactResponse = ContactResponse()
|
|
|
|
..accept = false
|
2023-08-05 19:34:00 -04:00
|
|
|
..identityMasterRecordKey = activeAccountInfo
|
2023-08-05 13:50:31 -04:00
|
|
|
.localAccount.identityMaster.masterRecordKey
|
|
|
|
.toProto();
|
|
|
|
final contactResponseBytes = contactResponse.writeToBuffer();
|
|
|
|
|
|
|
|
final identitySignature = await cs.sign(
|
|
|
|
activeAccountInfo.localAccount.identityMaster.identityPublicKey,
|
|
|
|
activeAccountInfo.userLogin.identitySecret.value,
|
|
|
|
contactResponseBytes);
|
|
|
|
|
|
|
|
final signedContactResponse = SignedContactResponse()
|
|
|
|
..contactResponse = contactResponseBytes
|
|
|
|
..identitySignature = identitySignature.toProto();
|
|
|
|
|
|
|
|
// Write the rejection to the invox
|
|
|
|
if (await contactRequestInbox.tryWriteProtobuf(
|
|
|
|
SignedContactResponse.fromBuffer, signedContactResponse,
|
|
|
|
subkey: 1) !=
|
|
|
|
null) {
|
|
|
|
log.error('failed to reject contact invitation');
|
2023-08-05 21:01:27 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> createContact({
|
|
|
|
required ActiveAccountInfo activeAccountInfo,
|
|
|
|
required proto.Profile profile,
|
|
|
|
required IdentityMaster remoteIdentity,
|
|
|
|
required TypedKey remoteConversation,
|
|
|
|
required OwnedDHTRecordPointer localConversation,
|
|
|
|
}) async {
|
|
|
|
final accountRecordKey =
|
|
|
|
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
|
|
|
|
|
|
|
|
// Create Contact
|
|
|
|
final contact = Contact()
|
|
|
|
..editedProfile = profile
|
|
|
|
..remoteProfile = profile
|
|
|
|
..remoteIdentity = jsonEncode(remoteIdentity.toJson())
|
|
|
|
..remoteConversationKey = remoteConversation.toProto()
|
|
|
|
..localConversation = localConversation.toProto()
|
|
|
|
..showAvailability = false;
|
|
|
|
|
|
|
|
// Add Contact to account's list
|
|
|
|
// if this fails, don't keep retrying, user can try again later
|
|
|
|
await (await DHTShortArray.openOwned(
|
|
|
|
proto.OwnedDHTRecordPointerProto.fromProto(
|
|
|
|
activeAccountInfo.account.contactList),
|
|
|
|
parent: accountRecordKey))
|
|
|
|
.scope((contactList) async {
|
|
|
|
if (await contactList.tryAddItem(contact.writeToBuffer()) == false) {
|
|
|
|
throw StateError('Failed to add contact');
|
2023-08-05 13:50:31 -04:00
|
|
|
}
|
|
|
|
});
|
2023-08-05 12:38:03 -04:00
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|