Exemplo n.º 1
0
        public void Test_IsValidDate_When_Not_Valid_Day()
        {
            var date = "2018-02-33T17:53:48.972Z";

            Assert.False(ValidationUtils.IsValidDate(date));
        }
Exemplo n.º 2
0
        public void Test_IsValidDate_When_Not_Valid_Year()
        {
            var date = "218-22-10T37:53:48.972Z";

            Assert.False(ValidationUtils.IsValidDate(date));
        }
Exemplo n.º 3
0
        public void Test_IsValidTransferValue_When_Null()
        {
            string date = null;

            Assert.Throws <ArgumentNullException>(() => ValidationUtils.IsValidDate(date));
        }
Exemplo n.º 4
0
        public void Test_IsValidTransferValue_When_Empty_String()
        {
            var date = "";

            Assert.False(ValidationUtils.IsValidDate(date));
        }
Exemplo n.º 5
0
        public void Test_IsValidDate_When_Valid()
        {
            var date = "2018-02-10T17:53:48.972Z";

            Assert.True(ValidationUtils.IsValidDate(date));
        }
Exemplo n.º 6
0
        public void Test_IsValidDate_When_Not_Valid_YearToSmall()
        {
            var date = "2017-22-10T17:53:48.972Z";

            Assert.False(ValidationUtils.IsValidDate(date));
        }
Exemplo n.º 7
0
        public Transaction AddNewTransaction(Transaction tranData)
        {
            // Validate the transaction & add it to the pending transactions
            if (!ValidationUtils.IsValidAddress(tranData.From))
            {
                throw new ArgumentException("Invalid sender address:" + tranData.From);
            }

            if (!ValidationUtils.IsValidAddress(tranData.To))
            {
                throw new ArgumentException("Invalid recipient address:" + tranData.To);
            }

            if (!ValidationUtils.IsValidPublicKey(tranData.SenderPubKey))
            {
                throw new ArgumentException("Invalid public key:" + tranData.SenderPubKey);
            }

            var senderAddr = CryptoUtils.GetAddressFromPublicKey(tranData.SenderPubKey);

            if (senderAddr != tranData.From)
            {
                throw new ArgumentException("The public key should match the sender address:" + tranData.SenderPubKey);
            }

            if (!ValidationUtils.IsValidTransferValue(tranData.Value))
            {
                throw new ArgumentException("Invalid transfer value: " + tranData.Value);
            }

            if (!ValidationUtils.IsValidFee(tranData.Fee))
            {
                throw new ArgumentException("Invalid transaction fee: " + tranData.Fee);
            }

            if (!ValidationUtils.IsValidDate(tranData.DateCreated))
            {
                throw new ArgumentException("Invalid date: " + tranData.DateCreated);
            }

            if (!ValidationUtils.IsValidSignatureFormat(CryptoUtils.SignatureByHex(tranData.SenderSignature)))
            {
                throw new ArgumentException("Invalid or missing signature. Expected signature format: BigInteger[2] " + tranData.SenderSignature);
            }


            var tran = new Transaction(
                tranData.From,
                tranData.To,
                tranData.Value,
                tranData.Fee,
                tranData.DateCreated,
                tranData.Data,
                tranData.SenderPubKey,
                null, // the transactionDataHash is auto-calculated
                tranData.SenderSignature

                );

            //tran.IsSignatureValid = tranData.IsSignatureValid;

            // Check for duplicated transactions (to avoid "replay attack")
            var tranDataHex = CryptoUtils.BytesToHex(tran.TransactionDataHash);

            if (this.FindTransactionByDataHash(tranDataHex) != null)
            {
                throw new ArgumentException("Duplicated transaction: " + tranDataHex);
            }


            if (!CryptoUtils.VerifySignature(CryptoUtils.SignatureByHex(tran.SenderSignature), tran.TransactionDataHash))
            {
                throw new ArgumentException("Invalid signature: " + tranData.SenderSignature);
            }


            var balances = this.GetAccountBalance(tran.From);

            if (balances.ConfirmedBalance < tran.Fee)
            {
                throw new ArgumentException("Unsufficient sender balance at address:  " + tran.From);
            }

            //if (this.PendingTransactions.Any(s => CryptoUtils.BytesToHex(s.TransactionDataHash) == CryptoUtils.BytesToHex(tran.TransactionDataHash)))
            //    throw new ArgumentException("Trying to add duplicate transaction:  " + CryptoUtils.BytesToHex(tran.TransactionDataHash));

            this.PendingTransactions.Add(tran);
            Console.WriteLine("Added pending transaction: " + JsonConvert.SerializeObject(tran));

            return(tran);
        }