/// <summary> /// Executes the wallet function. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="functionType">Name of the method.</param> /// <param name="parameters">The parameters.</param> /// <returns>the function result</returns> protected T ExecuteWalletFunction <T>(WalletFunctionType functionType, params object[] parameters) { var rawResult = ExecuteWalletRpcFunction(functionType, parameters); if (rawResult == null) { return(default(T)); } var json = rawResult["result"].ToString(); if (string.IsNullOrEmpty(json)) { return(default(T)); } if (typeof(T) == typeof(string)) { return((T)(object)json); } if (typeof(T) == typeof(bool)) { return((T)(object)bool.Parse(json)); } return(JsonConvert.DeserializeObject <T>(json)); }
/// <summary> /// Executes the wallet RPC function. /// </summary> /// <param name="functionName">Name of the function.</param> /// <param name="parameters">The parameters.</param> /// <returns>the json object response of the call</returns> private JObject ExecuteWalletRpcFunction(WalletFunctionType functionName, params object[] parameters) { var webRequest = CreateWebRequest(); var jsonRequest = new JObject { new JProperty("jsonrpc", "1.0"), new JProperty("id", "1"), new JProperty("method", functionName.ToString().ToLower()), new JProperty("params", new JArray(parameters)) }; var s = JsonConvert.SerializeObject(jsonRequest); var byteArray = Encoding.UTF8.GetBytes(s); webRequest.ContentLength = byteArray.Length; using (var dataStream = webRequest.GetRequestStream()) { dataStream.Write(byteArray, 0, byteArray.Length); } using (var webResponse = webRequest.GetResponse()) { using (var streamReader = new StreamReader(webResponse.GetResponseStream(), true)) { var jsonData = streamReader.ReadToEnd(); try { return((JObject)JsonConvert.DeserializeObject(jsonData)); } catch { } return(JObject.Parse(jsonData)); } } }