AccountModel.cs 8.6 KB

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