crypto work

This commit is contained in:
Christien Rioux 2024-05-27 22:58:37 -04:00
parent e04fd7ee77
commit 8a5af51ec7
8 changed files with 53 additions and 36 deletions

View File

@ -33,8 +33,8 @@ class ActiveAccountInfo {
identitySecret.value,
utf8.encode('VeilidChat Conversation'));
final messagesCrypto =
await VeilidCryptoPrivate.fromSecret(identitySecret.kind, sharedSecret);
final messagesCrypto = await VeilidCryptoPrivate.fromSharedSecret(
identitySecret.kind, sharedSecret);
return messagesCrypto;
}

View File

@ -146,15 +146,13 @@ class SingleContactMessagesCubit extends Cubit<SingleContactMessagesState> {
// Open reconciled chat record key
Future<void> _initReconciledMessagesCubit() async {
final accountRecordKey =
_activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
final tableName = _localConversationRecordKey.toString();
_reconciledMessagesCubit = DHTLogCubit(
open: () async => DHTLog.openOwned(_reconciledChatRecord,
debugName:
'SingleContactMessagesCubit::_initReconciledMessagesCubit::'
'ReconciledMessages',
parent: accountRecordKey),
xxx whats the right encryption for reconciled messages cubit?
final crypto = VeilidCryptoPrivate.fromTypedKey(kind, secretKey);
_reconciledMessagesCubit = TableDBArrayCubit(
open: () async => TableDBArray.make(table: tableName, crypto: crypto),
decodeElement: proto.Message.fromBuffer);
_reconciledSubscription =
_reconciledMessagesCubit!.stream.listen(_updateReconciledMessagesState);
@ -461,7 +459,7 @@ class SingleContactMessagesCubit extends Cubit<SingleContactMessagesState> {
DHTLogCubit<proto.Message>? _sentMessagesCubit;
DHTLogCubit<proto.Message>? _rcvdMessagesCubit;
DHTLogCubit<proto.Message>? _reconciledMessagesCubit;
TableDBArrayCubit<proto.Message>? _reconciledMessagesCubit;
late final PersistentQueue<proto.Message> _unreconciledMessagesQueue;
late final PersistentQueue<proto.Message> _sendingMessagesQueue;

View File

@ -129,9 +129,9 @@ class ContactInvitationListCubit
await contactRequestInbox.eventualWriteBytes(Uint8List(0),
subkey: 1,
writer: contactRequestWriter,
crypto: await VeilidCryptoPrivate.fromTypedKeyPair(
TypedKeyPair.fromKeyPair(
contactRequestInbox.key.kind, contactRequestWriter)));
crypto: await DHTRecordPool.privateCryptoFromTypedSecret(TypedKey(
kind: contactRequestInbox.key.kind,
value: contactRequestWriter.secret)));
// Create ContactInvitation and SignedContactInvitation
final cinv = proto.ContactInvitation()

View File

@ -28,16 +28,16 @@ class ContactRequestInboxCubit
final pool = DHTRecordPool.instance;
final accountRecordKey =
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
final writerKey = contactInvitationRecord.writerKey.toVeilid();
final writerSecret = contactInvitationRecord.writerSecret.toVeilid();
final recordKey =
contactInvitationRecord.contactRequestInbox.recordKey.toVeilid();
final writer = TypedKeyPair(
kind: recordKey.kind, key: writerKey, secret: writerSecret);
final writerTypedSecret =
TypedKey(kind: recordKey.kind, value: writerSecret);
return pool.openRecordRead(recordKey,
debugName: 'ContactRequestInboxCubit::_open::'
'ContactRequestInbox',
crypto: await VeilidCryptoPrivate.fromTypedKeyPair(writer),
crypto:
await DHTRecordPool.privateCryptoFromTypedSecret(writerTypedSecret),
parent: accountRecordKey,
defaultSubkey: 1);
}

View File

@ -28,13 +28,13 @@ class HomeAccountReadyChatState extends State<HomeAccountReadyChat> {
}
Widget buildChatComponent(BuildContext context) {
final activeChatRemoteConversationKey =
final activeChatLocalConversationKey =
context.watch<ActiveChatCubit>().state;
if (activeChatRemoteConversationKey == null) {
if (activeChatLocalConversationKey == null) {
return const EmptyChatWidget();
}
return ChatComponent.builder(
localConversationRecordKey: activeChatRemoteConversationKey);
localConversationRecordKey: activeChatLocalConversationKey);
}
@override

View File

@ -27,6 +27,9 @@ const int watchRenewalDenominator = 5;
// Maximum number of concurrent DHT operations to perform on the network
const int maxDHTConcurrency = 8;
// DHT crypto domain
const String cryptoDomainDHT = 'dht';
typedef DHTRecordPoolLogger = void Function(String message);
/// Record pool that managed DHTRecords and allows for tagged deletion
@ -547,9 +550,9 @@ class DHTRecordPool with TableDBBackedJson<DHTRecordPoolAllocations> {
writer: writer ??
openedRecordInfo.shared.recordDescriptor.ownerKeyPair(),
crypto: crypto ??
await VeilidCryptoPrivate.fromTypedKeyPair(openedRecordInfo
await privateCryptoFromTypedSecret(openedRecordInfo
.shared.recordDescriptor
.ownerTypedKeyPair()!));
.ownerTypedSecret()!));
openedRecordInfo.records.add(rec);
@ -612,8 +615,8 @@ class DHTRecordPool with TableDBBackedJson<DHTRecordPoolAllocations> {
writer: writer,
sharedDHTRecordData: openedRecordInfo.shared,
crypto: crypto ??
await VeilidCryptoPrivate.fromTypedKeyPair(
TypedKeyPair.fromKeyPair(recordKey.kind, writer)));
await privateCryptoFromTypedSecret(
TypedKey(kind: recordKey.kind, value: writer.secret)));
openedRecordInfo.records.add(rec);
@ -663,6 +666,11 @@ class DHTRecordPool with TableDBBackedJson<DHTRecordPoolAllocations> {
}
}
/// Generate default VeilidCrypto for a writer
static Future<VeilidCrypto> privateCryptoFromTypedSecret(
TypedKey typedSecret) async =>
VeilidCryptoPrivate.fromTypedKey(typedSecret, cryptoDomainDHT);
/// Handle the DHT record updates coming from Veilid
void processRemoteValueChange(VeilidUpdateValueChange updateValueChange) {
if (updateValueChange.subkeys.isNotEmpty) {

View File

@ -125,13 +125,14 @@ extension IdentityMasterExtension on IdentityMaster {
}
Future<List<AccountRecordInfo>> readAccountsFromIdentity(
{required SharedSecret identitySecret,
required String accountKey}) async {
{required SecretKey identitySecret, required String accountKey}) async {
// Read the identity key to get the account keys
final pool = DHTRecordPool.instance;
final identityRecordCrypto = await VeilidCryptoPrivate.fromSecret(
identityRecordKey.kind, identitySecret);
final identityRecordCrypto =
await DHTRecordPool.privateCryptoFromTypedSecret(
TypedKey(kind: identityRecordKey.kind, value: identitySecret),
);
late final List<AccountRecordInfo> accountRecordInfo;
await (await pool.openRecordRead(identityRecordKey,
@ -157,7 +158,7 @@ extension IdentityMasterExtension on IdentityMaster {
/// Creates a new Account associated with master identity and store it in the
/// identity key.
Future<AccountRecordInfo> addAccountToIdentity<T extends GeneratedMessage>({
required SharedSecret identitySecret,
required SecretKey identitySecret,
required String accountKey,
required Future<T> Function(TypedKey parent) createAccountCallback,
int maxAccounts = 1,

View File

@ -1,4 +1,5 @@
import 'dart:async';
import 'dart:convert';
import 'dart:typed_data';
import '../../../veilid_support.dart';
@ -16,15 +17,24 @@ class VeilidCryptoPrivate implements VeilidCrypto {
final VeilidCryptoSystem _cryptoSystem;
final SharedSecret _secretKey;
static Future<VeilidCryptoPrivate> fromTypedKeyPair(
TypedKeyPair typedKeyPair) async {
final cryptoSystem =
await Veilid.instance.getCryptoSystem(typedKeyPair.kind);
final secretKey = typedKeyPair.secret;
static Future<VeilidCryptoPrivate> fromTypedKey(
TypedKey typedKey, String domain) async {
final cryptoSystem = await Veilid.instance.getCryptoSystem(typedKey.kind);
final keyMaterial = Uint8List(0)
..addAll(typedKey.value.decode())
..addAll(utf8.encode(domain));
final secretKey = await cryptoSystem.generateHash(keyMaterial);
return VeilidCryptoPrivate._(cryptoSystem, secretKey);
}
static Future<VeilidCryptoPrivate> fromSecret(
static Future<VeilidCryptoPrivate> fromTypedKeyPair(
TypedKeyPair typedKeyPair, String domain) async {
final typedSecret =
TypedKey(kind: typedKeyPair.kind, value: typedKeyPair.secret);
return fromTypedKey(typedSecret, domain);
}
static Future<VeilidCryptoPrivate> fromSharedSecret(
CryptoKind kind, SharedSecret secretKey) async {
final cryptoSystem = await Veilid.instance.getCryptoSystem(kind);
return VeilidCryptoPrivate._(cryptoSystem, secretKey);