2019-02-10 19:53:54 +00:00
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< meta http-equiv = "X-Frame-Options" value = "sameorigin" >
< title > DAPP Loading...< / title >
< link rel = "shortcut icon" href = "./tera.ico" type = "image/vnd.microsoft.icon" >
< script type = "text/javascript" src = "./JS/crypto-client.js" > < / script >
< script type = "text/javascript" src = "./JS/coinlib.js" > < / script >
< script type = "text/javascript" src = "./JS/client.js" > < / script >
< script type = "text/javascript" src = "./JS/terahashlib.js" > < / script >
< script >
2019-04-07 15:09:59 +00:00
window.Storage=window.localStorage;
2019-02-10 19:53:54 +00:00
var DapNumber="#template-number#";
var glSmart=parseInt(DapNumber);
var CONFIG_DATA={};
var NumDappGet=0;
var NumDappInfo=0;
var arr = new Uint8Array(6);
window.crypto.getRandomValues(arr);
var glSession=GetHexFromArr(arr);
//DAPP TRANSFER
function SendMessage(Data)
{
var win = window.frames.dapp;
if(!win)
{
console.log("Error window.frames.dapp");
//try latter
setTimeout(function ()
{
win.postMessage(Data, "*");
},200);
return;
}
//console.log("To dapp: "+JSON.stringify(Data))
win.postMessage(Data, "*");
}
var glProgramSetHash;
function listener(event)
{
var Data=event.data;
if(!Data || typeof Data!=="object")
return;
switch (Data.cmd)
{
case "pay":
{
AddToInvoiceList(Data);
break;
}
case "setstorage":
{
2019-04-07 15:09:59 +00:00
Storage.setItem("DAPP-"+DapNumber+"-"+Data.Key,JSON.stringify(Data.Value));
2019-02-10 19:53:54 +00:00
break;
}
case "getstorage":
{
2019-04-07 15:09:59 +00:00
Data.Value=Storage.getItem("DAPP-"+DapNumber+"-"+Data.Key);
2019-02-10 19:53:54 +00:00
if(Data.Value)
Data.Value=JSON.parse(Data.Value);
SendMessage(Data);
break;
}
case "setcommon":
{
2019-04-07 15:09:59 +00:00
Storage.setItem("DAPP-"+Data.Key,JSON.stringify(Data.Value));
2019-02-10 19:53:54 +00:00
break;
}
case "getcommon":
{
2019-04-07 15:09:59 +00:00
Data.Value=Storage.getItem("DAPP-"+Data.Key);
2019-02-10 19:53:54 +00:00
if(Data.Value)
Data.Value=JSON.parse(Data.Value);
SendMessage(Data);
break;
}
case "DappCall":
{
if(!Data.Account)
Data.Account=BASE_ACCOUNT.Num;
GetData("DappStaticCall",{Account:Data.Account,MethodName:Data.MethodName,Params:Data.Params}, function (SetData)
{
if(SetData)
{
Data.Err=!SetData.result;
Data.RetValue=SetData.RetValue;
}
else
{
Data.Err=1;
}
SendMessage(Data);
});
break;
}
case "DappSendCall":
{
if(!Data.Account)
Data.Account=BASE_ACCOUNT.Num;
if(!Data.FromNum)
Data.FromNum=0;
SendCallMethod(Data.Account,Data.MethodName,Data.Params,Data.FromNum,glSmart);
break;
}
case "DappInfo":
{
//called every 1 sec from dapp-inner.js
var AllData=0;
if(!NumDappGet || NumDappGet%60===0)
AllData=1;
NumDappGet++;
2019-04-07 15:09:59 +00:00
var Key=Storage.getItem("idPubKey");
2019-02-10 19:53:54 +00:00
GetData("DappInfo",{Smart:glSmart,Key:Key,Session:glSession,NumDappInfo:NumDappInfo,AllData:AllData,AllAccounts:Data.AllAccounts}, function (SetData)
{
if(SetData)
{
Data.Err=!SetData.result;
Data.cache=SetData.cache;
if(SetData.result)
{
CONFIG_DATA=SetData;
SMART=SetData.Smart;
BASE_ACCOUNT=SetData.Account;
SetBlockChainConstant(SetData);
NumDappInfo=SetData.NumDappInfo;
SetArrLog(SetData.ArrLog);
for(var key in SetData)
Data[key]=SetData[key];
Data.OPEN_PATH=OPEN_PATH;
if(!Data.PubKey)
{
//support web/light wallet mode:
2019-04-07 15:09:59 +00:00
Data.PubKey=Storage.getItem("idPubKey");
Data.WalletIsOpen=IsPrivateMode(Storage.getItem("idPrivKey"));
2019-02-10 19:53:54 +00:00
Data.WalletCanSign=Data.WalletIsOpen;
}
2019-04-05 10:13:44 +00:00
CONFIG_DATA.WalletCanSign=Data.WalletCanSign;
CONFIG_DATA.PubKey=Data.PubKey;
2019-02-10 19:53:54 +00:00
}
SendMessage(Data);
}
});
break;
}
case "DappWalletList":
2019-04-07 15:09:59 +00:00
var Key=Storage.getItem("idPubKey");
2019-02-10 19:53:54 +00:00
Data.Params={Smart:glSmart,Key:Key};
case "DappSmartHTMLFile":
case "DappBlockFile":
case "DappAccountList":
case "DappSmartList":
case "DappBlockList":
case "DappTransactionList":
{
if(Data.cmd==="DappBlockFile")
{
//check storage
var StrKeyStorage=Data.Params.BlockNum+"-"+Data.Params.TrNum;
2019-04-07 15:09:59 +00:00
var Storage2=sessionStorage;
var SavedTextData=Storage2[StrKeyStorage];
2019-02-10 19:53:54 +00:00
if(SavedTextData)
{
var SetData=JSON.parse(SavedTextData);
Data.Err=!SetData.result;
Data.arr=SetData.arr;
Data.Body=SetData.Body;
SendMessage(Data);
return;
}
}
Data.Params.Session=glSession;
GetData(Data.cmd,Data.Params, function (SetData,responseText)
{
if(SetData)
{
Data.Err=!SetData.result;
Data.arr=SetData.arr;
Data.Body=SetData.Body;
SendMessage(Data);
if(StrKeyStorage & & SetData.result)
{
2019-04-07 15:09:59 +00:00
Storage2[StrKeyStorage]=responseText;
2019-02-10 19:53:54 +00:00
}
}
});
break;
}
case "SetStatus":
{
SetStatus(escapeHtml(Data.Message));
break;
}
case "SetError":
{
SetError(escapeHtml(Data.Message));
break;
}
case "CheckInstall":
{
CheckInstall();
break;
}
case "SetLocationHash":
{
glProgramSetHash=1;
window.location.hash="#"+Data.Message;
glProgramSetHash=0;
break;
}
case "OpenLink":
{
2019-04-07 15:09:59 +00:00
var Path=Data.Message.substr(0,200);
if(IsLocalClient() & & Path.substr(0,6)==="/dapp/")
Path="?dapp="+Path.substr(6);
//console.log("OpenLink:"+Path);
window.open(Path);
2019-02-10 19:53:54 +00:00
break;
}
case "ComputeSecret":
{
2019-02-12 18:41:41 +00:00
ComputeSecret(Data.Account,Data.PubKey,function (Result)
2019-02-10 19:53:54 +00:00
{
Data.Result=Result;
SendMessage(Data);
});
break;
}
// case "GetLink":
// {
// GetData(Data.url,Data.Params, function (SetData)
// {
// if(SetData)
// {
// SendMessage({cmd:"GetLink",Result:SendData});
// }
// });
// break;
// }
2019-03-18 15:36:08 +00:00
case "SetNoScale":
{
SetNoScale();
break;
}
2019-02-10 19:53:54 +00:00
}
}
if (window.addEventListener)
{
window.addEventListener("message", listener);
} else
{
// IE8
window.attachEvent("onmessage", listener);
}
var SMART={},BASE_ACCOUNT={},OPEN_PATH="";
window.onload=function()
{
if(window.location.hash)
OPEN_PATH=window.location.hash.substr(1);
2019-04-07 15:09:59 +00:00
if(IsLocalClient())
2019-02-10 19:53:54 +00:00
{
DapNumber=window.location.search.substr(6);
2019-04-07 15:09:59 +00:00
if(Storage.getItem("MainServer"))
2019-02-10 19:53:54 +00:00
{
2019-04-07 15:09:59 +00:00
MainServer=JSON.parse(Storage.getItem("MainServer"));
2019-02-10 19:53:54 +00:00
}
}
glSmart=parseInt(DapNumber);
2019-04-07 15:09:59 +00:00
var Key=Storage.getItem("idPubKey");
2019-02-10 19:53:54 +00:00
GetData("DappInfo",{Smart:glSmart, Key:Key, Session:glSession, AllData:1}, function (SetData)
{
if(!SetData || !SetData.result)
return;
CONFIG_DATA=SetData;
SMART=SetData.Smart;
BASE_ACCOUNT=SetData.Account;
SetBlockChainConstant(SetData);
document.title=SMART.Name;
2019-03-16 16:08:05 +00:00
var HTMLBlock=BASE_ACCOUNT.SmartState.HTMLBlock;
var HTMLTr=BASE_ACCOUNT.SmartState.HTMLTr;
if(!glDebugPath & & HTMLBlock)
2019-02-10 19:53:54 +00:00
{
2019-03-16 16:08:05 +00:00
console.log("Load HTML from: /file/"+HTMLBlock+"/"+HTMLTr);
GetData("DappBlockFile",{BlockNum:HTMLBlock,TrNum:HTMLTr}, function (SetData)
2019-02-10 19:53:54 +00:00
{
if(SetData & & SetData.result)
{
CreateFrame(SetData);
}
});
}
else
{
GetData("DappSmartHTMLFile",{Smart:glSmart,DebugPath:glDebugPath}, function (SetData)
{
if(SetData & & SetData.result)
{
CreateFrame(SetData);
}
});
}
});
if(!window.sha3)
LoadLib("./JS/sha3.js");
LoadSignLib();
window.addEventListener("popstate", function(event)
{
OPEN_PATH=window.location.hash.substr(1);
if(!glProgramSetHash)
SendMessage({cmd:"History",OPEN_PATH:OPEN_PATH});
}, false);
}
function CreateFrame(SetData)
{
var SriptLW="";
if(MainServer)
{
var StrPath=GetProtocolServerPath(MainServer);
//console.log("StrPath:"+StrPath);
//console.log("WAS:\n"+SetData.Body);
//поддрежка старого кода dapp (после обновления dapp этот код возможно будет удален)
SetData.Body=SetData.Body.replace(/\/[0-9a-z_-]+.css\">/g,StrPath+"/CSS$&");
SetData.Body=SetData.Body.replace(/\/[0-9a-z_-]+.js\">/g,StrPath+"/JS$&");
//TODO: переделать на нормальный парсинг без регулярки
//поддержка перенаправления в блокчейн
SetData.Body=SetData.Body.replace(/\/file\/[0-9]+\/[0-9]+\"/g,StrPath+"$&");
SriptLW="< script > w i n d o w . P R O T O C O L _ S E R V E R _ P A T H = ' " + S t r P a t h + " ' ; < \ / s c r i p t > " ;
//console.log("NEW:\n"+SetData.Body);
}
var iframe = document.createElement('iframe');
iframe.name = 'dapp';
iframe.sandbox="allow-scripts";
SetData.Body='\
< meta charset = "UTF-8" > \
< meta http-equiv = "X-Frame-Options" value = "sameorigin" > \
< script type = "text/javascript" src = "./JS/crypto-client.js" > < \ / s c r i p t > \
< script type = "text/javascript" src = "./JS/coinlib.js" > < \ / s c r i p t > \
< script type = "text/javascript" src = "./JS/client.js" > < \ / s c r i p t > \
< script type = "text/javascript" src = "./JS/dapp-inner.js" > < \ / s c r i p t > \
< script type = "text/javascript" src = "./JS/terahashlib.js" > < \ / s c r i p t > \
'+SriptLW+SetData.Body;
iframe.srcdoc=SetData.Body;
document.getElementsByTagName('body')[0].appendChild(iframe);
2019-03-18 15:36:08 +00:00
}
2019-02-10 19:53:54 +00:00
2019-03-18 15:36:08 +00:00
function SetNoScale()
{
// var newchild = document.createElement("meta")
// newchin.name = "viewport"
// newchild.content = "width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0;"
// window.parent.document.getElementsByTagName("head")[0].appendChild(newchild);
}
2019-02-10 19:53:54 +00:00
2019-04-05 10:13:44 +00:00
var idInstallApp=0;
2019-02-10 19:53:54 +00:00
function CheckInstall()
{
2019-04-05 10:13:44 +00:00
if(!idInstallApp)
idInstallApp=setInterval(RunCheckInstall,2000);
}
function RunCheckInstall()
{
if(CONFIG_DATA & & CONFIG_DATA.ArrWallet & & CONFIG_DATA.ArrWallet.length===0)
2019-02-10 19:53:54 +00:00
{
2019-04-05 10:13:44 +00:00
var StrRef;
if(MainServer)
StrRef='< A class = "btcreate" href = "./web-wallet.html#TabAccounts" > Accounts< / A > ';
else
2019-02-10 19:53:54 +00:00
{
2019-04-07 15:09:59 +00:00
if(Storage.getItem("BIGWALLET"))
2019-04-05 10:13:44 +00:00
StrRef='< A class = "btcreate" href = "/wallet.html#TabAccounts" > Config< / A > '
2019-02-10 19:53:54 +00:00
else
2019-04-05 10:13:44 +00:00
StrRef='< A class = "btcreate" href = "/web-wallet.html#TabAccounts" > Accounts< / A > ';
2019-02-10 19:53:54 +00:00
}
2019-04-05 10:13:44 +00:00
var Str='< DIV style = "background-color: #c8ecff;" > For install this app press: < button class = "btcreate" onclick = "InstallApp()" > Create account< / button > or goto '+StrRef+' tab and Set smart number < B > '+SMART.Num+'< / B > to your account< DIV > ';
SetStatus(Str);
}
2019-02-10 19:53:54 +00:00
}
//LIB
function SetArrLog(arr)
{
var Str="";
if(arr)
for(var i=0;i< arr.length ; i + + )
{
var Item=arr[i];
if(!Item.final)
continue;
if(Item.text.indexOf("Add to blockchain")>=0)
continue;
var TR=MapSendTransaction[Item.key];
if(TR & & !TR.WasSend & & Item.final)
{
var Data={};
Data.cmd="OnEvent";
Data.Description="Error: "+Item.text;
Data.Error=1;
//console.log(JSON.stringify(Item));
SendMessage(Data);
TR.WasSend=1;
}
}
}
function IsPrivateMode(PrivKeyStr)
{
if(PrivKeyStr & & PrivKeyStr.length===64)
return 1;
else
return 0;
}
function SetStatus(Str)
{
var id = $("idStatus");
id.innerHTML=Str;
console.log(id.innerText);
}
function SetError(Str,bNoSound)
{
SetStatus("< DIV style = 'color:red' > < B > "+Str+"< / B > < / DIV > ");
}
2019-04-05 10:13:44 +00:00
function CreateNewWebKeys()
{
var arr = new Uint8Array(32);
window.crypto.getRandomValues(arr);
var PrivKey=GetHexFromArr(sha3(arr));
2019-04-07 15:09:59 +00:00
Storage.setItem("idPrivKey",PrivKey);
Storage.setItem("idPubKey",GetHexFromArr(SignLib.publicKeyCreate(PrivKey,1)));
CONFIG_DATA.PubKey=Storage.getItem("idPubKey");
2019-04-05 10:13:44 +00:00
console.log("CreateNewWebKeys: "+CONFIG_DATA.PubKey);
}
2019-02-10 19:53:54 +00:00
function InstallApp()
{
2019-04-05 10:13:44 +00:00
if(!CONFIG_DATA.WalletCanSign)
{
2019-04-07 15:09:59 +00:00
if(Storage.getItem("BIGWALLET"))
2019-04-05 10:13:44 +00:00
{
SetError("Pls, open wallet");
return;
}
CreateNewWebKeys();
}
SetStatus("Calculate Tx, wait pls ...");
//SendTrCreateAccWait(0,SMART.Name,SMART.Num);
//console.log("PubKey: "+CONFIG_DATA.PubKey);
var TR=GetTrCreateAcc(0,CONFIG_DATA.PubKey,SMART.Name,0,SMART.Num);
var Body=GetBodyCreateAcc(TR);
SendTransaction(Body,TR);
if(idInstallApp)
{
clearInterval(idInstallApp);
idInstallApp=setInterval(RunCheckInstall,30000);
}
2019-02-10 19:53:54 +00:00
}
< / script >
< style >
iframe
{
left:0;
top: 20px;
width: 100%;
height: 97vh;
border: 0;
}
#idStatus
{
text-align: center;
position:fixed;
top: 0;
left: 0;
width: 100%;
2019-04-07 15:09:59 +00:00
height:18px;
2019-02-10 19:53:54 +00:00
padding: 0;
margin: 0;
background-color:white;
}
2019-04-05 10:13:44 +00:00
.btcreate
{
2019-04-07 15:09:59 +00:00
height:18px;
2019-04-05 10:13:44 +00:00
background-color: #347867;
color: white;
cursor: pointer;
border-radius: 3px;
padding: 2px;
text-decoration: none;
border: 1px solid gray;
}
2019-02-10 19:53:54 +00:00
< / style >
< / head >
< body >
< DIV id = "idStatus" > < / DIV >
< / body >
< / html >
< script >
var glDebugPath="";
2019-04-07 15:09:59 +00:00
//glDebugPath="./dapp-smart/forum.html";
//glDebugPath="./dapp-smart/DEX.html";
2019-02-10 19:53:54 +00:00
//glDebugPath="./dapp-smart/OnlineStore.html";
2019-04-07 15:09:59 +00:00
//glDebugPath="./dapp-smart/BTC-coin.html";
//glDebugPath="./dapp-smart/static/miner-list.html";
//glDebugPath="./dapp-smart/static/ReadAccountArr.html";
//{"HTMLBlock":24201380,"HTMLTr":0}
2019-02-10 19:53:54 +00:00
< / script >