> ## 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.

# Cosmos signing

> Sign the protobuf SignDoc verbatim with CosmJS and submit the resulting hash.

On Cosmos SDK chains, `unsignedTransaction` is a hex-encoded protobuf `SignDoc`, built for direct (protobuf) signing, not amino. Decode it, sign it exactly as returned, and never rebuild it from chain state: the `SignDoc` already embeds the account's sequence, the fee, and the gas, and resigning a rebuilt document produces a signature over different bytes that the chain rejects.

## 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 Cosmos wallet capable of direct signing, such as `DirectSecp256k1HdWallet` from `@cosmjs/proto-signing`.
* The `cosmosPubKey` argument: a bech32-encoded public key (`cosmospub1...`), required by most Cosmos yields' `mechanics.arguments.enter.fields` alongside `amount` and, for native staking, `validatorAddress`. Confirm the exact required fields per yield before building the request; do not assume every Cosmos yield needs all three.

```typescript theme={null}
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { toBech32 } from "@cosmjs/encoding";

const wallet = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix: "cosmos" });
const [account] = await wallet.getAccounts();

// account.pubkey is the raw compressed secp256k1 key; the API expects it bech32-encoded.
const cosmosPubKey = toBech32("cosmospub", account.pubkey);
const address = account.address; // used as the action's `address` field
```

## Decode, sign, and submit

```typescript theme={null}
import { DirectSecp256k1HdWallet } from "@cosmjs/proto-signing";
import { StargateClient } from "@cosmjs/stargate";
import { fromHex, fromBase64 } from "@cosmjs/encoding";
import { SignDoc, TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";

const signer = await DirectSecp256k1HdWallet.fromMnemonic(mnemonic, { prefix: "cosmos" });
const [account] = await signer.getAccounts();
const client = await StargateClient.connect(process.env.COSMOS_RPC_URL!);

for (const tx of transactions) {
  // Decode the SignDoc verbatim; do not re-derive it from account state.
  const signDoc = SignDoc.decode(fromHex(tx.unsignedTransaction as string));

  const { signed, signature } = await signer.signDirect(account.address, signDoc);

  // Assemble the broadcastable TxRaw from the SIGNED doc, not the original.
  const txRawBytes = TxRaw.encode({
    bodyBytes: signed.bodyBytes,
    authInfoBytes: signed.authInfoBytes,
    signatures: [fromBase64(signature.signature)],
  }).finish();

  const result = await client.broadcastTx(txRawBytes);

  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: result.transactionHash }),
  });

  // Wait for CONFIRMED before the next stepIndex.
}
```

## 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.

> [!WARNING]
>
> A missing or misplaced `cosmosPubKey` is the most common Cosmos error: `POST /v1/actions/enter` (or `/exit`) returns `400 Bad Request`. Send it as a flat field inside `arguments`, alongside `amount` and `validatorAddress`, not nested under any wrapper.

## 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.