@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 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 = Blockchain.QNMAddress;
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;
string tid;
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)
{
@bcMain.url:@bcMain.port
Address: @bcMain.address
Balance: @bcMain.balance
@*
Add smart contract:
*@
@*
*@
@*
@ctrSelected.Code
@ctrSelected.Name
@bcMain.id;
*@
Balance: @balance
Wallet:@myWallet
Token balance:@balanceToken
@**@
Export address:
@((MarkupString)qrr)
@*Result: @result, gas: @gas
*@
}
@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 COMulti()
{
await bcMain.ChangeOwnerMulti();
}
async Task Transactions()
{
//bcMain.Transactions("0x862A2Ac8D1dB37085c786981A8F5bd54594f7162");
//await bcMain.SubmitTransaction("0xD3ae749f1253b320ea2b90dc235ed72c80447AFF", 144, "0xe5D682717955d6C35d465A3485625C64655a04f4");
var t1hash = await bcMain.GetTByHash(tid);
}
async Task Mint1000()
{
var acc = AccountModel.Find(accountSelected);
var bc = bcMain;
var addr = await acc.GetOrCreateActualAddress(bc);
await bcMain.Mint(tokenAmount, addr);
return "";
}
async Task PrivateKeyQR()
{
var code = await ExportPrivateKey();
code = HyperCube.Models.Blockchain.QRtest(code);
qrr = $"";
return "";
}
async Task ExportPrivateKey()
{
var wallet = await GetWallet();
if (wallet != null)
{
var acc = AccountModel.Find(accountSelected);
string text = getFiles(wallet, "key", acc.PWDHash);
qrr = $"{text}
";
return text;
//return await JsRuntime.InvokeAsync("test", text);
}
return "";
}
async Task DloadJSON()
{
var wallet = await GetWallet();
if (wallet != null)
{
string text = getFiles(wallet, "json");
qrr = $"{text}
";
return await JsRuntime.InvokeAsync("test", text);
}
return "";
}
async Task 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 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(Blockchain.MultiOwnerContractRinkeby, 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 GetSHA3(string code)
{
var res = await bcMain.GetSHA3(code);
Console.WriteLine($"GetSHA3 {code} {res}");
var ret = res.Substring(2, 8);
return ret;
}
async Task 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(Blockchain.QNMAddress, 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("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 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();
//}
}