Introducing a new way to review your PRs.

Read the announcement

solana-foundation / solana-web3.js

Solana JavaScript SDK

View on GitHub

23 documents · Updated Nov 16, 2026

docs / message-compilation-and-versions

Message compilation and versions

Message compilation turns a payer and a sequence of transaction instructions into the compact message that a transaction signs and sends. It assigns account indexes, records signer and write access in a header, and supports legacy messages plus versioned messages with distinct wire rules. Version 0 messages can load extra addresses from address lookup tables, while version 1 uses static keys only and carries transaction resource settings in the message. See Messages for the wider message model, Legacy and versioned transactions for the transaction boundary, and Address lookup table program for table ownership and use.

Compilation model

Instruction inputs

An instruction names a program, an ordered set of account metadata, and program input bytes. Program helpers create those input bytes from a program instruction index and its fields before message compilation begins. The message compiler keeps the instruction order and later replaces each public key with a compact index.

References: packages/…/compiled-keys.ts• L25-57 packages/…/account-keys.ts• L44-78 packages/…/instruction.ts• L13-33

Account roles

The compiler combines all account references into one key set. Repeated references merge their strongest required roles, while the payer is always a writable signer. Program accounts are marked as invoked so that the compiler can keep them in the static key set.

The resulting static keys use this order:

  • Writable signer accounts.
  • Read-only signer accounts.
  • Writable non-signer accounts.
  • Read-only non-signer accounts.

This order lets the header describe access rights with counts instead of one flag per key. The first writable signer must be the fee payer, and the static set cannot exceed 256 keys.

References: packages/…/compiled-keys.ts• L8-12 packages/…/compiled-keys.ts• L44-57 packages/…/compiled-keys.ts• L60-103

Instruction indexes

After the key set is chosen, MessageAccountKeys presents static keys followed by lookup-loaded writable keys and then lookup-loaded read-only keys. Each instruction program and account reference is mapped to an index in that combined view. The compact instruction stores the program index, account indexes, and raw data bytes.

All indexes are one-byte values. Compilation fails when the combined key view is larger than 256 entries or when an instruction refers to a key that is not in the view.

References: packages/…/account-keys.ts• L8-26 packages/…/account-keys.ts• L44-78 packages/…/index.ts• L35-48

Message forms

Legacy messages

A legacy message contains one static account-key list and has no address table lookup section. Message.compile creates the header and static key order, then stores compiled instruction data with program and account indexes. Its version is identified as legacy.

References: packages/…/legacy.ts• L74-145

Version 0 messages

A version 0 message keeps signer and program-related accounts in its static list and can move eligible non-signer accounts into address lookup tables. It carries the same header, recent blockhash, and compiled instructions as a legacy message, plus lookup descriptors that name a table and its writable and read-only indexes.

MessageV0.compile checks the supplied lookup tables in order. It extracts only matching accounts that are not signers and are not invoked. Extracted accounts leave the static set and become lookup-loaded accounts. If no account from a table is eligible, that table does not appear in the message.

References: packages/…/v0.ts• L49-77 packages/…/v0.ts• L200-238 packages/…/compiled-keys.ts• L106-137

Version 1 messages

A version 1 message keeps its account keys in the static key list and does not expose address table lookups. It carries resource limits and transaction prioritization as message-level settings instead of Compute Budget program instructions. These settings include the compute unit limit, heap size, loaded account data limit, and priority fee. An unset setting is omitted.

Version 1 supports up to 4096 bytes when framed as a transaction, compared with 1232 bytes for the older formats. Its transaction framing is message-first, and RPC readers must request support for transaction version 1. Version 1 compilation also limits the number of instructions to the format's supported maximum.

Version 1 supports compilation, serialization, and deserialization in this module. Its wire form carries a configuration mask and the enabled settings in a fixed order. Callers therefore can create and inspect version 1 message bytes through this message API.

References: packages/…/v1.ts• L61-80 packages/…/v1.ts• L124-147 packages/…/v1.ts• L149-175 packages/…/v1.ts• L191-215

Version selection

The first serialized byte distinguishes message forms. Legacy messages use that byte as the first header field, so its high bit is clear. Versioned messages set the high bit and use the lower seven bits for the version number. The shared deserializer routes legacy bytes to Message.from, version 0 bytes to MessageV0.deserialize, and version 1 bytes to MessageV1.deserialize; other versions are rejected.

References: packages/…/versioned.ts• L11-43 packages/…/legacy.ts• L219-227 packages/…/v0.ts• L290-308 packages/…/v1.ts• L217-226

Header construction

Header counts

The message header describes the account key order with three counts: the number of required signatures, the number of read-only signed accounts at the end of the signed range, and the number of read-only unsigned accounts at the end of the unsigned range. Signatures therefore correspond to the first numRequiredSignatures account keys.

The same counts determine whether a static account is writable. In a version 0 message, lookup-loaded accounts are never signers, and the writable lookup segment comes before the read-only lookup segment. Version 1 uses the same header model but has no lookup-loaded account segment.

References: packages/…/index.ts• L10-23 packages/…/legacy.ts• L147-164 packages/…/v0.ts• L127-151 packages/…/v1.ts• L102-121

Lookup resolution

A version 0 message stores lookup table keys and indexes rather than the loaded public keys. To inspect the full account list, the caller must provide either the resolved writable and read-only addresses or the matching AddressLookupTableAccount objects. Resolution follows each descriptor and preserves its writable and read-only groups.

Resolution fails when a required table is missing, an index is outside the table, or the supplied loaded-address counts do not match the descriptors. A version 0 message with lookups cannot produce its full account view without this resolution step.

References: packages/…/v0.ts• L79-113 packages/…/v0.ts• L142-185 packages/…/account-keys.ts• L20-42

Wire serialization

Legacy layout

Legacy serialization produces the bytes that are signed for a legacy transaction. It writes the three header counts, a short-vector encoded key count, each 32-byte account key, the recent blockhash, and the ordered instruction list. Each instruction contains a program index, a short-vector encoded account-index list, and a short-vector encoded data payload.

References: packages/…/legacy.ts• L178-195 packages/…/shortvec-encoding.ts• L15-27

Version 0 layout

Version 0 serialization adds a version prefix before the header. After the static keys and instructions, it writes the lookup descriptor count and each descriptor's table key, writable index list, and read-only index list. Counts for keys, instructions, lookups, account indexes, and data use the same short-vector length form.

The message is bounded by the transaction packet data size. Serialization returns only the bytes that were written, not the unused space in the working buffer.

References: packages/…/v0.ts• L224-309 packages/…/v0.ts• L311-424 packages/…/constants.ts• L1-10

Version 1 layout

Version 1 uses its own wire layout after the version prefix and header. It records a configuration mask followed by only the enabled transaction settings, then the blockhash, static keys, and compiled instructions. It does not write an address lookup table section.

Version 1 supports serialization in this module. Its instruction headers and payloads preserve the program indexes, account indexes, and instruction data needed to reconstruct the compiled instructions.

References: packages/…/v1.ts• L149-175 packages/…/v1.ts• L177-215

Wire deserialization

Legacy decoding

Legacy decoding reads the header, key list, blockhash, and instruction list in wire order. It rejects a byte stream whose first byte indicates a versioned message, which prevents a version 0 message from being interpreted as a legacy message. Instruction data is restored to the legacy message representation while compiled accessors expose raw bytes.

References: packages/…/legacy.ts• L197-227

Version 0 decoding

Version 0 decoding first checks the version prefix and accepts only version 0. It then restores the header, static keys, blockhash, compiled instructions, and lookup descriptors without resolving the table addresses. Address resolution remains a separate operation because deserialization has only the table keys and indexes.

References: packages/…/v0.ts• L290-512 packages/…/versioned.ts• L21-35

Version 1 decoding

Version 1 decoding checks the version prefix, validates the configuration mask, and restores the header, blockhash, static keys, transaction settings, and compiled instructions. It rejects unknown configuration bits and requires the input to end after the last instruction. It does not resolve or load addresses from lookup tables because version 1 messages contain no lookup descriptors.

References: packages/…/v1.ts• L177-227 packages/…/versioned.ts• L27-43

Round-trip boundary

Serialization and deserialization preserve the message structure needed to sign or inspect it. A version 0 round trip preserves lookup descriptors, but it does not embed the current contents of the lookup tables. The caller must resolve those tables again when it needs the complete account-key view. Version 1 supports a complete serialization round trip through this module and preserves its optional transaction settings through the configuration mask.

References: packages/…/v0.ts• L240-308 packages/…/v1.ts• L149-227

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep the account segment order aligned between CompiledKeys, MessageAccountKeys, and the writable checks in all message versions.
  • Keep the version prefix rules aligned between serialization, legacy decoding, version 0 and version 1 decoding, and VersionedMessage.deserialize.
  • Keep lookup descriptor order and loaded-address segment order aligned so instruction indexes resolve to the same public keys after deserialization.
  • Keep all length prefixes and one-byte indexes within the wire limits used by the packet-size and short-vector encodings.
  • Keep version 1 configuration-mask bits aligned with the order and types of the optional settings that follow the mask.

Important failure modes

TriggerConsequenceGuard
More than 256 static or combined account keysCompilation cannot represent an account index in one byteCompiledKeys and MessageAccountKeys reject the overflow
An instruction key is absent from the compiled account viewThe instruction cannot be encoded with valid indexesAccount-key compilation raises an unknown-key error
A version 0 lookup table is not supplied during account resolutionThe full account list is unavailablegetAccountKeys requires resolved addresses or table accounts
A lookup index is outside its tableThe message points to no valid public keyLookup resolution rejects the index
A versioned message uses an unsupported versionThe message cannot be decodedVersionedMessage.deserialize rejects versions other than 0 and 1
A version 0 message is passed to legacy decodingThe message could be parsed with the wrong layoutLegacy decoding rejects a set version prefix
A version 1 message has unknown configuration bits or a configuration value of the wrong kindThe message is rejected instead of producing an ambiguous configurationVersion 1 configuration decoding validates supported bits and value types

Key files

FileWhy to read it
packages/…/compiled-keys.tsShows how instruction accounts become deduplicated roles, header counts, and static keys.
packages/…/account-keys.tsShows how static and lookup-loaded keys form one index space for compiled instructions.
packages/…/legacy.tsDefines legacy message compilation, access rules, serialization, and decoding.
packages/…/v0.tsDefines version 0 compilation, lookup use, serialization, and decoding.
packages/…/v1.tsDefines version 1 static-key messages, transaction settings, serialization, and deserialization limits.
packages/…/versioned.tsDefines version detection and the common deserialization boundary.
packages/…/index.tsDefines the shared header, lookup descriptor, and compiled-instruction shapes.
packages/…/instruction.tsExplains how program instruction fields become the data bytes carried by a message.