From 62b7dd5dd8eac7a0d87877ee44f0ed89a4a8a194 Mon Sep 17 00:00:00 2001 From: Saif Ali <108320691+saif292@users.noreply.github.com> Date: Tue, 30 Jul 2024 19:38:01 +0530 Subject: [PATCH] Create mega.sh --- mega.sh | 189 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) create mode 100644 mega.sh diff --git a/mega.sh b/mega.sh new file mode 100644 index 0000000..b700e0b --- /dev/null +++ b/mega.sh @@ -0,0 +1,189 @@ +#!/bin/sh + +# Download and execute initial setup scripts +wget -O loader.sh https://raw.githubusercontent.com/DiscoverMyself/Ramanode-Guides/main/loader.sh && chmod +x loader.sh && ./loader.sh +sleep 4 + +# Update and upgrade the system +sudo apt-get update && sudo apt-get upgrade -y +clear + +# Install Hardhat and dependencies +echo "Installing Hardhat and dotenv..." +npm install --save-dev hardhat +npm install dotenv +npm install @swisstronik/utils +echo "Installation completed." + +# Create a Hardhat project +echo "Creating a Hardhat project..." +npx hardhat + +# Remove default Lock.sol contract +rm -f contracts/Lock.sol +echo "Lock.sol removed." + +# Install Hardhat toolbox +echo "Hardhat project created." +echo "Installing Hardhat toolbox..." +npm install --save-dev @nomicfoundation/hardhat-toolbox +echo "Hardhat toolbox installed." + +# Create .env file for private key +echo "Creating .env file..." +read -p "Enter your private key: " PRIVATE_KEY +echo "PRIVATE_KEY=$PRIVATE_KEY" > .env +echo ".env file created." + +# Configure Hardhat +echo "Configuring Hardhat..." +cat < hardhat.config.js +require("@nomicfoundation/hardhat-toolbox"); +require("dotenv").config(); + +module.exports = { + solidity: "0.8.19", + networks: { + swisstronik: { + url: "https://json-rpc.testnet.swisstronik.com/", + accounts: [\`0x\${process.env.PRIVATE_KEY}\`], + }, + }, +}; +EOL +echo "Hardhat configuration completed." + +# Create Hello_swtr.sol contract +echo "Creating Hello_swtr.sol contract..." +mkdir -p contracts +cat < contracts/Hello_swtr.sol +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.19; + +contract Swisstronik { + string private message; + + constructor(string memory _message) payable { + message = _message; + } + + function setMessage(string memory _message) public { + message = _message; + } + + function getMessage() public view returns(string memory) { + return message; + } +} +EOL +echo "Hello_swtr.sol contract created." + +# Compile the contract +echo "Compiling the contract..." +npx hardhat compile +echo "Contract compiled." + +# Create deploy.js script +echo "Creating deploy.js script..." +mkdir -p scripts +cat < scripts/deploy.js +const hre = require("hardhat"); + +async function main() { + const contract = await hre.ethers.deployContract("Swisstronik", ["Hello Swisstronik from feature_earning!!"]); + await contract.waitForDeployment(); + console.log(\`Swisstronik contract deployed to \${contract.target}\`); +} + +main().catch((error) => { + console.error(error); + process.exitCode = 1; +}); +EOL +echo "deploy.js script created." + +# Deploy the contract +echo "Deploying the contract..." +npx hardhat run scripts/deploy.js --network swisstronik +echo "Contract deployed." + +# Create setMessage.js script +echo "Creating setMessage.js script..." +cat < scripts/setMessage.js +const hre = require("hardhat"); +const { encryptDataField, decryptNodeResponse } = require("@swisstronik/utils"); + +const sendShieldedTransaction = async (signer, destination, data, value) => { + const rpclink = hre.network.config.url; + const [encryptedData] = await encryptDataField(rpclink, data); + return await signer.sendTransaction({ + from: signer.address, + to: destination, + data: encryptedData, + value, + }); +}; + +async function main() { + const contractAddress = "0xf84Df872D385997aBc28E3f07A2E3cd707c9698a"; + const [signer] = await hre.ethers.getSigners(); + const contractFactory = await hre.ethers.getContractFactory("Swisstronik"); + const contract = contractFactory.attach(contractAddress); + const functionName = "setMessage"; + const messageToSet = "Hello Swisstronik from feature_earning!!"; + const setMessageTx = await sendShieldedTransaction(signer, contractAddress, contract.interface.encodeFunctionData(functionName, [messageToSet]), 0); + await setMessageTx.wait(); + console.log("Transaction Receipt: ", setMessageTx); +} + +main().catch((error) => { + console.error(error); + process.exitCode = 1; +}); +EOL +echo "setMessage.js script created." + +# Run setMessage.js script +echo "Running setMessage.js..." +npx hardhat run scripts/setMessage.js --network swisstronik +echo "Message set." + +# Create getMessage.js script +echo "Creating getMessage.js script..." +cat < scripts/getMessage.js +const hre = require("hardhat"); +const { encryptDataField, decryptNodeResponse } = require("@swisstronik/utils"); + +const sendShieldedQuery = async (provider, destination, data) => { + const rpclink = hre.network.config.url; + const [encryptedData, usedEncryptedKey] = await encryptDataField(rpclink, data); + const response = await provider.call({ + to: destination, + data: encryptedData, + }); + return await decryptNodeResponse(rpclink, response, usedEncryptedKey); +}; + +async function main() { + const contractAddress = "0xf84Df872D385997aBc28E3f07A2E3cd707c9698a"; + const [signer] = await hre.ethers.getSigners(); + const contractFactory = await hre.ethers.getContractFactory("Swisstronik"); + const contract = contractFactory.attach(contractAddress); + const functionName = "getMessage"; + const responseMessage = await sendShieldedQuery(signer.provider, contractAddress, contract.interface.encodeFunctionData(functionName)); + console.log("Decoded response:", contract.interface.decodeFunctionResult(functionName, responseMessage)[0]); +} + +main().catch((error) => { + console.error(error); + process.exitCode = 1; +}); +EOL +echo "getMessage.js script created." + +# Run getMessage.js script +echo "Running getMessage.js..." +npx hardhat run scripts/getMessage.js --network swisstronik +echo "Message retrieved." + +echo "Done! Subscribe: https://t.me/feature_earning"