Currently only tested with Webpack and NextJS. If you are using Rollup or Vite please report any issues here

Install

Choose your favorite package manager:

npm install @inco/js
# or
bun install @inco/js
# or
yarn add @inco/js

Usage

A typical usage of @inco/js includes 3 steps:

  1. Encrypting a value.
  2. Posting the ciphertext to the contract, which will perform confidential computes on it.
  3. Requesting a reencryption of the result of the computation.

1. Encrypt a value

import { getViemChain, supportedChains } from '@inco/js';
import { Lightning } from '@inco/js/lite';
import { createWalletClient } from 'viem';

// Setup: do it once at initialization
const chainId = supportedChains.baseSepolia;
const zap = Lightning.latest('testnet', chainId); // Connect to Inco's latest public testnet
const walletClient = createWalletClient({
  chain: getViemChain(chainId),
  account: /* Choose your account, e.g. from window.ethereum */,
  transport: /* Choose your transport, e.g. from Alchemy */,
});
const dappAddress = '0x00000000000000000000000000000000deadbeef'; // Put your contract address here

// Encrypt the plaintext value
const plaintext = 42;
const ciphertext = await zap.encrypt(plaintext, {
  accountAddress: walletClient.account.address,
  dappAddress,
});

console.log(ciphertext); // A long hex string representing the encrypted value

2. Post the ciphertext to the contract

This step does not require any specific @inco/js functionality. We recommend using viem to interact with the blockchain. Specifically, use the writeContract method to submit transactions. Pass the ciphertext from the previous encryption step as the input ciphertext parameter of type bytes.

3. Request a reencryption

Following transaction submission, the Inco covalidator processes the computation request. The contract stores the computation result as a handle on-chain. This handle, referenced below as resultHandle, is a hexadecimal string of type Handle that serves as a reference to the encrypted computation output.

import { Hex } from "viem";

// Request a re-encryption of the result ciphertext
const resultHandle = "0x..." as Hex; // Retrieve the handle from the contract, e.g. using viem
const reencryptor = await zap.getReencryptor(walletClient); // Use same walletClient as previous step
const resultPlaintext = await reencryptor({ handle: resultHandle });

console.log(resultPlaintext.value); // The decrypted value