docsFunctionsData Accessdecrypt

decrypt

Decrypts data that has been encrypted using Keypo.

Note: decryption can currently only be done in a client/browser environment. If you want to decrypt something server-side, please use the server-sdk. We will be updating the docs with instructions for this SDK soon!

Signature

async function decrypt(
  dataIdentifier: string,
  wallet: ethers.Signer,
  config: DecryptConfig,
  debug?: boolean
): Promise<{ decryptedData: Uint8Array, metadata: DataMetadata }>

Description

The decrypt function retrieves and decrypts data that was previously encrypted with Keypo. It uses the unique dataIdentifier to locate the data. The function must be invoked using an ethers v5 signer corresponding to an account that either encrypted the data or has been granted access to it.

Parameters

ParameterTypeRequiredDescription
dataIdentifierstringYesThe unique identifier of the encrypted data.
walletethers.SignerYesAn ethers.js v5 signer that has permission to decrypt the data.
configDecryptConfigYesConfiguration object containing necessary parameters for decryption.
debugbooleanNoWhen set to true, enables debug statements during the decryption process. Default is false.

DecryptConfig Properties

Note: use init to automatically load the config.

PropertyTypeRequiredDescription
registryContractAddressstringYesThe address of the permissions registry contract.
chainstringYesThe blockchain network to use.
expirationstringYesISO timestamp for when the decryption session should expire.
apiUrlstringYesThe base URL of the Keypo API.

Returns

Promise<{ decryptedData: Uint8Array, metadata: DataMetadata }> - A Promise that resolves to an object containing:

  • decryptedData: The decrypted data as a Uint8 byte array
  • metadata: The metadata object associated with the data, containing type information and other details needed for proper restoration

Examples

Basic Usage (Node.js)

import { init, decrypt, postProcess } from "@keypo/typescript-sdk";
import { ethers } from "ethers";
 
const dataId = "ec67b6f8fd39025e7fe39e2d3aea42e765acc2728af2ebd2301bdf940c5b76ab";
 
// Load your wallet and Keypo config
const provider = new ethers.providers.JsonRpcProvider(RPC_URL);
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
const config = await init("https://api.keypo.io");
 
// Decrypt data
const { decryptedData, metadata } = await decrypt(
  dataId, 
  wallet, 
  config.decryptConfig
  ); 
 
// Decrypt and convert to original format using postProcess
const originalData = keypo.postProcess(decryptedData, metadata);

Browser Usage with Viem/Wagmi

This SDK requires a signer object from ethers v5. If you are using viem or wagmi in your application, you will need to convert your Viem Wallet Client to an ethers.Signer.

For detailed instructions on how to do this, please refer to the official Viem Ethers v5 Migration Guide: https://viem.sh/docs/ethers-migration.

Notes

  • This SDK is compatible with ethers v5. Please ensure you are using this version in your project.
  • You must use the exact data identifier that was assigned during encryption.
  • Use list or search to find the data identifier for data you want to decrypt.
  • Only accounts with appropriate permissions can decrypt the data.
  • The returned metadata contains all information needed to restore the original data format using postProcess.
  • For optimal handling of the decrypted data, use the postProcess function with the returned metadata.
  • When debug is enabled, the function will log detailed information about the decryption process.

See Also

  • list - For finding data identifiers
  • encrypt - For encrypting data
  • postProcess - For converting decrypted data back to its original format
  • search - For finding data identifiers filtered by the name metadata field