示例#1
0
        private async Task <List <Hash> > GetNewHashes(string addresse, List <Hash> shortStorageHashes)
        {
            var addresses = new List <Address> {
                new Address(addresse)
            };
            var transactions = await this.repository.FindTransactionsByAddressesAsync(addresses);

            return(IotaHelper.FilterNewHashes(transactions, shortStorageHashes));
        }
示例#2
0
        public async Task <List <TryteStringMessage> > GetMessagesAsync(string address, int retryNumber = 1, bool getChatMessages = false, bool dontLoadSql = false, bool alwaysLoadSql = false)
        {
            var messagesList = new List <TryteStringMessage>();
            var cachedHashes = new List <Hash>();

            if (!dontLoadSql)
            {
                var cachedTransactions = await this.TransactionCache.LoadTransactionsByAddressAsync(new Address(address));

                var alreadyLoaded = this.AddressLoadedCheck(address);
                foreach (var cachedTransaction in cachedTransactions)
                {
                    cachedHashes.Add(cachedTransaction.TransactionHash);

                    if (!alreadyLoaded || alwaysLoadSql)
                    {
                        messagesList.Add(new TryteStringMessage
                        {
                            Message = cachedTransaction.TransactionTrytes,
                            Stored  = true
                        });
                    }
                }

                // if more or equal to 2 * ChiotaConstants.MessagesOnAddress messages on address, don't try to load new messages
                if (cachedTransactions.Count >= (2 * ChiotaConstants.MessagesOnAddress))
                {
                    return(messagesList);
                }
            }

            var transactions = await this.Repository.FindTransactionsByAddressesAsync(new List <Address> {
                new Address(address)
            });

            var hashes = transactions.Hashes.Union(cachedHashes, new TryteComparer <Hash>()).ToList();

            foreach (var transactionsHash in hashes)
            {
                var bundle = await this.Repository.GetBundleAsync(transactionsHash);

                var message = new TryteStringMessage {
                    Message = IotaHelper.ExtractMessage(bundle), Stored = false
                };
                await this.TransactionCache.SaveTransactionAsync(
                    new TransactionCacheItem { Address = new Address(address), TransactionHash = transactionsHash, TransactionTrytes = message.Message });

                messagesList.Add(message);
            }


            return(messagesList);
        }
示例#3
0
        public async Task <User> UniquePublicKey()
        {
            var publicKeyList = await IotaHelper.GetPublicKeysAndContactAddresses(this.user.TangleMessenger, this.user.PublicKeyAddress, true);

            var requestAdressTrytes = new TryteString(this.user.NtruKeyPair.PublicKey.ToBytes().EncodeBytesAsString() + ChiotaConstants.LineBreak + this.user.RequestAddress + ChiotaConstants.End);

            // after a snapshot, upload public key again
            if (publicKeyList.Count == 0)
            {
                await this.user.TangleMessenger.SendMessageAsync(requestAdressTrytes, this.user.PublicKeyAddress);
            }
            else if (publicKeyList.Count > 1)
            {
                // more than one key at this address
                // generate a new public key address based on a changed seed until you find an unused address
                // this way the attacker doesn't know the next public key address
                List <Contact> newContacts;
                var            addresses = new List <Address> {
                    new Address(this.user.PublicKeyAddress)
                };
                do
                {
                    var newSeed          = this.user.Seed.ToString().Substring(0, 75) + addresses[0].ToString().Substring(0, 6);
                    var addressGenerator = new AddressGenerator(new Kerl(), new KeyGenerator(new Kerl(), new IssSigningHelper()));
                    addresses = await Task.Factory.StartNew(() => addressGenerator.GetAddresses(new Seed(newSeed), SecurityLevel.Medium, 0, 1));

                    // returns also null if something wrong with ntru key pair
                    newContacts = await IotaHelper.GetPublicKeysAndContactAddresses(this.user.TangleMessenger, addresses[0].ToString(), true);

                    if (newContacts == null || newContacts.Count == 0)
                    {
                        await this.user.TangleMessenger.SendMessageAsync(requestAdressTrytes, addresses[0].ToString());
                    }

                    this.user.PublicKeyAddress = addresses[0].ToString();
                }while (newContacts?.Count > 1);
            }

            return(this.user);
        }
示例#4
0
        public async Task <List <TryteStringMessage> > GetMessagesAsync(string addresse, int retryNumber = 1, bool getChatMessages = false, bool dontLoadSql = false)
        {
            var roundNumber        = 0;
            var messagesList       = new List <TryteStringMessage>();
            var tableList          = new List <SqLiteMessage>();
            var shortStorageHashes = new List <Hash>();

            try
            {
                if (!dontLoadSql)
                {
                    tableList = await this.sqLite.LoadTransactions(addresse);

                    var alreadyLoaded = this.AddressLoadedCheck(addresse);
                    foreach (var sqlLiteMessage in tableList)
                    {
                        shortStorageHashes.Add(new Hash(sqlLiteMessage.TransactionHash));
                        var message = new TryteStringMessage
                        {
                            Message = new TryteString(sqlLiteMessage.MessageTryteString),
                            Stored  = true
                        };
                        if (!alreadyLoaded)
                        {
                            messagesList.Add(message);
                        }
                    }
                }
            }
            catch
            {
                // ignored
            }

            // if more than 2*Chiotaconstants.MessagesOnAddress messages on address, don't try to load new messages
            var chatCheck = true;

            if (getChatMessages)
            {
                chatCheck = tableList.Count < (2 * ChiotaConstants.MessagesOnAddress);
            }

            while (roundNumber < retryNumber && chatCheck)
            {
                try
                {
                    this.UpdateNode(roundNumber);

                    var hashes = await this.GetNewHashes(addresse, shortStorageHashes);

                    foreach (var transactionsHash in hashes)
                    {
                        var bundle = await this.repository.GetBundleAsync(transactionsHash);

                        var message = new TryteStringMessage {
                            Message = IotaHelper.ExtractMessage(bundle), Stored = false
                        };
                        await this.sqLite.SaveTransaction(addresse, transactionsHash, message.Message.ToString());

                        messagesList.Add(message);
                    }

                    retryNumber = 0;
                }
                catch
                {
                    roundNumber++;
                }
            }

            return(messagesList);
        }