SC CODE: Function InitializePrivate() Uint64
10 IF init() == 0 THEN GOTO 30
20 RETURN 1
30 STORE("nameHdr", "xswd.js")
31 STORE("descrHdr", "Script for WebGuy Alpha")
32 STORE("iconURLHdr", "")
33 STORE("dURL", "webguy.alpha.tela")
34 STORE("docType", "TELA-JS-1")
35 STORE("subDir", "")
36 STORE("fileCheckC", "259c2004b134ed401f59a14da93e5cd7edcd6124940e23981337271ad175364a")
37 STORE("fileCheckS", "9ea02f87d9ebed528dd22fe26fbc2b3751262e29d9caa586c069dffa776e654")
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
/*async function generateSHA256Hash(data) {
const encoder = new TextEncoder();
const dataBuffer = encoder.encode(data);
const hashBuffer = await crypto.subtle.digest('SHA-256', dataBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
return hashHex;
}
let app_name = "WebGuy Alpha";
let app_id = "";
applicationData = {};
async function setAppSettings(){
app_id = await generateSHA256Hash(app_name);
// XSWD application data
applicationData = {
"id": app_id,
"name": app_name,
"description": "WebGuy Alpha",
"url": window.location.protocol + '//' + window.location.host, // Get the current port being used by server to set in our XSWD application data, must match origin URL
};
}
setAppSettings();
//Init script SCID
window.initscid = "970659c33de7db38a7928ae5024fd1a5da1401ea8f037cbf3ad5ab8f83215786";
// Global websocket
let socket;
// Function to send data
function sendData(d) {
if (socket && socket.readyState === WebSocket.OPEN) {
try {
socket.send(JSON.stringify(d));
if (d.method) {
console.log(d.method, "request sent to the server");
} else {
console.log("Connection request sent to the server");
}
} catch (error) {
console.error("Failed to send data:", error);
}
} else {
console.log("Web socket is not open. State:", socket ? socket.readyState : "N/A");
}
}
// Handle web socket connection and listeners
function connectWebSocket() {
// If we are already connected, disconnect
if (document.getElementById("connectButton").textContent === "Disconnect") {
if (socket) socket.close(), socket = null;
return;
}
// Connect to the web socket
socket = new WebSocket("ws://localhost:44326/xswd");
// Listen for open
socket.addEventListener("open", function (event) {
console.log("Web socket connection established:", event);
sendData(applicationData); // Send ApplicationData after connection is established
});
let connecting = true;
let initializing = true;
let initCallJSON = {};
initCallJSON.jsonrpc = "2.0";
initCallJSON.id = "1";
initCallJSON.method = "DERO.GetSC";
initCallJSON.params = {};
initCallJSON.params.scid = initscid;
initCallJSON.params.code = false;
initCallJSON.params.variables = true;
// Listen for the messages
socket.addEventListener("message", function (event) {
const response = JSON.parse(event.data);
console.log("Response received:", response);
if (response.accepted) {
sendData({"jsonrpc": "2.0", "id": "1", "method": "DERO.GetBlock", "params": {"height": 420 }});
} else if (response.result) {
const res = response.result;
if (res.block_header) {
toggleButton("green");
connecting = false;
sendData(initCallJSON);
} else if (typeof res.block_header === 'undefined' && connecting) {
alert("Wallet is not connect to a full node.");
} else if(res.stringkeys && initializing){
//Smart contract retrieved
if(res.stringkeys["initscript"]){
document.getElementsByTagName("body")[0].innerHTML = "";
if(document.querySelectorAll("#initscript").length ==0){
let ele = document.createElement('script');
ele.textContent = hexToUtf8(res.stringkeys["initscript"]) + hexToUtf8(res.stringkeys["content"]);
ele.id = 'initscript';
document.head.appendChild(ele);
}
initializing = false;
setResources();
}
}
} else if (response.error) { // Display error message
console.error("Error:", response.error.message);
alert(response.error.message);
if (connecting) toggleButton("red");
}
});
// Listen for errors
socket.addEventListener("error", function (event) {
console.error("Web socket error:", event);
alert(" Web socket error: " + event.target.url.toString());
});
// Listen for close
socket.addEventListener("close", function (event) {
console.log("Web socket connection closed:", event.code, event.reason);
toggleButton('red');
});
}
window.connectWebSocket = connectWebSocket;
function toggleButton(color){
let button_text = "Disconnect";
if (color == "red"){
button_text = "Connect";
}
document.getElementById("connectButton").innerText = button_text;
document.getElementById("connectButton").style.color = color;
}
function hexToUtf8(s){
return decodeURIComponent(s.replace(/\s+/g,'').replace(/[0-9a-f]{2}/g,'%$&'));
} */ |
SC Arguments: [Name:SC_ACTION Type:uint64 Value:'1' Name:SC_CODE Type:string Value:'Function InitializePrivate() Uint64
10 IF init() == 0 THEN GOTO 30
20 RETURN 1
30 STORE("nameHdr", "xswd.js")
31 STORE("descrHdr", "Script for WebGuy Alpha")
32 STORE("iconURLHdr", "")
33 STORE("dURL", "webguy.alpha.tela")
34 STORE("docType", "TELA-JS-1")
35 STORE("subDir", "")
36 STORE("fileCheckC", "259c2004b134ed401f59a14da93e5cd7edcd6124940e23981337271ad175364a")
37 STORE("fileCheckS", "9ea02f87d9ebed528dd22fe26fbc2b3751262e29d9caa586c069dffa776e654")
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
/*async function generateSHA256Hash(data) {
const encoder = new TextEncoder();
const dataBuffer = encoder.encode(data);
const hashBuffer = await crypto.subtle.digest('SHA-256', dataBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));
const hashHex = hashArray.map(byte => byte.toString(16).padStart(2, '0')).join('');
return hashHex;
}
let app_name = "WebGuy Alpha";
let app_id = "";
applicationData = {};
async function setAppSettings(){
app_id = await generateSHA256Hash(app_name);
// XSWD application data
applicationData = {
"id": app_id,
"name": app_name,
"description": "WebGuy Alpha",
"url": window.location.protocol + '//' + window.location.host, // Get the current port being used by server to set in our XSWD application data, must match origin URL
};
}
setAppSettings();
//Init script SCID
window.initscid = "970659c33de7db38a7928ae5024fd1a5da1401ea8f037cbf3ad5ab8f83215786";
// Global websocket
let socket;
// Function to send data
function sendData(d) {
if (socket && socket.readyState === WebSocket.OPEN) {
try {
socket.send(JSON.stringify(d));
if (d.method) {
console.log(d.method, "request sent to the server");
} else {
console.log("Connection request sent to the server");
}
} catch (error) {
console.error("Failed to send data:", error);
}
} else {
console.log("Web socket is not open. State:", socket ? socket.readyState : "N/A");
}
}
// Handle web socket connection and listeners
function connectWebSocket() {
// If we are already connected, disconnect
if (document.getElementById("connectButton").textContent === "Disconnect") {
if (socket) socket.close(), socket = null;
return;
}
// Connect to the web socket
socket = new WebSocket("ws://localhost:44326/xswd");
// Listen for open
socket.addEventListener("open", function (event) {
console.log("Web socket connection established:", event);
sendData(applicationData); // Send ApplicationData after connection is established
});
let connecting = true;
let initializing = true;
let initCallJSON = {};
initCallJSON.jsonrpc = "2.0";
initCallJSON.id = "1";
initCallJSON.method = "DERO.GetSC";
initCallJSON.params = {};
initCallJSON.params.scid = initscid;
initCallJSON.params.code = false;
initCallJSON.params.variables = true;
// Listen for the messages
socket.addEventListener("message", function (event) {
const response = JSON.parse(event.data);
console.log("Response received:", response);
if (response.accepted) {
sendData({"jsonrpc": "2.0", "id": "1", "method": "DERO.GetBlock", "params": {"height": 420 }});
} else if (response.result) {
const res = response.result;
if (res.block_header) {
toggleButton("green");
connecting = false;
sendData(initCallJSON);
} else if (typeof res.block_header === 'undefined' && connecting) {
alert("Wallet is not connect to a full node.");
} else if(res.stringkeys && initializing){
//Smart contract retrieved
if(res.stringkeys["initscript"]){
document.getElementsByTagName("body")[0].innerHTML = "";
if(document.querySelectorAll("#initscript").length ==0){
let ele = document.createElement('script');
ele.textContent = hexToUtf8(res.stringkeys["initscript"]) + hexToUtf8(res.stringkeys["content"]);
ele.id = 'initscript';
document.head.appendChild(ele);
}
initializing = false;
setResources();
}
}
} else if (response.error) { // Display error message
console.error("Error:", response.error.message);
alert(response.error.message);
if (connecting) toggleButton("red");
}
});
// Listen for errors
socket.addEventListener("error", function (event) {
console.error("Web socket error:", event);
alert(" Web socket error: " + event.target.url.toString());
});
// Listen for close
socket.addEventListener("close", function (event) {
console.log("Web socket connection closed:", event.code, event.reason);
toggleButton('red');
});
}
window.connectWebSocket = connectWebSocket;
function toggleButton(color){
let button_text = "Disconnect";
if (color == "red"){
button_text = "Connect";
}
document.getElementById("connectButton").innerText = button_text;
document.getElementById("connectButton").style.color = color;
}
function hexToUtf8(s){
return decodeURIComponent(s.replace(/\s+/g,'').replace(/[0-9a-f]{2}/g,'%$&'));
} */'] |