Exemple #1
0
        public void DebugToConsole(IEnumerable <BCTransaction> transactionList)
        {
            BCTransactionProvider bc       = new BCTransactionProvider();
            BCTransaction         prevItem = null;

            foreach (var item in transactionList)
            {
                bool isValid = true;
                if (prevItem != null)
                {
                    isValid = bc.IsValid(prevItem, item);
                }

                if (isValid == false)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                }

                Console.WriteLine($"Transaction Id: {item.TransactionId}");
                Console.WriteLine($"Transaction Previous Id: {item.TransactionPreviousId}");
                Console.WriteLine($"Owner Id: {item.OwnerId}");
                Console.WriteLine($"Date Created: {item.DateCreated}");
                Console.WriteLine($"Transaction amount: {item.Amount}");
                Console.WriteLine($"Balance: {item.Balance}"); Console.WriteLine($"Transaction Hash (simple): {item.TransactionHashSimple}");
                Console.WriteLine($"Transaction Hash (crypto): {item.TransactionHashCrypto}");
                Console.WriteLine("--------------------------------------------");
                Console.ResetColor();

                prevItem = item;
            }
        }
        private void InitializeBCTransactions()
        {
            _bcTransactions = new List <BCTransaction>();

            BCTransaction         newBCTrans;
            BCTransactionProvider bctProvider;

            newBCTrans =
                new BCTransaction {
                TransactionId = "0", OwnerId = "ABC", TransactionPreviousId = "0", DateCreated = Convert.ToDateTime("2019-04-01 14:45"), Amount = 0
            };

            _bcTransactions.Add(newBCTrans);


            //Create some mock transactions
            for (int i = 1; i < _numberOfTransactions + 1; i++)
            {
                bctProvider = new BCTransactionProvider();

                Random random = new Random();

                int randomNumber = random.Next(-500, 500);
                if (randomNumber == 0)
                {
                    randomNumber = random.Next(10, 500);
                }

                newBCTrans = bctProvider.AddTransaction(newBCTrans.TransactionId, newBCTrans.TransactionHashCrypto, i.ToString(), "ABC", DateTime.Now, randomNumber, newBCTrans.Balance);
                _bcTransactions.Add(newBCTrans);
            }
        }
        public bool IsValid(BCTransaction previousTransaction, BCTransaction nextTransaction)
        {
            var transHashes    = BlockChainer.GetHashedValue(previousTransaction.TransactionId, previousTransaction.TransactionHashCrypto, nextTransaction);
            var validationHash = nextTransaction.TransactionHashCrypto;

            return(transHashes.Item2 == validationHash);
        }
        public BCTransaction AddTransaction(string transactionPreviousId, string transactionPreviousHashId, string transactionId, string ownderId, DateTime dateCreated, decimal amount, decimal balance)
        {
            BCTransaction bCTransaction = new BCTransaction()

            {
                TransactionPreviousId = transactionPreviousId,
                TransactionId         = transactionId,
                OwnerId     = ownderId,
                DateCreated = dateCreated,
                Amount      = amount,
                Balance     = balance + amount
            };

            var transHashes = BlockChainer.GetHashedValue(transactionPreviousId, transactionPreviousHashId, bCTransaction);

            bCTransaction.TransactionHashSimple = transHashes.Item1;
            bCTransaction.TransactionHashCrypto = transHashes.Item2;
            return(bCTransaction);
        }
Exemple #5
0
        public void SaveToFile(IEnumerable <BCTransaction> transactionList, string filename)
        {
            BCTransactionProvider bc       = new BCTransactionProvider();
            BCTransaction         prevItem = null;

            using (StreamWriter sw = new StreamWriter($@"{filename}"))
            {
                sw.WriteLine("Transaction Id;Transaction Previous Id;Owner Id;Date Created;Transaction amount;Balance;Hash;HashCrypto;IsValid");

                foreach (var item in transactionList)
                {
                    bool isValid = true;
                    if (prevItem != null)
                    {
                        isValid = bc.IsValid(prevItem, item);
                    }
                    sw.WriteLine($"{item.TransactionId};{item.TransactionPreviousId};{item.OwnerId};{item.DateCreated};{item.Amount};{item.Balance};'{item.TransactionHashSimple}';{item.TransactionHashCrypto};{isValid}");

                    prevItem = item;
                }
            }
        }
Exemple #6
0
        public static (string, string) GetHashedValue(string transactionPreviousId, string transactionPreviousHashId, BCTransaction bcTransaction)
        {
            string transactionHashSimple = $"{transactionPreviousId}:{bcTransaction.TransactionId}";

            string data = transactionPreviousHashId + bcTransaction.TransactionId + bcTransaction.DateCreated.ToString("yyyyMMddHHmmss") + bcTransaction.Amount.ToString() + bcTransaction.OwnerId;

            string transactionHashCrypto = CryptoSha256.ComputeHash(data);

            return(transactionHashSimple, transactionHashCrypto);
        }