decryptServices method

Future<List<DecryptedServiceDto>> decryptServices(
  1. String myId,
  2. Uint8List? contactKey,
  3. List<ServiceDto> services
)

Implementation

Future<List<DecryptedServiceDto>> decryptServices(String myId, Uint8List? contactKey, List<ServiceDto> services) async {
  return Future.wait(services.map((s) async {
    var key = contactKey ?? (await this.decryptEncryptionKeys(myId, s.encryptionKeys)).firstOrNull?.formatAsKey().fromHexString();
    if (key == null) {
      throw FormatException("Cannot get encryption key for ${s.id} and hcp ${myId}");
    }

    if (s.encryptedSelf != null) {
      final decryptedData = base64.decoder.convert(s.encryptedSelf!).decryptAES(key);
      return DecryptedServiceDto.fromJson({...s.toJson(), ...toJsonDeep(json.decode(String.fromCharCodes(decryptedData)))})!;
    } else {
      return DecryptedServiceDto.fromJson({
        ...s.toJson(),
        'content': Map.fromEntries((await Future.wait(s.content.entries
            .map((e) async => MapEntry(e.key, {'compoundValue': (await decryptServices(myId, key, e.value.compoundValue)).toList()})))))
      })!;
    }
  }));
}