123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- @page "/blockchains"
- @using System.Text.RegularExpressions;
- @using Console = HyperCube.Utils.AdvConsole;
- @namespace HyperCube.Models
- @attribute [Authorize]
- <h1>Blockchain</h1>
- @code
- {
- string hidecontr = "visible";
- string hidereceipt = "hidden";
- string result = "";
- string newcode = "";
- string newname = "";
- string balance = "";
- string sha3 = "";
- string function1 = "";
- string function_processed = "";
- string contractAddDisabled = "visible";
- SmartContract ctrSelected = new SmartContract();
- int ct;
- //ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff fff7
- string accountSelected;
- int contrtest
- {
- get
- {
- return ct;
- }
- set
- {
- ct = value;
- Console.WriteLine("ch contrtest val " + value);
- var ctr = GetContract(value);
- if (ctr != null)
- {
- Console.WriteLine("ctr found " + ctr.Name);
- ctrSelected = ctr;
- }
- }
- }
- }
- @if (Blockchain.newData)
- {
- @foreach (var bc in Blockchain.loaded.Values)
- {
- <p>
- <b>
- @bc.url:@bc.port
- </b>, address: @bc.address
- </p>
- }
- }
- <h4>Ethereum dev network</h4>
- <p style="visibility:@hidecontr">
- Add smart contract:
- <p><input id="newname" @bind="newname" placeholder="Имя нового контракта" /></p>
- <p><input id="newcode" size="100" @bind="newcode" name="code" type="text" placeholder="Исходный код" /></p>
- <p><input id="bytecode" size="100" @bind="bytecode" name="bytecode" type="text" placeholder="Байткод" @bind:event="oninput" /></p>
- <p><input id="function1" size="100" @bind="function1" name="function1" type="text" placeholder="Функция №1 - Объявление" /></p>
- <p><input disabled id="function_processed" size="100" @bind="function_processed" name="function_processed" type="text" placeholder="Функция" /></p>
- <button @onclick="checkCode">checkCode</button>
- <div style="visibility: @contractAddDisabled">
- <button @onclick="AddContract">Add</button>
- </div>
- <p><input id="getsha3" size="100" @bind="sha3" name="getsha3" type="text" placeholder="SHA3" @bind:event="oninput" /></p>
- @*<button @onclick="GetSHA3_2">GetSHA3</button>*@
- @*<p>
- 0x60806040526000805534801561001457600080fd5b50610101806100246000396000f3006080604052600436106053576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680635b34b966146058578063a87d942c14606c578063f5c5ad83146094575b600080fd5b348015606357600080fd5b50606a60a8565b005b348015607757600080fd5b50607e60ba565b6040518082815260200191505060405180910390f35b348015609f57600080fd5b5060a660c3565b005b60016000808282540192505081905550565b60008054905090565b600160008082825403925050819055505600a165627a7a723058207815571f5c1d82bff3c0512a4cbc7a1f1c2063fa9a0de61913b76925f041e9ed0029
- </p>*@
- <br />
- </p>
- @*<p style="visibility:@hidereceipt">
- Get smart contract address:
- <button @onclick="GetReceipt">Check transaction receipt</button>
- <input id="bytecode" size="100" @bind="bytecode" name="bytecode" type="text" placeholder="Transaction address" @bind:event="oninput">
- <br />
- </p>*@
- <p>
- <select @bind="contrtest">
- <option value="0">[Select Contract]</option>
- @if (Blockchain.loaded.Count > 0)
- {
- result = Blockchain.loaded.Count.ToString();
- if (Blockchain.GetMain().contracts.Count > 0)
- {
- @foreach (var c in Blockchain.GetMain().contracts.Values)
- {
- <option value="@c.ID">
- [@c.ID] @c.Name [@c.Address]
- </option>
- }
- }
- }
- </select>
- <button @onclick="deleteContract">Удалить контракт</button>
- <p>
- @ctrSelected.Code
- </p>
- @ctrSelected.Name
- <br />
- </p>
- <p>
- <select @bind="accountSelected">
- <option value="0">[Select Account]</option>
- @if (Blockchain.loaded.Count > 0)
- {
- @foreach (var acc in AccountModel.Loaded.Values)
- {
- <option value="@acc.UUID">
- [@acc.Email] @acc.Name
- </option>
- }
- }
- </select>
- @*<button @onclick="CreateBlockchainAccount">CreateBlockchainAccount for @accountSelected</button>*@
- <button @onclick="GetBalance">GetBalance</button> Balance: @balance
- <button @onclick="Transfer50">Transfer 50</button>
- <button @onclick="TransferContract">Transfer 10 by Contract</button>
- </p>
- <p>Result: @result</p>
- @code
- {
- private Random r = new Random();
- private string bcURL = "127.0.0.1";
- private int bcport = 8545;
- private string bytecode = "";
- private string error = "";
- string lastTransAddr = "";
- string contractAddress = "";
- int focus = 0;
- //Verify
- //function verify( address payable verifier, address payable initiator, uint8 article_value, uint8 verify_complexity)
- //0x608060405234801561001057600080fd5b50610190806100206000396000f3fe60806040526004361061001e5760003560e01c8063619837de14610023575b600080fd5b61009f6004803603608081101561003957600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803560ff169060200190929190803560ff1690602001909291905050506100a1565b005b600060058302820160ff1690508063ffffffff164710610153578473ffffffffffffffffffffffffffffffffffffffff166108fc8360ff169081150290604051600060405180830381858888f19350505050158015610104573d6000803e3d6000fd5b508373ffffffffffffffffffffffffffffffffffffffff166108fc6005850260ff169081150290604051600060405180830381858888f19350505050158015610151573d6000803e3d6000fd5b505b505050505056fea2646970667358221220847188fccb79ed4087ece85f7a8d9c3eaf187342166d7a66131b00a13bb0577b64736f6c63430007040033
- async Task deleteContract()
- {
- var main = Blockchain.GetMain();
- if (main != null)
- {
- main.contracts.Remove(ctrSelected.ID);
- main.contractNames.Remove(ctrSelected.Name);
- MySQLConnector.Instance().SQLInsert($"delete from smart_contracts where id ={ctrSelected.ID}");
- MySQLConnector.Instance().SQLInsert($"delete from functions where contract_id ={ctrSelected.ID}");
- ctrSelected = null;
- }
- }
- async Task checkCode()
- {
- //string pattern = @"function\s+(\S+)\s*\((.*)\)\s*{.*}";
- string pattern = @"function\s+(\S+)\s*\((.*)\).*";
- string replacement_name = "$1";
- string replacement_params = "$2";
- string input = function1;
- string resultname = Regex.Replace(input, pattern, replacement_name).Trim();
- string param_pattern = @"(uint8|address)";
- var final = resultname + "(";
- if (input != resultname)
- {
- //function_processed += "name " + resultname + " ";
- string resultparams = Regex.Replace(input, pattern, replacement_params).Trim();
- if (input != resultparams)
- {
- //function_processed += "params " + resultparams+" matches: ";
- int i = 0;
- foreach (Match match in Regex.Matches(resultparams, param_pattern))
- {
- if (i > 0)
- final += "," + match.Value;
- else
- final += match.Value;
- i++;
- }
- }
- }
- function_processed = final + ")";
- sha3 = await GetSHA3(function_processed);
- if (function1 != "()" && function1 != "")
- contractAddDisabled = "";
- }
- async Task<string> GetSHA3(string code)
- {
- var res = await Blockchain.GetSHA3(code);
- Console.WriteLine($"GetSHA3 {code} {res}");
- var ret = res.Substring(2, 8);
- return ret;
- }
- async Task<string> GetSHA3_2()
- {
- var res = await Blockchain.GetSHA3(sha3);
- Console.WriteLine($"GetSHA3 {res}");
- var ret = res.Substring(2, 8);
- return ret;
- }
- protected async Task TransferContract()
- {
- var ttt = "123";
- Console.WriteLine($"len {ttt.Length}");
- try
- {
- var myacc = AccountModel.Current;
- Console.WriteLine($"TransferContract run");
- Console.WriteLine($"TransferContract {myacc.Name}");
- var recipient = AccountModel.Find(accountSelected);
- if (recipient != null)
- {
- //var b2h = Blockchain.bin2hex("getCount()");
- //Console.WriteLine("bin2hex " + b2h);
- var part1 = await Blockchain.GetSHA3("myTransfer(address)");
- //var part2 = await Blockchain.GetSHA3(recipient.eth_address);
- var zerofill = Blockchain.zerofill(part1.Substring(0, 10), 32, true);
- //var zerofill2 = Blockchain.zerofill(part2, 32, true);
- var paramstring = "0x" + zerofill + recipient.eth_address.Remove(0, 2);
- Console.WriteLine($"paramstring {paramstring}");
- //todo pack myTransfer(address)
- //todo pack contr.Address
- if (Blockchain.GetMain().contractNames.ContainsKey("Transfer"))
- {
- var contr = Blockchain.GetMain().contractNames["Transfer"];
- var res = await Blockchain.GetMain().RunContractWrite(contr.Address, $"{paramstring}");
- Console.WriteLine("TransferContract result " + res);
- var receipt = await Blockchain.GetMain().GetReceipt(res);
- Console.WriteLine("TransferContract receipt " + receipt);
- }
- }
- //TODO run smart contract with verifier wallet
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- }
- private async Task Transfer50()
- {
- var acc = AccountModel.Find(accountSelected);
- var res = await Blockchain.GetMain().SendTransaction(Blockchain.GetMain().address, acc.eth_address, "0x32");
- }
- private async Task GetBalance()
- {
- if (accountSelected != "")
- {
- var acc = AccountModel.Find(accountSelected);
- if (acc != null)
- {
- var res = await acc.GetBalance();
- balance = res.ToString();
- //var balanceInt = AccountModel.ConvertBalance(res);
- //balance = balanceInt.ToString();
- }
- }
- }
- private async Task CreateBlockchainAccount()
- {
- AccountModel account = AccountModel.Find(accountSelected);
- if (account != null)
- {
- var res = await Blockchain.GetMain().CreateBlockchainAccount(account);
- }
- }
- private async Task ChangeContract()
- {
- Console.WriteLine("ChangeContract " + contrtest);
- //TODO result as transaction addr
- //result = await Blockchain.loaded[0].GetReceipt(result);
- }
- private async Task RunContract()
- {
- //TODO result as transaction addr
- }
- private async Task AddContract()
- {
- if (Blockchain.loaded.Count > 0)
- {
- var taddr = await Blockchain.GetMain().AddContract(newname, newcode, bytecode);
- if (taddr.Length > 0 && taddr[0] != "")
- {
- result = taddr[0].ToString();
- //hidereceipt = "visible";
- var caddr = await Blockchain.GetMain().GetReceipt(result, true);
- if (caddr != "")
- {
- result = "contractAddress: " + caddr;
- contractAddress = caddr;
- //var id = (int) taddr[1];
- var smc = taddr[1] as SmartContract;//Blockchain.GetMain().contracts[id];
- smc.Address = contractAddress;
- MySQLConnector.Instance().SQLInsert($"update smart_contracts set address='{contractAddress}' where id= {smc.ID}");
- //var run = await Blockchain.GetMain().RunContractWrite(contractAddress, "0x11111111");
- //result = "run: " + run;
- smc.AddFunction(function_processed, sha3);
- }
- }
- }
- //private Post post = new Post();
- }
- SmartContract GetContract(int id)
- {
- if (Blockchain.GetMain().contracts.ContainsKey(id))
- return Blockchain.GetMain().contracts[id];
- else
- return null;
- }
- //private async Task GetBlockChain()
- //{
- // //if (Blockchain.loaded.Count == 0)
- // //{
- // Blockchain bc = new HyperCube.Models.Blockchain(bcURL, bcport);
- // result = await bc.Initialize();
- // if (result != "")
- // hidecontr = "visible";
- // //bcURL = bc.GetAddress();
- // //bc.ExecuteContract();
- // //bcURL = bc.address;
- // //hidecontr = "hidden";
- // //}
- // //private Post post = new Post();
- //}
- }
|