feat: Implement getting fingerprints

This commit is contained in:
PapaTutuWawa 2023-06-15 16:18:42 +02:00
parent af33ed51d1
commit f1ec8d1793
2 changed files with 47 additions and 3 deletions

View File

@ -1,4 +1,3 @@
import 'dart:convert';
import 'package:cryptography/cryptography.dart';
import 'package:hex/hex.dart';
import 'package:meta/meta.dart';
@ -356,6 +355,7 @@ class OmemoDoubleRatchet {
..message = headerBytes;
}
/// Returns a copy of the ratchet.
OmemoDoubleRatchet clone() {
return OmemoDoubleRatchet(
dhs,
@ -375,6 +375,15 @@ class OmemoDoubleRatchet {
);
}
/// Computes the fingerprint of the double ratchet, according to
/// XEP-0384.
Future<String> get fingerprint async {
final curveKey = await ik.toCurve25519();
return HEX.encode(
await curveKey.getBytes(),
);
}
@visibleForTesting
Future<bool> equals(OmemoDoubleRatchet other) async {
final dhrMatch = dhr == null

View File

@ -755,8 +755,43 @@ class OmemoManager {
await _leaveRatchetCriticalSection(jid);
}
// TODO
Future<List<DeviceFingerprint>?> getFingerprintsForJid(String jid) async => null;
/// If ratchets with [jid] exists, returns a list of fingerprints for each
/// ratchet.
///
/// If not ratchets exists, returns null.
Future<List<DeviceFingerprint>?> getFingerprintsForJid(String jid) async {
await _getFingerprintsForJidImpl(jid);
final result = await _getFingerprintsForJidImpl(jid);
await _leaveRatchetCriticalSection(jid);
return result;
}
Future<List<DeviceFingerprint>?> _getFingerprintsForJidImpl(String jid) async {
// Check if we know of the JID.
if (!_deviceList.containsKey(jid)) {
return null;
}
final devices = _deviceList[jid]!;
final fingerprints = List<DeviceFingerprint>.empty(growable: true);
for (final device in devices) {
final ratchet = _ratchetMap[RatchetMapKey(jid, device)];
if (ratchet == null) {
_log.warning('getFingerprintsForJid: Ratchet $jid:$device not found.');
continue;
}
fingerprints.add(
DeviceFingerprint(
device,
await ratchet.fingerprint,
),
);
}
return fingerprints;
}
/// Returns the device used for encryption and decryption.
Future<OmemoDevice> getDevice() => _deviceLock.synchronized(() => _device);