Start
Examples
Patterns to copy. Replace the keys and account names with your own.
Transfer HIVE
import { Transaction, PrivateKey } from 'hive-tx'
const tx = new Transaction()
await tx.addOperation('transfer', {
from: 'myaccount',
to: 'recipient',
amount: '1.000 HIVE',
memo: 'Thank you!'
})
tx.sign(PrivateKey.from('5J...active-key'))
const { tx_id, status } = await tx.broadcast()
Post or comment
import { Transaction, PrivateKey, utils } from 'hive-tx'
const tx = new Transaction()
await tx.addOperation('comment', {
parent_author: '',
parent_permlink: 'hive',
author: 'myaccount',
permlink: utils.createPermlink('Hello'),
title: 'Hello',
body: 'First post',
json_metadata: JSON.stringify({ tags: ['hive'], app: 'hive-tx' })
})
tx.sign(PrivateKey.from('5J...posting-key'))
await tx.broadcast()
A root post has an empty parent_author, and parent_permlink is its first tag. A reply sets both to the parent comment.
custom_json
await tx.addOperation('custom_json', {
required_auths: [],
required_posting_auths: ['myaccount'],
id: 'follow',
json: JSON.stringify(['follow', {
follower: 'myaccount',
following: 'alice',
what: ['blog']
}])
})
For follows, mutes, reblogs, and communities, the social helpers in utilities build this for you.
Sign and verify
import { Transaction, PrivateKey, Signature } from 'hive-tx'
const tx = new Transaction()
await tx.addOperation('vote', {
voter: 'myaccount',
author: 'some-author',
permlink: 'some-post-permlink',
weight: 10000
})
const privateKey = PrivateKey.from('5J...posting-key')
const { signatures } = tx.sign(privateKey)
const { digest } = tx.digest()
const ok1 = privateKey.createPublic().verify(digest, signatures[0])
const ok2 =
Signature.from(signatures[0]).getPublicKey(digest).toString() ===
privateKey.createPublic().toString()
Multi-signature
tx.sign([ownerKey, activeKey])
// or, one at a time; signatures accumulate
tx.sign(ownerKey)
tx.sign(activeKey)
Wait for an irreversible block
const result = await tx.broadcast(true)
console.log(result.status)
// 'within_irreversible_block' | 'expired_irreversible' | 'too_old' | 'unknown'
Handle a rejected broadcast
import { RPCError } from 'hive-tx'
try {
await tx.broadcast()
} catch (e) {
if (e instanceof RPCError) {
console.log('Rejected by the chain:', e.message) // e.g. missing required posting authority
} else {
throw e // every node failed
}
}
JSON-RPC
import { callRPC } from 'hive-tx'
const accounts = await callRPC('condenser_api.get_accounts', [['hiveio']])
const props = await callRPC('condenser_api.get_dynamic_global_properties')
console.log(accounts[0].name, props.head_block_number)
Method names are in the Hive API definitions.
Quorum
import { callWithQuorum } from 'hive-tx'
const accounts = await callWithQuorum('condenser_api.get_accounts', [['hiveio']], 2)
Two nodes must return identical JSON. This guards against a wrong or malicious node; it does not check how far behind a node is.
Account history filter
import { callRPC, utils } from 'hive-tx'
const filter = utils.makeBitMaskFilter([
utils.operations.transfer,
utils.operations.vote,
utils.operations.comment
])
const history = await callRPC('condenser_api.get_account_history', [
'myaccount',
-1,
100,
...filter
])
REST
import { callREST } from 'hive-tx'
const balance = await callREST('balance', '/accounts/{account-name}/balances', {
'account-name': 'alice'
})
To browse the endpoints, open a REST node such as rpc.mahdiyari.info.
Encrypt a memo
import { Memo, PrivateKey } from 'hive-tx'
const encrypted = await Memo.encodeForAccount(
PrivateKey.from('5J...sender-memo-key'),
'receiver',
'#Hello World!'
)
await tx.addOperation('transfer', {
from: 'sender',
to: 'receiver',
amount: '0.001 HIVE',
memo: encrypted
})
Only text that starts with # is encrypted.
Decrypt a memo
const text = Memo.decode(PrivateKey.from('5J...recipient-memo-key'), encryptedMemo)
Generate keys
import { PrivateKey } from 'hive-tx'
const privateKey = PrivateKey.randomKey()
console.log(privateKey.inspect()) // masked WIF, safe to log
console.log(privateKey.createPublic().toString())
Keys from a master password
const owner = PrivateKey.fromLogin('username', 'master-password', 'owner')
const active = PrivateKey.fromLogin('username', 'master-password', 'active')
const posting = PrivateKey.fromLogin('username', 'master-password', 'posting')
const memo = PrivateKey.fromLogin('username', 'master-password', 'memo')
Validate a username
import { utils } from 'hive-tx'
for (const name of ['alice', 'a', 'BadName']) {
const error = utils.validateUsername(name)
console.log(name, error ?? 'valid')
}
Witness set properties
import { utils, Transaction, PrivateKey } from 'hive-tx'
const operation = utils.buildWitnessSetProperties('mywitness', {
key: 'STM...current-signing-key',
account_creation_fee: '3.000 HIVE',
account_subsidy_budget: 10000,
account_subsidy_decay: 330782,
maximum_block_size: 65536,
hbd_interest_rate: 1500,
hbd_exchange_rate: { base: '0.250 HBD', quote: '1.000 HIVE' },
url: 'https://example.com'
})
const tx = new Transaction()
await tx.addOperation(...operation)
tx.sign(PrivateKey.from('5J...signing-key'))
await tx.broadcast()
key is required and must be the witness's current signing public key; the chain checks it. Sign with the matching private signing key.
Browser
<script type="module">
import { PrivateKey } from
'https://cdn.jsdelivr.net/npm/hive-tx@7/dist/index.mjs'
const pub = PrivateKey.fromLogin('alice', 'x', 'posting').createPublic()
console.log(pub.toString())
</script>