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

# Custom signers

> Server keys, Privy server wallets, KMS-backed signers, any wallet the SDK does not recognize, and sponsored deposits

`createCloak` recognizes common wallets on its own. For everything else, it accepts a
`@solana/kit` signer or a plain object with `signMessage` and `signTransaction`. This page covers
those cases and how to sponsor deposit fees.

## A key your server holds

`signerFromSecretKey` turns a 64-byte Solana secret key (the `Keypair.secretKey` layout) or a 32-byte
seed into a `@solana/kit` `KeyPairSigner`. The key is imported as non-extractable.

```ts theme={null}
import { createCloak, encryptedStorage, signerFromSecretKey } from "@cloak.dev/sdk";

declare const db: { get(key: string): Promise<string | null>; set(key: string, value: string): Promise<void> };

const secret = Uint8Array.from(JSON.parse(process.env.SOLANA_SECRET_KEY_JSON!) as number[]);
const wallet = await signerFromSecretKey(secret);

const cloak = await createCloak({
  wallet,
  rpc: process.env.SOLANA_RPC_URL,
  storage: encryptedStorage(db, process.env.STORAGE_SECRET!), // outlives the process
});
```

A web3.js `Keypair` is accepted as is. For a server that should not depend on any wallet at all, use
business mode (`account: <32-byte key>`). See [Backend integration](https://docs.cloak.ag/sdk/backend-integration.md).

## Privy server wallets

`createSolanaKitSigner` from `@privy-io/node` returns a `@solana/kit` signer that signs messages and
transactions through Privy. Pass it to `createCloak` as the wallet. The SDK uses its signing methods
and ignores its ability to send, because the SDK submits every transaction itself.

```ts theme={null}
import { PrivyClient } from "@privy-io/node";
import { createSolanaKitSigner } from "@privy-io/node/solana-kit";
import { address } from "@solana/kit";
import { createCloak, memoryStorage } from "@cloak.dev/sdk";

const privy = new PrivyClient({
  appId: process.env.PRIVY_APP_ID!,
  appSecret: process.env.PRIVY_APP_SECRET!,
});

const signer = createSolanaKitSigner(privy, {
  walletId: process.env.PRIVY_WALLET_ID!,
  address: address(process.env.PRIVY_WALLET_ADDRESS!),
});

const cloak = await createCloak({ wallet: signer, rpc: process.env.SOLANA_RPC_URL, storage: memoryStorage() });
```

Tested against `@privy-io/node` 0.42.0. The account opens from the server wallet's signature, so it
is the same account each time that wallet signs.

## Any other wallet: the `CustomWallet` shape

Pass an object with the wallet's address and byte-level signing functions. This fits KMS and HSM
signers, MPC services, and wallets the SDK does not know.

```ts theme={null}
import { createCloak, type CustomWallet } from "@cloak.dev/sdk";

declare const kms: {
  signEd25519(keyId: string, message: Uint8Array): Promise<Uint8Array>; // 64-byte signature
  signSolanaTransaction(keyId: string, wireTransaction: Uint8Array): Promise<Uint8Array>;
};

const wallet: CustomWallet = {
  chain: "solana",
  address: process.env.KMS_WALLET_ADDRESS!,
  name: "Treasury KMS",
  // The Cloak account message, and nothing else, goes through here.
  signMessage: (message) => kms.signEd25519("treasury", message),
  // Receives the full serialized transaction and returns it with this wallet's signature filled in.
  signTransaction: (wire) => kms.signSolanaTransaction("treasury", wire),
};

const cloak = await createCloak({ wallet, rpc: process.env.SOLANA_RPC_URL });
```

| Field | Needed for |
| - | - |
| `signMessage` | Opening the account. Must return a 64-byte ed25519 signature, the same one every time for the same message |
| `signTransaction` | Shielding from Solana. Receives wire bytes, returns signed wire bytes |
| `sendTransaction` | EVM and Sui custom wallets only, for shields from those chains |

Leave out `signMessage` and the wallet can still shield, but cannot open an account by itself.
`signerFromTransactionBytes({ address, signTransaction, signMessage })` builds the equivalent kit
signer for the low-level API.

## Checking a wallet before you rely on it

```ts theme={null}
import { recognizeWallet } from "@cloak.dev/sdk";

declare const input: unknown;

const [handle] = await recognizeWallet(input);
console.log(handle.name, handle.kind, handle.networks);
console.log("can open the account:", handle.canOpenAccount, "can move money:", handle.canSend);
```

`recognizeWallet` throws `wallet.unrecognized` for anything it cannot use.

## Sponsored deposits

A shield from Solana is paid by the wallet that funds it. To let a wallet with no SOL shield, the
low-level `transact` accepts `externalFeePayer`: an adapter for a fee-payer service that covers the
network fee and rent and is reimbursed in an SPL token inside the same transaction.

```ts theme={null}
import type { ExternalFeePayerAdapter } from "@cloak.dev/sdk";
import type { Address, Instruction } from "@solana/kit";

declare const feePayerService: {
  payer(): Promise<Address>;
  topUp(payer: Address): Promise<Instruction[]>;
  paymentFor(unsignedTxBase64: string): Promise<Instruction>;
  cosign(partiallySignedTxBase64: string): Promise<string>;
};

export const feePayer: ExternalFeePayerAdapter = {
  getPayerPubkey: () => feePayerService.payer(),
  buildTopUpInstructions: (payer) => feePayerService.topUp(payer),
  getPaymentInstruction: (unsignedTxBase64) => feePayerService.paymentFor(unsignedTxBase64),
  cosign: (partiallySignedTxBase64) => feePayerService.cosign(partiallySignedTxBase64),
};
```

Pass it as `externalFeePayer` on the deposit's `TransactOptions`. Limits:

* **Deposits only.** Sends, withdrawals and swaps are already paid for the user.
* **Not with a `KeyPairSigner` depositor.** The depositor must be a wallet signer (wallet adapter,
  Wallet Standard, a Privy signer).
* **Not available through `createCloak`.** Its `shield` call takes no fee payer.
* Top-up instructions must not include ComputeBudget instructions; the SDK adds its own.

## Related

* [Backend integration](https://docs.cloak.ag/sdk/backend-integration.md)
* [Low-level API](https://docs.cloak.ag/sdk/low-level-api.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.