/// <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);
        }
        /// <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);
        }
        /// <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);
        }
        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);
                        }
                    }
                }
            }
        }
        /// <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;
        }
        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);
                }
            }
        }