Skip to content
Lean Key
Reference

Program accounts

The vault account byte by byte, its address derivation, rent, token accounts, the closed flag and the tombstone.

Lean Key has one kind of account of its own: the vault. There are no global, config or admin accounts. Token balances live in ordinary token accounts owned by the vault.

Program ids

NetworkProgram id
localnetTwoKWxqD6kL6ow9bAEZ3Kir9hUFKbocX4LZR4j5R3W6 (fixed keypair in scripts/localnet/program-keypair.json)
devnetTwoKhewkScEnH75rbs9nC5ScrsFQT93DrHsSSZvpVHn
mainnetKeyW6VkKqn3awBMcun9aLPKJuCP77BFM7KE66CyzCjE

Source: PROGRAM_ID in packages/core/src/brand.ts.

The vault account

91 bytes, owned by the Lean Key program. All integers are little-endian.

OffsetSizeFieldTypeSet byNotes
01versionu8InitVaultAlways 1. Every other instruction refuses an account whose version is not 1 (NotVault, 6002). Written last by InitVault.
11bumpu8InitVaultThe bump seed of the vault's address, used when the vault signs token CPIs.
232owneraddressInitVaultThe Ed25519 owner. Never changes. Every other instruction requires this account to sign as account 0.
348vault_idu64InitVaultThe vault number in the address seeds.
428key_indexu64InitVault, every withdrawal0 at opening; plus 1 after every successful withdrawal or close.
5032pk_hash32 bytesInitVault, every withdrawalSHA-256 hash of the current one-time public key. Replaced by the intent's next_pk_hash after every success.
828created_slotu64InitVaultThe slot from the Clock sysvar when the vault was opened.
901flagsu8InitVault, CloseVaultBit 0 (value 1): closed. The other bits are 0.

Decoding in TypeScript:

import { decodeVault, VAULT_SIZE, VAULT_OWNER_OFFSET } from "@twokey/core";

const info = await connection.getAccountInfo(vaultAddress);
const v = decodeVault(new Uint8Array(info!.data)); // throws if not 91 bytes or version ≠ 1
v.keyIndex;   // bigint
v.pkHash;     // Uint8Array(32)
v.closed;     // flags & 1

findVaults lists every vault of an owner with getProgramAccounts, filtered on dataSize = 91 and memcmp of the owner at offset 2 (VAULT_OWNER_OFFSET).

The vault address

A vault lives at a program-derived address of the Lean Key program:

seeds = [ "vault" (5 bytes ASCII), owner (32 bytes), vault_id (u64 LE, 8 bytes) ]
import { vaultAddress } from "@twokey/core";
const [vault, bump] = vaultAddress(programId, owner, 0n); // findProgramAddressSync

InitVault receives the bump in its instruction data and checks the address itself: it computes SHA-256 of "vault" ‖ owner ‖ vault_id ‖ bump ‖ program_id ‖ "ProgramDerivedAddress" and compares it with the vault account's address. A mismatch is BadAddress (6004). The program does not use the runtime's signature on the vault account as evidence: the runtime passes on signatures the transaction already carries, so without this check a keypair's address could pose as a vault (tested: "refuses a keypair address even when that keypair signs"). An address on the Ed25519 curve cannot become a vault anyway, because no program can sign for it when creating the account.

The clients always use the canonical bump from findProgramAddressSync, and leankey verify VAULT reports whether the vault's address and stored bump match that derivation. The program itself accepts any bump for which the address matches.

Rent

The vault keeps its rent-exempt minimum at all times. The program computes it from the Rent sysvar for 91 bytes:

minimum = (128 + 91) × lamports_per_byte_year × exemption_threshold

The threshold must be exactly 2.0, or 1.0 (once SIMD-0194 is active). Any other value is refused with UnsupportedSysvar rather than guessed. Every multiplication is checked for overflow.

  • InitVault counts whatever is already at the address (anyone can send lamports there) and transfers only the shortfall from the owner.
  • WithdrawSol refuses an amount above balance − minimum (BelowRent, 6006).
  • CloseVault sends exactly balance − minimum and leaves the minimum.

The withdrawable SOL of a vault is therefore its balance minus the minimum. The clients read the minimum with getMinimumBalanceForRentExemption(91); the CLI prints both, for example 1.50152424 SOL (1.5 SOL withdrawable; 0.00152424 SOL rent-exempt minimum) on a local validator.

The Lean theorem vault_stays_rent_exempt states that no instruction takes an open vault below the minimum, and that opening one funds it to at least the minimum.

Token accounts

A vault holds tokens in token accounts whose owner (authority) is the vault's address. The clients use the vault's associated token account for each mint, computed for an off-curve owner:

ATA = PDA of the Associated Token Account program with seeds [ vault, token_program_id, mint ]

The program does not require the associated address. It accepts any token account that:

  • is owned by the token program given to the instruction, which must be SPL Token (TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA) or Token-2022 (TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb), else IncorrectProgramId;
  • has at least 165 bytes of data;
  • holds the expected mint in bytes 0 to 31;
  • names the vault in bytes 32 to 63 (the token account's owner field).

Anything else is BadTokenAccount (6008). The vault signs the TransferChecked and CloseAccount CPIs with its seeds ["vault", owner, vault_id, bump], read from its own data.

Token-2022 mints with a transfer hook

v1 does not pass a transfer-hook program its extra accounts, so it refuses those mints. For a Token-2022 mint longer than 166 bytes the program walks the extension list (type u16, length u16, value) from byte 166, at most 64 entries. An extension of type 14 (TransferHook) whose program id (value bytes 32 to 63) is not all zero is refused with TransferHook (6007); a TransferHook entry shorter than 64 bytes or running past the end of the data is BadMint (6009). Other extensions are not inspected; the token program applies them during TransferChecked. The clients run the same check before signing, and the CLI refuses to deposit such a token.

Closed vaults and tombstones

CloseVault sweeps everything above the rent-exempt minimum to the destination, sets bit 0 of flags, and rotates the key like any withdrawal. The account is not deleted. It stays as a tombstone: 91 bytes, owned by the program, version 1, holding the rent-exempt minimum.

  • It can never be opened again. InitVault only creates a vault where the address still belongs to the System Program and holds no data; otherwise it fails with AlreadyOpen (6003). If closing deleted the account, the address would become free, and anyone holding only the owner key could open a new vault there with their own seed.
  • The program does not block a closed vault. The flag is informational. Every instruction still works with both signatures; tokens left in a closed vault can still be withdrawn (tested: "tokens left in a closed vault can still be withdrawn"), and so can SOL sent to it after closing.
  • The clients treat it as closed. The CLI refuses to deposit into it and to close it again; leankey status prints closed (tombstone; tokens still withdrawable).

Events

Every successful instruction logs five numbers with sol_log_64, which the runtime prints as Program log: 0x…, 0x…, 0x…, 0x0, 0x0:

InstructionValues
InitVault0, 0, 0, 0, 0
WithdrawSol1, key index before, lamports sent, 0, 0
WithdrawToken2, key index before, token amount, 0, 0
CloseTokenAccount3, key index before, 0, 0, 0
CloseVault4, key index before, lamports swept, 0, 0

parseEventLogs in packages/core/src/decode.ts reads them, counting only lines logged while the Lean Key program is the innermost program running.

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.