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:
- The instruction data is at least 1 byte long, else
InvalidInstructionData. - Byte 0 is 0 to 4, else
InvalidInstructionData. - 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. - The data length is exactly the instruction's length, else
InvalidInstructionData.
| Byte 0 | Instruction | Accounts | Data length |
|---|---|---|---|
| 0 | InitVault | 3 | 42 |
| 1 | WithdrawSol | 3 | 665 |
| 2 | WithdrawToken | 6 | 666 |
| 3 | CloseTokenAccount | 5 | 657 |
| 4 | CloseVault | 3 | 657 |
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
| # | Account | Signer | Writable | Checks |
|---|---|---|---|---|
| 0 | owner | yes | yes | Pays the rent shortfall. Becomes the vault's owner. |
| 1 | vault | no | yes | Must be the PDA for owner, vault_id and bump; must still belong to the System Program with no data. |
| 2 | System Program | no | no | Must be 11111111111111111111111111111111. |
Data (42 bytes)
| Offset | Size | Field |
|---|---|---|
| 0 | 1 | 0 |
| 1 | 8 | vault_id, u64 |
| 9 | 1 | bump |
| 10 | 32 | pk_hash_0, the hash of one-time key #0 |
Checks, in order
- Owner signed:
MissingRequiredSignature. - Owner and vault writable:
InvalidAccountData. - Account 2 is the System Program:
IncorrectProgramId. - The vault address is still owned by the System Program and has no data:
AlreadyOpen(6003). This also refuses every closed vault (tombstone). - pk_hash_0 is not 32 zero bytes:
ZeroNextKey(6011). - 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)andAssign(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
| # | Account | Signer | Writable | Checks |
|---|---|---|---|---|
| 0 | owner | yes | only if it is also the destination | Must equal the vault's stored owner. |
| 1 | vault | no | yes | An open Lean Key vault of this program. |
| 2 | destination | no | yes | Any writable account. May be the owner (account 0); may not be the vault. |
Data (665 bytes)
| Offset | Size | Field |
|---|---|---|
| 0 | 1 | 1 |
| 1 | 8 | amount, lamports, u64 |
| 9 | 32 | next_pk_hash |
| 41 | 624 | wots_sig |
Checks, in order
- 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).
- owner (account 0) signed:
- Destination writable:
InvalidAccountData. - amount is not 0:
ZeroAmount(6010). - next_pk_hash is not 32 zero bytes:
ZeroNextKey(6011). - 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). - The vault holds at least its rent-exempt minimum, and amount ≤ balance − minimum:
BelowRent(6006). - The destination's new balance does not overflow 64 bits:
ArithmeticOverflow.
Effects
- Vault balance − amount, destination balance + amount.
- key_index + 1 (checked:
ArithmeticOverflowat 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
| # | Account | Signer | Writable | Checks |
|---|---|---|---|---|
| 0 | owner | yes | no | Must equal the vault's stored owner. |
| 1 | vault | no | yes | An open Lean Key vault of this program. |
| 2 | vault token account | no | yes | Owned by the token program; at least 165 bytes; mint = account 4; owner field = the vault. |
| 3 | destination token account | no | yes | Owned by the token program. Its mint is checked by the token program. |
| 4 | mint | no | no | Owned by the token program; at least 82 bytes; no transfer hook. |
| 5 | token program | no | no | SPL Token or Token-2022. |
All six must be distinct.
Data (666 bytes)
| Offset | Size | Field |
|---|---|---|
| 0 | 1 | 2 |
| 1 | 8 | amount, token base units, u64 |
| 9 | 1 | decimals (passed to TransferChecked; not part of the intent) |
| 10 | 32 | next_pk_hash |
| 42 | 624 | wots_sig |
Checks, in order
- The vault check (as for WithdrawSol):
MissingRequiredSignature,InvalidAccountData,NotVault(6002),NotOwner(6001). - Account 5 is SPL Token or Token-2022:
IncorrectProgramId. - Vault token account writable (
InvalidAccountData); owned by the token program, at least 165 bytes, mint field = account 4, owner field = the vault (BadTokenAccount, 6008). - Destination token account writable (
InvalidAccountData) and owned by the token program (BadTokenAccount, 6008). - 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 isTransferHook(6007); a malformed transfer-hook entry isBadMint(6009). - amount is not 0:
ZeroAmount(6010). - 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). TransferCheckedCPI, 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
amountbase 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
| # | Account | Signer | Writable | Checks |
|---|---|---|---|---|
| 0 | owner | yes | only if it is also the destination | Must equal the vault's stored owner. |
| 1 | vault | no | yes | An open Lean Key vault of this program. |
| 2 | vault token account | no | yes | Owned by the token program; at least 165 bytes; owner field = the vault. |
| 3 | destination | no | yes | Receives the token account's rent. May be the owner (account 0). |
| 4 | token program | no | no | SPL Token or Token-2022. |
Data (657 bytes)
| Offset | Size | Field |
|---|---|---|
| 0 | 1 | 3 |
| 1 | 32 | next_pk_hash |
| 33 | 624 | wots_sig |
Checks, in order
- The vault check:
MissingRequiredSignature,InvalidAccountData,NotVault(6002),NotOwner(6001). - Account 4 is SPL Token or Token-2022:
IncorrectProgramId. - Vault token account writable (
InvalidAccountData); owned by the token program, at least 165 bytes, owner field = the vault (BadTokenAccount, 6008). - Destination writable:
InvalidAccountData. - 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). CloseAccountCPI, 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
| # | Account | Signer | Writable | Checks |
|---|---|---|---|---|
| 0 | owner | yes | only if it is also the destination | Must equal the vault's stored owner. |
| 1 | vault | no | yes | An open Lean Key vault of this program. |
| 2 | destination | no | yes | Any writable account. May be the owner (account 0); may not be the vault. |
Data (657 bytes)
| Offset | Size | Field |
|---|---|---|
| 0 | 1 | 4 |
| 1 | 32 | next_pk_hash |
| 33 | 624 | wots_sig |
Checks, in order
- The vault check:
MissingRequiredSignature,InvalidAccountData,NotVault(6002),NotOwner(6001). - Destination writable:
InvalidAccountData. - 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). - The vault holds at least its rent-exempt minimum:
BelowRent(6006). - 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.