AccountModel.cs 3.2 KB

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