> ## Documentation Index
> Fetch the complete documentation index at: https://docs.inco.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Encryption

> Client-side encryption utilities for Solana programs

# Encryption

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

## Import

```typescript theme={null}
import { encryptValue, EncryptionError } from '@inco/solana-sdk/encryption';
```

## Basic Usage

```typescript theme={null}
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

```typescript theme={null}
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

```typescript theme={null}
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`

```typescript theme={null}
type EncryptableValue = bigint | boolean | number;
```

## Error Handling

```typescript theme={null}
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 Message                                                                       | Cause                                              | Solution                                                   |
| ----------------------------------------------------------------------------------- | -------------------------------------------------- | ---------------------------------------------------------- |
| `Cannot encrypt null or undefined value`                                            | Passed `null` or `undefined` to `encryptValue()`   | Ensure the value is defined before encrypting              |
| `Floating-point numbers are not supported. Use integers or BigInt instead`          | Passed a decimal number like `10.5`                | Use integers (`10`) or BigInt (`10n`) instead              |
| `Unsupported value type. Only bigint, number (integers), and boolean are supported` | Passed a string, object, or other unsupported type | Convert to BigInt for amounts, use boolean for flags       |
| `Encryption failed: <details>`                                                      | Internal encryption error                          | Check the error details; may indicate SDK misconfiguration |

### Validation Examples

```typescript theme={null}
// ✅ 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
```
