AccountModel.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. namespace HyperCube.Models
  10. {
  11. public enum Role { Admin = 1, Verifier, Initiator, Requester }
  12. public class AccountModel: ComponentBase
  13. {
  14. [Inject]
  15. AppData AppData { get; set; }
  16. [Inject]
  17. ProtectedSessionStorage storage { get; set; }
  18. //public event EventHandler<int> RolesChanged;
  19. public Action<int> RolesChanged;
  20. public byte bsel = 0;
  21. public byte blockchain_selected
  22. {
  23. get { return bsel; }
  24. set {
  25. bsel = value;
  26. bcselupdate();
  27. }
  28. }
  29. public List<Role> Roles = new();
  30. public void AddRole(Role role)
  31. {
  32. Roles.Add(role);
  33. RolesChanged?.Invoke(Roles.Count);
  34. Console.WriteLine($"role added, count: {Roles.Count}");
  35. }
  36. async Task bcselupdate()
  37. {
  38. await MySQLConnector.Instance().SQLInsert($"update aspnetusers set blockchain_selected = {blockchain_selected} where Id='{UUID}'");
  39. }
  40. public void RemoveRole(Role role)
  41. {
  42. Roles.Remove(role);
  43. RolesChanged?.Invoke(Roles.Count);
  44. Console.WriteLine($"role removed, сount: {Roles.Count}");
  45. }
  46. public static Dictionary<string, AccountModel> Loaded = new();
  47. string eth_address { get; set; }
  48. string eth_address1 { get; set; }
  49. /// <summary>
  50. /// ASP Identity ID
  51. /// </summary>
  52. public string UUID { get; set; }
  53. public string Name { get; set; }
  54. public string Email { get; set; }
  55. public string PWDHash { get; set; }
  56. public Role AccRole { get; set; }
  57. public string GetActualAddress(Blockchain bc)
  58. {
  59. string addr;
  60. if (bc.port == 8666)
  61. addr = eth_address1;
  62. else
  63. addr = eth_address;
  64. return addr;
  65. }
  66. public async Task<string> GetOrCreateActualAddress(Blockchain bc)
  67. {
  68. string addr;
  69. if (bc.port == 8666)
  70. addr = eth_address1;
  71. else
  72. addr = eth_address;
  73. if (addr == null || addr == "")
  74. addr = await bc.CreateBlockchainAccount(this, PWDHash);
  75. return addr;
  76. }
  77. public void SetActualAddress(string value, Blockchain bc)
  78. {
  79. if (bc.port == 8666)
  80. eth_address1 = value;
  81. else
  82. eth_address = value;
  83. }
  84. public AccountModel(){}
  85. public async Task<Blockchain> GetSelectedBlockChain()
  86. {
  87. //Console.WriteLine($"AppData null { AppData == null}");
  88. //var res = await storage.GetAsync<AccountModel>("acc");
  89. //var accountCurrent = res.Value;
  90. //var accountCurrent = AppData.CurrentAccount;
  91. //Console.WriteLine($"AppData acc null { accountCurrent == null}");
  92. if (Blockchain.loaded.Count > blockchain_selected)
  93. {
  94. var bc = Blockchain.loaded[blockchain_selected];
  95. Console.WriteLine($"GetMain blockchain_selected {blockchain_selected} " + bc.address);
  96. return bc;
  97. }
  98. else
  99. {
  100. Console.WriteLine($"Error: blockchains loaded {Blockchain.loaded.Count}");
  101. blockchain_selected = (byte)(Blockchain.loaded.Count - 1);
  102. }
  103. return null;
  104. }
  105. public static AccountModel FindByMail(string mail)
  106. {
  107. var selectedUsers = from user in Loaded.Values
  108. where user.Email == mail
  109. select user;
  110. return selectedUsers.First();
  111. }
  112. public static AccountModel Find(string uuid)
  113. {
  114. if (uuid == null)
  115. return null;
  116. if (Loaded.ContainsKey(uuid))
  117. return Loaded[uuid];
  118. else
  119. return null;
  120. }
  121. public static BigInteger ConvertBalance(string hex)
  122. {
  123. if (hex != null && hex.Length > 0)
  124. {
  125. Console.WriteLine($"ConvertBalance {hex}");
  126. string newHex = hex.Remove(0, 2);
  127. var balance = System.Numerics.BigInteger.Parse("0" + newHex, System.Globalization.NumberStyles.HexNumber);
  128. Console.WriteLine($"ConvertBalance {hex} {balance}");
  129. return balance;
  130. }
  131. return 0;
  132. }
  133. public static async void InitializeAccounts()
  134. {
  135. Loaded = await MySQLConnector.Instance().SQLSelectASPUsers();
  136. Console.WriteLine("InitializeAccounts");
  137. foreach (var acc in Loaded)
  138. {
  139. acc.Value.LoadRoles();
  140. var wallets = await MySQLConnector.Instance().SQLSelectComplex($"select * from account_wallets where account_uuid='{acc.Value.UUID}'");
  141. if (wallets.Count > 0)
  142. {
  143. foreach (var wallet in wallets)
  144. {
  145. var bc_id = Convert.ToInt32(wallet["blockchain_id"]);
  146. var wallet_id = Convert.ToString(wallet["uuid"]);
  147. if (bc_id == 0)
  148. acc.Value.eth_address = wallet_id;
  149. else
  150. acc.Value.eth_address1 = wallet_id;
  151. Console.WriteLine($"acc {acc.Value.Name} wallet0 {acc.Value.eth_address} wallet1 {acc.Value.eth_address1}");
  152. }
  153. }
  154. }
  155. }
  156. public async void LoadRoles()
  157. {
  158. var rolesSQL = await MySQLConnector.Instance().SQLSelectComplex($"select * from account_roles where account_uuid ='{this.UUID}'");
  159. if (rolesSQL.Count > 0)
  160. {
  161. foreach (var role in rolesSQL)
  162. {
  163. var role_id = Convert.ToUInt32(role["role_id"]);
  164. AccRole = (Role) role_id;
  165. if (!Roles.Contains(AccRole))
  166. {
  167. Console.WriteLine($"LoadRoles uuid {UUID} roleid {role_id} AccRole {AccRole}");
  168. AddRole(AccRole);
  169. }
  170. //functionId = Convert.ToInt32(role["id"]);
  171. //functionName = role["name_with_args"].ToString();
  172. //functionCompiledHeader = role["compiled_header"].ToString();
  173. //Console.WriteLine($"{ID} LoadFunction {functionId} name {functionName} header {functionCompiledHeader}");
  174. }
  175. }
  176. }
  177. //public async Task CreateEthAddress(string pass)
  178. //{
  179. // var addr = await Blockchain.GetMain().CreateBlockchainAccount(this, pass);
  180. //}
  181. public async Task<string> GetBalance()
  182. {
  183. var bc = await GetSelectedBlockChain();
  184. var res = await bc.GetBalance(this);
  185. //var balanceInt = ConvertBalance(res);
  186. //string balance = balanceInt.ToString();
  187. //Console.WriteLine($"GetBalance {balance}");
  188. return res;
  189. }
  190. }
  191. }