Ver código fonte

работаю с кошельками

Rimmon 4 anos atrás
pai
commit
1405cb1562
3 arquivos alterados com 94 adições e 50 exclusões
  1. 48 37
      Models/Account.cs
  2. 17 7
      Models/Blockchain.cs
  3. 29 6
      Pages/Blockchains.razor

+ 48 - 37
Models/Account.cs

@@ -1,55 +1,66 @@
-using System;
-using System.Threading.Tasks;
-using System.Collections.Generic;
-
-namespace HyperCube.Models
-{
-    public enum Role { Admin = 0, Verifier, User }
-
-    public class Account
-    {
-        public static Dictionary<string, Account> loaded = new();
-        public static Account current;
-        public string eth_address { get; set; }
-
-        /// <summary>
-        /// Internal ID
-        /// </summary>
-        public uint ID { 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 Account()
-        {
+using System;
+using System.Numerics;
+using System.Threading.Tasks;
+using System.Collections.Generic;
+
+namespace HyperCube.Models
+{
+    public enum Role { Admin = 0, Verifier, User }
+
+    public class Account
+    {
+        public static Dictionary<string, Account> loaded = new();
+        public static Account current;
+        public string eth_address { get; set; }
+
+        /// <summary>
+        /// Internal ID
+        /// </summary>
+        public uint ID { 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 Account()
+        {
         }
 
         public static Account GetCurrent()
         {
             return current;
-        }
-
+        }
+
         public static Account Find(string email)
         {
+            if (email == null)
+                return null;
             if (loaded.ContainsKey(email))
                 return loaded[email];
             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");
         }
 
-        public async static Task GetEthAddress()
+        public async Task GetEthAddress()
         {
-            var addr = await Blockchain.GetMain().CreateBlockchainAccount();
-        }
-    }
-}
+            var addr = await Blockchain.GetMain().CreateBlockchainAccount(this);
+        }
+    }
+}

+ 17 - 7
Models/Blockchain.cs

@@ -28,10 +28,15 @@ namespace HyperCube.Models
             this.port = port;
         }
 
-        public async Task<string> CreateBlockchainAccount()
+        public async Task<string> CreateBlockchainAccount(Account account)
         {
-            var res = await GetMain().RunFunctionWrite("personal_newAccount", "password");
-            Console.WriteLine($"CreateBlockchainAccount {res}");
+            var res = await GetMain().RunFunction("personal_newAccount", "\"password\"");
+
+            Console.WriteLine($"CreateBlockchainAccount {account}: {res}");
+            var query = $"update aspnetusers set eth_address='{res}' where UserName='{account.Name}'";
+            account.eth_address = res;
+            Console.WriteLine($"CreateBlockchainAccount query {query}");
+            MySQLConnector.Instance().SQLInsert(query);
             return res;
         }
 
@@ -79,19 +84,24 @@ namespace HyperCube.Models
             return answer;
         }
 
-        public async Task<string> RunFunctionWrite(string name, string parms)
+        public async Task<string> RunFunction(string name, string parms)
         {
-            Console.WriteLine($"RunFunctionWrite {name} params {parms}");
-            var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"{name}\",\"params\":[\"{parms}\"], \"id\":1}}";
+            Console.WriteLine($"RunFunction {name} params {parms}");
+            var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"{name}\",\"params\":[{parms}], \"id\":1}}";
 
             var answer = await Post.PostRequestAsync(req);
             dynamic jsonDe = JsonConvert.DeserializeObject(answer);
 
-            Console.WriteLine("RunFunctionWrite result " + jsonDe.result);
+            Console.WriteLine("RunFunction result " + jsonDe.result);
             return jsonDe.result;
             //return "test answer";
         }
 
+        public async Task<string> GetBalance(string address)
+        {
+            var ret = await RunFunction("eth_getBalance", $"\"{address}\",\"latest\"");
+            return ret;
+        }
 
         public async void RunContractRead()
         {

+ 29 - 6
Pages/Blockchains.razor

@@ -9,10 +9,11 @@
     string result = "";
     string newcode = "";
     string newname = "";
+    string balance = "";
     SmartContract ctrSelected = new SmartContract();
     int ct;
-
-    string listaccounts;
+    //ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff fff7
+    string accountSelected;
     int contrtest
     {
         get
@@ -95,7 +96,7 @@
     </p>
 
 <p>
-    <select @bind="listaccounts">
+    <select @bind="accountSelected">
         <option value="0">[Select Account]</option>
         @if (Blockchain.loaded.Count > 0)
         {
@@ -107,7 +108,8 @@
             }
         }
     </select>
-    <button @onclick="CreateBlockchainAccount">CreateBlockchainAccount for @listaccounts</button>
+    <button @onclick="CreateBlockchainAccount">CreateBlockchainAccount for @accountSelected</button>
+    <button @onclick="GetBalance">GetBalance</button> Balance: @balance
 </p>
 
 
@@ -125,10 +127,31 @@
     string contractAddress = "";
     int focus = 0;
 
+    private async Task GetBalance()
+    {
+        var addr = Blockchain.GetMain().address;
+        if (accountSelected != "")
+        {
+            var acc = Account.Find(accountSelected);
+            if (acc != null)
+            {
+                addr = acc.eth_address;
+            }
+        }
+
+        var res = await Blockchain.GetMain().GetBalance(addr);
+        var balanceInt = Account.ConvertBalance(res);
+        balance = balanceInt.ToString();
+        //Console.WriteLine($"GetBalance {balance}");
+    }
+
     private async Task CreateBlockchainAccount()
     {
-        var res = await Blockchain.GetMain().CreateBlockchainAccount();
-        Console.WriteLine($"CreateBlockchainAccount {res}");
+        Account account = Account.Find(accountSelected);
+        if (account != null)
+        {
+            var res = await Blockchain.GetMain().CreateBlockchainAccount(account);
+        }
     }