Overview
The Transaction class provides a high-level interface for creating, signing, and broadcasting transactions to the Hive blockchain. It handles transaction creation, operation management, signature collection, and broadcasting with automatic retry logic.
Constructor
const tx = new Transaction ( options ? )
Configuration options for the transaction transaction
TransactionType | Transaction
Existing transaction to wrap or clone
Transaction expiration in milliseconds (max 86400000 / 24 hours)
Example
import { Transaction } from 'hive-tx'
// Create new transaction with default 60 second expiration
const tx = new Transaction ()
// Create with custom expiration (5 minutes)
const tx = new Transaction ({ expiration: 300_000 })
// Wrap existing transaction
const tx = new Transaction ({ transaction: existingTx })
Methods
addOperation
Adds an operation to the transaction. If no transaction exists, creates one automatically by fetching current blockchain state.
await tx . addOperation ( operationName , operationBody )
The type of operation to add (e.g., ‘transfer’, ‘vote’, ‘comment’, ‘custom_json’)
The operation data/parameters for the specified operation type. Type-safe based on operation name.
Promise that resolves when the operation is added
Example
import { Transaction } from 'hive-tx'
const tx = new Transaction ()
// Add a transfer operation
await tx . addOperation ( 'transfer' , {
from: 'alice' ,
to: 'bob' ,
amount: '1.000 HIVE' ,
memo: 'Payment for services'
})
// Add a vote operation
await tx . addOperation ( 'vote' , {
voter: 'alice' ,
author: 'bob' ,
permlink: 'my-post' ,
weight: 10000
})
// Multiple operations in one transaction
await tx . addOperation ( 'transfer' , {
from: 'alice' ,
to: 'bob' ,
amount: '5.000 HIVE' ,
memo: ''
})
await tx . addOperation ( 'transfer' , {
from: 'alice' ,
to: 'carol' ,
amount: '3.000 HIVE' ,
memo: ''
})
sign
Signs the transaction with one or more private keys. Supports multi-signature transactions by accepting an array of keys or calling this method multiple times.
const signedTx = tx . sign ( keys )
keys
PrivateKey | PrivateKey[]
required
Single private key or array of private keys to sign with
The signed transaction object with signatures appended
Example
import { Transaction , PrivateKey } from 'hive-tx'
const tx = new Transaction ()
await tx . addOperation ( 'transfer' , {
from: 'alice' ,
to: 'bob' ,
amount: '1.000 HIVE' ,
memo: ''
})
// Sign with single key
const activeKey = PrivateKey . fromString ( '5JdeC9P7Pbd1uGdFVEsJ41EkEnADbbHGq6p1BwFxm6txNBsQnsw' )
const signedTx = tx . sign ( activeKey )
// Sign with multiple keys at once (multi-sig)
const key1 = PrivateKey . fromLogin ( 'alice' , 'password' , 'active' )
const key2 = PrivateKey . fromLogin ( 'bob' , 'password' , 'active' )
tx . sign ([ key1 , key2 ])
// Or sign individually for multi-sig
tx . sign ( key1 )
tx . sign ( key2 )
broadcast
Broadcasts the signed transaction to the Hive network. Automatically handles retries and duplicate transaction detection.
const result = await tx . broadcast ( checkStatus ? )
Whether to wait for transaction confirmation:
false (default): Returns immediately after broadcast. Transaction is not guaranteed to be included in a block.
true: Waits for transaction to be included in an irreversible block or expire before returning.
Broadcast result containing transaction ID and status The transaction ID (first 40 characters of transaction hash)
Transaction status:
'unknown': Broadcast successful, status not checked
'within_irreversible_block': Transaction included in irreversible block
'expired_irreversible': Transaction expired
'too_old': Transaction too old to check
Example
import { Transaction , PrivateKey } from 'hive-tx'
const tx = new Transaction ()
await tx . addOperation ( 'transfer' , {
from: 'alice' ,
to: 'bob' ,
amount: '1.000 HIVE' ,
memo: ''
})
const key = PrivateKey . fromLogin ( 'alice' , 'password' , 'active' )
tx . sign ( key )
// Broadcast without waiting
const result = await tx . broadcast ()
console . log ( `Transaction ID: ${ result . tx_id } ` )
console . log ( `Status: ${ result . status } ` ) // 'unknown'
// Broadcast and wait for confirmation
const result = await tx . broadcast ( true )
console . log ( `Transaction ID: ${ result . tx_id } ` )
console . log ( `Status: ${ result . status } ` ) // 'within_irreversible_block'
digest
Computes the transaction digest containing the transaction ID and hash. The digest is used for signature creation and verification.
const digestData = tx . digest ()
Digest data containing transaction hash and ID 32-byte transaction hash (chain ID + serialized transaction)
40-character transaction ID (first 40 hex chars of SHA256 hash)
Example
import { Transaction } from 'hive-tx'
const tx = new Transaction ()
await tx . addOperation ( 'vote' , {
voter: 'alice' ,
author: 'bob' ,
permlink: 'my-post' ,
weight: 10000
})
const { digest , txId } = tx . digest ()
console . log ( 'Transaction ID:' , txId )
console . log ( 'Digest bytes:' , digest )
addSignature
Adds a pre-computed signature to the transaction. Useful for external signing tools or hardware wallets.
const signedTx = tx . addSignature ( signature )
Hex-encoded signature string (exactly 130 characters)
The transaction with the signature added
Example
import { Transaction } from 'hive-tx'
const tx = new Transaction ()
await tx . addOperation ( 'transfer' , {
from: 'alice' ,
to: 'bob' ,
amount: '1.000 HIVE' ,
memo: ''
})
// Get digest for external signing
const { digest } = tx . digest ()
// Sign externally (e.g., hardware wallet)
const externalSignature = await hardwareWallet . sign ( digest )
// Add signature to transaction
tx . addSignature ( externalSignature )
// Broadcast
await tx . broadcast ()
checkStatus
Checks the current status of the transaction on the blockchain. Usually called internally after broadcasting.
const status = await tx . checkStatus ()
returns
Promise<TransactionStatus>
Current transaction status from the blockchain Transaction status:
'within_reversible_block': In a block, but not yet irreversible
'within_irreversible_block': Confirmed in irreversible block
'within_mempool': Waiting in mempool
'expired_reversible': Expired before becoming irreversible
'expired_irreversible': Expired and confirmed as expired
'too_old': Transaction too old to determine status
'unknown': Status cannot be determined
Example
import { Transaction , PrivateKey } from 'hive-tx'
const tx = new Transaction ()
await tx . addOperation ( 'vote' , {
voter: 'alice' ,
author: 'bob' ,
permlink: 'my-post' ,
weight: 10000
})
const key = PrivateKey . fromLogin ( 'alice' , 'password' , 'posting' )
tx . sign ( key )
// Broadcast
await tx . broadcast ()
// Check status manually
const status = await tx . checkStatus ()
console . log ( 'Transaction status:' , status )
Complete Example
import { Transaction , PrivateKey } from 'hive-tx'
async function sendTransfer () {
try {
// Create transaction with 5 minute expiration
const tx = new Transaction ({ expiration: 300_000 })
// Add transfer operation
await tx . addOperation ( 'transfer' , {
from: 'alice' ,
to: 'bob' ,
amount: '10.000 HIVE' ,
memo: 'Payment for services rendered'
})
// Sign with active key
const activeKey = PrivateKey . fromLogin ( 'alice' , 'password' , 'active' )
tx . sign ( activeKey )
// Broadcast and wait for confirmation
const result = await tx . broadcast ( true )
console . log ( 'Transfer successful!' )
console . log ( 'Transaction ID:' , result . tx_id )
console . log ( 'Status:' , result . status )
return result
} catch ( error ) {
console . error ( 'Transaction failed:' , error . message )
throw error
}
}
sendTransfer ()
Error Handling
import { Transaction , PrivateKey , RPCError } from 'hive-tx'
try {
const tx = new Transaction ()
await tx . addOperation ( 'transfer' , {
from: 'alice' ,
to: 'bob' ,
amount: '1000000.000 HIVE' , // Insufficient balance
memo: ''
})
const key = PrivateKey . fromLogin ( 'alice' , 'password' , 'active' )
tx . sign ( key )
await tx . broadcast ()
} catch ( error ) {
if ( error instanceof RPCError ) {
console . error ( 'RPC Error:' , error . message )
console . error ( 'Error code:' , error . code )
} else {
console . error ( 'Error:' , error . message )
}
}
See Also