AccountModel.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 Action<int> RolesChanged;
  47. public byte bsel = 0;
  48. public byte blockchain_selected
  49. {
  50. get { return bsel; }
  51. set {
  52. bsel = value;
  53. bcselupdate();
  54. }
  55. }
  56. public List<Role> Roles = new();
  57. public void AddRole(Role role)
  58. {
  59. Roles.Add(role);
  60. RolesChanged?.Invoke(Roles.Count);
  61. //Console.WriteLine($"role added, count: {Roles.Count}");
  62. }
  63. public void RemoveRole(Role role)
  64. {
  65. Roles.Remove(role);
  66. RolesChanged?.Invoke(Roles.Count);
  67. //Console.WriteLine($"role removed, сount: {Roles.Count}");
  68. }
  69. async Task bcselupdate()
  70. {
  71. await MySQLConnector.Instance().SQLInsert($"update aspnetusers set blockchain_selected = {blockchain_selected} where Id='{UUID}'");
  72. }
  73. public static Dictionary<string, AccountModel> Loaded = new();
  74. string eth_address { get; set; }
  75. string eth_address1 { get; set; }
  76. /// <summary>
  77. /// ASP Identity ID
  78. /// </summary>
  79. public string UUID { get; set; }
  80. public string Name { get; set; }
  81. public string Email { get; set; }
  82. public string PWDHash { get; set; }
  83. public Role AccRole { get; set; }
  84. public List<string> GetRoleDisplayName()
  85. {
  86. List<string> _rolesDisplayName = new();
  87. foreach (Role role in Roles)
  88. {
  89. _rolesDisplayName.Add(
  90. role.GetType()
  91. .GetMember(role.ToString())
  92. .First()
  93. .GetCustomAttribute<DisplayAttribute>()
  94. .GetName()
  95. );
  96. }
  97. return _rolesDisplayName;
  98. }
  99. public string GetActualAddress(Blockchain bc)
  100. {
  101. string addr;
  102. if (bc.port == 8666)
  103. addr = eth_address1;
  104. else
  105. addr = eth_address;
  106. return addr;
  107. }
  108. public async Task<string> GetOrCreateActualAddress(Blockchain bc)
  109. {
  110. string addr;
  111. if (bc.port == 8666)
  112. addr = eth_address1;
  113. else
  114. addr = eth_address;
  115. Console.WriteLine($"got address1 {eth_address1}");
  116. if (addr == null || addr == "")
  117. addr = await bc.CreateBlockchainAccount(this, PWDHash);
  118. return addr;
  119. }
  120. public void SetActualAddress(string value, Blockchain bc)
  121. {
  122. if (bc.port == 8666)
  123. eth_address1 = value;
  124. else
  125. eth_address = value;
  126. }
  127. public AccountModel(){}
  128. public async Task<Blockchain> GetSelectedBlockChain()
  129. {
  130. //Console.WriteLine($"AppData null { AppData == null}");
  131. //var res = await storage.GetAsync<AccountModel>("acc");
  132. //var accountCurrent = res.Value;
  133. //var accountCurrent = AppData.CurrentAccount;
  134. //Console.WriteLine($"AppData acc null { accountCurrent == null}");
  135. if (Blockchain.loaded.Count > blockchain_selected)
  136. {
  137. var bc = Blockchain.loaded[blockchain_selected];
  138. Console.WriteLine($"GetMain blockchain_selected {blockchain_selected} " + bc.address);
  139. return bc;
  140. }
  141. else
  142. {
  143. Console.WriteLine($"Error: blockchains loaded {Blockchain.loaded.Count}");
  144. blockchain_selected = (byte)(Blockchain.loaded.Count - 1);
  145. }
  146. return null;
  147. }
  148. public static AccountModel FindByMail(string mail)
  149. {
  150. var selectedUsers = from user in Loaded.Values
  151. where user.Email == mail
  152. select user;
  153. return selectedUsers.First();
  154. }
  155. public static AccountModel Find(string uuid)
  156. {
  157. if (uuid == null)
  158. return null;
  159. if (Loaded.ContainsKey(uuid))
  160. return Loaded[uuid];
  161. else
  162. return null;
  163. }
  164. public static BigInteger ConvertBalance(string hex)
  165. {
  166. if (hex != null && hex.Length > 0)
  167. {
  168. Console.WriteLine($"ConvertBalance {hex}");
  169. string newHex = hex.Remove(0, 2);
  170. var balance = System.Numerics.BigInteger.Parse("0" + newHex, System.Globalization.NumberStyles.HexNumber);
  171. Console.WriteLine($"ConvertBalance {hex} {balance}");
  172. return balance;
  173. }
  174. return 0;
  175. }
  176. public static async void InitializeAccounts()
  177. {
  178. Loaded = await MySQLConnector.Instance().SQLSelectASPUsers();
  179. Console.WriteLine("InitializeAccounts");
  180. foreach (var acc in Loaded)
  181. {
  182. acc.Value.LoadRoles();
  183. var wallets = await MySQLConnector.Instance().SQLSelectComplex($"select * from account_wallets where account_uuid='{acc.Value.UUID}'");
  184. if (wallets.Count > 0)
  185. {
  186. foreach (var wallet in wallets)
  187. {
  188. var bc_id = Convert.ToInt32(wallet["blockchain_id"]);
  189. var wallet_id = Convert.ToString(wallet["uuid"]);
  190. if (bc_id == 0)
  191. acc.Value.eth_address = wallet_id;
  192. else
  193. acc.Value.eth_address1 = wallet_id;
  194. Console.WriteLine($"acc {acc.Value.Name} wallet0 {acc.Value.eth_address} wallet1 {acc.Value.eth_address1}");
  195. }
  196. }
  197. }
  198. }
  199. public async void LoadRoles()
  200. {
  201. var rolesSQL = await MySQLConnector.Instance().SQLSelectComplex($"select * from account_roles where account_uuid ='{this.UUID}'");
  202. if (rolesSQL.Count > 0)
  203. {
  204. foreach (var role in rolesSQL)
  205. {
  206. var role_id = Convert.ToUInt32(role["role_id"]);
  207. AccRole = (Role) role_id;
  208. if (!Roles.Contains(AccRole))
  209. {
  210. //Console.WriteLine($"LoadRoles uuid {UUID} roleid {role_id} AccRole {AccRole}");
  211. AddRole(AccRole);
  212. }
  213. //functionId = Convert.ToInt32(role["id"]);
  214. //functionName = role["name_with_args"].ToString();
  215. //functionCompiledHeader = role["compiled_header"].ToString();
  216. //Console.WriteLine($"{ID} LoadFunction {functionId} name {functionName} header {functionCompiledHeader}");
  217. }
  218. }
  219. }
  220. //public async Task CreateEthAddress(string pass)
  221. //{
  222. // var addr = await Blockchain.GetMain().CreateBlockchainAccount(this, pass);
  223. //}
  224. public async Task<string> GetBalance()
  225. {
  226. var bc = await GetSelectedBlockChain();
  227. var res = await bc.GetBalance(this);
  228. //var balanceInt = ConvertBalance(res);
  229. //string balance = balanceInt.ToString();
  230. //Console.WriteLine($"GetBalance {balance}");
  231. return res;
  232. }
  233. }
  234. }