Blockchains.razor 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. @page "/blockchains"
  2. @using System.Text.RegularExpressions;
  3. @using Console = HyperCube.Utils.AdvConsole;
  4. @namespace HyperCube.Models
  5. @attribute [Authorize]
  6. <h1>Blockchain</h1>
  7. @code
  8. {
  9. string hidecontr = "hidden";
  10. string hidereceipt = "hidden";
  11. string result = "";
  12. string newcode = "";
  13. string newname = "";
  14. string balance = "";
  15. string sha3 = "";
  16. string function1 = "";
  17. string function_processed = "";
  18. string contractAddDisabled = "hidden";
  19. SmartContract ctrSelected = new SmartContract();
  20. int ct;
  21. //ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff fff7
  22. string accountSelected;
  23. int contrtest
  24. {
  25. get
  26. {
  27. return ct;
  28. }
  29. set
  30. {
  31. ct = value;
  32. Console.WriteLine("ch contrtest val " + value);
  33. var ctr = GetContract(value);
  34. if (ctr != null)
  35. {
  36. Console.WriteLine("ctr found " + ctr.Name);
  37. ctrSelected = ctr;
  38. }
  39. }
  40. }
  41. }
  42. @if (Blockchain.newData)
  43. {
  44. @foreach (var bc in Blockchain.loaded.Values)
  45. {
  46. <p>
  47. <b>
  48. @bc.url:@bc.port
  49. </b>, address: @bc.address
  50. </p>
  51. }
  52. }
  53. <h4>Ethereum dev network</h4>
  54. <p>
  55. Add blockchain connection:
  56. <input name="bcurl" @bind="bcURL" type="text" placeholder="URL" @bind:event="oninput">
  57. <input name="bport" @bind="bcport" type="number" placeholder="port">
  58. </p>
  59. <p style="visibility:@hidecontr">
  60. Add smart contract:
  61. <p><input id="newname" @bind="newname" placeholder="Имя нового контракта" /></p>
  62. <p><input id="newcode" size="100" @bind="newcode" name="code" type="text" placeholder="Исходный код" /></p>
  63. <p><input id="bytecode" size="100" @bind="bytecode" name="bytecode" type="text" placeholder="Байткод" @bind:event="oninput" /></p>
  64. <p><input id="function1" size="100" @bind="function1" name="function1" type="text" placeholder="Функция №1 - Объявление" /></p>
  65. <p><input disabled id="function_processed" size="100" @bind="function_processed" name="function_processed" type="text" placeholder="Функция" /></p>
  66. <button @onclick="checkCode">checkCode</button>
  67. <div style="visibility: @contractAddDisabled">
  68. <button @onclick="AddContract">Add</button>
  69. </div>
  70. <p><input id="getsha3" size="100" @bind="sha3" name="getsha3" type="text" placeholder="Get SHA3" @bind:event="oninput" /></p>
  71. <button @onclick="GetSHA3_2">GetSHA3</button>
  72. @*<p>
  73. 0x60806040526000805534801561001457600080fd5b50610101806100246000396000f3006080604052600436106053576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680635b34b966146058578063a87d942c14606c578063f5c5ad83146094575b600080fd5b348015606357600080fd5b50606a60a8565b005b348015607757600080fd5b50607e60ba565b6040518082815260200191505060405180910390f35b348015609f57600080fd5b5060a660c3565b005b60016000808282540192505081905550565b60008054905090565b600160008082825403925050819055505600a165627a7a723058207815571f5c1d82bff3c0512a4cbc7a1f1c2063fa9a0de61913b76925f041e9ed0029
  74. </p>*@
  75. <br />
  76. </p>
  77. @*<p style="visibility:@hidereceipt">
  78. Get smart contract address:
  79. <button @onclick="GetReceipt">Check transaction receipt</button>
  80. <input id="bytecode" size="100" @bind="bytecode" name="bytecode" type="text" placeholder="Transaction address" @bind:event="oninput">
  81. <br />
  82. </p>*@
  83. <p>
  84. <select @bind="contrtest">
  85. <option value="0">[Select Contract]</option>
  86. @if (Blockchain.loaded.Count > 0)
  87. {
  88. result = Blockchain.loaded.Count.ToString();
  89. if (Blockchain.GetMain().contracts.Count > 0)
  90. {
  91. @foreach (var c in Blockchain.GetMain().contracts.Values)
  92. {
  93. <option value="@c.ID">
  94. [@c.ID] @c.Name [@c.Address]
  95. </option>
  96. }
  97. }
  98. }
  99. </select>
  100. <p>
  101. <button @onclick="deleteContract">Удалить контракт</button>
  102. </p>
  103. <p>
  104. @ctrSelected.Code
  105. </p>
  106. @ctrSelected.Name
  107. <br />
  108. </p>
  109. <p>
  110. <select @bind="accountSelected">
  111. <option value="0">[Select Account]</option>
  112. @if (Blockchain.loaded.Count > 0)
  113. {
  114. @foreach (var acc in AccountModel.Loaded.Values)
  115. {
  116. <option value="@acc.UUID">
  117. [@acc.Email] @acc.Name
  118. </option>
  119. }
  120. }
  121. </select>
  122. @*<button @onclick="CreateBlockchainAccount">CreateBlockchainAccount for @accountSelected</button>*@
  123. <button @onclick="GetBalance">GetBalance</button> Balance: @balance
  124. <button @onclick="Transfer50">Transfer 50</button>
  125. <button @onclick="TransferContract">Transfer 10 by Contract</button>
  126. </p>
  127. <p>Result: @result</p>
  128. @code
  129. {
  130. private Random r = new Random();
  131. private string bcURL = "127.0.0.1";
  132. private int bcport = 8545;
  133. private string bytecode = "";
  134. private string error = "";
  135. string lastTransAddr = "";
  136. string contractAddress = "";
  137. int focus = 0;
  138. //Verify
  139. //function verify( address payable verifier, address payable initiator, uint8 article_value, uint8 verify_complexity)
  140. //0x608060405234801561001057600080fd5b50610190806100206000396000f3fe60806040526004361061001e5760003560e01c8063619837de14610023575b600080fd5b61009f6004803603608081101561003957600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803560ff169060200190929190803560ff1690602001909291905050506100a1565b005b600060058302820160ff1690508063ffffffff164710610153578473ffffffffffffffffffffffffffffffffffffffff166108fc8360ff169081150290604051600060405180830381858888f19350505050158015610104573d6000803e3d6000fd5b508373ffffffffffffffffffffffffffffffffffffffff166108fc6005850260ff169081150290604051600060405180830381858888f19350505050158015610151573d6000803e3d6000fd5b505b505050505056fea2646970667358221220847188fccb79ed4087ece85f7a8d9c3eaf187342166d7a66131b00a13bb0577b64736f6c63430007040033
  141. async Task deleteContract()
  142. {
  143. var main = Blockchain.GetMain();
  144. if (main != null)
  145. {
  146. main.contracts.Remove(ctrSelected.ID);
  147. main.contractNames.Remove(ctrSelected.Name);
  148. MySQLConnector.Instance().SQLInsert($"delete from smart_contracts where id ={ctrSelected.ID}");
  149. MySQLConnector.Instance().SQLInsert($"delete from functions where contract_id ={ctrSelected.ID}");
  150. ctrSelected = null;
  151. }
  152. }
  153. async Task checkCode()
  154. {
  155. //string pattern = @"function\s+(\S+)\s*\((.*)\)\s*{.*}";
  156. string pattern = @"function\s+(\S+)\s*\((.*)\).*";
  157. string replacement_name = "$1";
  158. string replacement_params = "$2";
  159. string input = function1;
  160. string resultname = Regex.Replace(input, pattern, replacement_name).Trim();
  161. string param_pattern = @"(uint8|address)";
  162. var final = resultname + "(";
  163. if (input != resultname)
  164. {
  165. //function_processed += "name " + resultname + " ";
  166. string resultparams = Regex.Replace(input, pattern, replacement_params).Trim();
  167. if (input != resultparams)
  168. {
  169. //function_processed += "params " + resultparams+" matches: ";
  170. int i = 0;
  171. foreach (Match match in Regex.Matches(resultparams, param_pattern))
  172. {
  173. if (i > 0)
  174. final += "," + match.Value;
  175. else
  176. final += match.Value;
  177. i++;
  178. }
  179. }
  180. }
  181. function_processed = final + ")";
  182. sha3 = await GetSHA3(function_processed);
  183. if (function1 != "()" && function1 != "")
  184. contractAddDisabled = "";
  185. }
  186. async Task<string> GetSHA3(string code)
  187. {
  188. var res = await Blockchain.GetSHA3(code);
  189. Console.WriteLine($"GetSHA3 {code} {res}");
  190. var ret = res.Substring(2, 8);
  191. return ret;
  192. }
  193. async Task<string> GetSHA3_2()
  194. {
  195. var res = await Blockchain.GetSHA3(sha3);
  196. Console.WriteLine($"GetSHA3 {res}");
  197. var ret = res.Substring(2, 8);
  198. return ret;
  199. }
  200. protected async Task TransferContract()
  201. {
  202. var ttt = "123";
  203. Console.WriteLine($"len {ttt.Length}");
  204. try
  205. {
  206. var myacc = AccountModel.Current;
  207. Console.WriteLine($"TransferContract run");
  208. Console.WriteLine($"TransferContract {myacc.Name}");
  209. var recipient = AccountModel.Find(accountSelected);
  210. if (recipient != null)
  211. {
  212. //var b2h = Blockchain.bin2hex("getCount()");
  213. //Console.WriteLine("bin2hex " + b2h);
  214. var part1 = await Blockchain.GetSHA3("myTransfer(address)");
  215. //var part2 = await Blockchain.GetSHA3(recipient.eth_address);
  216. var zerofill = Blockchain.zerofill(part1.Substring(0, 10), 32, true);
  217. //var zerofill2 = Blockchain.zerofill(part2, 32, true);
  218. var paramstring = "0x" + zerofill + recipient.eth_address.Remove(0, 2);
  219. Console.WriteLine($"paramstring {paramstring}");
  220. //todo pack myTransfer(address)
  221. //todo pack contr.Address
  222. if (Blockchain.GetMain().contractNames.ContainsKey("Transfer"))
  223. {
  224. var contr = Blockchain.GetMain().contractNames["Transfer"];
  225. var res = await Blockchain.GetMain().RunContractWrite(contr.Address, $"{paramstring}");
  226. Console.WriteLine("TransferContract result " + res);
  227. var receipt = await Blockchain.GetMain().GetReceipt(res);
  228. Console.WriteLine("TransferContract receipt " + receipt);
  229. }
  230. }
  231. //TODO run smart contract with verifier wallet
  232. }
  233. catch (Exception e)
  234. {
  235. Console.WriteLine(e.Message);
  236. }
  237. }
  238. private async Task Transfer50()
  239. {
  240. var acc = AccountModel.Find(accountSelected);
  241. var res = await Blockchain.GetMain().SendTransaction(Blockchain.GetMain().address, acc.eth_address, "0x32");
  242. }
  243. private async Task GetBalance()
  244. {
  245. if (accountSelected != "")
  246. {
  247. var acc = AccountModel.Find(accountSelected);
  248. if (acc != null)
  249. {
  250. var res = await acc.GetBalance();
  251. var balanceInt = AccountModel.ConvertBalance(res);
  252. balance = balanceInt.ToString();
  253. }
  254. }
  255. }
  256. private async Task CreateBlockchainAccount()
  257. {
  258. AccountModel account = AccountModel.Find(accountSelected);
  259. if (account != null)
  260. {
  261. var res = await Blockchain.GetMain().CreateBlockchainAccount(account);
  262. }
  263. }
  264. private async Task ChangeContract()
  265. {
  266. Console.WriteLine("ChangeContract " + contrtest);
  267. //TODO result as transaction addr
  268. //result = await Blockchain.loaded[0].GetReceipt(result);
  269. }
  270. private async Task RunContract()
  271. {
  272. //TODO result as transaction addr
  273. }
  274. private async Task AddContract()
  275. {
  276. if (Blockchain.loaded.Count > 0)
  277. {
  278. var taddr = await Blockchain.GetMain().AddContract(newname, newcode, bytecode);
  279. if (taddr.Length > 0 && taddr[0] != "")
  280. {
  281. result = taddr[0].ToString();
  282. //hidereceipt = "visible";
  283. var caddr = await Blockchain.GetMain().GetReceipt(result, true);
  284. if (caddr != "")
  285. {
  286. result = "contractAddress: " + caddr;
  287. contractAddress = caddr;
  288. //var id = (int) taddr[1];
  289. var smc = taddr[1] as SmartContract;//Blockchain.GetMain().contracts[id];
  290. smc.Address = contractAddress;
  291. MySQLConnector.Instance().SQLInsert($"update smart_contracts set address='{contractAddress}' where id= {smc.ID}");
  292. //var run = await Blockchain.GetMain().RunContractWrite(contractAddress, "0x11111111");
  293. //result = "run: " + run;
  294. smc.AddFunction(function_processed, sha3);
  295. }
  296. }
  297. }
  298. //private Post post = new Post();
  299. }
  300. SmartContract GetContract(int id)
  301. {
  302. if (Blockchain.GetMain().contracts.ContainsKey(id))
  303. return Blockchain.GetMain().contracts[id];
  304. else
  305. return null;
  306. }
  307. private async Task GetBlockChain()
  308. {
  309. //if (Blockchain.loaded.Count == 0)
  310. //{
  311. Blockchain bc = new HyperCube.Models.Blockchain(bcURL, bcport);
  312. result = await bc.Initialize();
  313. if (result != "")
  314. hidecontr = "visible";
  315. //bcURL = bc.GetAddress();
  316. //bc.ExecuteContract();
  317. //bcURL = bc.address;
  318. //hidecontr = "hidden";
  319. //}
  320. //private Post post = new Post();
  321. }
  322. }