/// Public constructor.
 public MsgShareDocument(CommercioDoc document)
 {
     Trace.Assert(document != null);
     // Assigns the properties
     this.document = document;
     base.setProperties("commercio/MsgShareDocument", _toJson());
 }
예제 #2
0
        // Alternate constructor from Json JObject
        public CommercioDocChecksum(JObject json)
        {
            this.value     = (String)json["value"];
            this.algorithm = CommercioDoc.ParseCommercioDocChecksumAlgorithm((String)json["algorithm"]);

            //Object outValue;
            //if (json.TryGetValue("value", out outValue))
            //    this.value = outValue as String;
            //if (json.TryGetValue("algorithm", out outValue))
            //    this.algorithm = (CommercioDocChecksumAlgorithm) outValue;
        }
예제 #3
0
        /// Creates a CommercioDoc from the given [wallet],
        /// [recipients], [id], [metadata]
        /// and optionally [contentUri], [checksum],
        /// [doSign], [encryptedData], [aesKey].
        public static async Task <CommercioDoc> fromWallet(
            Wallet wallet,
            List <String> recipients,
            String id,
            CommercioDocMetadata metadata,
            String contentUri                  = null,
            CommercioDocChecksum checksum      = null,
            CommercioDoSign doSign             = null,
            List <EncryptedData> encryptedData = null,
            KeyParameter aesKey                = null
            )
        {
            // Build a generic document
            CommercioDoc commercioDocument = new CommercioDoc(
                senderDid: wallet.bech32Address,
                recipientDids: recipients,
                uuid: id,
                contentUri: contentUri,
                metadata: metadata,
                checksum: checksum,
                encryptionData: null,
                doSign: doSign
                );

            // Encrypt its contents, if necessary
            if ((encryptedData != null) && (encryptedData.Count > 0))
            {
                // Get a default aes key for encryption if needed
                if (aesKey == null)
                {
                    aesKey = KeysHelper.generateAesKey();
                }


                // Encrypt its contents, if necessary
                commercioDocument = await DocsUtils.encryptField(
                    commercioDocument,
                    aesKey,
                    encryptedData,
                    recipients,
                    wallet
                    );
            }
            return(commercioDocument);
        }
예제 #4
0
        // Alternate constructor from Json JObject
        public CommercioDoSign(JObject json)
        {
            this.storageUri         = (String)json["storage_uri"];
            this.signerInstance     = (String)json["signer_instance"];
            this.sdnData            = ((JArray)json["sdn_data"]).Select(elem => (CommercioDoc.ParseCommercioSdnData(elem.ToString()))).ToList();
            this.vcrId              = (String)json["vcr_id"];
            this.certificateProfile = (String)json["certificate_profile"];

            //Object outValue;
            //if (json.TryGetValue("storage_uri", out outValue))
            //    this.storageUri = outValue as String;
            //if (json.TryGetValue("signer_instance", out outValue))
            //    this.signerInstance = outValue as String;
            //if (json.TryGetValue("sdn_data", out outValue))
            //    this.sdnData = outValue as List<CommercioSdnData>;
            //if (json.TryGetValue("vcr_id", out outValue))
            //    this.vcrId = vcrId as String;
            //if (json.TryGetValue("certificate_profile", out outValue))
            //    this.certificateProfile = outValue as String;
        }
        // This doesn't seem to be used, although it is there in Dart code... I will keep it as a placeholder
        // Use static HttpClient to avoid exhausting system resources for network connections.
        // private static HttpClient client = new HttpClient();

        #endregion

        #region Properties
        #endregion

        #region Constructors
        #endregion

        #region Public Methods

        /// Creates a new transaction that allows to share the document associated
        /// with the given [contentUri] and having the given [metadata]
        /// and [checksum]. If [encryptedData] is specified, encrypts the proper
        /// data for the specified [recipients] and then sends the transaction
        /// to the blockchain.
        public static async Task <TransactionResult> shareDocument(
            String id,
            CommercioDocMetadata metadata,
            List <String> recipients,
            Wallet wallet,
            CommercioDoSign doSign             = null,
            CommercioDocChecksum checksum      = null,
            KeyParameter aesKey                = null,
            List <EncryptedData> encryptedData = null,
            StdFee fee            = null,
            String contentUri     = null,
            BroadcastingMode mode = BroadcastingMode.SYNC
            )
        {
            // Build a generic document
            CommercioDoc commercioDoc = await CommercioDocHelper.fromWallet(
                wallet : wallet,
                recipients : recipients,
                id : id,
                metadata : metadata,
                checksum : checksum,
                contentUri : contentUri,
                doSign : doSign,
                encryptedData : encryptedData,
                aesKey : aesKey
                );


            // Build the tx message
            MsgShareDocument msg = new MsgShareDocument(document: commercioDoc);

            // Careful here, Eugene: we are passing a list of BaseType containing the derived MsgSetDidDocument msg
            return(await TxHelper.createSignAndSendTx(new List <StdMsg> {
                msg
            }, wallet, fee : fee, mode : mode));
        }
        // 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()
                           )
                       ));
        }