Ejemplo n.º 1
0
        private void ValidateTransaction(Transaction transaction)
        {
            if (!CryptoUtil.IsAddressValid(transaction.FromAddress))
            {
                throw new AddressNotValidException($"{transaction.FromAddress} is not valid address");
            }

            if (!CryptoUtil.IsAddressValid(transaction.ToAddress))
            {
                throw new AddressNotValidException($"{transaction.ToAddress} is not valid address");
            }

            if (transaction == null)
            {
                throw new ArgumentException("'transaction' object cannot be null");
            }

            //check whetehr we know that transaction
            if (PendingTransactions.FirstOrDefault(t => t.TransactionHash == transaction.TransactionHash) != null)
            {
                return;
            }

            //check whether transaction is already mined
            if (BlockChain.Any(b => b.Value.Transactions.Any(t => t.TransactionHash == transaction.TransactionHash)))
            {
                return;
            }

            if (string.IsNullOrWhiteSpace(transaction.SenderPublicKey))
            {
                throw new TransactionNotValidException("Sender signature cannot be empty");
            }

            bool isHashValid = TransactionValidator.IsValidateHash(transaction);

            if (!isHashValid)
            {
                throw new TransactionNotValidException("Transaction not Valid! Tranascion is chnaged by middle man");
            }

            bool isVerified = TransactionValidator.IsValid(transaction);

            if (!isVerified)
            {
                throw new TransactionNotValidException("Transaction not Valid! Signutre is not valid");
            }

            string address = TransactionValidator.GetAddress(transaction.SenderPublicKey);

            if (address != transaction.FromAddress)
            {
                throw new TransactionNotValidException("Provided address is not valid.");
            }
        }