AccountModel.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System;
  2. using System.Linq;
  3. using System.Numerics;
  4. using System.Threading.Tasks;
  5. using System.Collections.Generic;
  6. using Console = HyperCube.Utils.AdvConsole;
  7. using Microsoft.AspNetCore.Components;
  8. using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
  9. using System.ComponentModel.DataAnnotations;
  10. using System.Reflection;
  11. namespace HyperCube.Models
  12. {
  13. public enum Role {
  14. [Display(Name = "Администратор")]
  15. Admin = 1,
  16. [Display(Name = "Эксперт")]
  17. Expert,
  18. [Display(Name = "Старатель")]
  19. Miner,
  20. [Display(Name = "Заказчик")]
  21. Customer,
  22. [Display(Name = "Инвестор")]
  23. Investor,
  24. [Display(Name = "Спонсор")]
  25. Sponsor,
  26. [Display(Name = "Наблюдатель")]
  27. Observer
  28. }
  29. public class AccountModel: ComponentBase
  30. {
  31. [Inject]
  32. AppData AppData { get; set; }
  33. [Inject]
  34. ProtectedSessionStorage storage { get; set; }
  35. //public event EventHandler<int> RolesChanged;
  36. int ratingExpert = 1;
  37. int ratingProspector = 1;
  38. public int GetRatingExpert()
  39. {
  40. return ratingExpert;
  41. }
  42. public int GetRatingProspector()
  43. {
  44. return ratingProspector;
  45. }
  46. public Dictionary<int, Notification> notifications = new();
  47. public Action<int> RolesChanged;
  48. public byte bsel = 0;
  49. public byte blockchain_selected
  50. {
  51. get { return bsel; }
  52. set {
  53. bsel = value;
  54. bcselupdate();
  55. }
  56. }
  57. public List<Role> Roles = new();
  58. public async Task SendTestNotification(AccountModel sender, string text)
  59. {
  60. Notification notification = new(NotificationType.System, text, "A new message!", UUID, sender.UUID);
  61. int newid = await notification.Send();
  62. /// adding only in local collection yet
  63. notifications.Add(newid, notification);
  64. }
  65. public void AddRole(Role role)
  66. {
  67. Roles.Add(role);
  68. RolesChanged?.Invoke(Roles.Count);
  69. //Console.WriteLine($"role added, count: {Roles.Count}");
  70. }
  71. public void RemoveRole(Role role)
  72. {
  73. Roles.Remove(role);
  74. RolesChanged?.Invoke(Roles.Count);
  75. //Console.WriteLine($"role removed, сount: {Roles.Count}");
  76. }
  77. async Task bcselupdate()
  78. {
  79. await MySQLConnector.Instance().SQLInsert($"update aspnetusers set blockchain_selected = {blockchain_selected} where Id='{UUID}'");
  80. }
  81. public static Dictionary<string, AccountModel> Loaded = new();
  82. string eth_address { get; set; }
  83. string eth_address1 { get; set; }
  84. /// <summary>
  85. /// ASP Identity ID
  86. /// </summary>
  87. public string UUID { get; set; }
  88. public string Name { get; set; }
  89. public string Email { get; set; }
  90. public string PWDHash { get; set; }
  91. public Role AccRole { get; set; }
  92. public List<string> GetRoleDisplayName()
  93. {
  94. List<string> _rolesDisplayName = new();
  95. foreach (Role role in Roles)
  96. {
  97. _rolesDisplayName.Add(
  98. role.GetType()
  99. .GetMember(role.ToString())
  100. .First()
  101. .GetCustomAttribute<DisplayAttribute>()
  102. .GetName()
  103. );
  104. }
  105. return _rolesDisplayName;
  106. }
  107. public string GetActualAddress(Blockchain bc)
  108. {
  109. string addr;
  110. if (bc.port == 8666)
  111. addr = eth_address1;
  112. else
  113. addr = eth_address;
  114. return addr;
  115. }
  116. public async Task<string> GetOrCreateActualAddress(Blockchain bc)
  117. {
  118. string addr;
  119. if (bc.port == 8666)
  120. addr = eth_address1;
  121. else
  122. addr = eth_address;
  123. Console.WriteLine($"got address1 {eth_address1}");
  124. if (addr == null || addr == "")
  125. addr = await bc.CreateBlockchainAccount(this, PWDHash);
  126. return addr;
  127. }
  128. public void SetActualAddress(string value, Blockchain bc)
  129. {
  130. if (bc.port == 8666)
  131. eth_address1 = value;
  132. else
  133. eth_address = value;
  134. }
  135. public AccountModel(){}
  136. public async Task<Blockchain> GetSelectedBlockChain()
  137. {
  138. //Console.WriteLine($"AppData null { AppData == null}");
  139. //var res = await storage.GetAsync<AccountModel>("acc");
  140. //var accountCurrent = res.Value;
  141. //var accountCurrent = AppData.CurrentAccount;
  142. //Console.WriteLine($"AppData acc null { accountCurrent == null}");
  143. if (Blockchain.loaded.Count > blockchain_selected)
  144. {
  145. var bc = Blockchain.loaded[blockchain_selected];
  146. Console.WriteLine($"GetMain blockchain_selected {blockchain_selected} " + bc.address);
  147. return bc;
  148. }
  149. else
  150. {
  151. Console.WriteLine($"Error: blockchains loaded {Blockchain.loaded.Count}");
  152. blockchain_selected = (byte)(Blockchain.loaded.Count - 1);
  153. }
  154. return null;
  155. }
  156. public static AccountModel FindByMail(string mail)
  157. {
  158. var selectedUsers = from user in Loaded.Values
  159. where user.Email == mail
  160. select user;
  161. return selectedUsers.First();
  162. }
  163. public static AccountModel Find(string uuid)
  164. {
  165. if (uuid == null)
  166. return null;
  167. if (Loaded.ContainsKey(uuid))
  168. return Loaded[uuid];
  169. else
  170. return null;
  171. }
  172. public static BigInteger ConvertBalance(string hex)
  173. {
  174. if (hex != null && hex.Length > 0)
  175. {
  176. Console.WriteLine($"ConvertBalance {hex}");
  177. string newHex = hex.Remove(0, 2);
  178. var balance = System.Numerics.BigInteger.Parse("0" + newHex, System.Globalization.NumberStyles.HexNumber);
  179. Console.WriteLine($"ConvertBalance {hex} {balance}");
  180. return balance;
  181. }
  182. return 0;
  183. }
  184. public async void FillAccount()
  185. {
  186. LoadRoles();
  187. var wallets = await MySQLConnector.Instance().SQLSelectComplex($"select * from account_wallets where account_uuid='{UUID}'");
  188. if (wallets.Count > 0)
  189. {
  190. foreach (var wallet in wallets)
  191. {
  192. var bc_id = Convert.ToInt32(wallet["blockchain_id"]);
  193. var wallet_id = Convert.ToString(wallet["uuid"]);
  194. if (bc_id == 0)
  195. eth_address = wallet_id;
  196. else
  197. eth_address1 = wallet_id;
  198. Console.WriteLine($"acc {Name} wallet0 {eth_address} wallet1 {eth_address1}");
  199. }
  200. }
  201. }
  202. public static async void InitializeAccounts()
  203. {
  204. Loaded = await MySQLConnector.Instance().SQLSelectASPUsers();
  205. Console.WriteLine("InitializeAccounts");
  206. foreach (var acc in Loaded.Values)
  207. {
  208. acc.FillAccount();
  209. }
  210. }
  211. public async void LoadRoles()
  212. {
  213. var rolesSQL = await MySQLConnector.Instance().SQLSelectComplex($"select * from account_roles where account_uuid ='{this.UUID}'");
  214. if (rolesSQL.Count > 0)
  215. {
  216. foreach (var role in rolesSQL)
  217. {
  218. var role_id = Convert.ToUInt32(role["role_id"]);
  219. AccRole = (Role) role_id;
  220. if (!Roles.Contains(AccRole))
  221. {
  222. //Console.WriteLine($"LoadRoles uuid {UUID} roleid {role_id} AccRole {AccRole}");
  223. AddRole(AccRole);
  224. }
  225. //functionId = Convert.ToInt32(role["id"]);
  226. //functionName = role["name_with_args"].ToString();
  227. //functionCompiledHeader = role["compiled_header"].ToString();
  228. //Console.WriteLine($"{ID} LoadFunction {functionId} name {functionName} header {functionCompiledHeader}");
  229. }
  230. }
  231. }
  232. //public async Task CreateEthAddress(string pass)
  233. //{
  234. // var addr = await Blockchain.GetMain().CreateBlockchainAccount(this, pass);
  235. //}
  236. public async Task<string> GetBalance()
  237. {
  238. var bc = await GetSelectedBlockChain();
  239. var res = await bc.GetBalance(this);
  240. //var balanceInt = ConvertBalance(res);
  241. //string balance = balanceInt.ToString();
  242. //Console.WriteLine($"GetBalance {balance}");
  243. return res;
  244. }
  245. }
  246. }