encryptField function

Future<CommercioDoc> encryptField(
  1. CommercioDoc doc,
  2. Uint8List aesKey,
  3. Set<CommercioEncryptedData> encryptedData,
  4. List<String> recipients,
  5. Wallet wallet, {
  6. Client? client,
})

Transforms doc into one having the proper fields encrypted as specified inside the encryptedData list. All the fields will be encrypted using the specified aesKey. This key will later be encrypted for each and every Did specified into the recipients list. The overall encrypted data will be put inside the proper document field.

Throws ArgumentError if:

Implementation

Future<CommercioDoc> encryptField(
  CommercioDoc doc,
  Uint8List aesKey,
  Set<CommercioEncryptedData> encryptedData,
  List<String> recipients,
  Wallet wallet, {
  http.Client? client,
}) async {
  // -----------------
  // --- Encryption
  // -----------------

  // Encrypt the contents
  String? encryptedContentUri;
  if (encryptedData.contains(CommercioEncryptedData.CONTENT_URI)) {
    if (doc.contentUri == null) {
      throw ArgumentError(
        'Document contentUri field can not be null if the encryptedData arguments contains CommercioEncryptedData.CONTENT_URI',
      );
    }

    encryptedContentUri = hex.encode(
      EncryptionHelper.encryptStringWithAes(doc.contentUri!, aesKey),
    );
  }

  String? encryptedMetadataContentUri;
  if (encryptedData.contains(CommercioEncryptedData.METADATA_CONTENT_URI)) {
    encryptedMetadataContentUri = hex.encode(
      EncryptionHelper.encryptStringWithAes(doc.metadata.contentUri, aesKey),
    );
  }

  String? encryptedMetadataSchemaUri;
  if (encryptedData.contains(CommercioEncryptedData.METADATA_SCHEMA_URI)) {
    if (doc.metadata.schema == null) {
      throw ArgumentError(
        'Document metadata.schema field can not be null if the encryptedData arguments contains CommercioEncryptedData.METADATA_SCHEMA_URI',
      );
    }
    encryptedMetadataSchemaUri = hex.encode(
      EncryptionHelper.encryptStringWithAes(doc.metadata.schema!.uri, aesKey),
    );
  }

  // ---------------------
  // --- Keys creation
  // ---------------------

  // Get the recipients Did Documents
  final recipientsWithDDO = await Future.wait(recipients.map((r) async {
    final didDoc = await IdHelper.getDidDocument(r, wallet, client: client);

    return MapEntry(r, didDoc);
  }));

  // Throw if any of the recipients does not have an identity associated to them
  for (final recipient in recipientsWithDDO) {
    if (recipient.value == null) {
      throw WalletIdentityNotFoundException.fromAddress(recipient.key);
    }
  }

  // Create the encryption key field
  final encryptionKeys = recipientsWithDDO.map((recipient) {
    final encryptedAesKey = EncryptionHelper.encryptBytesWithRsa(
      aesKey,
      recipient.value!.encryptionKey!.publicKey,
    );
    return CommercioDocEncryptionDataKey(
      recipientDid: recipient.key,
      value: hex.encode(encryptedAesKey),
    );
  }).toList();

  // Copy the metadata
  var metadataSchema = doc.metadata.schema;
  if (metadataSchema != null) {
    metadataSchema = CommercioDocMetadataSchema(
      version: metadataSchema.version,
      uri: encryptedMetadataSchemaUri ?? metadataSchema.uri,
    );
  }

  // Return a copy of the document
  return CommercioDoc(
    senderDid: doc.senderDid,
    recipientDids: doc.recipientDids,
    uuid: doc.uuid,
    checksum: doc.checksum,
    contentUri: encryptedContentUri ?? doc.contentUri,
    metadata: CommercioDocMetadata(
      contentUri: encryptedMetadataContentUri ?? doc.metadata.contentUri,
      schema: metadataSchema,
      schemaType: doc.metadata.schemaType,
    ),
    encryptionData: CommercioDocEncryptionData(
      keys: encryptionKeys,
      encryptedData: encryptedData,
    ),
    doSign: doc.doSign,
  );
}