> ## Documentation Index
> Fetch the complete documentation index at: https://docs.yield.xyz/llms.txt
> Use this file to discover all available pages before exploring further.

# Solana signing

> Detect legacy vs versioned encoding, sign with @solana/web3.js, and submit.

On Solana, `unsignedTransaction` is a serialized transaction, hex or base64 encoded depending on the yield, and either a legacy `Transaction` or a versioned (v0) `VersionedTransaction`. Detect both before signing; a single client that only handles one combination will fail on yields built the other way.

## Prerequisites

* An action's `transactions[]`; see [Transaction signing](https://docs.yield.xyz/guides/transaction-signing/index.md) for decoding by chain family and [Enter and exit flows](https://docs.yield.xyz/guides/earn/enter-exit-flows.md) for the request and poll loop.
* A Solana keypair, or a connected wallet such as Phantom for browser signing.

## Decode, sign, and submit

```typescript theme={null}
import { Connection, Transaction, VersionedTransaction, Keypair } from "@solana/web3.js";
import bs58 from "bs58";

const connection = new Connection(process.env.SOLANA_RPC_URL!, "confirmed");
const keypair = Keypair.fromSecretKey(bs58.decode(process.env.SOLANA_PRIVATE_KEY!));

function decode(unsignedTransaction: string): Buffer {
  // Try hex first, fall back to base64. Both encodings are in live use across yields.
  return /^[0-9a-fA-F]+$/.test(unsignedTransaction)
    ? Buffer.from(unsignedTransaction, "hex")
    : Buffer.from(unsignedTransaction, "base64");
}

for (const tx of transactions) {
  const txBytes = decode(tx.unsignedTransaction as string);

  // Some yields return a legacy Transaction, others a v0 VersionedTransaction.
  // Only the deserialize attempt is guarded; signing and broadcast errors must propagate.
  let signature: string;
  try {
    const versioned = VersionedTransaction.deserialize(txBytes);
    versioned.sign([keypair]);
    signature = await connection.sendRawTransaction(versioned.serialize());
  } catch {
    const legacy = Transaction.from(txBytes);
    legacy.sign(keypair);
    signature = await connection.sendRawTransaction(legacy.serialize());
  }

  await connection.confirmTransaction(signature, "confirmed");

  // The signature IS the transaction hash on Solana.
  await fetch(`https://api.yield.xyz/v1/transactions/${tx.id}/submit-hash`, {
    method: "PUT",
    headers: { "x-api-key": process.env.YIELD_API_KEY!, "content-type": "application/json" },
    body: JSON.stringify({ hash: signature }),
  });
}
```

> [!NOTE]
>
> Solana transactions carry a recent blockhash that is only valid for a short window (about 150 blocks). If signing takes longer than that, the broadcast fails; request a new action rather than reusing the stale `unsignedTransaction`.

## Browser wallets (Phantom)

`window.phantom.solana` accepts a deserialized transaction directly and signs and sends it in one call, returning `{ signature }` rather than a separate broadcast step:

```typescript theme={null}
const phantomSolana = window.phantom?.solana;
if (!phantomSolana?.isPhantom) throw new Error("Phantom Solana wallet not found");
await phantomSolana.connect();

const txBytes = decode(tx.unsignedTransaction as string);
let transaction: VersionedTransaction | Transaction;
try {
  transaction = VersionedTransaction.deserialize(txBytes);
} catch {
  transaction = Transaction.from(txBytes);
}

const { signature } = await phantomSolana.signAndSendTransaction(transaction);
await connection.confirmTransaction(signature, "confirmed");
```

This is unrelated to EVM browser signing; a Solana-connected Phantom exposes `window.phantom.solana`, not the `window.phantom.ethereum` provider used for EVM networks.

## Submit the signed transaction instead

To hand Yield.xyz the signed bytes rather than broadcasting yourself, call `POST /v1/transactions/{tx.id}/submit` with `{ "signedTransaction": ... }` instead of `submit-hash`. Use one path per transaction, never both. See [Transaction signing](https://docs.yield.xyz/guides/transaction-signing/index.md#sign-and-choose-a-submission-path) for when each fits.

## Next steps

> [card] [Transaction signing](https://docs.yield.xyz/guides/transaction-signing/index.md)
>
> The shared contract across chain families: decode, validate, sign, submit.

> [card] [Enter and exit flows](https://docs.yield.xyz/guides/earn/enter-exit-flows.md)
>
> Build the request, branch on executionPattern, and poll to a terminal status.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.