Start
Quick start
Cast a vote, send a transfer, call an API, and derive a key.
Install
npm install hive-tx
Cast a vote
- Create a
Transaction. - Add a
voteoperation. - Sign it with the voter's posting key.
- Broadcast it.
import { Transaction, PrivateKey } from 'hive-tx'
const tx = new Transaction()
await tx.addOperation('vote', {
voter: 'guest123',
author: 'guest123',
permlink: '20191107t125713486z-post',
weight: 9500
})
const privateKey = PrivateKey.from(
'5JRaypasxMx1L97ZUX7YuC5Psb5EAbF821kkAGtBj7xCJFQcbLg'
)
tx.sign(privateKey)
const result = await tx.broadcast()
console.log(result.tx_id)
Note
This is the posting key of
guest123, a public demo account, so the example runs as-is and casts a real 95% vote. Use your own account and key in your app.
weight runs from -10000 to 10000: 10000 is a full upvote, and a negative weight is a downvote.
Which key to use
Your private keys are on your Hive wallet's keys page. Use the lowest role an operation needs: posting for votes, comments, and follows; active for transfers and other fund operations. Never put an owner key in app code. If one transaction mixes posting and active operations, sign it with both keys.
Send a transfer
const tx = new Transaction()
await tx.addOperation('transfer', {
from: 'sender',
to: 'receiver',
amount: '1.000 HIVE',
memo: 'Thanks'
})
tx.sign(PrivateKey.from('5J...active-key'))
const result = await tx.broadcast()
console.log(result.tx_id, result.status)
Call an API
import { callRPC } from 'hive-tx'
const accounts = await callRPC('condenser_api.get_accounts', [['mahdiyari']])
console.log(accounts[0].name, accounts[0].balance)
callRPC returns the result directly and throws RPCError when the node returns an error.
Derive keys
import { PrivateKey } from 'hive-tx'
const fromWif = PrivateKey.from('5JRaypasxMx1L97ZUX7YuC5Psb5EAbF821kkAGtBj7xCJFQcbLg')
const random = PrivateKey.randomKey()
const fromPassword = PrivateKey.fromLogin('username', 'password', 'posting')
console.log(fromWif.createPublic().toString())
More patterns are on the examples page.