In order to store an encrypted values on-chain, a plain text can be converted into a cipher text on the client side using with the public key of the network.
Node.js
The first step is to retrieve the network's public key by connecting to our RPC and using createInstance from fhevmjs:
const { createInstance } =require("fhevmjs");const { JsonRpcProvider } =require("ethers");constprovider=newJsonRpcProvider(`https://testnet.inco.org`);// Contract address of TFHE.solconstFHE_LIB_ADDRESS="0x000000000000000000000000000000000000005d";let _instance;constgetInstance=async () => {if (_instance) return _instance;constnetwork=awaitprovider.getNetwork();constchainId=+network.chainId.toString(); // chainId: 9090console.log("network", network);console.log("chainId", chainId);// Get blockchain public keyconstret=awaitprovider.call({ to:FHE_LIB_ADDRESS,// first four bytes of keccak256('fhePubKey(bytes1)') + 1 byte for library data:"0xd9d47bb001", });constdecoded=AbiCoder.defaultAbiCoder().decode(["bytes"], ret);constpublicKey= decoded[0]; _instance =awaitcreateInstance({ chainId, publicKey });};
The functions encrypt8, encrypt16 and encrypt32 are available to encrypt your plain text into a cipher text. For example: