Blockchains.razor 13 KB

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