Sign and Broadcast Transaction

To complete any staking or withdrawal operation on Sui, sign the unsigned transaction provided by the Staking API and broadcast it to the network.

1. Prepare the transaction

Retrieve an unsigned serialized transaction from the relevant API endpoint, e.g., staking/stake or staking/withdraw.

2. Sign the transaction

Sign the unsigned transaction using your local signer or the following code:

import { Ed25519Keypair, RawSigner } from '@mysten/sui.js';

const keypair = Ed25519Keypair.fromSecretKey(
  Uint8Array.from(Buffer.from(process.env.PRIVATE_KEY!, 'hex'))
);
const signer = new RawSigner(keypair, provider);

// Convert unsigned hex string to bytes
const txBytes = Uint8Array.from(Buffer.from(process.env.UNSIGNED_TX!, 'hex'));

const { signature } = await signer.signTransactionBlock(txBytes);

console.log('Signed TX:', signature);

Upon successful execution, the script prints the signed transaction, ready for broadcasting.

3. Send the transaction

Broadcast the signed transaction to the Sui network by sending a POST request to /api/v1/sui/{network}/transaction/send.

Example request (for testnet network):

curl --request POST \
     --url https://api-test.p2p.org/api/v1/sui/testnet/transaction/send \
     --header 'accept: application/json' \
     --header 'Authorization: Bearer <token>' \
     --header 'content-type: application/json' \
     --data '
{
  "transaction": "0x1f2e3d4c5b6a7e8f9a0b1c2d3e4f5g6h7i8j9k0l1m2n3o4p5q6r7s8t9u0v1w2x3y4z5a6b7c8d9e0f1g2h3i4j5k6l7m8n9o0p",
  "signature": "0x696f4402d7151fb49e52b629de3ce3098f3dda7721a7425c000b4f26653709e3"
}
'
  • signedTransaction — signed transaction in the hexadecimal format which needs to be broadcasted to the network.
  • signature — signature of the staker account address.

Example response:

{
    "error": null,
    "result": {
        "transactionDigest": "DiXajfAeTLVhiQZuW8aH2UD1XsCEoZVH4twRC5PjeK5"
    }
}
  • transactionDigest — hash of the transaction.

Use the transactionDigest object to check the transaction status via explorer, e.g., example of the broadcasted transaction.

What's next?