Swisstronik Docs
  • 🇨🇭About Swisstronik
    • What is Swisstronik?
    • Swisstronik vs Ethereum
    • Useful links
  • 🌐General
    • Intel SGX
    • Governance
      • On-Chain Proposal
      • Formatting a Proposal
      • Submitting a Proposal
      • Governable Parameters
    • Community Pool
    • Accounts
    • Transactions
    • Gas and Fees
    • Coin & Tokens
  • ⚒️Development
    • Endpoints
    • Connect wallets
      • MetaMask (EVM) - Manual
      • Keplr (Cosmos)
      • Testnet Metamask (EVM) - Chainlist
      • Testnet MetaMask (EVM) - Manual
      • Testnet Keplr (Cosmos)
    • Get test coins
    • SwisstronikJS SDK
      • SwisstronikJS Functions
      • Swisstronik & Ethereum transactions
      • Transaction - code sample
      • Swisstronik & Ethereum calls
      • Call - code sample
    • Swisstronik CLI
      • Installation
      • Configuration
      • Key management
      • Queries
      • Sending SWTR
      • For SDI Issuers
    • Guides
      • Contract deployment - Hardhat
        • 1. Setting up the Hardhat environment
        • 2. Configure hardhat.config.js
        • 3. Write and compile the smart contract
        • 4. Deploy the smart contract
        • 5. Interact with the contract - Transaction
        • 6. Interact with the contract - Call
      • Contract deployment - Remix IDE
      • Deployment & Interaction PERC-20
        • ERC-20 & PERC-20
        • Sample PERC20 contract
        • Deployment & Interaction with contract
        • How to use encryption
      • Contract verification
      • SDI for dapp developers
    • Local testnet
    • Web3JS, Ethers, Viem and other third-party libraries
  • 🖥️Node setup
    • Setup SGX
      • Supported Hardware
      • Setup Intel SGX
    • Setup node
      • Mainnet
        • v1.0.1 Upgrade
      • Testnet
        • Install from .deb package
        • Configure node
        • Upgrade your testnet node
          • Swisstronik v3 Testnet
          • Swisstronik v4 Testnet
          • Swisstronik v5 Testnet
          • Swisstronik v5.1 Testnet
          • Swisstronik v6 Testnet
          • Swisstronik v7 Testnet
          • Swisstronik v8 Testnet
        • Seed Node
        • Live Peers
        • Genesis Mirror Download
    • CLI Cheatsheet
  • 🤝Delegators
    • What is a delegator?
    • Delegator Guide (CLI)
    • Delegators Security
Powered by GitBook
On this page
  • 1. encryptDataField( nodeURL, data ) => Promise<[string, Uint8Array]>
  • 2. decryptNodeResponse ( nodeURL, data, usedEncryptedKey ) => Promise<Uint8Array>
  1. Development
  2. SwisstronikJS SDK

SwisstronikJS Functions

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
 */
function encryptDataField(nodeURL: string, data: string): Promise<[string, Uint8Array]>;

//Implementation
const rpclink = "https://json-rpc.testnet.swisstronik.com/"
const data = "0x18160ddd" //function selector + encoded function arguments)

const [encryptedData, usedEncryptedKey] = await encryptDataField(rpclink, data);
//encryptedData == data encrypted using the encryptionKey
//usedEncryptedKey == key generated using x25519 with the node

2. decryptNodeResponse ( nodeURL, data, usedEncryptedKey ) => Promise<Uint8Array>

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.

/**
 * 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] = await encryptDataField(rpclink, data);
 
 //Call to the blockchain using ethers.js
 const response = await provider.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 Uint8Array
 const decryptedResponse = await decryptNodeResponse(rpclink, response, usedEncryptedKey);
  
  //Decode the decryptedResponse using ethers.js
 const decodedResponse = 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🚀

PreviousSwisstronikJS SDKNextSwisstronik & Ethereum transactions

Last updated 11 months ago

The function will decrypt the encrypted response and will return an Uint8Array, which can be decoded usingto translate it back to a readable state.

⚒️
contract.interface.decodeFunctionResult()