Beispiel #1
0
        public override Task <ResponseCheckTx> CheckTx(RequestCheckTx request, ServerCallContext context)
        {
            var tx = request.Tx;

            if (tx.Length <= 4)
            {
                Int32 txCheck = Int32.Parse(tx.ToString());
                Logger.Debug("CheckTx: " + txCheck);
                var msg = "Invalid nonce. Expected >= ${txCount}, got ${txCheck}";

                if (txCheck < txCount)
                {
                    Console.WriteLine("CheckTx ERROR: " + msg);

                    return(Task.FromResult(new ResponseCheckTx
                    {
                        Code = CodeType.BadNonce,
                        Log = msg
                    }));
                }
            }

            Logger.Debug("CheckTx: OK");

            return(Task.FromResult(new ResponseCheckTx
            {
                Code = CodeType.Ok
            }));
        }
Beispiel #2
0
        public void ShouldRouteTransaction()
        {
            var transactionHandlerRouter = _serviceProvider.GetService(typeof(TransactionHandlerRouter)) as TransactionHandlerRouter;

            // initialize certificates list
            if (_serviceProvider.GetService(typeof(IOptions <ApplicationSettings>)) is IOptions <ApplicationSettings> applicationSettings)
            {
                applicationSettings.Value.SecuritySettings.PublicCertificates = new List <X509Certificate2>();
            }

            var transaction = new CreateNumber {
                Owner = "A", PhoneNumber = "1", TransactionTime = DateTime.Now
            };
            var json  = JsonConvert.SerializeObject(transaction, new TransactionSerializerSettings());
            var token = new TransactionToken(Encoding.UTF8.GetBytes(json));
            // fake signing
            var signatureField = typeof(TransactionToken).GetField("_signature", BindingFlags.NonPublic | BindingFlags.Instance);

            signatureField.SetValue(token, Encoding.UTF8.GetBytes("1234"));

            var request = new RequestCheckTx {
                Tx = ByteString.CopyFromUtf8(token.ToTokenString())
            };

            // fake context
            var ctor    = typeof(ServerCallContext).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First();
            var context = (ServerCallContext)ctor.Invoke(new object[] { null, "TX", "localhost", DateTime.Now, new Metadata(), CancellationToken.None, null, null });

            var expected        = ResponseHelper.Check.Unauthorized();
            var responseCheckTx = transactionHandlerRouter?.RouteCheckTx(request, context).Result;

            Assert.AreEqual(expected, responseCheckTx);
        }
Beispiel #3
0
        public Task <ResponseCheckTx> RouteCheckTx(RequestCheckTx request, ServerCallContext context)
        {
            var resolved = ResolveTransactionHandler(() => request.Tx);

            return(Handle(resolved.Handler, resolved.Payload,
                          (handler, payload) => handler.CheckTx(resolved.TransactionToken, payload, request, context)));
        }
Beispiel #4
0
        public virtual Task <ResponseCheckTx> RequestCheckTx(RequestCheckTx req)
        {
            var tx = req.Tx;

            if (tx.Length <= 4)
            {
                Int32 txCheck = Int32.Parse(tx.ToString());
                Console.WriteLine("CheckTx: " + txCheck);
                var msg = "Invalid nonce. Expected >= ${txCount}, got ${txCheck}";

                if (txCheck < txCount)
                {
                    Console.WriteLine("CheckTx ERROR: " + msg);

                    return(Task.FromResult(new ResponseCheckTx
                    {
                        Code = CodeType.BadNonce,
                        Log = msg
                    }));
                }
            }

            Console.WriteLine("CheckTx: OK");

            return(Task.FromResult(new ResponseCheckTx
            {
                Code = CodeType.Ok
            }));
        }
        public Task <ResponseCheckTx> RouteCheckTx(RequestCheckTx request, ServerCallContext context)
        {
            var data    = ResolveRequestTx(request.Tx);
            var handler = _transactionHandlerFactory.CreateHandlerFor(data.Payload);

            _logger.LogInformation($"Handling CheckTx {data.Payload.GetType()} with handler {handler.GetType()}");
            return(handler.CheckTx(data.TransactionToken, data.Payload, request, context));
        }
Beispiel #6
0
        public async Task <ResponseCheckTx> CheckTx(TransactionToken transactionToken, BaseTransaction data,
                                                    RequestCheckTx request, ServerCallContext context)
        {
            if (!(data is T payload))
            {
                return(ResponseHelper.Check.NoPayload());
            }

            return(await CheckTx(transactionToken, payload, request, context).ConfigureAwait(false));
        }
Beispiel #7
0
 protected abstract Task <ResponseCheckTx> CheckTx(TransactionToken transactionToken, T payload,
                                                   RequestCheckTx request,
                                                   ServerCallContext context);
        public async Task <ResponseCheckTx> CheckTx(TransactionToken transactionToken, object data, RequestCheckTx request, ServerCallContext context)
        {
            if (!(data is DenyTransferRequest payload))
            {
                return(ResponseHelper.Check.NoPayload());
            }

            var callNumber = await _callNumberRepository.Get(payload.PhoneNumber);

            if (callNumber == null)
            {
                return(ResponseHelper.Check.Create(CodeType.UnknownCallNumber, "Unknown call number."));
            }

            if (!IsVerifiedCaller(transactionToken, callNumber.Owner))
            {
                return(ResponseHelper.Check.Unauthorized());
            }

            if (callNumber.TransferRequestedTo != payload.NewOwner)
            {
                return(ResponseHelper.Check.Create(CodeType.UnknownNewOwner, "Unknown new owner."));
            }

            if (!callNumber.TransferRequestStarted.HasValue)
            {
                return(ResponseHelper.Check.Create(CodeType.NoTransferInitiated, "Transfer was not initiated."));
            }

            return(ResponseHelper.Check.Ok());
        }
Beispiel #9
0
        public async Task <ResponseCheckTx> CheckTx(TransactionToken transactionToken, object data, RequestCheckTx request, ServerCallContext context)
        {
            if (!(data is RequestTransfer payload))
            {
                return(ResponseHelper.Check.NoPayload());
            }

            if (!IsVerifiedCaller(transactionToken, payload.NewOwner))
            {
                return(ResponseHelper.Check.Unauthorized());
            }

            _logger.LogInformation("Received valid CheckTx request");

            var callNumber = await _callNumberRepository.Get(payload.PhoneNumber);

            if (callNumber == null)
            {
                return(ResponseHelper.Check.Create(CodeType.UnknownCallNumber, "Unknown call number."));
            }

            if (callNumber.TransferRequestStarted.HasValue)
            {
                return(ResponseHelper.Check.Create(CodeType.TransferAlreadyStarted, "Transfer has already been started."));
            }

            _logger.LogInformation("CheckTx Result: ok");

            return(ResponseHelper.Check.Ok());
        }
 public override Task <ResponseCheckTx> CheckTx(RequestCheckTx request, ServerCallContext context)
 {
     Console.WriteLine("CheckTx has been called.");
     return(_transactionHandlerRouter.RouteCheckTx(request, context));
 }
Beispiel #11
0
 public override ResponseCheckTx CheckTx(RequestCheckTx request, CallOptions options)
 {
     return(base.CheckTx(request, options));
 }
        public async Task <ResponseCheckTx> CheckTx(TransactionToken transactionToken, object data, RequestCheckTx request, ServerCallContext context)
        {
            if (!IsVerifiedCaller(transactionToken))
            {
                return(ResponseHelper.Check.Unauthorized());
            }

            if (!(data is CreateNumber payload))
            {
                return(ResponseHelper.Check.NoPayload());
            }

            _logger.LogInformation("Received valid CheckTx request");

            var result = await _callNumberRepository.Get(payload.PhoneNumber);

            _logger.LogInformation($"CheckTx Result: {result == null}");
            return(ResponseHelper.Check.Create(result == null ? CodeType.Ok : CodeType.PhoneNumberAlreadyExists, result == null ? "" : "Phonenumber already exists."));
        }