123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541 |
- @page "/blockchains"
- @page "/blockchains/{Smart}"
- @using System.Text.RegularExpressions;
- @using Console = HyperCube.Utils.AdvConsole;
- @inject NavigationManager NavigationManager
- @inject IJSRuntime JsRuntime
- @using HyperCube.Models
- @using Microsoft.AspNetCore.Identity;
- @inject AuthenticationStateProvider AuthenticationStateProvider
- @inject UserManager<IdentityUser> UserManager
- @using Microsoft.AspNetCore.Components.Server.ProtectedBrowserStorage;
- @inject ProtectedSessionStorage storage
- @attribute [Authorize]
- @code
- {
- string hidecontr = "visible";
- string hidereceipt = "hidden";
- string result = "";
- string qrr = "";
- string newcode = "";
- string newname = "";
- string balance = "";
- string sha3 = "";
- string function1 = "";
- string function_processed = "";
- string contractAddDisabled = "";
- string tokenContract = "0xe5D682717955d6C35d465A3485625C64655a04f4";
- string tokenName
- {
- get { return Blockchain.tokenName; }
- set { }
- }
- string tokenSymbol
- {
- get { return Blockchain.symbol; }
- set { }
- }
- string tokenDecimals
- {
- get { return Blockchain.decimals; }
- set { }
- }
- string tokenBalance
- {
- get { return Blockchain.tokenBalance; }
- set { }
- }
- string tokenAmount;
- string addressTo;
- 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.blockChainsInitialized)
- {
- <select @bind="account.blockchain_selected" style="color:green; width:30%; margin:20px; margin-left:0px">
- <option value=0>Ethereum Dev Network</option>
- <option value=1>Ethereum Test Network</option>
- </select>
- <p><b>@bcMain.url:@bcMain.port </b><br /> <b>Address:</b> @bcMain.address<br /> <b>Balance:</b> @bcMain.balance</p>
- <br>
- <p></p>
- <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>
- <br />
- </p>
- <div>
- <button @onclick="ImportERC20">ImportERC20</button>
- <p><input id="tc" size="100" @bind="tokenContract" name="code" type="text" placeholder="Адрес контракта токена" /></p>
- <p><input id="tn" size="100" @bind="tokenName" name="code" type="text" placeholder="" /></p>
- <p><input id="ts" size="100" @bind="tokenSymbol" name="code" type="text" placeholder="" /></p>
- <p><input id="td" size="100" @bind="tokenDecimals" name="code" type="text" placeholder="" /></p>
- <p><input id="tb" size="100" @bind="tokenBalance" name="code" type="text" placeholder="" /></p>
- </div>
- <div>
- <button @onclick="TransferToken">Transfer</button>
- <p><input id="tc" size="100" @bind="tokenAmount" name="code" type="text" placeholder="Количество" /></p>
- <p><input id="tn" size="100" @bind="addressTo" name="code" type="text" placeholder="Адрес получателя" /></p>
- </div>
- <p>
- <select @bind="contrtest">
- <option value="0">[Select Contract]</option>
- @if (Blockchain.loaded.Count > 0)
- {
- result = Blockchain.loaded.Count.ToString();
- if (bcMain.contracts.Count > 0)
- {
- @foreach (var c in bcMain.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>
- @bcMain.id;
- <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="GetBalance">GetBalance</button> Balance: @balance
- <button @onclick="Transfer50">Transfer 50</button>
- <button @onclick="Unlock">Unlock</button>
- <div>Wallet:@myWallet</div>
- <div>Token balance:@balanceHCB</div>
- <br><br>
- <button @onclick="EstimateGas">EstimateGas</button>
- <p>Export address:</p>
- <button id="btn" @onclick="DloadJSON">Download JSON file</button>
- <button id="btn" @onclick="ExportPrivateKey">Private Key</button>
- <button id="btn" @onclick="PrivateKeyQR">QR code</button>
- <button id="btn" @onclick="Mint1000">Mint 1000</button>
- </p>
- <br>
- <br>
- <div>@((MarkupString)qrr)</div>
- @*<p>Result: @result, gas: @gas</p>*@
- }
- @code
- {
- Blockchain bcMain;
- AccountModel account;
- private Random r = new Random();
- private string bcURL = Blockchain.URLdefault;
- private int bcport = Blockchain.defaultPort;
- private string bytecode = "";
- private string error = "";
- string lastTransAddr = "";
- string contractAddress = "";
- string gas = "";
- async Task<string> Mint1000()
- {
- var acc = AccountModel.Find(accountSelected);
- var bc = bcMain;
- var addr = await acc.GetOrCreateActualAddress(bc);
- await bcMain.Mint(tokenAmount, addr);
- return "";
- }
- async Task<string> PrivateKeyQR()
- {
- var code = await ExportPrivateKey();
- code = HyperCube.Models.Blockchain.QRtest(code);
- qrr = $"<img src=\"data: image / png; base64, {code}\"/>";
- return "";
- }
- async Task<string> ExportPrivateKey()
- {
- var wallet = await GetWallet();
- if (wallet != null)
- {
- var acc = AccountModel.Find(accountSelected);
- string text = getFiles(wallet, "key", acc.PWDHash);
- qrr = $"<p>{text}</p>";
- return text;
- //return await JsRuntime.InvokeAsync<string>("test", text);
- }
- return "";
- }
- async Task<string> DloadJSON()
- {
- var wallet = await GetWallet();
- if (wallet != null)
- {
- string text = getFiles(wallet, "json");
- qrr = $"<p>{text}</p>";
- return await JsRuntime.InvokeAsync<string>("test", text);
- }
- return "";
- }
- async Task<string> GetWallet()
- {
- if (accountSelected != "" && accountSelected != null && accountSelected != "0")
- {
- var acc = AccountModel.Find(accountSelected);
- if (acc != null)
- {
- myWallet = await acc.GetOrCreateActualAddress(bcMain);
- return myWallet;
- }
- }
- return null;
- }
- //web3.personal.importRawKey("private key", "password")
- //Verify
- //function verify( address payable verifier, address payable initiator, uint8 article_value, uint8 verify_complexity)
- //0x608060405234801561001057600080fd5b50610190806100206000396000f3fe60806040526004361061001e5760003560e01c8063619837de14610023575b600080fd5b61009f6004803603608081101561003957600080fd5b81019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803560ff169060200190929190803560ff1690602001909291905050506100a1565b005b600060058302820160ff1690508063ffffffff164710610153578473ffffffffffffffffffffffffffffffffffffffff166108fc8360ff169081150290604051600060405180830381858888f19350505050158015610104573d6000803e3d6000fd5b508373ffffffffffffffffffffffffffffffffffffffff166108fc6005850260ff169081150290604051600060405180830381858888f19350505050158015610151573d6000803e3d6000fd5b505b505050505056fea2646970667358221220847188fccb79ed4087ece85f7a8d9c3eaf187342166d7a66131b00a13bb0577b64736f6c63430007040033
- async Task<string> Unlock()
- {
- var bc = bcMain;
- //var ret = await bc.RunFunction("personal_unlockAccount", $"\"{bc.address}\",\"test_Password_212\",0");
- var acc = AccountModel.Find(accountSelected);
- var wallet = await GetWallet();
- var ret = await bc.RunFunction("personal_unlockAccount", $"\"{wallet}\",\"{acc.PWDHash}\",0");
- //address, passphrase, duration
- //{ "method": "personal_unlockAccount", "params": [string, string, number]}
- return ret;
- }
- async Task ImportERC20()
- {
- await bcMain.ImportERC20(tokenContract, myWallet);
- //await Blockchain.GetMain().ImportERC20("0x413D9500A675d9b645034aC5f4325BF12ddeb7c1");
- }
- async Task TransferToken()
- {
- await bcMain.TransferToken(bcMain.address, tokenContract, addressTo, tokenAmount);
- }
- async Task EstimateGas()
- {
- gas = await bcMain.GetEstimatedGasContractAdd(ctrSelected.ByteCode);
- }
- async Task deleteContract()
- {
- var main = bcMain;
- 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);
- //getFiles();
- function_processed = await bcMain.compileFunction(function1, false);
- if (function1 != "()" && function1 != "")
- contractAddDisabled = "";
- else
- contractAddDisabled = "visible";
- }
- async Task<string> GetSHA3(string code)
- {
- var res = await bcMain.GetSHA3(code);
- Console.WriteLine($"GetSHA3 {code} {res}");
- var ret = res.Substring(2, 8);
- return ret;
- }
- async Task<string> GetSHA3_2()
- {
- var res = await bcMain.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 bc = Blockchain.GetMain();
- // //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 radr = await recipient.GetOrCreateActualAddress(bc);
- // var paramstring = "0x" + zerofill + radr.Remove(0, 2);
- // Console.WriteLine($"paramstring {paramstring}");
- // //todo pack myTransfer(address)
- // //todo pack contr.Address
- // if (bc.contractNames.ContainsKey("Transfer"))
- // {
- // var contr = bc.contractNames["Transfer"];
- // var res = await bc.RunContractWrite(contr.Address, $"{paramstring}");
- // Console.WriteLine("TransferContract result " + res);
- // var receipt = await bc.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 bc = bcMain;
- var addr = await acc.GetOrCreateActualAddress(bc);
- var res = await bc.SendTransaction(bcMain.address, addr, 50);
- }
- private async Task GetBalance()
- {
- Console.WriteLine("accountSelected " + accountSelected);
- if (accountSelected != "" && accountSelected != null && accountSelected != "0")
- {
- var acc = AccountModel.Find(accountSelected);
- if (acc != null)
- {
- myWallet = await acc.GetOrCreateActualAddress(bcMain);
- balance = await acc.GetBalance();
- }
- }
- else
- {
- myWallet = bcMain.address;
- balance = await bcMain.GetBalance();
- }
- await bcMain.GetBalanceToken("0xe5D682717955d6C35d465A3485625C64655a04f4", myWallet);
- }
- //private async Task CreateBlockchainAccount(Blockchain bc, string pass)
- //{
- // AccountModel account = AccountModel.Find(accountSelected);
- // if (account != null)
- // {
- // var res = await bc.CreateBlockchainAccount(account, pass);
- // }
- //}
- 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 bcMain.AddContract(newname, newcode, bytecode);
- if (taddr.Length > 0 && taddr[0] != "")
- {
- result = taddr[0].ToString();
- //hidereceipt = "visible";
- var caddr = await bcMain.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 (bcMain != null)
- {
- if (bcMain.contracts.ContainsKey(id))
- return bcMain.contracts[id];
- }
- return null;
- }
- protected override async Task OnInitializedAsync()
- {
- //Console.WriteLine($"Blazor is {Smart}");
- Console.WriteLine($"Blockchains OnInitializedAsync");
- //account = AppData.CurrentAccount;
- //var res = await storage.GetAsync<AccountModel>("acc");
- account = await GetCurrentAcc();
- //Console.WriteLine($"myFlag {myFlag.Value.Name}");
- Console.WriteLine($"Blockchains account {account}");
- bcMain = await account.GetSelectedBlockChain();
- Console.WriteLine($"Blockchains OnInitializedAsync {bcMain}");
- await GetBalance();
- }
- private async Task<AccountModel> GetCurrentAcc()
- {
- AccountModel account = new();
- var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
- var user = authState.User;
- if (user.Identity.IsAuthenticated)
- {
- var currentUser = await UserManager.GetUserAsync(user);
- account.UUID = currentUser.Id;
- //account.Name = currentUser.UserName;
- //account.Email = currentUser.Email;
- var acc = AccountModel.Find(account.UUID);
- if (acc != null)
- account = acc;
- ///tmp
- //account.AccRole = Role.User;
- return account;
- }
- 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();
- //}
- }
|