Network and protocol utilities
Shared network and protocol utilities give the client one set of rules for choosing Solana clusters, moving data between objects and wire bytes, checking required invariants, signing data, hashing data, pacing time-based work, and using the available runtime network APIs. These utilities sit below connection, transaction, message, and loader flows so those systems can use the same protocol and platform behavior.
Network endpoint selection
Cluster endpoint selection maps a supported Solana cluster to its HTTP RPC address. The default is the secure Devnet endpoint, while callers can select Devnet, Testnet, or Mainnet Beta and can explicitly request an HTTP address; clusterApiUrl is exported through the public utility surface (packages/…/cluster.ts• L1-35, packages/…/index.ts• L1-5).
The connection layer accepts an HTTP or HTTPS RPC endpoint and derives a WebSocket endpoint when the caller does not provide one. This keeps request and subscription channels aligned while still allowing a caller to supply a separate WebSocket address (packages/…/connection.ts• L359-365, packages/…/connection.ts• L3204-3248).
References: packages/…/cluster.ts• L1-35 packages/…/index.ts• L1-5 packages/…/connection.ts• L359-365 packages/…/connection.ts• L3204-3248 packages/…/makeWebsocketUrl.ts• L1-26
Protocol invariant checks
Invariant checks stop malformed data and invalid client state before they reach signing, serialization, or RPC operations. The assertion helper converts a false condition into an error, while guarded array access reports a truncated byte buffer instead of allowing a silent read past the available data (packages/…/assert.ts• L1-8, packages/…/guarded-array-utils.ts• L1-34).
These checks protect boundaries between decoded wire data and higher-level objects. They enforce rules such as required signature counts, signature sizes, supported message versions, and valid account lookup sizes (packages/…/versioned.ts• L26-40, packages/…/versioned.ts• L112-125, packages/…/v0.ts• L427-440, packages/…/state.ts• L40-45).
References: packages/…/assert.ts• L1-8 packages/…/guarded-array-utils.ts• L1-34 packages/…/versioned.ts• L26-40 packages/…/versioned.ts• L112-125 packages/…/v0.ts• L427-440 packages/…/state.ts• L40-45
Ed25519 signing and verification
Ed25519 utilities provide the shared cryptographic boundary for Solana messages and keys. They sign message bytes with a secret key, verify a signature against message bytes and a public key, and answer whether a public key represents a valid curve point (packages/…/ed25519.ts• L11-49). The operations use the runtime crypto boundary for signing and verification while keeping the curve-point check available as a separate validation step.
The signing and verification paths normalize or copy input bytes before they pass them to the crypto APIs. This gives callers a stable operation even when source arrays are mutable. Key formats and their broader use in account and signature flows are described in Keys and signatures.
References: packages/…/ed25519.ts• L11-49
SHA-256 hashing
SHA-256 support provides a shared hash operation for protocol data that needs a fixed digest. The helper checks that the runtime can provide digest support, normalizes the input bytes, and returns the resulting 32-byte hash through the Web Crypto API (packages/…/sha256.ts• L1-20).
This boundary keeps hash callers independent of the runtime's byte-array representation and makes missing digest support fail before a protocol operation depends on it. It complements signing and verification without replacing those operations.
References: packages/…/sha256.ts• L1-20
Slot timing and asynchronous control
Timing utilities provide a common estimate of Solana slot duration and small async controls for work that must wait or stop competing with a slow operation. The slot constants derive milliseconds per slot from ticks per second and ticks per slot (packages/…/timing.ts• L1-23). The sleep helper delays a task, while promiseTimeout returns null when a competing promise does not finish in time (packages/…/sleep.ts• L1-4, packages/…/promise-timeout.ts• L1-14).
Connection and loader flows use these values to poll for new blockhashes, wait across slot boundaries, slow transaction submission, and retry rate-limited RPC requests (packages/…/connection.ts• L1682-1697, packages/…/connection.ts• L5695-5725, packages/…/loader.ts• L191-200, packages/…/loader.ts• L243-260). The timing is an operational estimate rather than a source of chain state; code that needs current chain progress checks the RPC result.
References: packages/…/timing.ts• L1-23 packages/…/sleep.ts• L1-4 packages/…/promise-timeout.ts• L1-14 packages/…/connection.ts• L1682-1697 packages/…/connection.ts• L5695-5725 packages/…/loader.ts• L191-200 packages/…/loader.ts• L243-260
Raw transaction submission and confirmation
Raw transaction support connects an already signed wire transaction to the connection's send and confirmation flow. sendAndConfirmRawTransaction maps confirmation options to send options, then confirms by a supplied block-height or durable-nonce strategy when present; otherwise it uses the transaction signature (packages/…/send-and-confirm-raw-transaction.ts• L25-100). It also accepts the older options-only form so existing callers can continue to use signature-based confirmation.
The helper uses the connection commitment when the caller does not provide one and falls back to finalized when neither side sets it. It sends only the supported send options to the connection. A failed status becomes a SendTransactionError that carries the signature and status data, so callers can inspect transaction logs after the RPC result is available (packages/…/send-and-confirm-raw-transaction.ts• L73-100, packages/…/errors.ts• L7-103). This gives raw wire transactions the same confirmation contract as signed transaction flows while allowing confirmation to use the validity rule that created the transaction.
The following sequence shows how raw bytes become a submitted signature and then a confirmed result.
sequenceDiagram
participant Caller
participant Helper as Raw transaction helper
participant Connection
participant RPC as Solana RPC
Caller->>Helper: Signed raw transaction and options
Helper->>Connection: Send raw transaction
Connection->>RPC: Submit encoded transaction
RPC-->>Connection: Transaction signature
Helper->>Connection: Confirm by strategy or signature
Connection->>RPC: Read confirmation status
RPC-->>Connection: Status
Connection-->>Helper: Success or transaction error
Helper-->>Caller: Signature or error
References: packages/…/send-and-confirm-raw-transaction.ts• L25-100 packages/…/connection.ts• L6038-6058 packages/…/connection.ts• L3858-3894 packages/…/errors.ts• L7-103
Signed transaction submission and confirmation
Signed transaction support prepares a transaction with its signers, submits it through the connection, and selects a confirmation rule that matches how the transaction remains valid. A durable nonce uses the nonce account and nonce value, a recent blockhash uses its last valid block height, and the fallback path confirms by signature (packages/…/send-and-confirm-transaction.ts• L24-112).
The helper also passes an abort signal to confirmation when the selected strategy supports cancellation. It rejects malformed nonce instructions before sending and reports an on-chain error through the same SendTransactionError contract used by raw transactions. If a caller supplies an abort signal with the deprecated signature-only fallback, the helper warns that this path cannot cancel confirmation (packages/…/send-and-confirm-transaction.ts• L44-110). This keeps transaction construction and wire submission separate from the shared confirmation result. The wider send and confirm lifecycle is described in Transaction send and confirm.
References: packages/…/send-and-confirm-transaction.ts• L24-112 packages/…/connection.ts• L3858-3894 packages/…/errors.ts• L7-103
Runtime network adaptation
The network layer presents one fetch contract to the client while adapting its source to the runtime. The default path uses the platform fetch API when available and falls back to a Node fetch provider when it is not (packages/…/fetch-impl.ts• L1-16). Browser and React Native forks bind the same contract to each runtime's global fetch types (packages/…/fetch-impl.ts• L1-4, packages/…/fetch-impl.ts• L1-4).
This boundary lets connection code focus on RPC behavior rather than runtime-specific transport setup. It also preserves extension points for custom fetch functions and request middleware supplied through connection configuration (packages/…/connection.ts• L3208-3239).
References: packages/…/fetch-impl.ts• L1-16 packages/…/fetch-impl.ts• L1-4 packages/…/fetch-impl.ts• L1-4 packages/…/connection.ts• L3208-3239
Related pages
- Cluster and encoding helpers explains cluster URL selection and Solana compact wire encoding in more detail.
- Transaction send and confirm explains the broader transaction submission and confirmation flow.
- Keys and signatures explains key formats and signature use in account and transaction flows.
- Platform runtime forks explains how supported runtimes provide compatible network behavior.
- Connection and RPC explains the client boundary for RPC requests and subscriptions.
Change checklist
When these files or sections are changed, remember to consider these:
- A change to supported cluster names or endpoint schemes must stay aligned with the public utility export and connection WebSocket derivation.
- A change to guarded byte access must preserve the failure behavior expected by all wire-data readers.
- A change to Ed25519 input handling or key formats must stay aligned with account, message, and signature consumers.
- A change to SHA-256 input normalization or runtime capability checks must stay aligned with every protocol consumer that expects a fixed digest.
- A change to slot timing constants must be checked against blockhash polling, rate pacing, and deployment slot waits.
- A change to raw or signed transaction confirmation inputs must preserve block-height, durable-nonce, abort, and fallback confirmation paths and their send-option mapping.
- A change to the fetch boundary must keep the default runtime path, browser fork, and React Native fork compatible with connection-level fetch overrides.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| An unsupported cluster name reaches endpoint selection | No usable RPC URL is returned and the caller receives an error | The cluster type narrows normal callers and runtime lookup rejects unknown names (packages/…/cluster.ts• L14-34) |
An endpoint does not start with http: or https: | Connection setup fails before RPC or WebSocket use | Endpoint validation rejects unsupported schemes (packages/…/connection.ts• L359-365) |
| A wire buffer ends before a declared field or length | Deserialization cannot safely construct a message or transaction | Guarded byte reads throw an end-of-buffer error (packages/…/guarded-array-utils.ts• L1-34) |
| A required signature is missing or has the wrong size | Signing or transaction assembly fails instead of producing invalid wire data | Versioned transaction checks enforce signer count and 64-byte signatures (packages/…/versioned.ts• L26-40, packages/…/versioned.ts• L112-125) |
| An Ed25519 public key is not a valid curve point or a signature does not match the message | Verification returns false or the crypto operation rejects the input | Curve validation and the runtime Ed25519 verifier check the public key and signed bytes (packages/…/ed25519.ts• L13-19, packages/…/ed25519.ts• L30-49) |
| The runtime has no SHA-256 digest support | Hash-dependent protocol work fails before it produces a digest | The hash helper checks digest support before calling Web Crypto (packages/…/sha256.ts• L12-19) |
| A nonce transaction does not contain a valid advance-nonce instruction | The transaction is rejected before send and no confirmation can match its nonce | Signed transaction setup decodes the nonce instruction and throws on invalid nonce data (packages/…/send-and-confirm-transaction.ts• L44-55) |
| A raw transaction confirmation strategy does not match the transaction's validity rule | Confirmation can use the wrong completion or expiry condition | The helper distinguishes block-height and durable-nonce strategies before calling confirmTransaction (packages/…/send-and-confirm-raw-transaction.ts• L52-90) |
| RPC work is rate limited, a blockhash does not change, or confirmation is cancelled | Requests may fail repeatedly or confirmation can stop before a fresh result | Connection retries with delays, blockhash polling yields between checks, and supported confirmation paths race cancellation (packages/…/connection.ts• L1682-1697, packages/…/connection.ts• L3839-3894) |
Key files
| File | Why to read it |
|---|---|
| packages/…/cluster.ts | Defines the supported cluster choices and their RPC URL policy. |
| packages/…/assert.ts and packages/…/guarded-array-utils.ts | Show how protocol and buffer invariants fail early. |
| packages/…/ed25519.ts | Defines the shared public-key check, signing, and signature verification boundary. |
| packages/…/sha256.ts | Defines the shared SHA-256 digest boundary and runtime capability check. |
| packages/…/timing.ts | Defines the slot-time estimate used by polling and pacing flows. |
| packages/…/connection.ts | Shows how endpoint validation, WebSocket derivation, fetch selection, retries, blockhash timing, and transaction confirmation meet in the network client. |
| packages/…/send-and-confirm-raw-transaction.ts | Shows how signed wire bytes use confirmation strategies and report failed status. |
| packages/…/send-and-confirm-transaction.ts | Shows how signed transactions select nonce, block-height, or fallback confirmation. |
| packages/…/errors.ts | Defines the error contract for failed transaction submission and later log lookup. |
| packages/…/fetch-impl.ts | Defines the default runtime fetch boundary. |
| packages/…/fetch-impl.ts and packages/…/fetch-impl.ts | Show the supported runtime-specific fetch bindings. |
| packages/…/legacy.ts, packages/…/v0.ts, and packages/…/versioned.ts | Show how protocol utilities and invariant checks support the main Solana wire objects. |