示例#1
0
        protected override Task <bool> SearchPage(object searchParameters, ulong offset, int requestId)
        {
            var tcs = new TaskCompletionSource <bool>();

            if (offset >= Convert.ToUInt64(m_ids.Count))
            {
                tcs.SetResult(false);
                return(tcs.Task);
            }

            GetQuoteResponse.BeginGetQuote(
                AccountManager.GetAuthInfo,
                async response =>
            {
                GetQuoteResponse.EndGetQuote(response);
                var res = response.AsyncState as GetQuoteResponse;
                Results.AddRange(res.Quotes);

                await Task.Delay(TimeSpan.FromMilliseconds(100));
                tcs.SetResult(true);
            },
                requestId,
                m_ids.Skip(Convert.ToInt32(offset)).Take(Convert.ToInt32(OffsetIncrement)).ToList()
                );
            return(tcs.Task);
        }
示例#2
0
        // Interface Implementations
        public IDictionary <Security, decimal> GetQuotes(IEnumerable <Security> securities)
        {
            var symbolData  = GetSymbols(securities);
            var symbolIds   = symbolData.Select <SymbolData, ulong>(qsd => qsd.m_symbolId).ToList();
            var securityMap = securities.ToDictionary(BuildSymbolFromSecurity);

            var response = GetQuoteResponse.GetQuote(_tokenManager.GetAuthToken(), symbolIds);

            return(response.Quotes.Where(q => !double.IsInfinity(q.m_lastTradePrice) && !double.IsNaN(q.m_lastTradePrice))
                   .ToDictionary <Level1DataItem, Security, decimal>(key => securityMap[key.m_symbol], value => Convert.ToDecimal(value.m_lastTradePrice)));
        }
示例#3
0
        public byte[] HandleRequest(byte[] requestBytes)
        {
            object request = DiffPrivRequest.ParseRequest(requestBytes);

            if (request is Common.GetQuoteRequest)
            {
                GetQuoteResponse getQuoteResponse = new GetQuoteResponse(0, key_pair);
                return(getQuoteResponse.Encode());
            }
            if (request is InitializeDBRequest)
            {
                InitializeDBRequest r = (InitializeDBRequest)request;
                if (rows_received != 0)
                {
                    Console.Error.WriteLine("Received request to initialize DB after receiving rows");
                    return(DiffPrivSrvResponse.EncodeInitializeDBResponse(18));
                }

                if (r.budget_num < r.budget_den)
                {
                    Console.Error.WriteLine("Received request to initialize DB with budget < 1");
                    return(DiffPrivSrvResponse.EncodeInitializeDBResponse(16));
                }

                budget = new BigRational(r.budget_num, r.budget_den);
                rows.Clear();
                return(DiffPrivSrvResponse.EncodeInitializeDBResponse(0));
            }
            if (request is AddRowRequest)
            {
                byte[] ciphertext = ((AddRowRequest)request).ciphertext;
                byte[] plaintext;
                try
                {
                    plaintext = key_pair.Decrypt(ciphertext, false);
                }
                catch
                {
                    Console.Error.WriteLine("Received undecryptable add-row request");
                    return(DiffPrivSrvResponse.EncodeAddRowResponse());
                }

                HandleAddRowRequest(plaintext);
                return(DiffPrivSrvResponse.EncodeAddRowResponse());
            }
            if (request is QueryRequest)
            {
                QueryRequest r = (QueryRequest)request;
                return(HandleQueryRequest(r));
            }
            return(InvalidResponse.Encode());
        }
示例#4
0
        static void TestStuff()
        {
            CommonRoutines.SquirtBakedKey();
            byte[]           getQuoteResponseBytes = CommonRoutines.ParseFakeReply("68 05 CA 1A 1D 69 00 1B 21 31 8E D9 08 00 45 00 01 80 00 00 00 00 80 11 11 3C 0A 0A 0A 14 0A 0A 0A 0A 07 BF F4 96 01 6C 3F C7 01 00 00 00 00 00 00 00 57 00 00 01 00 00 00 00 07 73 73 68 2D 72 73 61 00 00 00 03 01 00 01 00 00 00 41 00 A9 52 43 21 04 37 54 10 11 92 9F 70 A8 D3 3B 4A 44 F3 62 C3 47 89 78 93 3E 38 D8 B8 FF 33 32 3F F8 8D 03 3B B0 D7 81 47 1A 79 75 A6 6F 38 57 D9 E7 82 9D 8D 51 7F 40 BA 50 4E 22 B5 95 95 13 B5 2A 5F C5 F6 6C F3 73 85 94 D2 72 4F A3 D9 C1 8E A9 09 7B 32 2F 38 14 FD 50 22 D1 FE B1 0B 16 CD 0B 5E D7 1B 5A CF 34 14 04 4D 49 91 25 44 A3 4C 4B 3E 01 B4 14 4F 33 FA D3 9B 84 A4 4A 23 7F 13 10 0E D4 FB EC 74 53 FD 52 62 95 8A AF D6 FC B6 DB 5B 07 43 3E 70 12 5E 71 9B 15 EB 82 81 60 01 0E A2 87 A0 86 12 E0 71 17 4E 2C 4F 4C 0E 29 57 D5 F2 73 8E 52 72 14 96 CD 19 17 60 AC D2 4C 02 5D EE D5 A7 D0 D4 14 F4 FE B0 71 8A 08 C7 86 94 0E B9 7F E9 ED E2 5C 97 30 C2 6C E0 72 2A CF EE BE 52 D1 32 48 75 00 0D 3C DA ED AA 5D 35 DC 0B B4 40 8D DB 65 F7 C1 56 54 D9 44 85 D1 1E 34 D1 17 A5 A9 41 9E 68 CD 0E 4C E1 BF 30 65 E3 0E 95 A2 46 EF A5 23 B5 5E 68 EA 1D 08 8A 12 19 57 05 34 B8 2E 71 DA 67 84 55 BB 74 92 D4 F3 B9 61 BE 56 8E D8 65 33 82 3E 7E 69 AF F1 92 59 CD 47 3F");
            GetQuoteResponse gr = new GetQuoteResponse(getQuoteResponseBytes);

            byte[] advanceCounterResponseBytes = CommonRoutines.ParseFakeReply("68 05 CA 1A 1D 69 00 1B 21 31 8E D9 08 00 45 00 00 70 00 00 00 00 80 11 12 4C 0A 0A 0A 14 0A 0A 0A 0A 07 BF F4 96 00 5C B6 A7 02 00 00 00 00 00 00 00 07 00 00 00 40 22 00 00 00 01 01 64 03 FC D3 33 B6 D2 25 38 08 7E FD 31 E6 64 D5 A9 C0 5D 26 E1 2E 4C 97 8D AC D5 EA 6D 50 5A C6 EF 98 62 DC 4B 54 AF 32 96 E3 5D EF 96 D3 B6 83 D3 80 8F 68 EE 8F 3E 9F 7C 9C B2 EB 50 EF F4 52 3B");
            AdvanceCounterResponse a           = new AdvanceCounterResponse(advanceCounterResponseBytes, gr.PublicKey);

            while (true)
            {
            }
        }
示例#5
0
        /// <summary>
        /// Get an accepted quote with ID.
        /// </summary>
        /// <param name="currency">The source currency of the quote.</param>
        /// <param name="privateKey">The privateKey of the sending address of the quote.</param>
        /// <param name="ID">ID of the accepted quote.</param>
        /// <response code="200">The quote with a specified ID.</response>
        /// <response code="400_InvalidUrlParameters">Invalid URL parameters.</response>
        /// <response code="403_SignatureMissing">X-REQUEST-SIGNATURE header is missing.</response>
        /// <response code="403_SignatureExpired">X-REQUEST-SIGNATURE has expired.</response>
        /// <response code="403_InvalidSignature">Invalid X-REQUEST-SIGNATURE.</response>
        /// <response code="404">Quote is not found.</response>
        /// <response code="500">Server error.</response>
        /// <returns></returns>
        public async Task <Result <GetQuoteResponse, ErrorResponse> > GetQuoteAsync(ECurrency currency, string privateKey, Guid?ID)
        {
            #region
            if (!currency.IsGluwaExchangeCurrency())
            {
                throw new ArgumentOutOfRangeException($"Unsupported currency: {currency}");
            }

            if (ID == null || ID == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(ID));
            }
            #endregion

            var    result     = new Result <GetQuoteResponse, ErrorResponse>();
            string requestUri = $"{mEnv.BaseUrl}/v1/Quotes/{ID}";

            try
            {
                using (HttpClient httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.Add(X_REQUEST_SIGNATURE, GluwaService.GetAddressSignature(privateKey, currency, mEnv));

                    using (HttpResponseMessage response = await httpClient.GetAsync(requestUri))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            GetQuoteResponse quoteResponse = await response.Content.ReadAsAsync <GetQuoteResponse>();

                            result.IsSuccess = true;
                            result.Data      = quoteResponse;

                            return(result);
                        }

                        string contentString = await response.Content.ReadAsStringAsync();

                        result.Error = ResponseHandler.GetError(response.StatusCode, requestUri, contentString);
                    }
                }
            }
            catch (HttpRequestException)
            {
                result.IsSuccess = false;
                result.Error     = ResponseHandler.GetExceptionError();
            }

            return(result);
        }
示例#6
0
        public GetQuoteResponse GetQuote(Guid id)
        {
            var response = new GetQuoteResponse();

            try
            {
                response.Quote = _quoteRepository.GetQuote(id);
                if (response.Quote == null)
                {
                    throw new Exception(string.Format("Error loading quote with id {0}", id));
                }
                response.IsSuccess = true;
            }
            catch (Exception e)
            {
                response.IsSuccess    = false;
                response.ErrorMessage = e.Message;
            }
            return(response);
        }
示例#7
0
        public byte[] HandleRequest(byte[] requestBytes)
        {
            object request = NotaryRequest.ParseRequest(requestBytes);

            if (request is Common.GetQuoteRequest)
            {
                GetQuoteResponse getQuoteResponse = new GetQuoteResponse(0, key_pair);
                return(getQuoteResponse.Encode());
            }
            if (request is AdvanceCounterRequest)
            {
                AdvanceCounterRequest r = (AdvanceCounterRequest)request;
                counter = counter + 1;
                byte[] header = new byte[1];
                header[0] = 34;
                byte[] new_counter_value_encoding = CommonRoutines.EncodeMPBigInteger(counter);
                byte[] notary_statement           = CommonRoutines.CombineByteArrays(header, new_counter_value_encoding, r.message);
                byte[] notary_attestation         = key_pair.SignData(notary_statement, CryptoConfig.MapNameToOID("SHA256"));
                return(NotarySrvResponse.EncodeAdvanceCounterResponse(0, notary_statement, notary_attestation));
            }
            return(InvalidResponse.Encode());
        }
示例#8
0
        public byte[] HandleRequest(byte[] requestBytes)
        {
            object request = TrIncRequest.ParseRequest(requestBytes);

            if (request is Common.GetQuoteRequest)
            {
                GetQuoteResponse getQuoteResponse = new GetQuoteResponse(0, key_pair);
                return(getQuoteResponse.Encode());
            }
            if (request is CreateCounterRequest)
            {
                CreateCounterRequest     r          = (CreateCounterRequest)request;
                RSACryptoServiceProvider public_key = CommonRoutines.DecodePublicKey(r.public_key);
                if (public_key == null)
                {
                    return(TrIncSrvResponse.EncodeCreateCounterResponse(3, 0));
                }
                TrIncCounter counter = new TrIncCounter(public_key);
                counters.Add(counter);
                UInt32 counter_index = (UInt32)(counters.Count - 1);
                return(TrIncSrvResponse.EncodeCreateCounterResponse(0, counter_index));
            }
            if (request is AdvanceCounterRequest)
            {
                AdvanceCounterRequest r = (AdvanceCounterRequest)request;
                if (r.counter_index < 0 || r.counter_index >= counters.Count)
                {
                    Console.Error.WriteLine("Received request for invalid counter index {0}", r.counter_index);
                    return(TrIncSrvResponse.EncodeAdvanceCounterResponse(1, new byte[0], new byte[0]));
                }

                TrIncCounter counter = counters[(int)r.counter_index];
                byte[]       req     = CommonRoutines.CombineByteArrays(r.new_counter_value, r.message);
                if (!counter.PublicKey.VerifyData(req, CryptoConfig.MapNameToOID("SHA256"), r.request_attestation))
                {
                    Console.Error.WriteLine("Received invalid request attestation");
                    return(TrIncSrvResponse.EncodeAdvanceCounterResponse(5, new byte[0], new byte[0]));
                }

                int        offset            = 0;
                BigInteger new_counter_value = CommonRoutines.DecodeMPBigInteger(r.new_counter_value, ref offset);
                if (new_counter_value < 0 || offset != r.new_counter_value.Length)
                {
                    Console.Error.WriteLine("Received invalid new counter value encoding");
                    return(TrIncSrvResponse.EncodeAdvanceCounterResponse(6, new byte[0], new byte[0]));
                }

                if (new_counter_value < counter.Value)
                {
                    Console.Error.WriteLine("New counter value requested {0} is smaller than current counter value {1}", new_counter_value, counter.Value);
                    return(TrIncSrvResponse.EncodeAdvanceCounterResponse(7, new byte[0], new byte[0]));
                }

                BigInteger old_counter_value = counter.Value;
                counter.Value = new_counter_value;

                byte[] header = new byte[1];
                header[0] = 34;
                byte[] counter_index_encoding     = CommonRoutines.EncodeBEWord(r.counter_index);
                byte[] old_counter_value_encoding = CommonRoutines.EncodeMPBigInteger(old_counter_value);
                byte[] new_counter_value_encoding = CommonRoutines.EncodeMPBigInteger(new_counter_value);
                byte[] trinc_statement            = CommonRoutines.CombineByteArrays(header, counter_index_encoding, old_counter_value_encoding, new_counter_value_encoding, r.message);
                byte[] trinc_attestation          = key_pair.SignData(trinc_statement, CryptoConfig.MapNameToOID("SHA256"));
                return(TrIncSrvResponse.EncodeAdvanceCounterResponse(0, trinc_statement, trinc_attestation));
            }
            return(InvalidResponse.Encode());
        }