123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- using System;
- using System.Numerics;
- using System.Threading.Tasks;
- using System.Collections.Generic;
- using Console = HyperCube.Utils.AdvConsole;
- namespace HyperCube.Models
- {
- public enum Role { Admin = 1, Verifier, Initiator, Requester }
- public class AccountModel
- {
- //public event EventHandler<int> RolesChanged;
- public Action<int> RolesChanged;
- public List<Role> Roles = new();
- public void AddRole(Role role)
- {
- Roles.Add(role);
- RolesChanged?.Invoke(Roles.Count);
- Console.WriteLine($"role added, Count{Roles.Count}");
- }
- public void RemoveRole(Role role)
- {
- Roles.Remove(role);
- RolesChanged?.Invoke(Roles.Count);
- Console.WriteLine($"role removed, Count{Roles.Count}");
- }
- public static Dictionary<string, AccountModel> Loaded = new();
- public static AccountModel Current;
- public string eth_address { get; set; }
- /// <summary>
- /// ASP Identity ID
- /// </summary>
- public string UUID { get; set; }
- public string Name { get; set; }
- public string Email { get; set; }
- public Role AccRole { get; set; }
- public AccountModel()
- {
- }
- public static AccountModel GetCurrent()
- {
- return Current;
- }
- public static AccountModel Find(string uuid)
- {
- if (uuid == null)
- return null;
- if (Loaded.ContainsKey(uuid))
- return Loaded[uuid];
- else
- return null;
- }
- public static BigInteger ConvertBalance(string hex)
- {
- string newHex = hex.Remove(0, 2);
- var balance = System.Numerics.BigInteger.Parse("0"+ newHex, System.Globalization.NumberStyles.HexNumber );
- Console.WriteLine($"ConvertBalance {hex} {balance}");
- return balance;
- }
- public static async void InitializeAccounts()
- {
- Loaded = await MySQLConnector.Instance().SQLSelectASPUsers();
- Console.WriteLine("InitializeAccounts");
- foreach (var acc in Loaded)
- {
- acc.Value.LoadRoles();
- }
- }
- public async void LoadRoles()
- {
- var rolesSQL = await MySQLConnector.Instance().SQLSelectComplex($"select * from account_roles where account_uuid ='{this.UUID}'");
- if (rolesSQL.Count > 0)
- {
- foreach (var role in rolesSQL)
- {
- var role_id = Convert.ToUInt32(role["role_id"]);
- AccRole = (Role) role_id;
- Console.WriteLine($"LoadRoles uuid {UUID} roleid {role_id} AccRole {AccRole}");
- if (!Roles.Contains(AccRole))
- AddRole(AccRole);
- else
- RemoveRole(AccRole);
- //functionId = Convert.ToInt32(role["id"]);
- //functionName = role["name_with_args"].ToString();
- //functionCompiledHeader = role["compiled_header"].ToString();
- //Console.WriteLine($"{ID} LoadFunction {functionId} name {functionName} header {functionCompiledHeader}");
- }
- }
- }
- public async Task GetEthAddress()
- {
- var addr = await Blockchain.GetMain().CreateBlockchainAccount(this);
- }
- public async Task<string> GetBalance()
- {
- var res = await Blockchain.GetMain().GetBalance(eth_address);
- var balanceInt = ConvertBalance(res);
- string balance = balanceInt.ToString();
- //Console.WriteLine($"GetBalance {balance}");
- return balance;
- }
- }
- }
|