AccountModel.cs 6.5 KB

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