AccountModel.cs 6.2 KB

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