Sign and Broadcast Transaction
To sign and broadcast a transaction to the Hyperliquid network, follow these steps:
- Retrieve an unsigned transaction (
unsignedTransaction
) from of one of the following requests responses: transfer hype from spot to staking balance, delegate, undelegate and withdraw. - Sign the transaction using the following code:
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/[email protected] node hyperliquid-signer.js $RAW_TX $PRIVATE_KEY **/
- Send the signed transaction using the Send a transaction to the network endpoint.
What's Next?
- Staking API reference
Updated 1 day ago