encryptServices method

Future<List<ServiceDto>> encryptServices(
  1. String myId,
  2. Set<String> delegations,
  3. Uint8List? contactKey,
  4. List<DecryptedServiceDto> services,
)

Implementation

Future<List<ServiceDto>> encryptServices(String myId, Set<String> delegations, Uint8List? contactKey, List<DecryptedServiceDto> services) async {
  return await 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.content.entries.every((e) =>
    e.value.compoundValue.isNotEmpty &&
        e.value.stringValue == null &&
        e.value.documentId == null &&
        e.value.measureValue == null &&
        e.value.medicationValue == null &&
        e.value.booleanValue == null &&
        e.value.numberValue == null &&
        e.value.instantValue == null &&
        e.value.fuzzyDateValue == null &&
        e.value.binaryValue == null &&
        e.value.range.isEmpty)) {
      return ServiceDto.fromJson({
        ...s.toJson(),
        'content': toJsonDeep(Map.fromEntries(await Future.wait(s.content.entries.map((e) async =>
            MapEntry(e.key, ContentDto(compoundValue: (await this.encryptServices(myId, delegations, contactKey, e.value.compoundValue))))))))
      })!;
    } else {
      return ServiceDto.fromJson({
        ...s.toJson(),
        'content': {},
        'encryptedSelf': base64.encoder.convert(Uint8List.fromList(json.encode({'content': s.content}).codeUnits).encryptAES(key))
      })!;
    }
  }));
}