const { createInstance } =require("fhevmjs");const { Wallet,JsonRpcProvider,Contract } =require("ethers");constcontractInfo=require("./EncryptedERC20.json");constCONTRACT_ADDRESS="0x309cf2aae85ad8a1db70ca88cfd4225bf17a7482";constprovider=newJsonRpcProvider(`https://devnet.zama.ai/`);constsigner=newWallet("0x92293977156de6e03b20b26708cb4496b523116190b5c32d77cee8286d0c41f6", provider);let _instance;constgetInstance=async () => {if (_instance) return _instance;// 1. Get chain idconstnetwork=awaitprovider.getNetwork();constchainId=+network.chainId.toString();// Get blockchain public keyconstret=awaitprovider.call({// fhe lib address, may need to be changed depending on network to:"0x000000000000000000000000000000000000005d",// first four bytes of keccak256('fhePubKey(bytes1)') + 1 byte for library data:"0xd9d47bb001", });constdecoded=ethers.AbiCoder.defaultAbiCoder().decode(["bytes"], ret);constpublicKey= decoded[0];// Create instance _instance =createInstance({ chainId, publicKey });return _instance;};constgetBalance=async () => {// Initialize contract with ethersconstcontract=newContract(CONTRACT_ADDRESS,contractInfo.abi, signer);// Get instance to encrypt amount parameterconstinstance=awaitgetInstance();// Generate token to decryptconstgeneratedToken=instance.generatePublicKey({ verifyingContract:CONTRACT_ADDRESS, });// Sign the public keyconstsignature=awaitsigner.signTypedData(generatedToken.eip712.domain, { Reencrypt:generatedToken.eip712.types.Reencrypt },// Need to remove EIP712Domain from typesgeneratedToken.eip712.message, );instance.setPublicKeySignature(CONTRACT_ADDRESS, signature);// Call the methodconstencryptedBalance=awaitcontract.balanceOf(generatedToken.publicKey, signature);// Decrypt the balanceconstbalance=instance.decrypt(CONTRACT_ADDRESS, encryptedBalance);return balance;};getBalance().then((balance) => {console.log(balance);});
Transfer tokens (node)
const { createInstance } =require("fhevmjs");const { Wallet,JsonRpcProvider,Contract } =require("ethers");constcontractInfo=require("./EncryptedERC20.json");constCONTRACT_ADDRESS="0x309cf2aae85ad8a1db70ca88cfd4225bf17a7482";constprovider=newJsonRpcProvider(`https://devnet.zama.ai/`);constsigner=newWallet("0x92293977156de6e03b20b26708cb4496b523116190b5c32d77cee8286d0c41f6", provider);let _instance;constgetInstance=async () => {if (_instance) return _instance;// 1. Get chain idconstnetwork=awaitprovider.getNetwork();constchainId=+network.chainId.toString(); // Need to be a number// Get blockchain public keyconstret=awaitprovider.call({// fhe lib address, may need to be changed depending on network to:"0x000000000000000000000000000000000000005d",// first four bytes of keccak256('fhePubKey(bytes1)') + 1 byte for library data:"0xd9d47bb001", });constdecoded=ethers.AbiCoder.defaultAbiCoder().decode(["bytes"], ret);constpublicKey= decoded[0];// Create instance _instance =createInstance({ chainId, publicKey });return _instance;};consttransfer=async (to, amount) => {// Initialize contract with ethersconstcontract=newContract(CONTRACT_ADDRESS,contractInfo.abi, signer);// Get instance to encrypt amount parameterconstinstance=awaitgetInstance();constencryptedAmount=instance.encrypt32(amount);consttransaction=await contract["transfer(address,bytes)"](to, encryptedAmount);return transaction;};transfer("0xa83a498Eee26f9594E3A784f204e507a5Fae3210",10);