/// <summary> /// /// </summary> /// <param name="id"></param> /// <param name="profilesDir"></param> /// <returns></returns> public static string LoadEncryptedProfile(HoardID id, string profilesDir) { if (!Directory.Exists(profilesDir)) { throw new HoardException(string.Format("Profile doesn't exists: {0}", id.ToString())); } var profileFiles = Directory.GetFiles(profilesDir, "*.keystore"); if (profileFiles.Length == 0) { throw new HoardException(string.Format("Profile doesn't exists: {0}", id.ToString())); } foreach (var fullPath in profileFiles) { string fileName = Path.GetFileName(fullPath); if ((fileName != null) && (fileName != System.String.Empty)) { string json = File.ReadAllText(fullPath); var details = JObject.Parse(json); if (details == null) { continue; } string address = details["address"].Value <string>(); if (new HoardID(address) == id) { return(json); } } } throw new HoardException(string.Format("Profile doesn't exists: {0}", id.ToString())); }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <param name="password"></param> /// <returns></returns> public EncryptedData EncryptPassword(HoardID id, string password) { try { byte[] encryptionKey = GenerateKey(ProvideEncryptionPhrase()); byte[] _iv = ProvideIV(); byte[] encryptedData = Helper.AESEncrypt(encryptionKey, Encoding.UTF8.GetBytes(password), _iv, 256); string hashedId = Helper.Keccak256HexHashString(id.ToString()); EncryptedData data = new EncryptedData(); data.pswd = BitConverter.ToString(encryptedData).Replace("-", string.Empty); return(data); } catch (Exception e) { throw new HoardException("Can't encrypt password for id " + id.ToString(), e); } }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <param name="encryptedData"></param> /// <returns></returns> public string DecryptPassword(HoardID id, EncryptedData encryptedData) { try { byte[] decryptionKey = GenerateKey(ProvideEncryptionPhrase()); byte[] decrypted = Helper.AESDecrypt(decryptionKey, WhisperService.HexStringToByteArray(encryptedData.pswd), ProvideIV(), 256); return(Encoding.UTF8.GetString(decrypted)); } catch (Exception e) { throw new HoardException("Can't decrypt password for id " + id.ToString(), e); } }
private async Task <GameItem[]> GetItemsClientRequest(HoardID accountId = null, string itemType = null, ulong?page = null, ulong?itemsPerPage = null) { var request = new RestRequest("items/", Method.GET); if (accountId != null) { request.AddQueryParameter("owner_address", accountId.ToString().EnsureHexPrefix()); } if (itemType != null) { request.AddQueryParameter("item_type", itemType); } if (page.HasValue) { request.AddQueryParameter("page", page.Value.ToString()); if (itemsPerPage != null) { request.AddQueryParameter("per_page", itemsPerPage.Value.ToString()); } } var response = await Client.ExecuteTaskAsync(request).ConfigureAwait(false); if (response.StatusCode == System.Net.HttpStatusCode.OK) { var gameItems = new List <GameItem>(); var result = JArray.Parse(response.Content); foreach (var item in result.Children <JObject>()) { gameItems.AddRange(JsonConvert.DeserializeObject <List <GameItem> >( item.GetValue("items").ToString(), new JsonConverter[] { new GameItemsConverter() } )); } return(gameItems.ToArray()); } return(null); }
/// <summary> /// /// </summary> /// <param name="userInputProvider"></param> /// <param name="id"></param> /// <param name="profilesDir"></param> /// <param name="passwordNeeded"></param> /// <returns></returns> public static async Task DeleteProfile(IUserInputProvider userInputProvider, HoardID id, string profilesDir, bool passwordNeeded) { if (!Directory.Exists(profilesDir)) { throw new HoardException(string.Format("Profile doesn't exists: {0}", id.ToString())); } string[] files = Directory.GetFiles(profilesDir, "*.keystore"); var keyStoreService = new Nethereum.KeyStore.KeyStoreService(); foreach (string file in files) { StreamReader jsonReader = new StreamReader(file); JObject jobj = JObject.Parse(jsonReader.ReadToEnd()); jsonReader.Close(); JToken valueAddress; if (jobj.TryGetValue("address", out valueAddress)) { HoardID actualId = new HoardID(valueAddress.Value <string>()); if (id == actualId) { Nethereum.Signer.EthECKey key = null; if (passwordNeeded) { string password = await userInputProvider.RequestInput(null, id, eUserInputType.kPassword, valueAddress.Value <string>()); try { key = new Nethereum.Signer.EthECKey(keyStoreService.DecryptKeyStoreFromJson(password, jobj.ToString()), true); } catch (Exception e) { throw new HoardException("Incorrect password", e); } } File.Delete(file); return; } } } throw new HoardException(string.Format("Profile doesn't exists: {0}", id.ToString())); }
/// <summary> /// /// </summary> /// <param name="id"></param> /// <param name="oldPassword"></param> /// <param name="newPassword"></param> /// <param name="profilesDir"></param> /// <returns></returns> public static string ChangePassword(HoardID id, string oldPassword, string newPassword, string profilesDir) { if (!Directory.Exists(profilesDir)) { throw new HoardException(string.Format("Profile doesn't exists: {0}", id.ToString())); } string[] files = Directory.GetFiles(profilesDir, "*.keystore"); var keyStoreService = new Nethereum.KeyStore.KeyStoreService(); foreach (string file in files) { StreamReader jsonReader = new StreamReader(file); JObject jobj = JObject.Parse(jsonReader.ReadToEnd()); jsonReader.Close(); JToken valueAddress; JToken name; if (jobj.TryGetValue("address", out valueAddress) && jobj.TryGetValue("name", out name)) { HoardID actualId = new HoardID(valueAddress.Value <string>()); if (id == actualId) { string newFile = null; try { var key = new Nethereum.Signer.EthECKey(keyStoreService.DecryptKeyStoreFromJson(oldPassword, jobj.ToString()), true); newFile = CreateAccountKeyStoreFile(key, newPassword, name.Value <string>(), profilesDir); } catch (Exception e) { throw new HoardException("Incorrect password", e); } if (newFile != null) { File.Delete(file); } return(newFile); } } } throw new HoardException(string.Format("Profile doesn't exists: {0}", id.ToString())); }
private async Task <ulong> GetItemsAmountClientRequest(HoardID accountId, string itemType = null) { var request = new RestRequest("items/balance/", Method.GET); request.AddQueryParameter("owner_address", accountId.ToString().EnsureHexPrefix()); if (itemType != null) { request.AddQueryParameter("item_type", itemType); } var response = await Client.ExecuteTaskAsync(request).ConfigureAwait(false); if (response.StatusCode == System.Net.HttpStatusCode.OK) { var result = JObject.Parse(response.Content); return(result.GetValue("balance").Value <ulong>()); } return(0); }
private string PathToID(HoardID userID) => Path.Combine(StoragePath, userID.ToString()) + ".hse";