Skip to main content

Encryption

The encryption module provides client-side data encryption using Inco JS SDK for Inco-compatible values.

Import

import { encryptValue, EncryptionError } from '@inco/solana-sdk/encryption';

Basic Usage

import { encryptValue } from '@inco/solana-sdk/encryption';

// Encrypt different TEE-compatible types
const encryptedInteger = await encryptValue(42);
const encryptedBigInt = await encryptValue(12345678901234567890n);
const encryptedBoolean = await encryptValue(true);

console.log(encryptedInteger);    // Hex-encoded encrypted data

Example: Encrypt for Confidential Token Transfer

import { encryptValue } from '@inco/solana-sdk/encryption';

// Encrypt a transfer amount (0.5 tokens with 9 decimals)
const transferAmount = 500000000n;
const encryptedHex = await encryptValue(transferAmount);

// Use in a confidential transfer
await program.methods
  .transfer(Buffer.from(encryptedHex, 'hex'), 0)
  .accounts({
    source: sourceTokenAccount,
    destination: destinationTokenAccount,
    authority: wallet.publicKey,
  })
  .rpc();

Real-World Example: Encrypt Multiple Values

import { encryptValue } from '@inco/solana-sdk/encryption';

const sensitiveData = {
  amount: 1000000000n,    // Amount in smallest unit (bigint)
  isVip: true,            // Boolean flag
  balance: 50075,         // Balance in cents (integer)
  userId: 12345,          // User ID as integer
};

const encrypted = {
  amount: await encryptValue(sensitiveData.amount),
  isVip: await encryptValue(sensitiveData.isVip),
  balance: await encryptValue(sensitiveData.balance),
  userId: await encryptValue(sensitiveData.userId),
};

// Use encrypted.amount to send to your program

API Reference

encryptValue(value)

Encrypts a single value for use with Inco’s TEE infrastructure. Parameters:
  • value: EncryptableValue - The value to encrypt
Returns: Promise<string> - Hex-encoded encrypted data

Types

EncryptableValue

type EncryptableValue = bigint | boolean | number;

Error Handling

import { encryptValue, EncryptionError } from '@inco/solana-sdk/encryption';

try {
  const encryptedHex = await encryptValue(42);
} catch (error) {
  if (error instanceof EncryptionError) {
    console.error('Encryption failed:', error.message);
    console.error('Cause:', error.cause);
  }
}

Encryption Errors

Thrown by encryptValue() as EncryptionError.
Error MessageCauseSolution
Cannot encrypt null or undefined valuePassed null or undefined to encryptValue()Ensure the value is defined before encrypting
Floating-point numbers are not supported. Use integers or BigInt insteadPassed a decimal number like 10.5Use integers (10) or BigInt (10n) instead
Unsupported value type. Only bigint, number (integers), and boolean are supportedPassed a string, object, or other unsupported typeConvert to BigInt for amounts, use boolean for flags
Encryption failed: <details>Internal encryption errorCheck the error details; may indicate SDK misconfiguration

Validation Examples

// ✅ Valid inputs
await encryptValue(42);           // number (integer)
await encryptValue(1000000000n);  // bigint
await encryptValue(true);         // boolean
await encryptValue(0);            // zero is valid

// ❌ Invalid inputs - will throw EncryptionError
await encryptValue(3.14);         // floating-point
await encryptValue("hello");      // string
await encryptValue({ a: 1 });     // object
await encryptValue(null);         // null
await encryptValue(undefined);    // undefined