AccountModel.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 List<Role> Roles = new();
  14. public void AddRole(Role role)
  15. {
  16. Roles.Add(role);
  17. RolesChanged?.Invoke(Roles.Count);
  18. Console.WriteLine($"role added, Count{Roles.Count}");
  19. }
  20. public void RemoveRole(Role role)
  21. {
  22. Roles.Remove(role);
  23. RolesChanged?.Invoke(Roles.Count);
  24. Console.WriteLine($"role removed, Count{Roles.Count}");
  25. }
  26. public static Dictionary<string, AccountModel> Loaded = new();
  27. public static AccountModel Current;
  28. public string eth_address { get; set; }
  29. /// <summary>
  30. /// ASP Identity ID
  31. /// </summary>
  32. public string UUID { get; set; }
  33. public string Name { get; set; }
  34. public string Email { get; set; }
  35. public Role AccRole { get; set; }
  36. public AccountModel()
  37. {
  38. }
  39. public static AccountModel GetCurrent()
  40. {
  41. return Current;
  42. }
  43. public static AccountModel Find(string uuid)
  44. {
  45. if (uuid == null)
  46. return null;
  47. if (Loaded.ContainsKey(uuid))
  48. return Loaded[uuid];
  49. else
  50. return null;
  51. }
  52. public static BigInteger ConvertBalance(string hex)
  53. {
  54. string newHex = hex.Remove(0, 2);
  55. var balance = System.Numerics.BigInteger.Parse("0"+ newHex, System.Globalization.NumberStyles.HexNumber );
  56. Console.WriteLine($"ConvertBalance {hex} {balance}");
  57. return balance;
  58. }
  59. public static async void InitializeAccounts()
  60. {
  61. Loaded = await MySQLConnector.Instance().SQLSelectASPUsers();
  62. Console.WriteLine("InitializeAccounts");
  63. foreach (var acc in Loaded)
  64. {
  65. acc.Value.LoadRoles();
  66. }
  67. }
  68. public async void LoadRoles()
  69. {
  70. var rolesSQL = await MySQLConnector.Instance().SQLSelectComplex($"select * from account_roles where account_uuid ='{this.UUID}'");
  71. if (rolesSQL.Count > 0)
  72. {
  73. foreach (var role in rolesSQL)
  74. {
  75. var role_id = Convert.ToUInt32(role["role_id"]);
  76. AccRole = (Role) role_id;
  77. Console.WriteLine($"LoadRoles uuid {UUID} roleid {role_id} AccRole {AccRole}");
  78. if (!Roles.Contains(AccRole))
  79. AddRole(AccRole);
  80. else
  81. RemoveRole(AccRole);
  82. //functionId = Convert.ToInt32(role["id"]);
  83. //functionName = role["name_with_args"].ToString();
  84. //functionCompiledHeader = role["compiled_header"].ToString();
  85. //Console.WriteLine($"{ID} LoadFunction {functionId} name {functionName} header {functionCompiledHeader}");
  86. }
  87. }
  88. }
  89. public async Task GetEthAddress()
  90. {
  91. var addr = await Blockchain.GetMain().CreateBlockchainAccount(this);
  92. }
  93. public async Task<string> GetBalance()
  94. {
  95. var res = await Blockchain.GetMain().GetBalance(eth_address);
  96. var balanceInt = ConvertBalance(res);
  97. string balance = balanceInt.ToString();
  98. //Console.WriteLine($"GetBalance {balance}");
  99. return balance;
  100. }
  101. }
  102. }