Browse Source

промежуточное

Rimmon 3 years ago
parent
commit
b3178c55ec
6 changed files with 122 additions and 82 deletions
  1. 1 1
      Models/AccountModel.cs
  2. 43 32
      Models/Blockchain.cs
  3. 1 0
      MySQLConnector.cs
  4. 19 15
      Pages/Blockchains.razor
  5. 26 34
      Post.cs
  6. 32 0
      Shared/MainLayout.razor

+ 1 - 1
Models/AccountModel.cs

@@ -12,7 +12,7 @@ namespace HyperCube.Models
     {
     {
         //public event EventHandler<int> RolesChanged;
         //public event EventHandler<int> RolesChanged;
         public Action<int> RolesChanged;
         public Action<int> RolesChanged;
-
+        public byte blockchain_selected;
         public List<Role> Roles = new();
         public List<Role> Roles = new();
 
 
         public void AddRole(Role role)
         public void AddRole(Role role)

+ 43 - 32
Models/Blockchain.cs

@@ -20,13 +20,12 @@ namespace HyperCube.Models
                 return "none";
                 return "none";
             }            
             }            
         }
         }
-        public static int selectedId;
         public static string URLdefault = "127.0.0.1";
         public static string URLdefault = "127.0.0.1";
         //public static int defaultPort = 8545;
         //public static int defaultPort = 8545;
         public static int defaultPort = 8666;
         public static int defaultPort = 8666;
         public static string defaultName = "Ethereum dev network";
         public static string defaultName = "Ethereum dev network";
 
 
-        public static bool newData = false;
+        public static bool blockChainsInitialized = false;
         public static Dictionary<int, Blockchain> loaded = new();
         public static Dictionary<int, Blockchain> loaded = new();
         public Dictionary<int, SmartContract> contracts = new();
         public Dictionary<int, SmartContract> contracts = new();
         public Dictionary<string, SmartContract> contractNames = new();
         public Dictionary<string, SmartContract> contractNames = new();
@@ -39,11 +38,14 @@ namespace HyperCube.Models
 
 
         public static void RegisterNetworks()
         public static void RegisterNetworks()
         {
         {
+            Console.WriteLine($"RegisterNetworks");
             var ethDev = new Blockchain(0, "Ethereum Dev Network", "127.0.0.1", 8545);            
             var ethDev = new Blockchain(0, "Ethereum Dev Network", "127.0.0.1", 8545);            
             var ethRinkeby = new Blockchain(1, "Ethereum Test Network", "127.0.0.1", 8666);
             var ethRinkeby = new Blockchain(1, "Ethereum Test Network", "127.0.0.1", 8666);
             ethDev.Initialize();
             ethDev.Initialize();
             ethRinkeby.Initialize();
             ethRinkeby.Initialize();
-            Console.WriteLine($"RegisterNetworks");
+
+
+            blockChainsInitialized = true;
         }
         }
 
 
         //public static Blockchain GetInstance()
         //public static Blockchain GetInstance()
@@ -98,17 +100,26 @@ namespace HyperCube.Models
 
 
         public static Blockchain GetMain()
         public static Blockchain GetMain()
         {
         {
-            if (loaded.Count > 0)
-                return loaded[selectedId];
+            try
+            {
+                if (loaded.Count > AccountModel.GetCurrent().blockchain_selected + 1)
+                    return loaded[AccountModel.GetCurrent().blockchain_selected];
+                else
+                    Console.WriteLine($"Error: blockchains loaded {loaded.Count}");                         
+            }
+            catch (Exception e)
+            {
+                Console.WriteLine("GetMain exception " + e.Message + ", stack trace:" + e.StackTrace);
+            }
             return null;
             return null;
         }
         }
 
 
         public async Task<string> Initialize()
         public async Task<string> Initialize()
-        {
-            string addr = await ListAccounts();
+        {            
             if (!loaded.ContainsKey(id))
             if (!loaded.ContainsKey(id))
-                loaded.Add(selectedId, this);
+                loaded.Add(AccountModel.GetCurrent().blockchain_selected, this);
             Console.WriteLine($"Initialize: {name }loaded blockchains " + loaded.Count);
             Console.WriteLine($"Initialize: {name }loaded blockchains " + loaded.Count);
+            string addr = await ListAccounts();            
 
 
             await LoadContracts();
             await LoadContracts();
             Console.WriteLine("LoadContracts count " + contracts.Count);
             Console.WriteLine("LoadContracts count " + contracts.Count);
@@ -116,6 +127,30 @@ namespace HyperCube.Models
             return $"{name} {url}:{port}";
             return $"{name} {url}:{port}";
         }
         }
 
 
+        public async Task<string> ListAccounts()
+        {
+            Console.WriteLine("ListAccounts");
+            string answer = "no";
+            //{ "jsonrpc":"2.0","method":"eth_getCode","params":["0x938cae6f6c21ed9d55196e96ef880f562e530553", "latest" ],"id":1}
+            //string req = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getCode\",\"params\":[\"0x874c6a51e62680d4594cd2555ed8fa47b63ed253\",\"latest\"],\"id\":1}";
+            string req = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_accounts\", \"params\":[],\"id\":1}";
+
+            answer = await Post.PostRequestAsync(req);
+
+            //string json = Encoding.UTF8.GetString(bytedata, 1, bytedata.Length - 1);
+            //try
+            //{
+            Console.WriteLine("Json string " + answer);
+            dynamic jsonDe = JsonConvert.DeserializeObject(answer);
+            address = jsonDe.result[0];
+            var query = $"update blockchains set address_main='{address}' where id=1";
+            Console.WriteLine("query " + query);
+            MySQLConnector.Instance().SQLInsert(query);
+            Console.WriteLine("Json addr " + jsonDe.result[0]);
+            var methodName = (string)jsonDe.name;            
+            return address;
+        }
+
         public async Task LoadContracts()
         public async Task LoadContracts()
         {
         {
             MySQLConnector dbCon = MySQLConnector.Instance();
             MySQLConnector dbCon = MySQLConnector.Instance();
@@ -245,30 +280,6 @@ namespace HyperCube.Models
             return null;
             return null;
         }
         }
 
 
-        public async Task<string> ListAccounts()
-        {
-            string answer = "no";
-            //{ "jsonrpc":"2.0","method":"eth_getCode","params":["0x938cae6f6c21ed9d55196e96ef880f562e530553", "latest" ],"id":1}
-            //string req = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_getCode\",\"params\":[\"0x874c6a51e62680d4594cd2555ed8fa47b63ed253\",\"latest\"],\"id\":1}";
-            string req = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_accounts\", \"params\":[],\"id\":1}";
-
-            answer = await Post.PostRequestAsync(req);
-
-            //string json = Encoding.UTF8.GetString(bytedata, 1, bytedata.Length - 1);
-            //try
-            //{
-            Console.WriteLine("Json string " + answer);
-            dynamic jsonDe = JsonConvert.DeserializeObject(answer);
-            address = jsonDe.result[0];
-            var query = $"update blockchains set address_main='{address}' where id=1";
-            Console.WriteLine("query " + query);
-            MySQLConnector.Instance().SQLInsert(query);
-            Console.WriteLine("Json addr " + jsonDe.result[0]);
-            var methodName = (string)jsonDe.name;
-            newData = true;
-            return address;
-        }
-
         public string GetAddress()
         public string GetAddress()
         {
         {
             //this.address = address;
             //this.address = address;

+ 1 - 0
MySQLConnector.cs

@@ -81,6 +81,7 @@ namespace HyperCube
                         newacc.eth_address = rdr.GetString(15);
                         newacc.eth_address = rdr.GetString(15);
                     if (!accs.ContainsKey(newacc.UUID))
                     if (!accs.ContainsKey(newacc.UUID))
                         accs.Add(newacc.UUID, newacc);
                         accs.Add(newacc.UUID, newacc);
+                    newacc.blockchain_selected = rdr.GetByte(17);
                 }
                 }
 
 
                 await Task.WhenAll();
                 await Task.WhenAll();

+ 19 - 15
Pages/Blockchains.razor

@@ -4,9 +4,13 @@
 
 
 @namespace HyperCube.Models
 @namespace HyperCube.Models
 
 
+@using Microsoft.AspNetCore.Identity;
+@inject AuthenticationStateProvider AuthenticationStateProvider
+@inject UserManager<IdentityUser> UserManager
+
 @attribute [Authorize]
 @attribute [Authorize]
 
 
-    <h1>Blockchain</h1>
+<h1>Blockchain</h1>
 
 
 @code
 @code
 {
 {
@@ -42,22 +46,22 @@
                 ctrSelected = ctr;
                 ctrSelected = ctr;
             }
             }
         }
         }
-    }
+    }    
 }
 }
 
 
-@if (Blockchain.newData)
+@if (Blockchain.blockChainsInitialized)
 {
 {
-    @foreach (var bc in Blockchain.loaded.Values)
-    {
-        <p>
-            <b>
-                @bc.url:@bc.port
-            </b>, address: @bc.address
-        </p>
-    }
-}
-<p>@Blockchain.selectedId</p>
-<select @bind="Blockchain.selectedId" style="color:green">
+        @foreach (var bc in Blockchain.loaded.Values)
+        {
+            <p>
+                <b>
+                    @bc.url:@bc.port
+                </b>, address: @bc.address
+            </p>
+        }
+
+<p>@AccountModel.GetCurrent().blockchain_selected</p>
+<select @bind="AccountModel.GetCurrent().blockchain_selected" style="color:green">
     <option value=0>Ethereum Dev Network</option>
     <option value=0>Ethereum Dev Network</option>
     <option value=1>Ethereum Test Network</option>
     <option value=1>Ethereum Test Network</option>
 </select>
 </select>
@@ -134,7 +138,7 @@
 </p>
 </p>
 
 
 <p>Result: @result, gas: @gas</p>
 <p>Result: @result, gas: @gas</p>
-
+}
 @code
 @code
     {
     {
     private Random r = new Random();
     private Random r = new Random();

+ 26 - 34
Post.cs

@@ -13,46 +13,38 @@ namespace HyperCube
     {
     {
         public static async Task<string> PostRequestAsync(string json)
         public static async Task<string> PostRequestAsync(string json)
         {
         {
-            Console.WriteLine($"json req {json} len {json.Length}");
-            string url = $"http://{Blockchain.URLdefault}:{Blockchain.defaultPort}";
-            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
+            var main = Blockchain.GetMain();
+            if (main != null)
+            {
+                Console.WriteLine($"json req {json} len {json.Length}");
+                string url = $"http://{main.url}:{main.port}";
+                var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
 
 
-            httpWebRequest.ContentType = "application/json";
-            httpWebRequest.Method = "POST";
-            httpWebRequest.Accept = "application/json";
-            
+                httpWebRequest.ContentType = "application/json";
+                httpWebRequest.Method = "POST";
+                httpWebRequest.Accept = "application/json";
+                Console.WriteLine($"PostRequestAsync {url}");
 
 
-            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
-            {
-                streamWriter.Write(json);
-            }
+                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
+                {
+                    streamWriter.Write(json);
+                }
 
 
-            HttpWebResponse response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
-            using (Stream stream = response.GetResponseStream())
-            {
-                using (StreamReader reader = new StreamReader(stream))
+                HttpWebResponse response = (HttpWebResponse)await httpWebRequest.GetResponseAsync();
+                using (Stream stream = response.GetResponseStream())
                 {
                 {
-                    //Console.WriteLine(reader.ReadToEnd());
+                    using (StreamReader reader = new StreamReader(stream))
+                    {
+                        //Console.WriteLine(reader.ReadToEnd());
 
 
-                    var result = reader.ReadLine();
-                    Console.WriteLine($"json result {result} len {result.Length}");
-                    response.Close();
-                    return result;
-                }
+                        var result = reader.ReadLine();
+                        Console.WriteLine($"json result {result} len {result.Length}");
+                        response.Close();
+                        return result;
+                    }
+                }                
             }
             }
-            
-            //using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
-            //{
-            //    streamWriter.Write(json);
-            //}
-
-            //var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
-            //using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
-            //{
-            //    var result = streamReader.ReadToEnd();
-            //    Console.WriteLine($"json result {result} len {result.Length}");
-            //    answer = result;
-            //}
+            return "null";
         }
         }
     }
     }
 }
 }

+ 32 - 0
Shared/MainLayout.razor

@@ -2,6 +2,10 @@
 @using HyperCube.Models;
 @using HyperCube.Models;
 @using Console = HyperCube.Utils.AdvConsole;
 @using Console = HyperCube.Utils.AdvConsole;
 
 
+@using Microsoft.AspNetCore.Identity;
+@inject AuthenticationStateProvider AuthenticationStateProvider
+@inject UserManager<IdentityUser> UserManager
+
 <AuthorizeView>
 <AuthorizeView>
     <Authorized>
     <Authorized>
         <div class="fullpage-wrapper">
         <div class="fullpage-wrapper">
@@ -41,7 +45,9 @@
     {
     {
         try
         try
         {
         {
+            AccountModel.Current = await GetCurrentAcc();
             //await Blockchain.GetInstance().Initialize();
             //await Blockchain.GetInstance().Initialize();
+            Console.WriteLine("RegisterNetworks");
             Blockchain.RegisterNetworks();
             Blockchain.RegisterNetworks();
             if (Blockchain.Connected != "" && Blockchain.Connected != "none")
             if (Blockchain.Connected != "" && Blockchain.Connected != "none")
             {
             {
@@ -59,4 +65,30 @@
             Console.WriteLine(e.Message + ", stack trace:" + e.StackTrace);
             Console.WriteLine(e.Message + ", stack trace:" + e.StackTrace);
         }
         }
     }
     }
+
+    private async Task<AccountModel> GetCurrentAcc()
+    {
+        AccountModel account = new();
+
+        var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
+        var user = authState.User;
+
+        if (user.Identity.IsAuthenticated)
+        {
+            var currentUser = await UserManager.GetUserAsync(user);
+            account.UUID = currentUser.Id;
+            //account.Name = currentUser.UserName;
+            //account.Email = currentUser.Email;
+
+            var acc = AccountModel.Find(account.UUID);
+            if (acc != null)
+                account = acc;
+            ///tmp
+
+            //account.AccRole = Role.User;
+            return account;
+        }
+
+        return null;
+    }
 }
 }