public static Block ComputeBlock(Transaction transaction)
        {
            CurrentBlockchain = DataManager.UploadBlockchainDictionary();

            var check         = true;
            var previousNonce = CurrentBlockchain.Values.Last().Nonce;
            var nonce         = FindHash(previousNonce + 1);

            while (check)
            {
                if (!CurrentBlockchain.ContainsKey(HexConvert.FromBytes(Hash.ComputeSha256FromString(nonce.ToString()))))
                {
                    check = false;
                }
                else
                {
                    nonce = FindHash(nonce + 1);
                }
            }

            var newBlock = new Block()
            {
                Id           = CurrentBlockchain.Values.Last().Id + 1,
                Hash         = Hash.ComputeSha256FromString(nonce.ToString()),
                PreviousHash = HexConvert.ToBytes(CurrentBlockchain.Keys.Last()),
                Nonce        = nonce,
                Timestamp    = DateTime.Now,
                Transaction  = transaction
            };

            return(newBlock);
        }
        public static int GetCash(Dictionary <string, Block> data, byte[] publicKey)
        {
            var currentKey = data.Last().Key;
            var cash       = 0;

            while (!data[currentKey].PreviousHash.SequenceEqual(data[currentKey].Hash))
            {
                cash += data[currentKey].Transaction.OutEntries
                        .Where(outEntry => outEntry.RecipientHash.SequenceEqual(publicKey))
                        .Sum(outEntry => (int)outEntry.Value);

                if (EntryConsistPub(data[currentKey].Transaction.InEntries, publicKey))
                {
                    return(cash);
                }

                if (data[currentKey].Id == 0)
                {
                    return((from outEntry in data[currentKey].Transaction.OutEntries
                            where outEntry.RecipientHash.SequenceEqual(publicKey)
                            select(int) outEntry.Value).FirstOrDefault());
                }

                currentKey = HexConvert.FromBytes(data[currentKey].PreviousHash);
            }

            return(0);
        }
        /// <summary>
        /// Генерация генезис блока
        /// </summary>
        public static Dictionary <string, Block> GenerateBlockchainGenesis(KeyPair keyPair)
        {
            var outEntry = new OutEntry()
            {
                RecipientHash = keyPair.PublicKey,
                Value         = 100
            };

            var genesisBlock = new Block()
            {
                Id           = 0,
                Hash         = genesisHash,
                Nonce        = 0,
                PreviousHash = null,
                Timestamp    = DateTime.Now,
                Transaction  = new Transaction()
                {
                    Id         = 0,
                    Signature  = EccService.Sign(genesisHash, keyPair.PrivateKey, keyPair.PublicKey),
                    Timestamp  = DateTime.Now,
                    InEntries  = null,
                    OutEntries = new List <OutEntry>()
                    {
                        outEntry
                    }
                }
            };

            var blockchain = new Dictionary <string, Block>()
            {
                { HexConvert.FromBytes(genesisHash), genesisBlock }
            };

            return(blockchain);
        }
        /// <summary>
        /// Поиск хэша
        /// </summary>
        private static int FindHash(int nonce)
        {
            for (int i = nonce; i < 100000000; i++)
            {
                var dataHash    = Hash.ComputeSha256FromString(i.ToString());
                var dataHashStr = HexConvert.FromBytes(dataHash);

                if (dataHashStr[dataHashStr.Length - 2].Equals('0'))
                {
                    return(i);
                }
            }
            return(-1);
        }
        public static int GetCashRec(Dictionary <string, Block> data, byte[] publicKey, string current)
        {
            if (EntryConsistPub(data[current].Transaction.InEntries, publicKey))
            {
                return(GetCashOut(data[current].Transaction.OutEntries, publicKey));
            }
            if (ConsistOutWithoutEntry(data[current].Transaction, publicKey))
            {
                return(GetCashOut(data[current].Transaction.OutEntries, publicKey) +
                       GetCashRec(data, publicKey, HexConvert.FromBytes(data[current].PreviousHash)));
            }

            return(GetCashRec(data, publicKey, HexConvert.FromBytes(data[current].PreviousHash)));
        }