public CommercioDocMetadata(String contentUri,
                     CommercioDocMetadataSchema schema,
                     String schemaType = "")
 {
     Trace.Assert(contentUri != null);
     //Trace.Assert(schema != null);
     // Trace.Assert(!String.IsNullOrEmpty(schemaType));
     Trace.Assert(schemaType != null);
     Trace.Assert((schema != null) || (!String.IsNullOrEmpty(schemaType)));
     this.contentUri = contentUri;
     this.schema = schema;
     this.schemaType = schemaType;
 }
        public CommercioDocMetadata(String contentUri,
                                    CommercioDocMetadataSchema schema,
                                    String schemaType = "")
        {
            //15/07/2021 Luigi Arena
            Trace.Assert(contentUri != null, "metadata.content_uri must not be null");
            Trace.Assert(contentUri.Length <= 512, "metadata.content_uri must have a valid length");

            //Trace.Assert(schema != null);
            // Trace.Assert(!String.IsNullOrEmpty(schemaType));
            Trace.Assert(schemaType != null, "metadata.schemaType must not be null");
            Trace.Assert(schemaType.Length <= 512, "metadata.schemaType must have a valid length");

            Trace.Assert((schema != null) || (!String.IsNullOrEmpty(schemaType)));


            this.contentUri = contentUri;
            this.schema     = schema;
            this.schemaType = schemaType;
        }
        // Here we have to address the Dart direct methods - to be encapsulated in a class in C#
        #region Properties

        #endregion

        #region Constructors

        #endregion

        #region Public Methods

        /// Transforms [this] document 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.
        public static async Task <CommercioDoc> encryptField(
            CommercioDoc doc,
            KeyParameter aesKey,
            List <EncryptedData> encryptedData,
            List <String> recipients,
            Wallet wallet
            )
        {
            // -----------------
            // --- Encryption
            // -----------------

            // Encrypt the contents
            String encryptedContentUri = null;

            if (encryptedData.Contains(EncryptedData.CONTENT_URI))
            {
                encryptedContentUri = HexEncDec.ByteArrayToString(EncryptionHelper.encryptStringWithAes(doc.contentUri, aesKey));
            }

            String encryptedMetadataContentUri = null;

            if (encryptedData.Contains(EncryptedData.METADATA_CONTENT_URI))
            {
                encryptedMetadataContentUri = HexEncDec.ByteArrayToString(EncryptionHelper.encryptStringWithAes(doc.metadata.contentUri, aesKey));
            }

            String encryptedMetadataSchemaUri = null;

            if (encryptedData.Contains(EncryptedData.METADATA_SCHEMA_URI))
            {
                String schemaUri = doc.metadata.schema?.uri;
                if (schemaUri != null)
                {
                    encryptedMetadataSchemaUri = HexEncDec.ByteArrayToString(EncryptionHelper.encryptStringWithAes(schemaUri, aesKey));
                }
            }

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

            // I will trasform all the Dart function instructions in imperative loops
            // Get the recipients Did Documents
            List <DidDocument> recipientsDidDocs = new List <DidDocument>();

            foreach (String recipient in recipients)
            {
                recipientsDidDocs.Add(await IdHelper.getDidDocument(recipient, wallet));
            }

            // Get a list of al the Did Documents and the associated encryption key
            List <_Pair> keys = new List <_Pair>();

            foreach (DidDocument didDoc in recipientsDidDocs)
            {
                if (didDoc != null)
                {
                    _Pair p = new _Pair(didDoc);
                    if (p.pubKey != null)
                    {
                        keys.Add(p);
                    }
                }
            }

            // Create the encryption key field
            List <CommercioDocEncryptionDataKey> encryptionKeys = new List <CommercioDocEncryptionDataKey>();

            foreach (_Pair pair in keys)
            {
                byte[] encryptedAesKey = EncryptionHelper.encryptBytesWithRsa(
                    aesKey.GetKey(),
                    pair.pubKey
                    );
                CommercioDocEncryptionDataKey dataKey = new CommercioDocEncryptionDataKey(
                    recipientDid: pair.document.id,
                    value: HexEncDec.ByteArrayToString(encryptedAesKey)
                    );
                encryptionKeys.Add(dataKey);
            }

            // Copy the metadata
            CommercioDocMetadataSchema metadataSchema = doc.metadata?.schema;

            if (metadataSchema != null)
            {
                metadataSchema = new CommercioDocMetadataSchema(
                    version: metadataSchema.version,
                    uri: encryptedMetadataSchemaUri ?? metadataSchema.uri
                    );
            }

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