コード例 #1
0
        private async Task <T> BuildAndSendRequest <T>(string function, JArray additionalParams, T defaultValue, CancellationToken ctoken)
        {
            try
            {
                if (IsConnected == false)
                {
                    ErrorCallbackProvider.ReportError("Whisper connection error!");
                    throw new WebException("Whisper connection error!");
                }

                JObject jobj = new JObject();
                jobj.Add("jsonrpc", JsonVersion);
                jobj.Add("method", function);
                if (additionalParams != null)
                {
                    jobj.Add("params", additionalParams);
                }
                jobj.Add("id", ++JsonId);
                await WebSocketProvider.Send(Encoding.UTF8.GetBytes(jobj.ToString()), ctoken);

                return(await ReceiveResponse <T>(JsonId, defaultValue, ctoken));
            }
            catch (WebSocketException e)
            {
                throw new HoardException("Whisper unknown exception:\n" + e.Message, e);
            }
            catch (Exception e)
            {
                throw new HoardException("Unknown exception:\n" + e.Message, e);
            }
        }
コード例 #2
0
        /// <summary>
        /// Helper function to create Profile object based on privateKey
        /// </summary>
        /// <param name="name">name of profile</param>
        /// <param name="privateKey">private key of account</param>
        /// <returns></returns>
        public static Profile CreateProfileDirect(string name, string privateKey)
        {
            ErrorCallbackProvider.ReportInfo("Generating user account.");
            var     ecKey   = new Nethereum.Signer.EthECKey(privateKey);
            Profile profile = new KeyStoreProfile(name, new HoardID(ecKey.GetPublicAddress()), privateKey.HexToByteArray());

            return(profile);
        }
コード例 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="addressOrName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task <Profile> RequestProfile(string addressOrName, string password)
        {
            ErrorCallbackProvider.ReportInfo("Requesting user account.");
            KeyStoreUtils.ProfileDesc accountDesc = await KeyStoreUtils.RequestProfile(UserInputProvider, addressOrName, ProfilesDir, password);

            if (accountDesc != null)
            {
                return(new KeyStoreProfile(accountDesc.Name, new HoardID(accountDesc.Address), accountDesc.PrivKey));
            }
            return(null);
        }
コード例 #4
0
        /// <summary>
        /// Creates new profile with given name
        /// </summary>
        /// <param name="name">name of profile </param>
        /// <returns>new account</returns>
        public async Task <Profile> CreateProfile(string name)
        {
            ErrorCallbackProvider.ReportInfo("Generating user account.");
            string password = await UserInputProvider.RequestInput(name, null, eUserInputType.kPassword, "new password");

            Tuple <string, byte[]> accountTuple = KeyStoreUtils.CreateProfile(name, password, ProfilesDir);
            Profile profile = new KeyStoreProfile(name, new HoardID(accountTuple.Item1), accountTuple.Item2);

            accountTuple.Item2.Initialize();
            return(profile);
        }
コード例 #5
0
 /// <summary>
 /// Returns amount of all items of the specified type belonging to a particular player with given type
 /// </summary>
 /// <param name="profile"></param>
 /// <param name="gameID"></param>
 /// <param name="itemType">Item type</param>
 /// <returns></returns>
 public async Task <ulong> GetPlayerItemsAmount(Profile profile, GameID gameID, string itemType)
 {
     if (Providers.ContainsKey(gameID))
     {
         IGameItemProvider c = Providers[gameID];
         return(await c.GetPlayerItemsAmount(profile, itemType).ConfigureAwait(false));
     }
     else
     {
         ErrorCallbackProvider.ReportWarning($"Game [{gameID.Name}] could not be found. Have you registered it properly?");
     }
     return(await Task.FromResult <ulong>(0));
 }
コード例 #6
0
        private async Task <SubscriptionResponse> ReceiveSubscriptionResponse(CancellationToken token)
        {
            if (IsConnected == false)
            {
                ErrorCallbackProvider.ReportError("Whisper connection error!");
                throw new WebException("Whisper connection error!");
            }

            while (true)
            {
                byte[] msgBytes = await WebSocketProvider.Receive(token);

                //we are skipping all messages that are not subscriptions
                if (msgBytes != null)
                {
                    string jsonMsg = Encoding.UTF8.GetString(msgBytes);
                    ErrorCallbackProvider.ReportInfo(jsonMsg);

                    JToken  message = null;
                    JObject json    = JObject.Parse(jsonMsg);
                    json.TryGetValue("error", out message);
                    if (message != null)
                    {
                        Error = message.ToString();
                        ErrorCallbackProvider.ReportError("Received error from Whisper service: " + Error);
                    }
                    else
                    {
                        json.TryGetValue("method", out JToken method);
                        if ((method != null) && (method.ToString() == "shh_subscription"))
                        {
                            JToken prms = null;
                            json.TryGetValue("params", out prms);
                            if (prms != null)
                            {
                                JObject jsonParams = prms.ToObject <JObject>();
                                jsonParams.TryGetValue("result", out message);
                                if (message != null)
                                {
                                    return(JsonConvert.DeserializeObject <SubscriptionResponse>(message.ToString()));
                                }
                            }
                        }
                        else
                        {
                            ErrorCallbackProvider.ReportWarning("Expected notification but got: " + jsonMsg);
                        }
                    }
                }
            }
        }
コード例 #7
0
        /// <summary>
        /// Returns all Game Items owned by player's subaccount in particular game
        /// </summary>
        /// <param name="profile"></param>
        /// <param name="gameID"></param>
        /// <param name="page">Page number</param>
        /// <param name="itemsPerPage">Number of items per page</param>
        /// <param name="itemType">Item type</param>
        /// <returns></returns>
        public async Task <GameItem[]> GetPlayerItems(Profile profile, GameID gameID, string itemType, ulong page, ulong itemsPerPage)
        {
            List <GameItem> items = new List <GameItem>();

            if (Providers.ContainsKey(gameID))
            {
                IGameItemProvider c = Providers[gameID];
                items.AddRange(await c.GetPlayerItems(profile, itemType, page, itemsPerPage).ConfigureAwait(false));
            }
            else
            {
                ErrorCallbackProvider.ReportWarning($"Game [{gameID.Name}] could not be found. Have you registered it properly?");
            }
            return(items.ToArray());
        }
コード例 #8
0
        /// <summary>
        /// Connects to BC and fills missing options.
        /// </summary>
        /// <param name="options">Hoard service options.</param>
        /// <returns></returns>
        public async Task Initialize(HoardServiceOptions options)
        {
            Options = options;

            //access point to block chain - a must have
            BCComm = BCCommFactory.Create(Options);
            string result = await BCComm.Connect();

            ErrorCallbackProvider.ReportInfo(result);

            //our default GameItemProvider
            if (Options.Game != GameID.kInvalidID)
            {
                await RegisterHoardGame(Options.Game);
            }

            DefaultGame = Options.Game;

            //init exchange service
            IExchangeService exchange = new HoardExchangeService(this);
            await exchange.Init();

            ExchangeService = exchange;
        }
コード例 #9
0
        private async Task <T> ReceiveResponse <T>(int reqId, T defaultValue, CancellationToken ctoken)
        {
            if (IsConnected == false)
            {
                ErrorCallbackProvider.ReportError("Whisper connection error!");
                throw new WebException("Whisper connection error!");
            }

            //wait for requested response (or timeout)
            while (true)
            {
                byte[] msgBytes = await WebSocketProvider.Receive(ctoken);

                if (msgBytes != null)
                {
                    string jsonMsg = Encoding.UTF8.GetString(msgBytes);
                    ErrorCallbackProvider.ReportInfo(jsonMsg);

                    JObject json = JObject.Parse(jsonMsg);
                    json.TryGetValue("error", out JToken error);
                    if (error != null)
                    {
                        Error = error.ToString();
                        ErrorCallbackProvider.ReportError("Received error from Whisper service: " + Error);
                        return(defaultValue);
                    }
                    else
                    {
                        json.TryGetValue("result", out JToken result);
                        if (result != null)
                        {
                            json.TryGetValue("id", out JToken respId);
                            if (respId.Value <int>() == reqId)
                            {
                                if (result.Type == JTokenType.Array || result.Type == JTokenType.Object)
                                {
                                    return(JsonConvert.DeserializeObject <T>(result.ToString()));
                                }
                                return(result.Value <T>());
                            }
                            else
                            {
                                ErrorCallbackProvider.ReportError("Waiting for response with ID: " + reqId + " but got " + respId.Value <int>());
                            }
                        }
                        else
                        {
                            json.TryGetValue("method", out JToken method);
                            if ((method != null) && (method.ToString() == "shh_subscription"))
                            {
                                json.TryGetValue("params", out JToken prms);
                                if (prms != null)
                                {
                                    JObject jsonParams = prms.ToObject <JObject>();
                                    jsonParams.TryGetValue("result", out JToken message);
                                    if (message != null)
                                    {
                                        OnSubscriptionMessage?.Invoke(JsonConvert.DeserializeObject <SubscriptionResponse>(message.ToString()));
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    ErrorCallbackProvider.ReportWarning("Conection was closed!");
                    return(defaultValue);
                }
            }
        }