# Integrate your HSM with Fordefi

If you must keep your private keys in your own HSM, you can still run on Fordefi. With an **external-signer vault**, the private key lives outside Fordefi, typically in your HSM, and Fordefi holds only the public key. You register the public key once. From then on the vault works like any other Fordefi vault for balances, policies, approvals, transaction history and webhooks.

In this model, you and Fordefi split the work:

- **Fordefi** builds, simulates and policy-checks each transaction, then broadcasts it.
- **Your HSM** signs it.


Signing is the only difference. Instead of using Fordefi's MPC signers (mobile app or API Signer), Fordefi hands you an attested signing request. You sign it in your HSM and return the signature, and Fordefi broadcasts the transaction.

## Why use an external-signer vault

- **Reuses your existing HSM setup.** If you already manage your own HSMs, whether for blockchain keys or other keys, you keep using the same infrastructure, policies and procedures for your Fordefi wallets. No key material moves to Fordefi.
- **The full Fordefi platform on top.** Chain coverage, transaction construction and fee handling, simulation, policy engine and approvals, broadcasting, tracking and webhooks.
- **One model for hot and cold wallets.** It works with an online HSM that signs in seconds and with an offline HSM that signs in batches.
- **No blind signing.** Your signer independently verifies what it is asked to sign.


## Who does what

| Domain | Fordefi | You |
|  --- | --- | --- |
| Keys | Stores the public key only | You generate, store and back up private keys in your HSM |
| Transactions | Builds it, sets fees and sequence numbers, simulates it | You initiate it through the API |
| Authorization | Applies your organization's policy and approval flow | You can apply your own policy at signing time |
| Signing | Attests to the exact transaction to be signed | You verify the attestation and sign in your HSM |
| After signing | Validates the signature, broadcasts, tracks to completion, sends webhooks | You react to webhooks and handle exceptions (retry, cancel, accelerate) |


## How it works

```mermaid
sequenceDiagram
    participant S as Your signer and HSM
    participant C as Your backend
    participant F as Fordefi
    participant B as Blockchain
    C->>F: Create transaction (signed API request)
    F->>F: Simulate, apply policy, collect approvals
    F-->>C: Webhook - approved
    C->>F: Finalize for signing
    F-->>C: Attested signing request
    C->>S: Attested signing request
    S->>S: Verify attestation and intent, apply own policy, sign
    S-->>C: Signatures
    C->>F: Submit signatures
    F->>B: Broadcast
    F-->>C: Webhooks - signed, pushed, mined, completed
```

1. **Create.** Your backend creates the transaction through the Fordefi API, for example `POST /api/v1/transactions/transfer`. The request is signed with your API user's request-signing key.
2. **Approve.** Fordefi simulates the transaction and runs it through your organization's policy and approval flow, exactly as for any other vault. The transaction reaches `approved`.
3. **Finalize.** Your backend calls `POST /api/v1/transactions/{id}/finalize-for-signing`. Fordefi fixes the final transaction (fees, sequence number, inputs) and its secure enclave signs an attestation over it. The transaction moves to `finalized_for_signing`, and the response carries the attestation in `attested_payload_for_signing`.
4. **Verify and sign.** Inside your signing environment, Fordefi's verification library checks the attestation and returns the exact payloads to sign. You can apply your own policy at this point, then sign in your HSM.
5. **Submit.** Your backend calls `POST /api/v1/transactions/{id}/submit-signature` with the signatures. Fordefi validates them, assembles the signed transaction, broadcasts it and tracks it to completion, sending a webhook on every state change.


## One-time setup

1. **Enable external keys.** Ask Fordefi CS to turn the capability on for your organization.
2. **Create an API user with a request-signing key** (ECDSA P-256). You sign every transaction request with it. That signed request is what your signer later checks the transaction against.
3. **Pin Fordefi's attestation key.** `GET /api/v1/enclave-keys` returns the enclave's attested signing public key (`intent_signing_key`). Your signer uses it to verify attestations.
4. **Register each HSM key as a vault**, one vault per key, by creating a vault with the public key instead of having Fordefi generate one:

```json
{
  "name": "Withdrawals - EVM",
  "type": "evm",
  "import_options": {
    "origin": "external_signer",
    "public_key_compressed": "<base64 compressed public key>",
    "chain_code": "<base64 32-byte BIP-32 chain code, optional>"
  }
}
```
5. **Subscribe to webhooks** for transaction state updates.


## What the signer verifies

The attestation is a signed token (JWS, ES256) produced by Fordefi's enclave. It contains the serialized transaction, the chain, the public keys expected to sign, and your original signed API request.

The verification library runs entirely on your side and does three things:

1. **Authenticates the attestation** against the pinned enclave key.
2. **Checks intent.** It parses the raw transaction and confirms that it matches the original signed request. For transfers it returns the sender, recipient, asset, amount and maximum fee, so you can enforce limits and allowlists on values taken from the transaction itself.
3. **Derives the signing payloads**, each with the public key and curve that must sign it.


Before signing, your signer must also check what the library cannot:

- The request was signed by your API user. Compare the initiating user's public key in the result with your API user's request-signing key. Without this check, a compromised enclave could sign a request with a key of its own.
- The sender is the vault you intended. The library confirms that the signing keys belong to your organization, but not which vault the request named, because the request identifies the vault only by its Fordefi ID. Map that ID to the vault's on-chain address and confirm that it matches the sender. For sponsored transfers, check the fee-payer key the same way.
- The fee is within your limit. The library reports the maximum fee but does not cap it.


The check is made against the raw transaction bytes and a request signed with your own API key, so your signer does not rely on Fordefi's backend to describe the transaction honestly.

## Supported chains and signature formats

Each signing payload is exactly the input your HSM signs. secp256k1 payloads are 32-byte digests that are already hashed, so sign them with raw ECDSA (for PKCS#11, CKM_ECDSA, not a mechanism that hashes first). Ed25519 payloads are the message, so pass them to Ed25519 as they are. Return one signature per payload, base64-encoded, in the order the library returned them.

| Chain family | Curve | Signature returned to Fordefi |
|  --- | --- | --- |
| EVM | secp256k1 | 65 bytes: r ‖ s ‖ v, with v = 0 or 1 and low s |
| Tron, XRP Ledger, Canton | secp256k1 | 65 bytes: r ‖ s ‖ v |
| Bitcoin, Litecoin, Bitcoin Cash, Dogecoin | secp256k1 | 64 bytes: r ‖ s with low s, one signature per input |
| Cosmos | secp256k1 | 64 bytes: r ‖ s |
| Solana, Sui, Aptos, TON | Ed25519 | 64-byte signature |


## Transaction states

| State | Meaning |
|  --- | --- |
| `waiting_for_approval`, `approved` | Fordefi policy and approvals, as for any vault |
| `finalized_for_signing` | The transaction is fixed and attested, and is waiting for your HSM's signature |
| `signed`, `pushed_to_blockchain`, `mined`, `completed` | Signature accepted, broadcast and confirmed |
| `aborted`, `error_signing`, `error_pushing_to_blockchain`, `stuck`, `dropped` | Exceptions, covered below |


## Operational considerations

- **Drive the flow from webhooks, idempotently.** Acknowledge each webhook quickly and sign asynchronously. A delivery that is not acknowledged in time is retried with the same event ID, so deduplicate on it. Finalizing a transaction twice is rejected. Submitting the same signatures twice is harmless.
- **Sequence numbers are reserved at finalization, not at broadcast.** On account-based chains such as EVM, several transactions from one vault can be finalized back to back, signed in one session and submitted in any order. This is what makes batch and offline signing practical.
- **Resolve every finalized transaction.** If the signer declines to sign, for example because your own policy rejects it, the reserved sequence number must be given back or used. Abort the transaction (allowed while no signature was submitted and it holds the vault's most recently reserved number), or, on EVM, release it with a cancel or accelerate transaction that uses the same number. Otherwise later transactions from that vault wait behind the gap. Chains without account sequence numbers (UTXO chains, Sui, Tron, Solana on a recent blockhash) are not blocked this way; reserved inputs free up shortly afterwards.
- **A failed broadcast keeps its signature.** If the node rejects the transaction, typically because the vault lacks native balance for fees, it moves to `error_pushing_to_blockchain`. Fix the cause and call `POST /api/v1/transactions/{id}/push`. No new signature is needed.
- **Stuck transactions are released the usual way.** Accelerating or cancelling creates a replacement transaction that goes through the same finalize, sign and submit steps. The verification library reports that it is a replacement, and of which transaction.
- **Offline HSMs on Solana need durable nonces.** A transaction built on a recent blockhash expires within minutes. For slower signing, give the vault a pool of durable nonce accounts; each finalized transaction reserves one until it lands or is released.
- **A second vault can pay the fees.** Both the sending vault and the fee-payer vault must be external-signer vaults, and both sign. Where the fee payer signs after the sender, `submit-signature` returns the next signing round.
- **Plan for large signing requests.** The attestation carries the full transaction and the original request. On chains with large transactions, such as Canton, the transaction alone runs to tens of kilobytes, against a few hundred bytes on EVM or Bitcoin, so size the transport to your HSM accordingly.
- **Last-resort recovery.** Fordefi can enable an endpoint that lets your API user resynchronize a vault's next sequence number with the chain. Use it only in exceptional cases.


## Current limitations

- **API only.** Finalizing and submitting signatures have no web-console equivalent, and are not yet in the public API reference. Vaults and transactions are visible in the console as usual.
- **Transfers get the full intent check.** They must be created through the transfer endpoint. Raw transactions are verified to match the signed request, but are not decoded into sender, recipient and amount.
- **No off-chain message signing** (personal or typed-data messages).
- **Extra signers.** Solana transactions that need a signer other than the vault and an optional fee-payer vault are not supported, and neither are Solana batch transactions.
- **Chain coverage is a subset of Fordefi's.** Stellar, Starknet and Stacks are not yet covered.
- **No Fordefi backup or recovery for these keys.** Fordefi holds no key shares, so key backup stays with you.