Learn how to create, sign, and broadcast transactions on the Hive blockchain
Transactions are the fundamental unit of interaction with the Hive blockchain. The Transaction class provides a complete lifecycle for creating, signing, and broadcasting operations.
Create - Initialize a transaction with default or custom expiration
Add Operations - Append one or more operations to execute
Sign - Sign with one or more private keys
Broadcast - Submit to the Hive network
1
Create Transaction
import { Transaction } from 'hive-tx'// Default 60 second expirationconst tx = new Transaction()// Custom expiration (3 minutes)const txCustom = new Transaction({ expiration: 180_000 })
2
Add Operations
// Add a vote operationawait tx.addOperation('vote', {voter: 'alice',author: 'bob',permlink: 'awesome-post',weight: 10000 // 100% upvote})// Add more operations to the same transactionawait tx.addOperation('transfer', {from: 'alice',to: 'bob',amount: '1.000 HIVE',memo: 'Thanks for the post!'})
3
Sign Transaction
import { PrivateKey } from 'hive-tx'const key = PrivateKey.from('5JdeC9P7Pbd1uGdFVEsJ41EkEnADbbHGq6p1BwFxm6txNBsQnsw')tx.sign(key)
4
Broadcast
// Broadcast and return immediatelyconst result = await tx.broadcast()console.log(result.tx_id) // Transaction ID// Broadcast and wait for confirmationconst confirmedResult = await tx.broadcast(true)console.log(confirmedResult.status) // 'within_irreversible_block'
Array of hex-encoded signatures (added when you call sign())
The ref_block_num and ref_block_prefix are automatically set from the current head block when you call addOperation(). This ties your transaction to recent blockchain state and prevents replay attacks.
try { await tx.broadcast()} catch (e) { // Error: Attempted to broadcast a transaction with no signatures // Solution: Call sign(key) before broadcasting}