Exemplo n.º 1
0
        public static async Task <List <Contact> > GetPublicKeysAndContactAddresses(TangleMessenger tangleMessenger, string receiverAddress, bool dontLoadSql = false)
        {
            var trytes = await tangleMessenger.GetMessagesAsync(receiverAddress, 3, false, dontLoadSql);

            var contacts = new List <Contact>();

            foreach (var tryte in trytes)
            {
                var trytesString = tryte.Message.ToString();
                if (!trytesString.Contains(ChiotaConstants.LineBreak))
                {
                    continue;
                }

                try
                {
                    var index           = trytesString.IndexOf(ChiotaConstants.LineBreak, StringComparison.Ordinal);
                    var publicKeyString = trytesString.Substring(0, index);
                    var bytesKey        = new TryteString(publicKeyString).DecodeBytesFromTryteString();

                    contacts.Add(
                        new Contact
                    {
                        NtruKey        = new NTRUPublicKey(bytesKey),
                        ContactAddress = trytesString.Substring(index + ChiotaConstants.LineBreak.Length, 81)
                    });
                }
                catch
                {
                    // ignored
                }
            }

            return(RemoveDuplicateContacts(contacts));
        }
Exemplo n.º 2
0
        public static async Task <List <MessageViewModel> > GetNewMessages(IAsymmetricKeyPair keyPair, Contact contact, TangleMessenger tangle)
        {
            var trytes = await tangle.GetMessagesAsync(contact.ChatAddress, 3, true);

            var messagesEncrypted = new List <ChatMessage>();

            foreach (var tryte in trytes)
            {
                try
                {
                    messagesEncrypted.Add(ReadChatMessage(tryte));
                }
                catch
                {
                    // ignored
                }
            }

            var sortedEncryptedMessages = messagesEncrypted.OrderBy(o => o.Date).ToList();
            var messages = new List <MessageViewModel>();

            for (var i = 0; i < sortedEncryptedMessages.Count - 1; i = i + 2)
            {
                if (TwoPartsOfMessage(sortedEncryptedMessages, i))
                {
                    TryteString encryptedMessage;
                    if (sortedEncryptedMessages[i].IsFirstPart)
                    {
                        encryptedMessage = new TryteString(sortedEncryptedMessages[i].Message + sortedEncryptedMessages[i + 1].Message);
                    }
                    else
                    {
                        encryptedMessage = new TryteString(sortedEncryptedMessages[i + 1].Message + sortedEncryptedMessages[i].Message);
                    }

                    var decryptedMessage = Encoding.UTF8.GetString(new NtruKex().Decrypt(keyPair, encryptedMessage.DecodeBytesFromTryteString()));
                    messages.Add(new MessageViewModel
                    {
                        Text           = decryptedMessage,
                        MessagDateTime = sortedEncryptedMessages[i].Date.ToLocalTime(),
                        IsIncoming     = sortedEncryptedMessages[i].Signature == contact.PublicKeyAddress.Substring(0, 30),
                        ProfileImage   = contact.ImageHash
                    });
                }
                else
                {
                    i--;
                }
            }

            return(messages);
        }