The library provides a convenient function that generates a JSON object based on the EIP-712 standard. This JSON object includes a public key and is specifically designed to facilitate data reencryption in a smart contract environment.
By utilizing this JSON object and having it signed by the user, a secure process of reencrypting data becomes possible within a smart contract. The signed JSON includes the necessary information, including the public key, which allows for seamless reencryption of the data.
FhevmInstance.generateToken
Parameters
options (required):
verifyingContract: string (required): The address of the contract
name: string (optional): The name used in the EIP712
version: string (optional): The version used in the EIP712
This method allows you to store the signature of a public key for a specific contract, in order to retrieve it later. The signature is also serialized in serializeKeypairs.
Parameters
contractAddress: string (required): address of the contract
signature: string (required): the signature of the EIP-712 token
Example
constinstance=awaitcreateInstance({ chainId, publicKey });constgeneratedToken=instance.generateToken({ name:'Authentication', verifyingContract:'0x1c786b8ca49D932AFaDCEc00827352B503edf16c',});// Ask for user to sign the tokenconstparams= [userAddress,JSON.stringify(generatedToken.token)];constsign=awaitwindow.ethereum.request({ method:'eth_signTypedData_v4', params,});// Store signatureinstance.setTokenSignature(contractAddress, sign);
FhevmInstance.hasKeypair
This method returns true if contract has a keypair and a signature.
Parameters
contractAddress: string (required): address of the contract
Returns
boolean
Example
constcontractAddress='0x1c786b8ca49D932AFaDCEc00827352B503edf16c';constinstance=awaitcreateInstance({ chainId, publicKey });constgeneratedToken=instance.generateToken({ name:'Authentication', verifyingContract:,});// Ask for user to sign the tokenconstparams= [userAddress,JSON.stringify(generatedToken.token)];constsign=awaitwindow.ethereum.request({ method:'eth_signTypedData_v4', params });console.log(instance.hasKeypair(contractAddress)); // false// Store signatureinstance.setTokenSignature(contractAddress, sign);console.log(instance.hasKeypair(contractAddress)); // true
FhevmInstance.getTokenSignature
This method returns the saved public key and signature for a specific contract. If the contract has no keypair or no signature, this method returns null.
Parameters
contractAddress: string (required): address of the contract
constcontractAddress='0x1c786b8ca49D932AFaDCEc00827352B503edf16c';constinstance=awaitcreateInstance({ chainId, publicKey });constgeneratedToken=instance.generateToken({ name:'Authentication', verifyingContract: contractAddress,});// Ask for user to sign the tokenconstparams= [userAddress,JSON.stringify(generatedToken.token)];constsign=awaitwindow.ethereum.request({ method:'eth_signTypedData_v4', params,});// Store signatureinstance.setTokenSignature(contractAddress, sign);// Now, since the signature is stored, we can fetch the public key and signature laterconst { publicKey,signature } =instance.getTokenSignature();console.log(publicKey); // Uint8Array(32) [192, 108, 9, ...]console.log(signature); // '0x6214e232b2dae4d8d2c99837dd1af004e1b...'constresponse=awaitcontract.balanceOf(publicKey, signature);instance.decrypt(contractAddress, response);
FhevmInstance.serializeKeypairs
This method is useful if you want to store contract keypairs in the user LocalStorage.