1. encryptDataField( nodeURL, data ) => Promise<[string, Uint8Array]>
This function helps you protect the information you send in transactions. It uses a special method (x25519) used to create a secret key that only you and the node can understand. Then, your data is put inside this locked box using Deoxys II encryption, this ensures that only you and the node can read and understand the information in transit.
The function returns a hexadecimal value which is the encrypted value of your data with the shared key with the node. This value can be safely included in your transaction data field. This way, even if someone intercepts your transaction, they won't be able to read, understand or decrypt your data.
/** * Encrypts provided transaction data using a random or provided encryption key * @param nodeURL URL of the node with JSON-RPC (e.g. https://json-rpc.testnet.swisstronik.com/) * @param data Raw transaction `data` field * @returns Encrypted `data` field for transaction in hex format & `usedEncryptedKey` in Uint8Array format */functionencryptDataField(nodeURL:string, data:string):Promise<[string,Uint8Array]>;//Implementationconstrpclink="https://json-rpc.testnet.swisstronik.com/"constdata="0x18160ddd"//function selector + encoded function arguments)const [encryptedData,usedEncryptedKey] =awaitencryptDataField(rpclink, data);//encryptedData == data encrypted using the encryptionKey//usedEncryptedKey == key generated using x25519 with the node
When you interact with the Swisstronik blockchain, you get responses back. But these responses are encrypted to keep them safe during their journey. The decryptNodeResponse function helps you unlock and understand these encrypted responses.
You need to provide the encrypted response (result from the call to the blockchain), the key (previously created using the encryptedDataField()), and the URL of the blockchain node.
The function will decrypt the encrypted response and will return an Uint8Array, which can be decoded using contract.interface.decodeFunctionResult() to translate it back to a readable state.
/** * Decrypts node response (call / transaction result data) * @param nodeURL URL of the node with JSON-RPC (e.g. https://json-rpc.testnet.swisstronik.com/) * @param encryptedResponse Encrypted returned data from the call to the blockchain * @param encryptionKey Key used for encryption * @returns Decrypted result */function decryptNodeResponse(nodeURL: string, encryptedResponse: string, encryptionKey: Uint8Array): Promise<Uint8Array>;
//Implementation//Encrypt the data (function selector + function arguments)const [encryptedData,usedEncryptedKey] =awaitencryptDataField(rpclink, data);//Call to the blockchain using ethers.jsconstresponse=awaitprovider.call({ to: destination,//Address of the smart contract to call data: encryptedData,//Data previously encrypted using encryptDataField(nodeURL,data) });//Decrypt the call result using decryptNodeResponse; this will return a Uint8ArrayconstdecryptedResponse=awaitdecryptNodeResponse(rpclink, response, usedEncryptedKey);//Decode the decryptedResponse using ethers.jsconstdecodedResponse=contract.interface.decodeFunctionResult(functionName, decryptedResponse)[0]
Let's deep into how to use these functions for sending transactions and making calls within the Swisstronik blockchain🚀