Use
Transactions
A Transaction holds operations, signatures, and an expiration.
Create
const tx = new Transaction()
const txLong = new Transaction({ expiration: 120_000 })
Expiration is in milliseconds. The default is 60000 (1 minute). The chain rejects anything over 86400000 (24 hours); the library does not check it for you.
To wrap an existing transaction object, for example one signed elsewhere:
const tx = new Transaction({ transaction: rawTx })
Add operations
addOperation is async: the first call fetches chain properties to set ref_block_num and ref_block_prefix. Those properties are cached for 2 seconds, so building several transactions in a row costs one request. A transaction can hold many operations.
await tx.addOperation('transfer', {
from: 'sender',
to: 'receiver',
amount: '1.000 HIVE',
memo: 'payment'
})
Sign
sign takes one PrivateKey or an array, appends the signatures, and returns the transaction. Call addOperation first; signing an empty transaction throws.
tx.sign(privateKey)
tx.sign([postingKey, activeKey])
Broadcast
const quick = await tx.broadcast()
const confirmed = await tx.broadcast(true)
| Argument | Returns when |
|---|---|
false (default) | A node accepts the transaction. status is unknown. |
true | The transaction is in an irreversible block, or it expired. |
Broadcasting an unsigned transaction throws. If a retry hits a duplicate-transaction error, the library ignores it, because the first attempt went through.
Errors
If the chain rejects the transaction, for example for a missing authority, too little RC, or an invalid operation, broadcast throws RPCError with the node's message. Network failures move on to the next node; when every retry fails, the last network error is thrown.
import { RPCError } from 'hive-tx'
try {
await tx.broadcast()
} catch (e) {
if (e instanceof RPCError) console.log(e.message)
else throw e
}
Check status
checkStatus asks a node what happened to the transaction. broadcast(true) polls it for you.
const { status } = await tx.checkStatus()
| Status | Meaning |
|---|---|
unknown | The node has no record of it. From broadcast(), it means the call did not wait. |
within_mempool | Received, not in a block yet. |
within_reversible_block | In a block that is not irreversible yet. |
within_irreversible_block | In an irreversible block. Final. |
expired_reversible | Expired without being included; not final yet. |
expired_irreversible | Expired without being included. Final. |
too_old | Older than the node tracks. |
broadcast(true) only returns within_irreversible_block, expired_irreversible, too_old, or unknown if the wait reaches the expiration.
Digest and transaction id
const { digest, txId } = tx.digest()
digest is the 32-byte hash that gets signed; use it to verify signatures. txId is the first 40 hex characters of the SHA-256 of the serialized transaction.
Add an external signature
When a wallet or another tool signs the digest, add its signature as a 130-character hex string:
tx.addSignature(hexSignature)