Skip to content
Lean Key
Reference

Instructions

Every Lean Key instruction with its accounts in order, its data layout, its checks in the order the program runs them, and its measured cost.

The program has five instructions. The first byte of the instruction data selects one. This page follows program/Twokey/Program.lean; the TypeScript builders with the same layouts are in packages/core/src/instructions.ts.

Dispatch

Before any instruction runs, the entrypoint checks:

  1. The instruction data is at least 1 byte long, else InvalidInstructionData.
  2. Byte 0 is 0 to 4, else InvalidInstructionData.
  3. The number of accounts is exactly the instruction's count (3, 3, 6, 5 or 3), else NotEnoughAccountKeys. Too many accounts is refused the same way as too few.
  4. The data length is exactly the instruction's length, else InvalidInstructionData.
Byte 0InstructionAccountsData length
0InitVault342
1WithdrawSol3665
2WithdrawToken6666
3CloseTokenAccount5657
4CloseVault3657

All integers are little-endian. Account entries must be full, non-duplicate entries unless a row below says an account may repeat account 0; a forbidden duplicate is InvalidAccountData.

The error names and numbers used below are listed in Errors.

0. InitVault

Creates a vault at ["vault", owner, vault_id] with key index 0 and the hash of the first one-time key. Only once per address.

Accounts

#AccountSignerWritableChecks
0owneryesyesPays the rent shortfall. Becomes the vault's owner.
1vaultnoyesMust be the PDA for owner, vault_id and bump; must still belong to the System Program with no data.
2System ProgramnonoMust be 11111111111111111111111111111111.

Data (42 bytes)

OffsetSizeField
010
18vault_id, u64
91bump
1032pk_hash_0, the hash of one-time key #0

Checks, in order

  1. Owner signed: MissingRequiredSignature.
  2. Owner and vault writable: InvalidAccountData.
  3. Account 2 is the System Program: IncorrectProgramId.
  4. The vault address is still owned by the System Program and has no data: AlreadyOpen (6003). This also refuses every closed vault (tombstone).
  5. pk_hash_0 is not 32 zero bytes: ZeroNextKey (6011).
  6. The vault's address equals the derivation from "vault", owner, vault_id, bump and the program id: BadAddress (6004).

Effects

  • If the address holds less than the rent-exempt minimum for 91 bytes, the owner transfers the difference (System Program transfer). Lamports already at the address count.
  • The vault signs Allocate(91) and Assign(program) with its seeds.
  • Writes bump, owner, vault_id, key_index 0, pk_hash_0, the current slot, flags 0 and, last, version 1.
  • Logs 0, 0, 0, 0, 0.

Measured: 4,461 compute units (LiteSVM, 36 runs; and on a local validator). Transaction size as measured: 278 bytes.

1. WithdrawSol

Sends amount lamports from the vault to the destination. The vault keeps its rent-exempt minimum. Rotates the key.

Accounts

#AccountSignerWritableChecks
0owneryesonly if it is also the destinationMust equal the vault's stored owner.
1vaultnoyesAn open Lean Key vault of this program.
2destinationnoyesAny writable account. May be the owner (account 0); may not be the vault.

Data (665 bytes)

OffsetSizeField
011
18amount, lamports, u64
932next_pk_hash
41624wots_sig

Checks, in order

  1. The vault check, shared by every withdrawal (checkVault):
    • owner (account 0) signed: MissingRequiredSignature;
    • vault writable: InvalidAccountData;
    • vault owned by this program, 91 bytes, version 1: NotVault (6002);
    • the owner stored in the vault is account 0: NotOwner (6001).
  2. Destination writable: InvalidAccountData.
  3. amount is not 0: ZeroAmount (6010).
  4. next_pk_hash is not 32 zero bytes: ZeroNextKey (6011).
  5. The WOTS+ signature verifies, for the intent (action 1, asset 0, amount, destination, next_pk_hash, this vault, its current key index), against the vault's pk_hash: BadSignature (6005).
  6. The vault holds at least its rent-exempt minimum, and amount ≤ balance − minimum: BelowRent (6006).
  7. The destination's new balance does not overflow 64 bits: ArithmeticOverflow.

Effects

  • Vault balance − amount, destination balance + amount.
  • key_index + 1 (checked: ArithmeticOverflow at 2^64 − 1), pk_hash := next_pk_hash.
  • Logs 1, key_index_before, amount, 0, 0.

A destination that held nothing must end up rent-exempt, or the runtime rejects the transaction. The clients check this before signing ("send at least … lamports so it is rent-exempt").

Measured on a local validator: 526,950 to 607,530 compute units (3 runs). In LiteSVM: 486,660 to 647,820 (13 runs). Transaction size as measured: 942 bytes.

2. WithdrawToken

TransferChecked of amount from the vault's token account to the destination token account, signed by the vault. SPL Token and Token-2022. Rotates the key.

Accounts

#AccountSignerWritableChecks
0owneryesnoMust equal the vault's stored owner.
1vaultnoyesAn open Lean Key vault of this program.
2vault token accountnoyesOwned by the token program; at least 165 bytes; mint = account 4; owner field = the vault.
3destination token accountnoyesOwned by the token program. Its mint is checked by the token program.
4mintnonoOwned by the token program; at least 82 bytes; no transfer hook.
5token programnonoSPL Token or Token-2022.

All six must be distinct.

Data (666 bytes)

OffsetSizeField
012
18amount, token base units, u64
91decimals (passed to TransferChecked; not part of the intent)
1032next_pk_hash
42624wots_sig

Checks, in order

  1. The vault check (as for WithdrawSol): MissingRequiredSignature, InvalidAccountData, NotVault (6002), NotOwner (6001).
  2. Account 5 is SPL Token or Token-2022: IncorrectProgramId.
  3. Vault token account writable (InvalidAccountData); owned by the token program, at least 165 bytes, mint field = account 4, owner field = the vault (BadTokenAccount, 6008).
  4. Destination token account writable (InvalidAccountData) and owned by the token program (BadTokenAccount, 6008).
  5. Mint owned by the token program and at least 82 bytes (BadMint, 6009). For a mint longer than 166 bytes, the Token-2022 extensions are walked: a transfer hook naming a program is TransferHook (6007); a malformed transfer-hook entry is BadMint (6009).
  6. amount is not 0: ZeroAmount (6010).
  7. next_pk_hash is not zero (ZeroNextKey, 6011) and the WOTS+ signature verifies for the intent (action 2, asset = the mint, amount, destination = the destination token account): BadSignature (6005).
  8. TransferChecked CPI, signed by the vault. Failures here are the token program's own errors: not enough tokens, a destination for another mint, wrong decimals, a frozen account.

Effects

  • The token program moves amount base units.
  • key_index + 1, pk_hash := next_pk_hash.
  • Logs 2, key_index_before, amount, 0, 0.

The clients prepend an idempotent creation of the recipient's associated token account when it does not exist yet; the fee payer pays its rent.

Measured on a local validator: SPL Token 488,065 to 528,355 (3 runs), Token-2022 506,072 to 570,504 (2 runs). In LiteSVM: SPL Token 568,645, Token-2022 530,284. Transaction size as measured: 1,042 bytes.

3. CloseTokenAccount

Closes an empty token account of the vault; its rent goes to the destination. Rotates the key.

Accounts

#AccountSignerWritableChecks
0owneryesonly if it is also the destinationMust equal the vault's stored owner.
1vaultnoyesAn open Lean Key vault of this program.
2vault token accountnoyesOwned by the token program; at least 165 bytes; owner field = the vault.
3destinationnoyesReceives the token account's rent. May be the owner (account 0).
4token programnonoSPL Token or Token-2022.

Data (657 bytes)

OffsetSizeField
013
132next_pk_hash
33624wots_sig

Checks, in order

  1. The vault check: MissingRequiredSignature, InvalidAccountData, NotVault (6002), NotOwner (6001).
  2. Account 4 is SPL Token or Token-2022: IncorrectProgramId.
  3. Vault token account writable (InvalidAccountData); owned by the token program, at least 165 bytes, owner field = the vault (BadTokenAccount, 6008).
  4. Destination writable: InvalidAccountData.
  5. next_pk_hash is not zero (ZeroNextKey, 6011) and the WOTS+ signature verifies for the intent (action 3, asset = the mint stored in the token account, amount 0, destination = account 3): BadSignature (6005).
  6. CloseAccount CPI, signed by the vault. The token program refuses to close an account that still holds tokens.

Effects

  • The token account is closed, its lamports go to the destination.
  • key_index + 1, pk_hash := next_pk_hash.
  • Logs 3, key_index_before, 0, 0, 0.

Measured on a local validator: 487,921. In LiteSVM: SPL Token 407,339, Token-2022 569,960. Transaction size as measured: 968 bytes.

4. CloseVault

Sends every lamport above the rent-exempt minimum to the destination and marks the vault closed. The account stays as a tombstone. Rotates the key.

Accounts

#AccountSignerWritableChecks
0owneryesonly if it is also the destinationMust equal the vault's stored owner.
1vaultnoyesAn open Lean Key vault of this program.
2destinationnoyesAny writable account. May be the owner (account 0); may not be the vault.

Data (657 bytes)

OffsetSizeField
014
132next_pk_hash
33624wots_sig

Checks, in order

  1. The vault check: MissingRequiredSignature, InvalidAccountData, NotVault (6002), NotOwner (6001).
  2. Destination writable: InvalidAccountData.
  3. next_pk_hash is not zero (ZeroNextKey, 6011) and the WOTS+ signature verifies for the intent (action 4, asset 0, amount 0, destination): BadSignature (6005).
  4. The vault holds at least its rent-exempt minimum: BelowRent (6006).
  5. The destination's new balance does not overflow: ArithmeticOverflow.

Effects

  • sweep := balance − minimum. Vault balance := minimum, destination balance + sweep.
  • flags |= 1 (closed).
  • key_index + 1, pk_hash := next_pk_hash.
  • Logs 4, key_index_before, sweep, 0, 0.

The program does not check for remaining token accounts. Tokens can be withdrawn from a closed vault with both signatures, as from an open one.

Measured on a local validator: 567,239. In LiteSVM: 526,949. Transaction size as measured: 934 bytes.

Deposits

There is no deposit instruction. To deposit SOL, transfer it to the vault's address. To deposit tokens, transfer them to a token account owned by the vault, normally its associated token account (leankey deposit VAULT AMOUNT --mint MINT creates it idempotently and sends with TransferChecked).

Building instructions in TypeScript

import {
  initVaultIx, withdrawSolIx, withdrawTokenIx, closeTokenAccountIx, closeVaultIx,
  setComputeUnitLimit, computeUnitsFor, Op,
} from "@twokey/core";

// InitVault: derives the vault and the canonical bump
const { vault, bump, instruction } = initVaultIx({ programId, owner, vaultId: 0n, pkHash0 });

// WithdrawSol: 665 bytes of data
const ix = withdrawSolIx({ programId, owner, vault, destination, amount, nextPkHash, signature });
const tx = [setComputeUnitLimit(computeUnitsFor(Op.WithdrawSol, steps)), ix];

These builders only lay out bytes. To sign and send correctly (placeholder preflight, journal rule, simulation), use prepareSend from packages/core/src/client.ts, as the CLI and the extension do.

Compute budget

A withdrawal needs far more than the default compute budget, so the clients always put a SetComputeUnitLimit instruction first. The limit comes from the intent: the verifier walks 255 × (⌊checksum / 256⌋ + 2) chain steps, between 510 and 6,375, and computeUnitsFor turns that into a limit with a margin, capped at 1,400,000. See WOTS+ parameters for the formula and the worst case.

Connect a wallet

Your wallet signs as the owner (Key A). Lean Key adds the post-quantum key (Key B).

Wallets

Any wallet that supports the Solana Wallet Standard appears under Installed. Lean Key never asks for your seed phrase on this site.