npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@cofhe/abi

v0.2.1

Published

ABI utilities for Fhenix FHE contracts

Readme

@cofhe/abi

Type-safe ABI utilities for Fhenix fully homomorphic encryption (FHE) smart contracts. Provides type-safe transformations between encrypted input/output types and their underlying primitive values.

Overview

Fhenix contracts use encrypted types (e.g., euint32, ebool) that are represented as structs in ABIs. This package bridges the gap between:

  • Encrypted types (struct InEuint32, euint32) - the ABI representation
  • Primitive types (bigint, boolean, string) - developer-friendly values
  • Encryptable types (EncryptableItem) - intermediate encryption format

The package provides compile-time type safety through TypeScript generics, ensuring encrypted values are correctly extracted, transformed, and inserted based on ABI definitions.

Installation

npm install @cofhe/abi
# or
pnpm add @cofhe/abi
# or
yarn add @cofhe/abi

Peer Dependencies:

  • @cofhe/sdk - Core encryption types and utilities
  • abitype - ABI type utilities

Quick Start

import { extractEncryptableValues, insertEncryptedValues, transformEncryptedReturnTypes } from '@cofhe/abi';
import type { CofheInputArgs, CofheInputArgsPreTransform, CofheReturnType } from '@cofhe/abi';
import { encrypt } from '@cofhe/sdk';

const abi = [
  {
    type: 'function',
    name: 'add',
    inputs: [
      { name: 'a', type: 'uint256', internalType: 'uint256' },
      { name: 'b', type: 'tuple', internalType: 'struct InEuint32', components: [...] }
    ],
    outputs: [{ name: '', type: 'uint256', internalType: 'euint32' }],
    stateMutability: 'nonpayable'
  }
] as const;

// 1. Prepare arguments with primitive values
const args: CofheInputArgsPreTransform<typeof abi, 'add'> = [100n, 200n];

// 2. Extract encryptable values
const encryptables = extractEncryptableValues(abi, 'add', args);
//    ^? [Encryptable.uint32(200n)]

// 3. Encrypt the values
const encrypted = await encrypt(encryptables);
//    ^? [EncryptedUint32Input]

// 4. Insert encrypted values back into arguments
const encryptedArgs: CofheInputArgs<typeof abi, 'add'> = insertEncryptedValues(abi, 'add', args, encrypted);
//    ^? [100n, EncryptedUint32Input]

// 5. Call contract and transform return value
const result = await contract.add(...encryptedArgs);
//    ^? bigint

// 6. Transform raw return value into correct encrypted value type
const transformed: CofheReturnType<typeof abi, 'add'> = transformEncryptedReturnTypes(abi, 'add', result);
//    ^? Euint32

Public API Reference

Return Types

CofheReturnType<abi, functionName, args?>

Type-level utility that infers the return type of a function, transforming encrypted return types to their typed representations.

Type Parameters:

  • abi - Contract ABI (must be const-asserted for type inference)
  • functionName - Function name (string literal)
  • args - Optional function arguments for overload disambiguation

Returns:

  • Transformed return type where encrypted types (euint32, ebool, etc.) are converted to typed objects ({ ctHash: bigint, utype: FheTypes })
  • Non-encrypted types remain unchanged
  • Supports single values, tuples, arrays, and nested structures

Supported Encrypted Return Types:

  • ebool{ ctHash: bigint; utype: FheTypes.Bool }
  • euint8{ ctHash: bigint; utype: FheTypes.Uint8 }
  • euint16{ ctHash: bigint; utype: FheTypes.Uint16 }
  • euint32{ ctHash: bigint; utype: FheTypes.Uint32 }
  • euint64{ ctHash: bigint; utype: FheTypes.Uint64 }
  • euint128{ ctHash: bigint; utype: FheTypes.Uint128 }
  • eaddress{ ctHash: bigint; utype: FheTypes.Uint160 }

transformEncryptedReturnTypes(abi, functionName, data)

Runtime function that transforms contract return values from bigint ciphertext hashes to typed encrypted return objects.

Parameters:

  • abi - Contract ABI
  • functionName - Function name
  • data - Raw return value(s) from contract call (single value or array)

Returns: Transformed return value matching CofheReturnType<abi, functionName> Works with multiple return values, nested structures and arrays.

Encrypted Inputs

CofheInputArgs<abi, functionName>

Type-level utility that infers function input arguments with encrypted types represented as encrypted input structs.

Type Parameters:

  • abi - Contract ABI (must be const-asserted for type inference)
  • functionName - Function name (string literal)

Returns:

  • Tuple type where encrypted inputs are represented as encrypted input structs (EncryptedUint32Input, EncryptedBoolInput, etc.)
  • Non-encrypted types use their primitive representations

CofheInputArgsPreTransform<abi, functionName>

Type-level utility that infers function input arguments with encrypted types represented as their underlying primitive values (before encryption).

Type Parameters:

  • abi - Contract ABI (must be const-asserted for type inference)
  • functionName - Function name (string literal)

Returns:

  • Tuple type where encrypted inputs use primitive types (bigint for uints, boolean for bool, string for address)
  • This is the format you provide to extractEncryptableValues

Supported Encrypted Input Types:

  • struct InEboolboolean (pre-transform) → EncryptedBoolInput (post-transform)
  • struct InEuint8bigint | stringEncryptedUint8Input
  • struct InEuint16bigint | stringEncryptedUint16Input
  • struct InEuint32bigint | stringEncryptedUint32Input
  • struct InEuint64bigint | stringEncryptedUint64Input
  • struct InEuint128bigint | stringEncryptedUint128Input
  • struct InEaddressstring | bigintEncryptedAddressInput

extractEncryptableValues(abi, functionName, args)

Extracts encryptable values from function arguments, converting primitive values to EncryptableItem objects ready for encryption.

Parameters:

  • abi - Contract ABI
  • functionName - Function name
  • args - Function arguments in CofheInputArgsPreTransform format (primitives)

Returns: Flat array of EncryptableItem objects in the order they appear in the ABI (depth-first traversal). Works with arrays (fixed length or unbounded) and nested structures.

insertEncryptedValues(abi, functionName, args, encryptedValues)

Re-inserts encrypted values back into function arguments, replacing primitive values with encrypted input structs.

Parameters:

  • abi - Contract ABI
  • functionName - Function name
  • args - Original function arguments in CofheInputArgsPreTransform format
  • encryptedValues - Array of encrypted values in the same order as returned by extractEncryptableValues

Returns: Function arguments in CofheInputArgs format (ready for contract calls)