AccountModel.cs 6.7 KB

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