SC CODE: // Copyright 2024. Civilware. All rights reserved.
// TELA Decentralized Web Document (TELA-DOC-1)
Function InitializePrivate() Uint64
10 IF init() == 0 THEN GOTO 30
20 RETURN 1
30 STORE("nameHdr", "scripts.js")
31 STORE("descrHdr", "JS")
32 STORE("iconURLHdr", "")
33 STORE("dURL", "scripts.js")
34 STORE("docType", "TELA-JS-1")
35 STORE("subDir", "js")
36 STORE("fileCheckC", "1434ebf4f7f7fbcae43ecac658076f185b364906d259084293c0c925c868b4a2")
37 STORE("fileCheckS", "2efa93881ae1199f7400f5d540b389d43f2395fe332d8c97d0a386b49b791ebf")
100 RETURN 0
End Function
Function init() Uint64
10 IF EXISTS("owner") == 0 THEN GOTO 30
20 RETURN 1
30 STORE("owner", address())
50 STORE("docVersion", "1.0.0")
60 STORE("hash", HEX(TXID()))
70 STORE("likes", 0)
80 STORE("dislikes", 0)
100 RETURN 0
End Function
Function address() String
10 DIM s as String
20 LET s = SIGNER()
30 IF IS_ADDRESS_VALID(s) THEN GOTO 50
40 RETURN "anon"
50 RETURN ADDRESS_STRING(s)
End Function
Function Rate(r Uint64) Uint64
10 DIM addr as String
15 LET addr = address()
16 IF r < 100 && EXISTS(addr) == 0 && addr != "anon" THEN GOTO 30
20 RETURN 1
30 STORE(addr, ""+r+"_"+BLOCK_HEIGHT())
40 IF r < 50 THEN GOTO 70
50 STORE("likes", LOAD("likes")+1)
60 RETURN 0
70 STORE("dislikes", LOAD("dislikes")+1)
100 RETURN 0
End Function
/*function gbid(elid){
return document.getElementById(elid);
}
var rando_dest="dero1qykyta6ntpd27nl0yq4xtzaf4ls6p5e9pqu0k2x4x3pqq5xavjsdxqgny8270"//"deto1qyvyeyzrcm2fzf6kyq7egkes2ufgny5xn77y6typhfx9s7w3mvyd5qqynr5hx""deto1qy2nxgts7wdn28ckc4l2tewphjcppqjfj69ddkxjn0ay8hlsjx73jqgmat5s8"//
var rando_signer=rando_dest
var doctypeselect = gbid('doc_type');
var docNameHdr = gbid('docNameHdr');
var docDescrHdr = gbid('docDescrHdr');
var docIconURLHdr = gbid('docIconURLHdr');
var docDURL = gbid('docDURL');
var docSubDir = gbid('docSubDir');
var docinputs = [docNameHdr,docDescrHdr,docIconURLHdr,docDURL,docSubDir];
docinputs.forEach((input) => {
input.addEventListener("keyup", docEdit);
input.addEventListener("change", docEdit);
input.addEventListener("blur", docEdit);
});
doctypeselect.addEventListener("change", docEdit);
function docEdit(eventorid) {
smartcontract.value = getDocSCCode();
let last_errors = errors.map(a => a.error);
let tid = '';
if(eventorid instanceof String || typeof eventorid === 'string'){
tid = eventorid;
}else{
tid = eventorid.target.id;
}
let ok = checkDocInputs(tid);
if(ok.ok){
addDocButton.removeAttribute("disabled");
}else {
addDocButton.setAttribute("disabled", true);
}
if(install_docs.length != 0){
deployDocsButton.removeAttribute("disabled");
}else {
deployDocsButton.setAttribute("disabled", true);
}
let same_error = JSON.stringify(ok.errors.map(a => a.error)) === JSON.stringify(last_errors);
if(gbid('dwarn') && (!same_error || ok.errors.length === 0)){gbid('dwarn').remove();}
if(ok.errors.length != 0){
let errorsText = "";
for (const error of ok.errors) {
errorsText = error.error + errorsText + "\n";
if(error.id !=='' && !same_error){
let warn = document.createElement("p");
warn.innerText = error.error;
warn.id ="dwarn";
warn.style.color = "red";
gbid(error.id).after(warn);
}
}
if(!same_error){
alert(errorsText);
}
}
}
var errors = [];
function checkDocInputs(tid){
let ok = true;
errors = [];
blob = new Blob([JSON.stringify(getQuoteJSON(smartcontract.value))+(fileCheckC ==""?'0'.repeat(128):"")])
if(blob.size > 20000){
if(tid === "docDescrHdr"){
errors.push({"id":tid,"type":"file","error":"Description or doc too large. " + blob.size +"bytes"});
}else{
errors.push({"id":tid,"type":"file","error":"Doc too large. " + blob.size +"bytes"});
}
ok = false;
}
if (docText.value.match(/\u002A\//)) {
errors.push({"id":tid,"type":"file","error":"Multi-line comments not allowed."});
ok = false;
}
if(
doctypeselect.value==""||
docNameHdr.value==""||
docDescrHdr.value==""||
docDURL.value==""||
fileCheckC==""||
fileCheckS==""||
docText.value==""||
filename==""
){
ok = false;
}
if(filename !=""){
let test_filename = "";
test_filename = filename.split('.').pop();
if("TELA-"+test_filename.toUpperCase()+"-1" != doctypeselect.value){
errors.push({"id":tid,"error":"File Extension doesn't match selected document type."});
ok = false;
}
if(install_docs.length > 0){
let full_loc = docSubDir.value + docNameHdr.value;
for (let x in install_docs) {
if(full_loc == install_docs[x].subdir + install_docs[x].filename){
errors.push({"id":tid,"error":"Doc already added!"});
ok = false;
}
}
}
if(installed_docs.length > 0){
let full_loc = docSubDir.value + docNameHdr.value;
for (let x in installed_docs) {
if(full_loc == installed_docs[x].subdir + installed_docs[x].filename){
errors.push({"id":tid,"error":"Doc already installed!"});
ok = false;
}
}
}
}
return {"ok":ok,"errors":errors};
}
var fileCheckC = "";
var fileCheckS = "";
var filename="";
var install_docs = [];
var installed_docs = [];
var fileContents = document.querySelector('input#open_file[type="file"]');
fileContents.addEventListener('change', async (e) => {
if(fileContents.files.length !== 1){
docText.value=""
filename=""
docNameHdr.value=""
fileContents.value=""
fileCheckC=""
fileCheckS=""
docEdit(e)
return
}
let file = fileContents.files[0]
let reader = new FileReader()
reader.onload = async () => {
docText.value = reader.result
filename = file.name
docNameHdr.value = filename
fileCheckC=""
fileCheckS=""
docEdit('open_file');
let signature = false
let result = errors.find(obj => obj.type === "file");
if(typeof result === "undefined"){
signature = await signData(docText.value)
}
if(!signature){
docText.value=""
filename=""
docNameHdr.value=""
fileContents.value=""
fileCheckC=""
fileCheckS=""
docEdit(e)
return
}
let signs = parseSignedFile(signature);
fileCheckC = signs.C;
fileCheckS = signs.S;
docEdit('open_file');
}
reader.readAsText(file)
});
function parseSignedFile(signed){
let c = signed.split('C: ', 2)[1];
let s = c.split('S: ', 2)[1];
c = c.split(/\r?\n/, 2)[0];
s = s.split(/\r?\n/, 2)[0];
return {"C":c,"S":s};
}
var smartcontract = gbid('smartcontract');
var docText = gbid('document');
var addDocButton = gbid('addDoc');
var deployDocsButton = gbid('deployDocs');
function getQuoteJSON(smart_contract){
let quoteJSON = {};
quoteJSON.jsonrpc = "2.0";
quoteJSON.id = "1";
quoteJSON.method = "DERO.GetGasEstimate";
quoteJSON.params = {};
quoteJSON.params.transfers = [{
destination: rando_dest,
amount:0,
Burn:0,
payload_rpc:[]
}];
quoteJSON.params.sc = smart_contract;
quoteJSON.params.sc_value = 0;
quoteJSON.params.sc_rpc = [];
quoteJSON.params.ringsize = 2;
quoteJSON.params.signer = rando_signer;
return quoteJSON;
}
addDocButton.addEventListener('click', function(event) {
let quoteJSON = getQuoteJSON(smartcontract.value);
let result = confirm("Estimated Cost: "+(niceRound(quoteJSON.params.sc.length * 2 * .00001)) +" DERO, Add Doc to Queue?");
if (result) {
addDoc();
} else {
alert("Action canceled.");
}
});
deployDocsButton.addEventListener('click', async function(event) {
let total_length = 0;
for (let x in install_docs) {
total_length += install_docs[x].contract.length;
}
let result = confirm("Estimated Total Cost for Docs: "+(niceRound(total_length * 2 * .00001)) +" DERO, Continue?");
if (result) {
deployDocsButton.setAttribute("disabled", true);
await installDocs();
alert("Documents Installed, Create an Index Next.");
if(install_docs.length != 0){
deployDocsButton.removeAttribute("disabled")
}
} else {
alert("Action canceled.");
}
});
//Install JSON
function getInstallJSON(smart_contract){
let installJSON = {};
installJSON.jsonrpc = "2.0";
installJSON.id = "1";
installJSON.method = "transfer";
installJSON.params = {};
installJSON.params.fees = 0;
installJSON.params.transfers = [{
destination: rando_dest,
amount:0,
Burn:0,
payload_rpc:[]
}];
installJSON.params.sc = smart_contract;
installJSON.params.sc_value = 0;
installJSON.params.sc_rpc = [];
installJSON.params.ringsize = 2;
return installJSON;
}
async function signData(data){
let JSONR = {};
JSONR.jsonrpc = "2.0";
JSONR.id = "0";
JSONR.method = "SignData";
JSONR.params = btoa(data);
response = await socketSend(JSONR);
if(response.result){
return atob(response.result.signature);
}else if(response.error){
alert(response.error.message);
}
return false;
}
function installList(){
let text=""
for (let x in install_docs) {
text += (install_docs[x].subdir !=""? install_docs[x].subdir + "/" :"") + install_docs[x].filename +"<br>"
}
return text
}
//Add Doc function
async function addDoc(){
install_docs.push({"subdir":docSubDir.value.replace(/^\/|\/|\/$/g, ''),"filename":filename,"docnamehdr":docNameHdr.value,"contract":smartcontract.value})
docNameHdr.value=""
filename=""
fileContents.value=""
fileCheckC=""
fileCheckS=""
gbid("install_output").innerHTML = installList();
docEdit('')
}
//Install Docs function
async function installDocs(){
if(install_docs.length > 0){
let progress = await installDoc(install_docs[0])
if(progress !== "success"){
gbid("installed_output").innerHTML = "Error occurred:"+ progress + "<br>" + gbid("installed_output").innerHTML;
}
return await installDocs();
}
return true
}
//Install Doc function
async function installDoc(doc){
let response = await socketSend(getInstallJSON(doc.contract));
if(response.result){
let installed = await confirmation(response.result.txid)
if (!installed) {
return "Problem Installing Doc, try again. TXID:"+response.result.txid
}
installed_docs.push({"subdir":doc.subdir,"filename":doc.filename,"scid":response.result.txid});
install_docs.shift();
gbid("install_output").innerHTML = installList();
let outputText ="";
for (let x in installed_docs) {
outputText += (installed_docs[x].subdir !=""? installed_docs[x].subdir + "/" :"") + installed_docs[x].filename + ":" + installed_docs[x].scid +"<br>";
}
let totdocs = install_docs.length + installed_docs.length;
let msg = "Installed Doc #"+installed_docs.length + " of "+ totdocs + "<br>"
if(installed_docs.length == totdocs){
msg = totdocs + " docs installed.<br>"
}
gbid("installed_output").innerHTML = msg + outputText;
return "success"
}else if(response.error){
return response.error.message
}
}
//Install Index function
async function installIndex(){
let response = await socketSend(getInstallJSON(indexSCCode.value));
if(response.result){
let installed = await confirmation(response.result.txid)
if (!installed) {
alert("Problem Installing Index, try again. TXID:"+response.result.txid);
return false;
}
gbid("indexscid").innerHTML = "Tela Site Index SCID:" + response.result.txid;
alert("Congrats, you're all done! This is your Tela Site SCID:" + response.result.txid);
}else if(response.error){
alert(response.error.message);
}
}
var indexNameHdr = gbid('indexNameHdr');
var indexDescrHdr = gbid('indexDescrHdr');
var indexIconURLHdr = gbid('indexIconURLHdr');
var indexDURL = gbid('indexDURL');
var indexSCCode = gbid('indexSCCode');
var deployIndexButton = gbid('deployIndex');
var generateIndexButton = gbid('generateIndex');
var indexinputs = [indexNameHdr,indexDescrHdr,indexIconURLHdr,indexDURL];
indexinputs.forEach((input) => {
input.addEventListener("keyup", indexEdit);
input.addEventListener("change", indexEdit);
input.addEventListener("blur", indexEdit);
});
function indexEdit(event) {
let docs ="";
let line_no = 40;
for (let x in installed_docs) {
docs += (line_no++) + ` STORE("DOC`+(parseFloat(x) + 1) + `", "` +installed_docs[x].scid + `")`+"\n";
}
indexSCCode.value = getIndexSC(docs);
let ok = checkIndexInputs();
if(ok.ok){
deployIndexButton.removeAttribute("disabled");
}else{
deployIndexButton.setAttribute("disabled", true);
}
if(ok.errors.length != 0){
let errorsText = "";
for (const error of ok.errors) {
errorsText = error + errorsText + "\n";
}
alert(errorsText);
}
}
deployIndexButton.addEventListener('click', function(event) {
let quoteJSON = getQuoteJSON(indexSCCode.value);
let result = confirm("Estimated Cost: "+(niceRound(quoteJSON.params.sc.length * 2 * .00001)) +" DERO, Install Tela-Index?");
if (result) {
installIndex(indexSCCode.value);
} else {
console.log("Action canceled.");
}
});
function checkIndexInputs(){
let ok = true;
let ierrors = [];
blob = new Blob([indexSCCode.value]);
if(blob.size > 20000){
ierrors.push("Doc too large.");
ok = false;
}
if(
indexNameHdr.value==""||
indexDescrHdr.value==""||
indexDURL.value==""||
indexSCCode.value==""||
installed_docs.length == 0
){
ok = false;
}
return {"ok":ok,"errors":ierrors};
}
function niceRound(number){
return Math.round(number*100000000)/100000000;
}
async function getHeight(){
let JSONR = {};
JSONR.jsonrpc = "2.0";
JSONR.method = "DERO.GetHeight";
response = await socketSend(JSONR);
if(response.result){
return response.result.height;
}else if(response.error){
await alert(response.error.message);
}
}
async function getTxStatus(txid){
let JSONR = {};
JSONR.jsonrpc = "2.0";
JSONR.id = 0;
JSONR.method = "DERO.GetTransaction";
JSONR.params = {};
JSONR.params.txs_hashes = [txid];
response = await socketSend(JSONR);
if(response.result){
if(response.result.status){
if(response.result.status == "OK"){
if(response.result.txs != null){
let tx = response.result.txs[0];
if ((!tx.in_pool && tx.valid_block=="")||tx.ignored) {
return false
}else if(tx.in_pool){
return false
}else{
return true
}
}
}
}
return false;
}else if(response.error){
console.log(response.error.message);
return "error"
}
}
var confirm_height=0;
async function confirmation(txid) {
confirm_height = await getHeight();
return new Promise(function (resolve) {
let attempts = 0;
const attempt = async() => {
attempts++;
let this_height = await getHeight();
if(this_height > confirm_height){
if(attempts > 36){
clearInterval(retryTimer);
resolve(false)
}
let status_ok = await getTxStatus(txid)
if(status_ok === true){
clearTimeout(retryTimer);
resolve(true)
}else if(status_ok==="error"){
attempts=attempts>0?attempts-1:0
}
}
}
let retryTimer = setInterval(attempt, 2000)
})
}
*/ |
SC Arguments: [Name:SC_ACTION Type:uint64 Value:'1' Name:SC_CODE Type:string Value:'// Copyright 2024. Civilware. All rights reserved.
// TELA Decentralized Web Document (TELA-DOC-1)
Function InitializePrivate() Uint64
10 IF init() == 0 THEN GOTO 30
20 RETURN 1
30 STORE("nameHdr", "scripts.js")
31 STORE("descrHdr", "JS")
32 STORE("iconURLHdr", "")
33 STORE("dURL", "scripts.js")
34 STORE("docType", "TELA-JS-1")
35 STORE("subDir", "js")
36 STORE("fileCheckC", "1434ebf4f7f7fbcae43ecac658076f185b364906d259084293c0c925c868b4a2")
37 STORE("fileCheckS", "2efa93881ae1199f7400f5d540b389d43f2395fe332d8c97d0a386b49b791ebf")
100 RETURN 0
End Function
Function init() Uint64
10 IF EXISTS("owner") == 0 THEN GOTO 30
20 RETURN 1
30 STORE("owner", address())
50 STORE("docVersion", "1.0.0")
60 STORE("hash", HEX(TXID()))
70 STORE("likes", 0)
80 STORE("dislikes", 0)
100 RETURN 0
End Function
Function address() String
10 DIM s as String
20 LET s = SIGNER()
30 IF IS_ADDRESS_VALID(s) THEN GOTO 50
40 RETURN "anon"
50 RETURN ADDRESS_STRING(s)
End Function
Function Rate(r Uint64) Uint64
10 DIM addr as String
15 LET addr = address()
16 IF r < 100 && EXISTS(addr) == 0 && addr != "anon" THEN GOTO 30
20 RETURN 1
30 STORE(addr, ""+r+"_"+BLOCK_HEIGHT())
40 IF r < 50 THEN GOTO 70
50 STORE("likes", LOAD("likes")+1)
60 RETURN 0
70 STORE("dislikes", LOAD("dislikes")+1)
100 RETURN 0
End Function
/*function gbid(elid){
return document.getElementById(elid);
}
var rando_dest="dero1qykyta6ntpd27nl0yq4xtzaf4ls6p5e9pqu0k2x4x3pqq5xavjsdxqgny8270"//"deto1qyvyeyzrcm2fzf6kyq7egkes2ufgny5xn77y6typhfx9s7w3mvyd5qqynr5hx""deto1qy2nxgts7wdn28ckc4l2tewphjcppqjfj69ddkxjn0ay8hlsjx73jqgmat5s8"//
var rando_signer=rando_dest
var doctypeselect = gbid('doc_type');
var docNameHdr = gbid('docNameHdr');
var docDescrHdr = gbid('docDescrHdr');
var docIconURLHdr = gbid('docIconURLHdr');
var docDURL = gbid('docDURL');
var docSubDir = gbid('docSubDir');
var docinputs = [docNameHdr,docDescrHdr,docIconURLHdr,docDURL,docSubDir];
docinputs.forEach((input) => {
input.addEventListener("keyup", docEdit);
input.addEventListener("change", docEdit);
input.addEventListener("blur", docEdit);
});
doctypeselect.addEventListener("change", docEdit);
function docEdit(eventorid) {
smartcontract.value = getDocSCCode();
let last_errors = errors.map(a => a.error);
let tid = '';
if(eventorid instanceof String || typeof eventorid === 'string'){
tid = eventorid;
}else{
tid = eventorid.target.id;
}
let ok = checkDocInputs(tid);
if(ok.ok){
addDocButton.removeAttribute("disabled");
}else {
addDocButton.setAttribute("disabled", true);
}
if(install_docs.length != 0){
deployDocsButton.removeAttribute("disabled");
}else {
deployDocsButton.setAttribute("disabled", true);
}
let same_error = JSON.stringify(ok.errors.map(a => a.error)) === JSON.stringify(last_errors);
if(gbid('dwarn') && (!same_error || ok.errors.length === 0)){gbid('dwarn').remove();}
if(ok.errors.length != 0){
let errorsText = "";
for (const error of ok.errors) {
errorsText = error.error + errorsText + "\n";
if(error.id !=='' && !same_error){
let warn = document.createElement("p");
warn.innerText = error.error;
warn.id ="dwarn";
warn.style.color = "red";
gbid(error.id).after(warn);
}
}
if(!same_error){
alert(errorsText);
}
}
}
var errors = [];
function checkDocInputs(tid){
let ok = true;
errors = [];
blob = new Blob([JSON.stringify(getQuoteJSON(smartcontract.value))+(fileCheckC ==""?'0'.repeat(128):"")])
if(blob.size > 20000){
if(tid === "docDescrHdr"){
errors.push({"id":tid,"type":"file","error":"Description or doc too large. " + blob.size +"bytes"});
}else{
errors.push({"id":tid,"type":"file","error":"Doc too large. " + blob.size +"bytes"});
}
ok = false;
}
if (docText.value.match(/\u002A\//)) {
errors.push({"id":tid,"type":"file","error":"Multi-line comments not allowed."});
ok = false;
}
if(
doctypeselect.value==""||
docNameHdr.value==""||
docDescrHdr.value==""||
docDURL.value==""||
fileCheckC==""||
fileCheckS==""||
docText.value==""||
filename==""
){
ok = false;
}
if(filename !=""){
let test_filename = "";
test_filename = filename.split('.').pop();
if("TELA-"+test_filename.toUpperCase()+"-1" != doctypeselect.value){
errors.push({"id":tid,"error":"File Extension doesn't match selected document type."});
ok = false;
}
if(install_docs.length > 0){
let full_loc = docSubDir.value + docNameHdr.value;
for (let x in install_docs) {
if(full_loc == install_docs[x].subdir + install_docs[x].filename){
errors.push({"id":tid,"error":"Doc already added!"});
ok = false;
}
}
}
if(installed_docs.length > 0){
let full_loc = docSubDir.value + docNameHdr.value;
for (let x in installed_docs) {
if(full_loc == installed_docs[x].subdir + installed_docs[x].filename){
errors.push({"id":tid,"error":"Doc already installed!"});
ok = false;
}
}
}
}
return {"ok":ok,"errors":errors};
}
var fileCheckC = "";
var fileCheckS = "";
var filename="";
var install_docs = [];
var installed_docs = [];
var fileContents = document.querySelector('input#open_file[type="file"]');
fileContents.addEventListener('change', async (e) => {
if(fileContents.files.length !== 1){
docText.value=""
filename=""
docNameHdr.value=""
fileContents.value=""
fileCheckC=""
fileCheckS=""
docEdit(e)
return
}
let file = fileContents.files[0]
let reader = new FileReader()
reader.onload = async () => {
docText.value = reader.result
filename = file.name
docNameHdr.value = filename
fileCheckC=""
fileCheckS=""
docEdit('open_file');
let signature = false
let result = errors.find(obj => obj.type === "file");
if(typeof result === "undefined"){
signature = await signData(docText.value)
}
if(!signature){
docText.value=""
filename=""
docNameHdr.value=""
fileContents.value=""
fileCheckC=""
fileCheckS=""
docEdit(e)
return
}
let signs = parseSignedFile(signature);
fileCheckC = signs.C;
fileCheckS = signs.S;
docEdit('open_file');
}
reader.readAsText(file)
});
function parseSignedFile(signed){
let c = signed.split('C: ', 2)[1];
let s = c.split('S: ', 2)[1];
c = c.split(/\r?\n/, 2)[0];
s = s.split(/\r?\n/, 2)[0];
return {"C":c,"S":s};
}
var smartcontract = gbid('smartcontract');
var docText = gbid('document');
var addDocButton = gbid('addDoc');
var deployDocsButton = gbid('deployDocs');
function getQuoteJSON(smart_contract){
let quoteJSON = {};
quoteJSON.jsonrpc = "2.0";
quoteJSON.id = "1";
quoteJSON.method = "DERO.GetGasEstimate";
quoteJSON.params = {};
quoteJSON.params.transfers = [{
destination: rando_dest,
amount:0,
Burn:0,
payload_rpc:[]
}];
quoteJSON.params.sc = smart_contract;
quoteJSON.params.sc_value = 0;
quoteJSON.params.sc_rpc = [];
quoteJSON.params.ringsize = 2;
quoteJSON.params.signer = rando_signer;
return quoteJSON;
}
addDocButton.addEventListener('click', function(event) {
let quoteJSON = getQuoteJSON(smartcontract.value);
let result = confirm("Estimated Cost: "+(niceRound(quoteJSON.params.sc.length * 2 * .00001)) +" DERO, Add Doc to Queue?");
if (result) {
addDoc();
} else {
alert("Action canceled.");
}
});
deployDocsButton.addEventListener('click', async function(event) {
let total_length = 0;
for (let x in install_docs) {
total_length += install_docs[x].contract.length;
}
let result = confirm("Estimated Total Cost for Docs: "+(niceRound(total_length * 2 * .00001)) +" DERO, Continue?");
if (result) {
deployDocsButton.setAttribute("disabled", true);
await installDocs();
alert("Documents Installed, Create an Index Next.");
if(install_docs.length != 0){
deployDocsButton.removeAttribute("disabled")
}
} else {
alert("Action canceled.");
}
});
//Install JSON
function getInstallJSON(smart_contract){
let installJSON = {};
installJSON.jsonrpc = "2.0";
installJSON.id = "1";
installJSON.method = "transfer";
installJSON.params = {};
installJSON.params.fees = 0;
installJSON.params.transfers = [{
destination: rando_dest,
amount:0,
Burn:0,
payload_rpc:[]
}];
installJSON.params.sc = smart_contract;
installJSON.params.sc_value = 0;
installJSON.params.sc_rpc = [];
installJSON.params.ringsize = 2;
return installJSON;
}
async function signData(data){
let JSONR = {};
JSONR.jsonrpc = "2.0";
JSONR.id = "0";
JSONR.method = "SignData";
JSONR.params = btoa(data);
response = await socketSend(JSONR);
if(response.result){
return atob(response.result.signature);
}else if(response.error){
alert(response.error.message);
}
return false;
}
function installList(){
let text=""
for (let x in install_docs) {
text += (install_docs[x].subdir !=""? install_docs[x].subdir + "/" :"") + install_docs[x].filename +"<br>"
}
return text
}
//Add Doc function
async function addDoc(){
install_docs.push({"subdir":docSubDir.value.replace(/^\/|\/|\/$/g, ''),"filename":filename,"docnamehdr":docNameHdr.value,"contract":smartcontract.value})
docNameHdr.value=""
filename=""
fileContents.value=""
fileCheckC=""
fileCheckS=""
gbid("install_output").innerHTML = installList();
docEdit('')
}
//Install Docs function
async function installDocs(){
if(install_docs.length > 0){
let progress = await installDoc(install_docs[0])
if(progress !== "success"){
gbid("installed_output").innerHTML = "Error occurred:"+ progress + "<br>" + gbid("installed_output").innerHTML;
}
return await installDocs();
}
return true
}
//Install Doc function
async function installDoc(doc){
let response = await socketSend(getInstallJSON(doc.contract));
if(response.result){
let installed = await confirmation(response.result.txid)
if (!installed) {
return "Problem Installing Doc, try again. TXID:"+response.result.txid
}
installed_docs.push({"subdir":doc.subdir,"filename":doc.filename,"scid":response.result.txid});
install_docs.shift();
gbid("install_output").innerHTML = installList();
let outputText ="";
for (let x in installed_docs) {
outputText += (installed_docs[x].subdir !=""? installed_docs[x].subdir + "/" :"") + installed_docs[x].filename + ":" + installed_docs[x].scid +"<br>";
}
let totdocs = install_docs.length + installed_docs.length;
let msg = "Installed Doc #"+installed_docs.length + " of "+ totdocs + "<br>"
if(installed_docs.length == totdocs){
msg = totdocs + " docs installed.<br>"
}
gbid("installed_output").innerHTML = msg + outputText;
return "success"
}else if(response.error){
return response.error.message
}
}
//Install Index function
async function installIndex(){
let response = await socketSend(getInstallJSON(indexSCCode.value));
if(response.result){
let installed = await confirmation(response.result.txid)
if (!installed) {
alert("Problem Installing Index, try again. TXID:"+response.result.txid);
return false;
}
gbid("indexscid").innerHTML = "Tela Site Index SCID:" + response.result.txid;
alert("Congrats, you're all done! This is your Tela Site SCID:" + response.result.txid);
}else if(response.error){
alert(response.error.message);
}
}
var indexNameHdr = gbid('indexNameHdr');
var indexDescrHdr = gbid('indexDescrHdr');
var indexIconURLHdr = gbid('indexIconURLHdr');
var indexDURL = gbid('indexDURL');
var indexSCCode = gbid('indexSCCode');
var deployIndexButton = gbid('deployIndex');
var generateIndexButton = gbid('generateIndex');
var indexinputs = [indexNameHdr,indexDescrHdr,indexIconURLHdr,indexDURL];
indexinputs.forEach((input) => {
input.addEventListener("keyup", indexEdit);
input.addEventListener("change", indexEdit);
input.addEventListener("blur", indexEdit);
});
function indexEdit(event) {
let docs ="";
let line_no = 40;
for (let x in installed_docs) {
docs += (line_no++) + ` STORE("DOC`+(parseFloat(x) + 1) + `", "` +installed_docs[x].scid + `")`+"\n";
}
indexSCCode.value = getIndexSC(docs);
let ok = checkIndexInputs();
if(ok.ok){
deployIndexButton.removeAttribute("disabled");
}else{
deployIndexButton.setAttribute("disabled", true);
}
if(ok.errors.length != 0){
let errorsText = "";
for (const error of ok.errors) {
errorsText = error + errorsText + "\n";
}
alert(errorsText);
}
}
deployIndexButton.addEventListener('click', function(event) {
let quoteJSON = getQuoteJSON(indexSCCode.value);
let result = confirm("Estimated Cost: "+(niceRound(quoteJSON.params.sc.length * 2 * .00001)) +" DERO, Install Tela-Index?");
if (result) {
installIndex(indexSCCode.value);
} else {
console.log("Action canceled.");
}
});
function checkIndexInputs(){
let ok = true;
let ierrors = [];
blob = new Blob([indexSCCode.value]);
if(blob.size > 20000){
ierrors.push("Doc too large.");
ok = false;
}
if(
indexNameHdr.value==""||
indexDescrHdr.value==""||
indexDURL.value==""||
indexSCCode.value==""||
installed_docs.length == 0
){
ok = false;
}
return {"ok":ok,"errors":ierrors};
}
function niceRound(number){
return Math.round(number*100000000)/100000000;
}
async function getHeight(){
let JSONR = {};
JSONR.jsonrpc = "2.0";
JSONR.method = "DERO.GetHeight";
response = await socketSend(JSONR);
if(response.result){
return response.result.height;
}else if(response.error){
await alert(response.error.message);
}
}
async function getTxStatus(txid){
let JSONR = {};
JSONR.jsonrpc = "2.0";
JSONR.id = 0;
JSONR.method = "DERO.GetTransaction";
JSONR.params = {};
JSONR.params.txs_hashes = [txid];
response = await socketSend(JSONR);
if(response.result){
if(response.result.status){
if(response.result.status == "OK"){
if(response.result.txs != null){
let tx = response.result.txs[0];
if ((!tx.in_pool && tx.valid_block=="")||tx.ignored) {
return false
}else if(tx.in_pool){
return false
}else{
return true
}
}
}
}
return false;
}else if(response.error){
console.log(response.error.message);
return "error"
}
}
var confirm_height=0;
async function confirmation(txid) {
confirm_height = await getHeight();
return new Promise(function (resolve) {
let attempts = 0;
const attempt = async() => {
attempts++;
let this_height = await getHeight();
if(this_height > confirm_height){
if(attempts > 36){
clearInterval(retryTimer);
resolve(false)
}
let status_ok = await getTxStatus(txid)
if(status_ok === true){
clearTimeout(retryTimer);
resolve(true)
}else if(status_ok==="error"){
attempts=attempts>0?attempts-1:0
}
}
}
let retryTimer = setInterval(attempt, 2000)
})
}
*/'] |