Browse Source

фиксы блокчейна

Rimmon 4 years ago
parent
commit
ac4a650f4e
4 changed files with 68 additions and 29 deletions
  1. 3 2
      Models/AccountModel.cs
  2. 53 17
      Models/Blockchain.cs
  3. 9 6
      Pages/Blockchains.razor
  4. 3 4
      Post.cs

+ 3 - 2
Models/AccountModel.cs

@@ -61,6 +61,7 @@ namespace HyperCube.Models
 
         public static BigInteger ConvertBalance(string hex)
         {
+            Console.WriteLine($"ConvertBalance {hex}");
             string newHex = hex.Remove(0, 2);
             var balance = System.Numerics.BigInteger.Parse("0"+ newHex, System.Globalization.NumberStyles.HexNumber );
             Console.WriteLine($"ConvertBalance {hex} {balance}");
@@ -100,9 +101,9 @@ namespace HyperCube.Models
             }            
         }
 
-        public async Task GetEthAddress()
+        public async Task CreateEthAddress(string pass)
         {
-            var addr = await Blockchain.GetMain().CreateBlockchainAccount(this);
+            var addr = await Blockchain.GetMain().CreateBlockchainAccount(this, pass);
         }
 
         public async Task<string> GetBalance()

+ 53 - 17
Models/Blockchain.cs

@@ -10,6 +10,13 @@ namespace HyperCube.Models
 {
     public class Blockchain
     {
+        public static Blockchain Dev
+        {
+            get {
+                return loaded[0];
+            }
+            set { }
+        }
         public static Dictionary<int, Blockchain> supported;
         public static string Connected
         {
@@ -82,15 +89,24 @@ namespace HyperCube.Models
             this.name = name;
         }
 
-        public async Task<string> CreateBlockchainAccount(AccountModel account)
+        public async Task<string> CreateBlockchainAccount(AccountModel account, string pass)
         {
-            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);
+            var res = await RunFunction("personal_newAccount", $"\"{pass}\"");
+            string query;
+            if (account != null)
+            {
+                Console.WriteLine($"CreateBlockchainAccount {account}: {res}");
+                query = $"update aspnetusers set eth_address='{res}' where UserName='{account.Name}'";
+                account.eth_address = res;
+                Console.WriteLine($"CreateBlockchainAccount query {query}");
+                MySQLConnector.Instance().SQLInsert(query);
+            }
+            else
+                Console.WriteLine($"CreateBlockchainAccount {id}: {res}");
+                query = $"update blockchains set address_main='{res}' where id={id}";
+                address = res;
+                Console.WriteLine($"CreateBlockchainAccount query {query}");
+                MySQLConnector.Instance().SQLInsert(query);
             return res;
         }
 
@@ -138,22 +154,35 @@ namespace HyperCube.Models
             //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);
+            answer = await Post.PostRequestAsync(this, 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";
+            var res = (Newtonsoft.Json.Linq.JArray) jsonDe.result;
+            if (res.Count > 0)
+            {
+                address = jsonDe.result[0];
+
+            }
+            else
+            {
+                address = await CreateEthAddress("test_Password_212");
+            }
+            var query = $"update blockchains set address_main='{address}' where id={id}";
             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<string> CreateEthAddress(string pass)
+        {
+            return await CreateBlockchainAccount(null, pass);
+        }
+
         public async Task LoadContracts()
         {
             MySQLConnector dbCon = MySQLConnector.Instance();
@@ -169,7 +198,7 @@ namespace HyperCube.Models
             var hex = bin2hex(code);
             var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"web3_sha3\",\"params\":[\"{hex}\"], \"id\":1}}";
 
-            var answer = await Post.PostRequestAsync(req);
+            var answer = await Post.PostRequestAsync(Blockchain.Dev, req);
             dynamic jsonDe = JsonConvert.DeserializeObject(answer);
 
             Console.WriteLine("result " + jsonDe.result);
@@ -181,7 +210,7 @@ namespace HyperCube.Models
             Console.WriteLine($"RunFunction {name} params {parms}");
             var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"{name}\",\"params\":[{parms}], \"id\":1}}";
 
-            var answer = await Post.PostRequestAsync(req);
+            var answer = await Post.PostRequestAsync(this, req);
             dynamic jsonDe = JsonConvert.DeserializeObject(answer);
 
             Console.WriteLine("RunFunction result " + jsonDe.result);
@@ -195,13 +224,20 @@ namespace HyperCube.Models
             return ret;
         }
 
+        public async Task<string> GetBalance()
+        {
+            var ret = await RunFunction("eth_getBalance", $"\"{address}\",\"latest\"");
+            var balanceInt = AccountModel.ConvertBalance(ret);
+            return balanceInt.ToString();
+        }
+
         public async void RunContractRead()
         {
             string answer = "no";
             string req = "{\"jsonrpc\":\"2.0\",\"method\":\"eth_call\",\"params\":[{\"to\":\"0x874c6a51e62680d4594cd2555ed8fa47b63ed253\", \"data\":\"0xa87d942c\"},\"latest\"],\"id\":1}";
             //string req = "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":\"0x0000000000000000000000000000000000000000000000000000000000000004\"}";
             //Console.WriteLine($"req " + req);
-            await Post.PostRequestAsync(req);
+            await Post.PostRequestAsync(this, req);
             //Console.WriteLine($"answer {answer} len {answer.Length}" );
             address = answer;
         }
@@ -231,7 +267,7 @@ namespace HyperCube.Models
             Console.WriteLine("transactionAddress " + transactionAddress);
             var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"eth_getTransactionReceipt\",\"params\":[\"{transactionAddress}\"], \"id\":1}}";
 
-            var answer = await Post.PostRequestAsync(req);
+            var answer = await Post.PostRequestAsync(this, req);
 
             dynamic jsonDe = JsonConvert.DeserializeObject(answer);
             var blockHash = jsonDe.result.blockHash;
@@ -251,7 +287,7 @@ namespace HyperCube.Models
             Console.WriteLine("bytecode " + bytecode);
             var req = $"{{ \"jsonrpc\":\"2.0\",\"method\":\"eth_sendTransaction\",\"params\":[{{\"from\":\"{address}\",\"gas\":\"0x31b2e\", \"data\":\"{bytecode}\"}}], \"id\":1}}";
 
-            var answer = await Post.PostRequestAsync(req);
+            var answer = await Post.PostRequestAsync(this, req);
             dynamic jsonDe = JsonConvert.DeserializeObject(answer);
             var res = jsonDe.result;
             Console.WriteLine("result AddContract transactionAddress: " + res);

+ 9 - 6
Pages/Blockchains.razor

@@ -276,25 +276,28 @@
 
     private async Task GetBalance()
     {
-        if (accountSelected != "")
+        Console.WriteLine("accountSelected " + accountSelected);
+        if (accountSelected != "" && accountSelected != null && accountSelected != "0")
         {
             var acc = AccountModel.Find(accountSelected);
             if (acc != null)
             {
-                var res = await acc.GetBalance();
-                var balanceInt = AccountModel.ConvertBalance(res);
-                balance = balanceInt.ToString();
+                balance = await acc.GetBalance();
             }
         }
+        else
+        {
+            balance = await Blockchain.GetMain().GetBalance();
+        }
 
     }
 
-    private async Task CreateBlockchainAccount()
+    private async Task CreateBlockchainAccount(string pass)
     {
         AccountModel account = AccountModel.Find(accountSelected);
         if (account != null)
         {
-            var res = await Blockchain.GetMain().CreateBlockchainAccount(account);
+            var res = await Blockchain.GetMain().CreateBlockchainAccount(account, pass);
         }
     }
 

+ 3 - 4
Post.cs

@@ -11,13 +11,12 @@ namespace HyperCube
 {
     public class Post
     {
-        public static async Task<string> PostRequestAsync(string json)
+        public static async Task<string> PostRequestAsync(Blockchain bc, string json)
         {
-            var main = Blockchain.GetMain();
-            if (main != null)
+            if (bc != null)
             {
                 Console.WriteLine($"json req {json} len {json.Length}");
-                string url = $"http://{main.url}:{main.port}";
+                string url = $"http://{bc.url}:{bc.port}";
                 var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
 
                 httpWebRequest.ContentType = "application/json";