Skip to content

Commit

Permalink
Control and local Relayed TXS as well as other refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
vncoelho committed Nov 21, 2023
1 parent 7f7fd38 commit 43c6745
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 24 deletions.
34 changes: 34 additions & 0 deletions assets/eco-scripts/activity_localStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function getActivitiesFromLocalStorage() {
var actitiviesRelayedTxs = getLocalStorage("myactivities");
if (actitiviesRelayedTxs)
return JSON.parse(actitiviesRelayedTxs)

return new [];
}

function storeActivitiesToLocalStorage() {
setLocalStorage("myactivities", JSON.stringify(RELAYED_TXS));
}

function restoreRelayedTxsFromLocalStorage() {
RELAYED_TXS = getActivitiesFromLocalStorage();
drawRelayedTXs();
}

function cleanAllRelayedTxs() {
swal({
title: "Delete all Relayed Txs activity?",
text: "This is permanent and you won't be able to retrieve it.",
icon: "warning",
buttons: ["Cancel", "Delete it!"],
dangerMode: true,
}).then((willDelete) => {
if (willDelete) {
RELAYED_TXS = [];
storeActivitiesToLocalStorage();
drawRelayedTXs();
} else {
swal("Safe! Relayed TXs are preserved.");
}
});
}
8 changes: 5 additions & 3 deletions assets/eco-scripts/contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ function deleteLocalContract() {
});
}

function invokeFunctionWithParams(contractHash, method, params, relay = false) {
function invokeFunctionWithParams(contractHash, method, params, relay) {
if (!checkIfWalletIsConnected())
return;

Expand Down Expand Up @@ -443,7 +443,8 @@ function invokeFunction() {
params.push(parameter);
}

invokeFunctionWithParams(CONTRACTS_TO_LIST[getCurrentSelectedContract()].hash, method.name, params);
var autoRelay = false;
invokeFunctionWithParams(CONTRACTS_TO_LIST[getCurrentSelectedContract()].hash, method.name, params, autoRelay);
}

function findNetworkFee() {
Expand Down Expand Up @@ -527,7 +528,8 @@ function deployContract() {
var expectedContractHash = Neon.experimental.getContractHash(sender, checkSum, LOCAL_CONTRACTS[contractToDeployID].manifest.name);
PENDING_CONTRACTS.push(expectedContractHash);

invokeFunctionWithParams(contractManagementHash, "deploy", params)
var autoRelay = false;
invokeFunctionWithParams(contractManagementHash, "deploy", params, autoRelay)
}

function getNep17HashByName(name) {
Expand Down
42 changes: 26 additions & 16 deletions assets/eco-scripts/rawRpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,17 @@ function rawRpcCall(fillRealTx = false, saveID = -1, relay = false, blinkRelay =
fillRealTxFromInvokeFunction();

if (saveID != -1)
{
// updates saved tx with input
RELAYED_TXS[saveID].push(data);
storeActivitiesToLocalStorage();
}

if (relay) {
signAndRelay(false);
signAndRelay();
} else {
if (blinkRelay && fillRealTx) {
$('#signAndRelayDivCollapse').collapse('show');
$("#relay_btn")[0].disabled = false;
addBlinkToElement("#relay_btn");
enableSignRelayAndBlink();
} else {
$("#relay_btn")[0].disabled = true;
$('#signAndRelayDivCollapse').collapse('hide');
Expand All @@ -47,6 +49,12 @@ function rawRpcCall(fillRealTx = false, saveID = -1, relay = false, blinkRelay =
}); //End of POST for search
}

function enableSignRelayAndBlink() {
$('#signAndRelayDivCollapse').collapse('show');
$("#relay_btn")[0].disabled = false;
addBlinkToElement("#relay_btn");
}

function drawSigners() {
var table = document.createElement("tbody");
var headerRow = document.createElement('tr');
Expand Down Expand Up @@ -110,10 +118,11 @@ function cleanRealTxInvoke() {
document.getElementById("tableSigners").innerHTML = "";
}

function signAndRelay(blinkRelay = true) {
function signAndRelay() {
if (!checkIfWalletIsConnected())
return;

//Network fee should be passed to DAPI
var netFee = Math.ceil(getFixed8Integer($("#net_fee")[0].value));
//============================================
//This is dapi call from parameters
Expand All @@ -133,8 +142,6 @@ function signAndRelay(blinkRelay = true) {
//============================================

var sysFee = Math.ceil(getFixed8Integer($("#sys_fee")[0].value));
//var sysFee = getFixed8Integer(500);
//var netFee = getFixed8Integer(600);
var validUntil = Number($("#valid_until")[0].value);
var script = $("#tx_script")[0].value;

Expand All @@ -161,29 +168,32 @@ function signAndRelay(blinkRelay = true) {
invokeFunction: $("#txtRPCJson").val(),
invokeFunctionOut: $("#txtRPCJsonOut").val()
});

// Create Field for TXs
RELAYED_TXS.push(relayedTX);
storeActivitiesToLocalStorage();


console.log(tx)
console.log(tx.serialize(true))
//console.log(tx)
//console.log(tx.serialize(true))
var rawTX = Neon.u.hex2base64(tx.serialize(true));


var jsonForInvokingFunction = {
"jsonrpc": "2.0",
"id": 2,
"method": "sendrawtransaction",
"params": [rawTX]
};

var jsonToCallStringified = JSON.stringify(jsonForInvokingFunction);

$("#txtRPCJson").val(jsonToCallStringified);
rawRpcCall(false, RELAYED_TXS.length - 1, false, blinkRelay);
//var client = new Neon.rpc.RPCClient(BASE_PATH_CLI);
//RELAYED_TXS.push(client.sendRawTransaction(tx));
var fillRealTx = false;
var autoRelay = false;
var autoBlink = false;
var savedTXID = RELAYED_TXS.length - 1;
rawRpcCall(fillRealTx, savedTXID, autoRelay, autoBlink);
}

function signTx(tx){
function signTx(tx) {
if (isMultiSig(CONNECTED_WALLET_ID)) {
var publicKeys = Neon.wallet.getPublicKeysFromVerificationScript(Neon.u.base642hex(ECO_WALLET[CONNECTED_WALLET_ID].account.contract.script))
var threshold = Neon.wallet.getSigningThresholdFromVerificationScript(Neon.u.base642hex(ECO_WALLET[CONNECTED_WALLET_ID].account.contract.script))
Expand Down
6 changes: 5 additions & 1 deletion assets/eco-scripts/relayed_txs.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ function removeRelayedTX(idToRemove) {

//===============================================================
function restoreTX(idToRestore) {
console.log(idToRestore)
var txToRestore = RELAYED_TXS[idToRestore];
//console.log(txToRestore);
console.log(txToRestore);

document.getElementById("tableSigners").appendChild(txToRestore[0].signers);
$("#sys_fee")[0].value = txToRestore[0].sysfee;
Expand All @@ -191,5 +192,8 @@ function restoreTX(idToRestore) {
$("#txtRPCJsonOut")[0].value = txToRestore[0].invokeFunctionOut;

goToTabAndClick("nav-rpc");

if (checkIfWalletIsConnected())
enableSignRelayAndBlink();
}
//===============================================================
3 changes: 2 additions & 1 deletion assets/eco-scripts/wallet_center.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,8 @@ function createNep17Tx() {
var contractHash = getNep17HashByName($("#labelForTransferAsset")[0].textContent);
var method = "transfer";

invokeFunctionWithParams(contractHash, method, params);
var autoRelay = false;
invokeFunctionWithParams(contractHash, method, params, autoRelay);

swal({
icon: "success",
Expand Down
17 changes: 14 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,7 @@ <h4 class="green-title">Request from Neo C# nodes (RPC-JSON API)</h4>
<div class="row mb-2">
<div class="col-md-12">
<button disabled type="button" id="relay_btn" style="width: 100%"
class="btn btn-default btn-1 btn-1-darker" onclick="signAndRelay(false)">
class="btn btn-default btn-1 btn-1-darker" onclick="signAndRelay()">
<i class="fa-solid fa-signature"></i> Sign and Relay to Blockchain
</button>
</div>
Expand Down Expand Up @@ -630,8 +630,14 @@ <h4 class="green-title">Request from Neo C# nodes (RPC-JSON API)</h4>

<div class="generalContainer">
<div class="standardContainer">
<h4 class="green-title">Historical Relayed TX's <button type="submit" onclick="drawRelayedTXs()"
class="btn btn-info btn-sm"><i class="fas fa-sync-alt"></i></button></h4>
<h4 class="green-title">Historical Relayed TX's
<button type="submit" onclick="drawRelayedTXs()" class="btn btn-info btn-sm"><i
class="fas fa-sync-alt"></i>
</button>
<button type="submit" onclick="cleanAllRelayedTxs()" class="btn btn-danger btn-sm">
Delete All <i class="fas fa-trash"></i>
</button>
</h4>
<div class="container">
<div class="row" style="padding:0 20px 0 20px;">
<table id="tableRelayedTxs" class="table text-white twalllet">
Expand Down Expand Up @@ -1425,6 +1431,8 @@ <h4 class="green-title">

<script src="assets/eco-scripts/compile_codes_localStorage.js"></script>

<script src="assets/eco-scripts/activity_localStorage.js"></script>

<script src="assets/eco-scripts/compile.js"></script>

<script src="assets/eco-scripts/opcodes.js"></script>
Expand Down Expand Up @@ -1497,6 +1505,9 @@ <h4 class="green-title">
//Add JSON RpcMethod to box
addJsonRPCMethodsToSelectionBox();

// Restores relayed txs from LocalStorage
restoreRelayedTxsFromLocalStorage();

// Call tutorial
if (getLocalStorage("disabledTutoral") != "1")
nextTutorial();
Expand Down

0 comments on commit 43c6745

Please sign in to comment.