Example #1
0
        public Model.EncryptedMessage Encrypt(string data, AccountWithPublicKey account, string secretPhrase)
        {
            var localMessageService = new LocalMessageService();
            var nonce     = localMessageService.CreateNonce();
            var encrypted = localMessageService.EncryptTextTo(account.PublicKey, data, nonce, true, secretPhrase);

            return(new Model.EncryptedMessage
            {
                Message = encrypted.ToString(),
                Nonce = nonce.ToString()
            });
        }
Example #2
0
 private CreateTransactionParameters.AlreadyEncryptedMessage GetEncryptedMessage()
 {
     if ((IsMessageEncryptionEnabled && EncryptMessage.Value) && !string.IsNullOrEmpty(Message))
     {
         var localMessageService = new LocalMessageService();
         var nonce     = localMessageService.CreateNonce();
         var encrypted = localMessageService.EncryptTextTo(_recipientPublicKey, Message, nonce,
                                                           true, _walletRepository.SecretPhrase);
         var encryptedMessage = new CreateTransactionParameters.AlreadyEncryptedMessage(encrypted, nonce, true, true, true);
         return(encryptedMessage);
     }
     return(null);
 }
Example #3
0
 private CreateTransactionParameters.AlreadyEncryptedMessageToSelf GetEncryptedNoteToSelfMessage()
 {
     if (!string.IsNullOrEmpty(NoteToSelfMessage))
     {
         var localMessageService = new LocalMessageService();
         var nonce           = localMessageService.CreateNonce();
         var encryptedToSelf = localMessageService.EncryptTextTo(_walletRepository.NxtAccountWithPublicKey.PublicKey,
                                                                 NoteToSelfMessage, nonce, true, _walletRepository.SecretPhrase);
         var encryptedMessageToSelf = new CreateTransactionParameters.AlreadyEncryptedMessageToSelf(encryptedToSelf, nonce, true, true);
         return(encryptedMessageToSelf);
     }
     return(null);
 }
Example #4
0
        public static void Main()
        {
            var        localMessageService     = new LocalMessageService();
            var        localTransactionService = new LocalTransactionService();
            var        messageService          = new MessageService();
            var        transactionService      = new TransactionService();
            const bool useCompression          = true;

            // Encrypt message to send locally
            const string message   = "Sending a permanent message";
            var          nonce     = localMessageService.CreateNonce();
            var          encrypted = localMessageService.EncryptTextTo(RecipientPublicKey, message, nonce, useCompression, SecretPhrase);

            // Encrypt message to self locally
            const string messageToSelf   = "Note to self: sending a permanent message";
            var          nonceToSelf     = localMessageService.CreateNonce();
            var          encryptedToSelf = localMessageService.EncryptTextTo(SenderPublicKey, messageToSelf, nonceToSelf, useCompression, SecretPhrase);

            // Prepare the transaction with your public key
            var parameters = new CreateTransactionByPublicKey(1440, Amount.Zero, SenderPublicKey)
            {
                EncryptedMessage       = new CreateTransactionParameters.AlreadyEncryptedMessage(encrypted, nonce, true, useCompression),
                EncryptedMessageToSelf = new CreateTransactionParameters.AlreadyEncryptedMessageToSelf(encryptedToSelf, nonceToSelf, true, useCompression)
            };
            var unsigned = messageService.SendMessage(parameters, Recipient).Result;

            // Verify the unsigned transaction bytes from the node (only needed if you cannot trust the node)
            localTransactionService.VerifySendMessageTransactionBytes(unsigned, parameters, Recipient);

            // Sign and broadcast
            var signed        = localTransactionService.SignTransaction(unsigned, SecretPhrase);
            var result        = transactionService.BroadcastTransaction(new TransactionParameter(signed.ToString())).Result;
            var transactionId = result.TransactionId;

            Console.WriteLine($"Sent transaction: {transactionId}");

            Console.ReadLine();
        }