public static async Task <RequestDidPowerUp> fromWallet(Wallet senderWallet, String pairwiseDid, List <StdCoin> amount, RSAPrivateKey privateKey)
        {
            // Get the timestamp
            String timestamp = GenericUtils.getTimeStampEpoch(); // RC 20200913: Be careful, the Dart version uses a different timestamp format non ISO-8601 - Why?
            String senderDid = senderWallet.bech32Address;

            // Build and sign the signature
            byte[] signedSignatureHash = SignHelper.signPowerUpSignature(
                senderDid: senderDid,
                pairwiseDid: pairwiseDid,
                timestamp: timestamp,
                rsaPrivateKey: privateKey);

            // Build the payload -*
            DidPowerUpRequestPayload payload = new DidPowerUpRequestPayload(
                senderDid: senderDid,
                pairwiseDid: pairwiseDid,
                timeStamp: timestamp,
                signature: Convert.ToBase64String(signedSignatureHash)
                );

            // =============
            // Encrypt proof
            // =============

            // Generate an AES-256 key
            KeyParameter aesKey = KeysHelper.generateAesKey(); // await ?

            // Encrypt the payload
            byte[] encryptedProof = EncryptionHelper.encryptStringWithAesGCM(JsonConvert.SerializeObject(payload), aesKey);

            // =================
            // Encrypt proof key
            // =================

            // Encrypt the key using the Tumbler public RSA key
            RSAPublicKey rsaPubTkKey = await EncryptionHelper.getGovernmentRsaPubKey(senderWallet.networkInfo.lcdUrl);

            byte[] encryptedProofKey = EncryptionHelper.encryptBytesWithRsa(aesKey.GetKey(), rsaPubTkKey);

            // Build the message
            RequestDidPowerUp request = new RequestDidPowerUp(
                claimantDid: senderDid,
                amount: amount,
                powerUpProof: Convert.ToBase64String(encryptedProof),
                uuid: Guid.NewGuid().ToString(),
                encryptionKey: Convert.ToBase64String(encryptedProofKey)
                );

            return(request);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        /// Given a [payload], creates a new AES-256 key and uses that to encrypt
        /// the payload itself.
        /// This is now a static method of the class - we cannot have function outside class in C#, while Dart can...
        public static async Task <ProofGenerationResult> generateProof(Object payload, String lcdUrl)
        {
            // Generate the AES key - no await here...
            KeyParameter aesKey = KeysHelper.generateAesKey();

            // Encrypt the payload
            String encryptionData = JsonConvert.SerializeObject(payload);

            byte[] encryptedPayload = EncryptionHelper.encryptStringWithAes(encryptionData, aesKey);

            // Generate nonce, concatenate with payload and encode(?)
            //*** 20200524 - I am not sure of encoding - I am already working with arrays...No encode until extensive testing
            byte[] nonce        = KeysHelper.generateRandomNonce(12);
            String encodedProof = Convert.ToBase64String(encryptedPayload.Concat(nonce).ToArray());

            // Encrypt the AES key
            RSAPublicKey rsaKey = await EncryptionHelper.getGovernmentRsaPubKey(lcdUrl);

            byte[] encryptedAesKey = EncryptionHelper.encryptBytesWithRsa(aesKey.GetKey(), rsaKey);
            String encodedAesKey   = Convert.ToBase64String(encryptedAesKey);


            return(new ProofGenerationResult(encryptedProof: encodedProof, encryptedAesKey: encodedAesKey));
        }