# Sign and Broadcast Transaction

To sign and broadcast a transaction to the Hyperliquid network, follow these steps:

1. Retrieve an unsigned transaction (`unsignedTransaction`) from of one of the following requests responses: [transfer hype from spot to staking balance](https://docs.p2p.org/reference/hyperliquid-transfer), [delegate](https://docs.p2p.org/reference/hyperliquid-delegate), [undelegate](https://docs.p2p.org/reference/hyperliquid-undelegate) and [withdraw](https://docs.p2p.org/reference/hyperliquid-withdraw).
2. Sign the transaction using the following code:

```javascript
import { signUserSignedAction, userSignedActionEip712Types, isValidPrivateKey } from "@nktkas/hyperliquid/signing";

async function signTransaction(privateKey, unsignedTransaction) {
  const action = unsignedTransaction.action;

  return Buffer.from(
    JSON.stringify({
      ...unsignedTransaction,
      signature: await signUserSignedAction({
        wallet: privateKey,
        action: action,
        types: userSignedActionEip712Types[action.type],
      }),
    })
  ).toString("base64");
}

(async () => {
  const [, , rawTransaction, privateKey] = process.argv;

  if (!rawTransaction || typeof rawTransaction !== "string") {
    console.error("Invalid raw transaction. It should be a base64-encoded string.");
    process.exit(1);
  }

  if (!privateKey || typeof privateKey !== "string" || !isValidPrivateKey(privateKey)) {
    console.error("Invalid private key.");
    process.exit(1);
  }

  const unsignedTransaction = JSON.parse(Buffer.from(rawTransaction, "base64").toString("utf-8"));

  if (
    !("action" in unsignedTransaction) ||
    !("type" in unsignedTransaction.action) ||
    !["cDeposit", "tokenDelegate", "cWithdraw"].includes(unsignedTransaction.action.type)
  ) {
    console.error("Invalid unsigned transaction.");
    process.exit(1);
  }

  console.log(await signTransaction(privateKey.startsWith("0x") ? privateKey : `0x${privateKey}`, unsignedTransaction));
})();

/**
 *  To run with env variables:
 *
 *  $ export RAW_TX= 'rawtxhere'
 *  $ export PRIVATE_KEY= 'privatekeyhere'
 *  $ npx --yes -p @nktkas/hyperliquid@0.23.1 node hyperliquid-signer.js $RAW_TX $PRIVATE_KEY
 **/
```

3. Send the signed transaction using the [Send a transaction to the network](https://docs.p2p.org/reference/hyperliquid-send) endpoint.<br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hyperbeat.org/hyperbeat-staking/staking-api/sign-and-broadcast-transaction.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
