Blockchains.razor 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. @page "/blockchains"
  2. @page "/blockchains/{Smart}"
  3. @using System.Text.RegularExpressions;
  4. @using Console = HyperCube.Utils.AdvConsole;
  5. @inject NavigationManager NavigationManager
  6. @inject IJSRuntime JsRuntime
  7. @using HyperCube.Models
  8. @using Microsoft.AspNetCore.Identity;
  9. @inject AuthenticationStateProvider AuthenticationStateProvider
  10. @inject UserManager<IdentityUser> UserManager
  11. @attribute [Authorize]
  12. <div class="tabs__content">
  13. <div class="tabs__controls">
  14. @if (account.Roles.Contains(Role.Admin) || account.Roles.Contains(Role.Initiator))
  15. {
  16. <a href="DocEdit" class="tabs__btn">Загрузка материалов</a>
  17. }
  18. @if (account.Roles.Contains(Role.Admin) || account.Roles.Contains(Role.Verifier))
  19. {
  20. <a href="Verifying" class="tabs__btn">Верификация</a>
  21. }
  22. <a class="tabs__btn">Библиотеки</a>
  23. @if (account.Roles.Contains(Role.Admin) || account.Roles.Contains(Role.Requester))
  24. {
  25. <a class="tabs__btn">Заказы</a>
  26. }
  27. </div>
  28. </div>
  29. <br>
  30. @*<h1>Blazor is @Smart!</h1>*@
  31. @code
  32. {
  33. string hidecontr = "visible";
  34. string hidereceipt = "hidden";
  35. string result = "";
  36. string newcode = "";
  37. string newname = "";
  38. string balance = "";
  39. string sha3 = "";
  40. string function1 = "";
  41. string function_processed = "";
  42. string contractAddDisabled = "";
  43. string tokenContract = "0xe5D682717955d6C35d465A3485625C64655a04f4";
  44. string tokenName
  45. {
  46. get { return Blockchain.tokenName; }
  47. set { }
  48. }
  49. string tokenSymbol
  50. {
  51. get { return Blockchain.symbol; }
  52. set { }
  53. }
  54. string tokenDecimals
  55. {
  56. get { return Blockchain.decimals; }
  57. set { }
  58. }
  59. string tokenBalance
  60. {
  61. get { return Blockchain.tokenBalance; }
  62. set { }
  63. }
  64. string tokenAmount;
  65. string addressTo;
  66. SmartContract ctrSelected = new SmartContract();
  67. int ct;
  68. //ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff fff7
  69. string accountSelected;
  70. int contrtest
  71. {
  72. get
  73. {
  74. return ct;
  75. }
  76. set
  77. {
  78. ct = value;
  79. Console.WriteLine("ch contrtest val " + value);
  80. var ctr = GetContract(value);
  81. if (ctr != null)
  82. {
  83. Console.WriteLine("ctr found " + ctr.Name);
  84. ctrSelected = ctr;
  85. }
  86. }
  87. }
  88. }
  89. @if (Blockchain.blockChainsInitialized)
  90. {
  91. <select @bind="AccountModel.GetCurrent().blockchain_selected" style="color:green">
  92. <option value=0>Ethereum Dev Network</option>
  93. <option value=1>Ethereum Test Network</option>
  94. </select>
  95. <p><b>@Blockchain.GetMain().url:@Blockchain.GetMain().port </b><br /> <b>Address:</b> @Blockchain.GetMain().address<br /> <b>Balance:</b> @Blockchain.GetMain().balance</p>
  96. @*@foreach(var bc in Blockchain.loaded.Values)
  97. {
  98. <p>
  99. <b>
  100. @bc.url:@bc.port
  101. </b>, address: @bc.address
  102. </p>
  103. }*@
  104. @*<p>@AccountModel.GetCurrent().blockchain_selected</p>*@
  105. <br>
  106. <p></p>
  107. <p style="visibility:@hidecontr">
  108. Add smart contract:
  109. <p><input id="newname" @bind="newname" placeholder="Имя нового контракта" /></p>
  110. <p><input id="newcode" size="100" @bind="newcode" name="code" type="text" placeholder="Исходный код" /></p>
  111. <p><input id="bytecode" size="100" @bind="bytecode" name="bytecode" type="text" placeholder="Байткод" @bind:event="oninput" /></p>
  112. <p><input id="function1" size="100" @bind="function1" name="function1" type="text" placeholder="Функция №1 - Объявление" /></p>
  113. <p><input disabled id="function_processed" size="100" @bind="function_processed" name="function_processed" type="text" placeholder="Функция" /></p>
  114. <button @onclick="checkCode">checkCode</button>
  115. <div style="visibility: @contractAddDisabled">
  116. <button @onclick="AddContract">Add</button>
  117. </div>
  118. @*<div>
  119. <button @onclick="TestHTTP">TestHTTP</button>
  120. </div>*@
  121. <p><input id="getsha3" size="100" @bind="sha3" name="getsha3" type="text" placeholder="SHA3" @bind:event="oninput" /></p>
  122. @*<button @onclick="GetSHA3_2">GetSHA3</button>*@
  123. @*<p>
  124. 0x60806040526000805534801561001457600080fd5b50610101806100246000396000f3006080604052600436106053576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680635b34b966146058578063a87d942c14606c578063f5c5ad83146094575b600080fd5b348015606357600080fd5b50606a60a8565b005b348015607757600080fd5b50607e60ba565b6040518082815260200191505060405180910390f35b348015609f57600080fd5b5060a660c3565b005b60016000808282540192505081905550565b60008054905090565b600160008082825403925050819055505600a165627a7a723058207815571f5c1d82bff3c0512a4cbc7a1f1c2063fa9a0de61913b76925f041e9ed0029
  125. </p>*@
  126. <br />
  127. </p>
  128. <div>
  129. <button @onclick="ImportERC20">ImportERC20</button>
  130. <p><input id="tc" size="100" @bind="tokenContract" name="code" type="text" placeholder="Адрес контракта токена" /></p>
  131. <p><input id="tn" size="100" @bind="tokenName" name="code" type="text" placeholder="" /></p>
  132. <p><input id="ts" size="100" @bind="tokenSymbol" name="code" type="text" placeholder="" /></p>
  133. <p><input id="td" size="100" @bind="tokenDecimals" name="code" type="text" placeholder="" /></p>
  134. <p><input id="tb" size="100" @bind="tokenBalance" name="code" type="text" placeholder="" /></p>
  135. </div>
  136. <div>
  137. <button @onclick="TransferToken">Transfer</button>
  138. <p><input id="tc" size="100" @bind="tokenAmount" name="code" type="text" placeholder="Количество" /></p>
  139. <p><input id="tn" size="100" @bind="addressTo" name="code" type="text" placeholder="Адрес получателя" /></p>
  140. </div>
  141. @*<p style="visibility:@hidereceipt">
  142. Get smart contract address:
  143. <button @onclick="GetReceipt">Check transaction receipt</button>
  144. <input id="bytecode" size="100" @bind="bytecode" name="bytecode" type="text" placeholder="Transaction address" @bind:event="oninput">
  145. <br />
  146. </p>*@
  147. <p>
  148. <select @bind="contrtest">
  149. <option value="0">[Select Contract]</option>
  150. @if (Blockchain.loaded.Count > 0)
  151. {
  152. result = Blockchain.loaded.Count.ToString();
  153. if (Blockchain.GetMain().contracts.Count > 0)
  154. {
  155. @foreach (var c in Blockchain.GetMain().contracts.Values)
  156. {
  157. <option value="@c.ID">
  158. [@c.ID] @c.Name [@c.Address]
  159. </option>
  160. }
  161. }
  162. }
  163. </select>
  164. <button @onclick="deleteContract">Удалить контракт</button>
  165. <p>
  166. @ctrSelected.Code
  167. </p>
  168. @ctrSelected.Name
  169. <br>
  170. @Blockchain.GetMain().id;
  171. <br />
  172. </p>
  173. <p>
  174. <select @bind="accountSelected">
  175. <option value="0">[Select Account]</option>
  176. @if (Blockchain.loaded.Count > 0)
  177. {
  178. @foreach (var acc in AccountModel.Loaded.Values)
  179. {
  180. <option value="@acc.UUID">
  181. [@acc.Email] @acc.Name
  182. </option>
  183. }
  184. }
  185. </select>
  186. @*<button @onclick="CreateBlockchainAccount">CreateBlockchainAccount for @accountSelected</button>*@
  187. <button @onclick="GetBalance">GetBalance</button> Balance: @balance
  188. <button @onclick="Transfer50">Transfer 50</button>
  189. <button @onclick="Unlock">Unlock</button>
  190. <div>Wallet:@myWallet</div>
  191. <div>Token balance:@balanceHCB</div>
  192. @*<button @onclick="TransferContract">Transfer 10 by Contract</button>*@
  193. <button @onclick="EstimateGas">EstimateGas</button>
  194. @*<button onclick="window.location.href='@mypath'">Download 1</button>*@
  195. <button id="btn" @onclick="Dload">Dload</button>
  196. </p>
  197. <p>Result: @result, gas: @gas</p>
  198. }
  199. @code
  200. {
  201. AccountModel account;
  202. private Random r = new Random();
  203. private string bcURL = Blockchain.URLdefault;
  204. private int bcport = Blockchain.defaultPort;
  205. private string bytecode = "";
  206. private string error = "";
  207. string lastTransAddr = "";
  208. string contractAddress = "";
  209. int focus = 0;
  210. string gas = "";
  211. async Task<string> Dload()
  212. {
  213. //string filepath = @"c:\Users\Администратор\AppData\Local\Ethereum\rinkeby\keystore\
  214. if (accountSelected != "" && accountSelected != null && accountSelected != "0")
  215. {
  216. var acc = AccountModel.Find(accountSelected);
  217. if (acc != null)
  218. {
  219. myWallet = await acc.GetOrCreateActualAddress(Blockchain.GetMain());
  220. string text = getFiles(myWallet);
  221. return await JsRuntime.InvokeAsync<string>("test", text);
  222. }
  223. }
  224. return "";
  225. }
  226. //Verify
  227. //function verify( address payable verifier, address payable initiator, uint8 article_value, uint8 verify_complexity)
  228. //0x608060405234801561001057600080fd5b50610190806100206000396000f3fe60806040526004361061001e5760003560e01c8063619837de14610023575b600080fd5b61009f6004803603608081101561003957600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803560ff169060200190929190803560ff1690602001909291905050506100a1565b005b600060058302820160ff1690508063ffffffff164710610153578473ffffffffffffffffffffffffffffffffffffffff166108fc8360ff169081150290604051600060405180830381858888f19350505050158015610104573d6000803e3d6000fd5b508373ffffffffffffffffffffffffffffffffffffffff166108fc6005850260ff169081150290604051600060405180830381858888f19350505050158015610151573d6000803e3d6000fd5b505b505050505056fea2646970667358221220847188fccb79ed4087ece85f7a8d9c3eaf187342166d7a66131b00a13bb0577b64736f6c63430007040033
  229. async Task<string> Unlock()
  230. {
  231. var bc = Blockchain.GetMain();
  232. var ret = await bc.RunFunction("personal_unlockAccount", $"\"{bc.address}\",\"test_Password_212\",0");
  233. //address, passphrase, duration
  234. //{ "method": "personal_unlockAccount", "params": [string, string, number]}
  235. return ret;
  236. }
  237. async Task ImportERC20()
  238. {
  239. await Blockchain.GetMain().ImportERC20(tokenContract);
  240. //await Blockchain.GetMain().ImportERC20("0x413D9500A675d9b645034aC5f4325BF12ddeb7c1");
  241. }
  242. async Task TransferToken()
  243. {
  244. await Blockchain.GetMain().TransferToken(Blockchain.GetMain().address, tokenContract, addressTo, tokenAmount);
  245. }
  246. async Task EstimateGas()
  247. {
  248. gas = await Blockchain.GetMain().GetEstimatedGasContractAdd(ctrSelected.ByteCode);
  249. }
  250. async Task deleteContract()
  251. {
  252. var main = Blockchain.GetMain();
  253. if (main != null)
  254. {
  255. main.contracts.Remove(ctrSelected.ID);
  256. main.contractNames.Remove(ctrSelected.Name);
  257. MySQLConnector.Instance().SQLInsert($"delete from smart_contracts where id ={ctrSelected.ID}");
  258. MySQLConnector.Instance().SQLInsert($"delete from functions where contract_id ={ctrSelected.ID}");
  259. ctrSelected = null;
  260. }
  261. }
  262. async Task checkCode()
  263. {
  264. ////string pattern = @"function\s+(\S+)\s*\((.*)\)\s*{.*}";
  265. //string pattern = @"function\s+(\S+)\s*\((.*)\).*";
  266. //string replacement_name = "$1";
  267. //string replacement_params = "$2";
  268. //string input = function1;
  269. //string resultname = Regex.Replace(input, pattern, replacement_name).Trim();
  270. //string param_pattern = @"(uint8|address)";
  271. //var final = resultname + "(";
  272. //if (input != resultname)
  273. //{
  274. // //function_processed += "name " + resultname + " ";
  275. // string resultparams = Regex.Replace(input, pattern, replacement_params).Trim();
  276. // if (input != resultparams)
  277. // {
  278. // //function_processed += "params " + resultparams+" matches: ";
  279. // int i = 0;
  280. // foreach (Match match in Regex.Matches(resultparams, param_pattern))
  281. // {
  282. // if (i > 0)
  283. // final += "," + match.Value;
  284. // else
  285. // final += match.Value;
  286. // i++;
  287. // }
  288. // }
  289. //}
  290. //function_processed = final + ")";
  291. //sha3 = await GetSHA3(function_processed);
  292. //getFiles();
  293. function_processed = await Blockchain.GetMain().compileFunction(function1, false);
  294. if (function1 != "()" && function1 != "")
  295. contractAddDisabled = "";
  296. else
  297. contractAddDisabled = "visible";
  298. }
  299. async Task<string> GetSHA3(string code)
  300. {
  301. var res = await Blockchain.GetMain().GetSHA3(code);
  302. Console.WriteLine($"GetSHA3 {code} {res}");
  303. var ret = res.Substring(2, 8);
  304. return ret;
  305. }
  306. async Task<string> GetSHA3_2()
  307. {
  308. var res = await Blockchain.GetMain().GetSHA3(sha3);
  309. Console.WriteLine($"GetSHA3 {res}");
  310. var ret = res.Substring(2, 8);
  311. return ret;
  312. }
  313. //protected async Task TransferContract()
  314. //{
  315. // var ttt = "123";
  316. // Console.WriteLine($"len {ttt.Length}");
  317. // try
  318. // {
  319. // var myacc = AccountModel.Current;
  320. // Console.WriteLine($"TransferContract run");
  321. // Console.WriteLine($"TransferContract {myacc.Name}");
  322. // var recipient = AccountModel.Find(accountSelected);
  323. // if (recipient != null)
  324. // {
  325. // var bc = Blockchain.GetMain();
  326. // //var b2h = Blockchain.bin2hex("getCount()");
  327. // //Console.WriteLine("bin2hex " + b2h);
  328. // var part1 = await Blockchain.GetSHA3("myTransfer(address)");
  329. // //var part2 = await Blockchain.GetSHA3(recipient.eth_address);
  330. // var zerofill = Blockchain.zerofill(part1.Substring(0, 10), 32, true);
  331. // //var zerofill2 = Blockchain.zerofill(part2, 32, true);
  332. // var radr = await recipient.GetOrCreateActualAddress(bc);
  333. // var paramstring = "0x" + zerofill + radr.Remove(0, 2);
  334. // Console.WriteLine($"paramstring {paramstring}");
  335. // //todo pack myTransfer(address)
  336. // //todo pack contr.Address
  337. // if (bc.contractNames.ContainsKey("Transfer"))
  338. // {
  339. // var contr = bc.contractNames["Transfer"];
  340. // var res = await bc.RunContractWrite(contr.Address, $"{paramstring}");
  341. // Console.WriteLine("TransferContract result " + res);
  342. // var receipt = await bc.GetReceipt(res);
  343. // Console.WriteLine("TransferContract receipt " + receipt);
  344. // }
  345. // }
  346. // //TODO run smart contract with verifier wallet
  347. // }
  348. // catch (Exception e)
  349. // {
  350. // Console.WriteLine(e.Message);
  351. // }
  352. //}
  353. private async Task Transfer50()
  354. {
  355. var acc = AccountModel.Find(accountSelected);
  356. var bc = Blockchain.GetMain();
  357. var addr = await acc.GetOrCreateActualAddress(bc);
  358. var res = await bc.SendTransaction(Blockchain.GetMain().address, addr, 50);
  359. }
  360. private async Task GetBalance()
  361. {
  362. Console.WriteLine("accountSelected " + accountSelected);
  363. if (accountSelected != "" && accountSelected != null && accountSelected != "0")
  364. {
  365. var acc = AccountModel.Find(accountSelected);
  366. if (acc != null)
  367. {
  368. myWallet = await acc.GetOrCreateActualAddress(Blockchain.GetMain());
  369. balance = await acc.GetBalance();
  370. }
  371. }
  372. else
  373. {
  374. balance = await Blockchain.GetMain().GetBalance();
  375. }
  376. }
  377. //private async Task CreateBlockchainAccount(Blockchain bc, string pass)
  378. //{
  379. // AccountModel account = AccountModel.Find(accountSelected);
  380. // if (account != null)
  381. // {
  382. // var res = await bc.CreateBlockchainAccount(account, pass);
  383. // }
  384. //}
  385. private async Task ChangeContract()
  386. {
  387. Console.WriteLine("ChangeContract " + contrtest);
  388. //TODO result as transaction addr
  389. //result = await Blockchain.loaded[0].GetReceipt(result);
  390. }
  391. private async Task RunContract()
  392. {
  393. //TODO result as transaction addr
  394. }
  395. private async Task AddContract()
  396. {
  397. if (Blockchain.loaded.Count > 0)
  398. {
  399. var taddr = await Blockchain.GetMain().AddContract(newname, newcode, bytecode);
  400. if (taddr.Length > 0 && taddr[0] != "")
  401. {
  402. result = taddr[0].ToString();
  403. //hidereceipt = "visible";
  404. var caddr = await Blockchain.GetMain().GetReceipt(result, true);
  405. if (caddr != "")
  406. {
  407. result = "contractAddress: " + caddr;
  408. contractAddress = caddr;
  409. //var id = (int) taddr[1];
  410. var smc = taddr[1] as SmartContract;//Blockchain.GetMain().contracts[id];
  411. smc.Address = contractAddress;
  412. MySQLConnector.Instance().SQLInsert($"update smart_contracts set address='{contractAddress}' where id= {smc.ID}");
  413. //var run = await Blockchain.GetMain().RunContractWrite(contractAddress, "0x11111111");
  414. //result = "run: " + run;
  415. smc.AddFunction(function_processed, sha3);
  416. }
  417. }
  418. }
  419. //private Post post = new Post();
  420. }
  421. SmartContract GetContract(int id)
  422. {
  423. if (Blockchain.GetMain().contracts.ContainsKey(id))
  424. return Blockchain.GetMain().contracts[id];
  425. else
  426. return null;
  427. }
  428. protected override async Task OnInitializedAsync()
  429. {
  430. //Console.WriteLine($"Blazor is {Smart}");
  431. account = AccountModel.Current;
  432. Console.WriteLine($"Blockchains OnInitializedAsync");
  433. await GetBalance();
  434. }
  435. //private async Task GetBlockChain()
  436. //{
  437. // //if (Blockchain.loaded.Count == 0)
  438. // //{
  439. // Blockchain bc = new HyperCube.Models.Blockchain(bcURL, bcport);
  440. // result = await bc.Initialize();
  441. // if (result != "")
  442. // hidecontr = "visible";
  443. // //bcURL = bc.GetAddress();
  444. // //bc.ExecuteContract();
  445. // //bcURL = bc.address;
  446. // //hidecontr = "hidden";
  447. // //}
  448. // //private Post post = new Post();
  449. //}
  450. }