Make sure you have installed SwisstronikJS by running npm i @swisstronik/utils
Within the scripts folder, create a file called getMessage.js
Let's write our getMessage.js script
Import hardhat and swisstronikJS functions
//Import hardhat and swisstronikJS functionsconsthre=require("hardhat");const { encryptDataField,decryptNodeResponse } =require("@swisstronik/utils");
Let's use sendShieldedQuery to send a call/query to the blockchain
/** * Send a shielded query/call to the Swisstronik blockchain. * * @param{object} provider - The provider object for making the call. * @param{string} destination - The address of the contract to call. * @param{string} data - Encoded data for the function call. * * @returns{Uint8Array} - Encrypted response from the blockchain. */constsendShieldedQuery=async (provider, destination, data) => {// Get the RPC link from the network configurationconstrpclink=hre.network.config.url;// Encrypt the call data using the SwisstronikJS function encryptDataField()const [encryptedData,usedEncryptedKey] =awaitencryptDataField(rpclink, data);// Execute the call/query using the providerconstresponse=awaitprovider.call({ to: destination, data: encryptedData, });// Decrypt the call result using SwisstronikJS function decryptNodeResponse()returnawaitdecryptNodeResponse(rpclink, response, usedEncryptedKey);};
Write the main script to use sendShieldedQuery function
asyncfunctionmain() {// Address of the deployed contractconstcontractAddress="0xf84Df872D385997aBc28E3f07A2E3cd707c9698a";// Get the signer (your account)const [signer] =awaithre.ethers.getSigners();// Construct a contract instance (must match the name of contract)constcontractFactory=awaithre.ethers.getContractFactory("Swisstronik");constcontract=contractFactory.attach(contractAddress);// Send a shielded query to retrieve data from the contractconstfunctionName="getMessage"; const responseMessage = await sendShieldedQuery(signer.provider, contractAddress, contract.interface.encodeFunctionData(functionName));
// Decode the Uint8Array response into a readable stringconsole.log("Decoded response:",contract.interface.decodeFunctionResult(functionName, responseMessage)[0]);}