Usage
Configure and use Aioha in 5 minutes or less.
Instantiation
Create an instance of Aioha. All providers are registered by default excluding HiveSigner, which is only registered when HiveSigner config is specified. This step also loads any persistent logins stored in localStorage
(if available). Returns an Aioha
object.
Provider-specific configs are optional with exception of HiveSigner which is required to register HiveSigner provider.
import { initAioha, Asset, KeyTypes, Providers } from '@aioha/aioha'
const aioha = initAioha({
hiveauth: {
name: 'Aioha',
description: 'Aioha test app'
},
hivesigner: {
app: 'ipfsuploader.app',
callbackURL: window.location.origin + '/hivesigner.html',
scope: ['login', 'vote']
}
})
Configuration
- HiveAuth
- HiveSigner
name
: Short name of the app. Default:Aioha Generic App
description
(optional): App descriptionicon
(optional): URL to app icon
Refer to the official documentation for complete details.
app
: HiveSigner app account username. Setup the app account by following the instructions here.callbackURL
: Callback URL. An example HTML page may be found here.scope
: Authorized operations HiveSigner may perform on behalf of the user using the access token. A HiveSigner popup window will be shown when performing any operation outside the specified scope.
Enums
- Providers
- KeyTypes
- Asset
The Providers
enum exported are as follows:
export enum Providers {
Keychain = 'keychain',
HiveSigner = 'hivesigner',
HiveAuth = 'hiveauth',
Ledger = 'ledger',
PeakVault = 'peakvault'
}
The KeyTypes
enum exported are as follows:
export enum KeyTypes {
Posting = 'posting',
Active = 'active',
Memo = 'memo'
}
The Asset
enum exported are as follows:
export enum Asset {
HIVE = 'HIVE',
HBD = 'HBD'
}
Login
Also known as Connect Wallet.
Login with specified provider and username. A signature request will be made to sign message msg
using keyType
key that is associated with the Hive account.
- Example
- Result
const login = await aioha.login(Providers.Keychain, 'hiveusername', {
msg: 'Hello World',
keyType: KeyTypes.Posting,
loginTitle: 'Login',
hiveauth: {
cbWait: (payload, evt, cancel) => {
// display HiveAuth QR code using `payload` as data
}
}
})
msg
: Message to be signedkeyType
: Key type to be used to sign the messageloginTitle
(optional): Title to be displayed on certain providershiveauth.cbWait
: Callback function returning auth payload. It shall display a QR code usingpayload
as data to be scanned by a HiveAuth PKSA. Login request may be cancelled before timeout by callingcancel()
.
{
"provider": "keychain",
"success": true,
"result": "2020647841097bca7d9e84c39df209ac2df9b42c5b194ee2a690304ce8c140690f5296566b5da0ae9d9950243871bf17fa26b34b671dc8284bf7fba767958a5508",
"publicKey": "STM5jruaymFQ93jwUp15r9SF5wPeaNvRKFE4nbbJv4vfHCk5a6zRu",
"username": "randomvlogs"
}
provider
: Name of provider selected by the user. See Providers Enum above for possibilities.success
: Boolean of whether login is successful or noterror
: Error message, if anyresult
: ECDSA signature (or HiveSigner access token for HiveSigner provider)publicKey
: Public key used to sign the message (undefined for HiveSigner provider)username
: Logged in username
Login and Decrypt Memo
Login with signatures above are preferred than with memo decryption due to limited support of memo cryptography in certain providers.
Login with specified provider and username by decrypting a memo.
- Example
- Result
const login = await aioha.loginAndDecryptMemo(Providers.Keychain, 'hiveusername', {
msg: 'Hello World',
keyType: KeyTypes.Posting
})
msg
: Memo to be decryptedkeyType
: Key type to be used to decrypt the message
{
"provider": "keychain",
"success": true,
"result": "#Login",
"username": "randomvlogs"
}
provider
: Name of provider selected by the user. See Providers Enum above for possibilities.success
: Boolean of whether the memo decryption is successful or noterror
: Error message, if anyresult
: Decrypted messageusername
: Logged in username
Login Non-Interactively
May be used when authentication info (i.e. access token) is already available. This is especially useful for one-click login.
const loginResult = aioha.loginNonInteractive(Providers.HiveSigner, url.searchParams.get('username')!, {
ignorePersistence: false, // optional, set to true to overwrite persistent logins already stored
hivesigner: {
accessToken: 'hivesigner_access_token_here',
expiry: 123456 // timestamp in seconds
}
})
Logout
Also known as Disconnect Wallet.
Logout the current authenticated user.
await aioha.logout()
Decrypt Memo
Decrypt a Hive memo or message.
- Example
- Result
const decrypted = await aioha.decryptMemo('#encryptedmessage', KeyTypes.Posting)
Required arguments include the encrypted message starting with #
and the KeyTypes
enum for the key type to be used.
{
"success": true,
"result": "#Hello World"
}
success
: Boolean of whether memo decryption is successful or noterror
: Error message, if anyresult
: Decrypted message
Sign Message
Retrieve an ECDSA signature for a message.
- Example
- Result
const signed = await aioha.signMessage('Message to sign', KeyTypes.Posting)
Required arguments include the message to sign and the KeyTypes
enum for the key type to be used.
{
"success": true,
"result": "205a8bf065ada352d309c546012b17b4e88f19fdeb704d9a26abe46f1c2f9bce3f4a4b05ffc4777e5005fc5c4b37cd7412a934b7170654df7761310e45cbf6483c",
"publicKey": "STM5jruaymFQ93jwUp15r9SF5wPeaNvRKFE4nbbJv4vfHCk5a6zRu"
}
success
: Boolean of whether message has been signed successfully or noterror
: Error message, if anyresult
: ECDSA signaturepublicKey
: Public key used to sign the message
Sign Transaction
Sign a Hive transaction without broadcasting it.
- Example
- Result
const signed = await aioha.signTx({
"ref_block_num": 27912,
"ref_block_prefix": 1175138206,
"expiration": "2024-06-07T10:23:57",
"operations": [
[
"transfer",
{
"from": "randomvlogs",
"to": "randomvlogs",
"amount": "0.001 HIVE",
"memo": "Aioha Test"
}
]
],
"extensions": []
}, KeyTypes.Active)
Required arguments include the message to sign and the KeyTypes
enum for the key type to be used (must not be KeyTypes.Memo
).
{
"success": true,
"result": {
"ref_block_num": 27912,
"ref_block_prefix": 1175138206,
"expiration": "2024-06-07T10:23:57",
"operations": [
[
"transfer",
{
"from": "randomvlogs",
"to": "randomvlogs",
"amount": "0.001 HIVE",
"memo": "Aioha Test"
}
]
],
"extensions": [],
"signatures": [
"20469faba4c128ef41c4e444ef5422e88ed9397d43a7c9713a8e59c170de45eea539b63ced8f4ff048442bdcca3b96848f6f740e655c971f9cae00b54535c8eea0"
]
}
}
success
: Boolean of whether transaction has been signed successfully or noterror
: Error message, if anyresult
: Full signed transaction
Sign and Broadcast Transaction
Sign and broadcast a Hive transaction.
- Example
- Result
const result = aioha.signAndBroadcastTx([
['vote', {
author: 'sagarkothari88',
permlink: '2024-06-05-daily-updates-from-sagarkothari88',
voter: 'techcoderx',
weight: 10000
}]
], KeyTypes.Posting)
Required arguments include an array of operations for the transaction and the KeyTypes
enum for the key type to be used (must not be KeyTypes.Memo
).
{
"success": true,
"result": "87c10767531071732fe15e6c34cf275900c342a3"
}
success
: Boolean of whether transaction has been signed and broadcasted successfully or noterror
: Error message, if anyresult
: Transaction ID
Getters
Registered Providers
List all registered providers.
aioha.getProviders() // ['keychain', 'peakvault', 'ledger', 'hiveauth', 'hivesigner']
Current Provider
Returns one of the providers above that the user logs in with.
aioha.getCurrentProvider()
Current User
Returns logged in Hive username.
aioha.getCurrentUser()
Is Logged In
Returns a boolean of whether a user is logged in or not.
aioha.isLoggedIn()
Is Provider Registered
Returns a boolean of whether the specified provider is registered. This does not indicate that the said provider is available to the user (i.e. due to required browser extension not installed).
aioha.isProviderRegistered(Providers.HiveAuth)
Is Provider Enabled
Returns a boolean of whether the specified provider is registered and available for use by the user.
aioha.isProviderEnabled(Providers.HiveAuth)
Setters
Set Hive API
Set a Hive API node for RPC calls made by Aioha (i.e. HIVE/VEST calculation, broadcast transaction)
aioha.setApi('https://techcoderx.com', [
'https://api.hive.blog',
'https://api.openhive.network',
// ...more fallback nodes
])
Set VSC Network ID
Set VSC network ID for VSC related functions.
aioha.vscSetNetId('testnet/0bf2e474-6b9e-4165-ad4e-a0d78968d20c')
Operations
The return value for these method calls will be equivalent to sign and broadcast transaction result above.
Social
- Vote
- Comment
- Delete Comment
- Reblog
- Follow/Ignore
const vote = await aioha.vote('author', 'permlink', 10000)
const voteMany = await aioha.voteMany([
{
author: 'author1',
permlink: 'permlink1',
weight: 10000
},
{
author: 'author2',
permlink: 'permlink2',
weight: 5000
}
])
const comment = await aioha.comment('pa', 'pp', 'permlink', 'title', 'body', { foo: 'bar' })
const commentWithOptions = await aioha.comment('pa', 'pp', 'permlink', 'title', 'body', { foo: 'bar' }, {
author: aioha.getCurrentUser(),
permlink: 'permlink',
max_accepted_payout: '1000000.000 HBD',
percent_hbd: 10000,
allow_votes: true,
allow_curation_rewards: true,
extensions: [[0, {
beneficiaries: [
{
account: 'alice',
weight: 100
}, {
account: 'bob',
weight: 150
}
]
}]]
})
The post or comment must contain no child comments or positive rshares.
const deleted = await aioha.deleteComment('permlinktodel')
const reblog = await aioha.reblog('author', 'permlink', true)
const removeReblog = await aioha.reblog('author', 'permlink', false)
const follow = await aioha.follow('author', true)
const unfollow = await aioha.follow('author', false)
const ignored = await aioha.ignore('authortoignore')
Custom JSON
Publish an arbitrary JSON data.
const customJson = await aioha.customJSON(KeyTypes.Posting, 'my-id', { foo: 'bar' }, 'Display Title')
Claim Rewards
Claim Hive rewards that have paid out from content reward pool.
const rewardClaim = await aioha.claimRewards()
Transfer
Transfer HIVE/HBD to another account.
- One Time
- Recurrent
// transfer 1 HIVE with memo
const xfer = await aioha.transfer('recipient', 1, Asset.HIVE, 'Transferred using Aioha with memo')
// transfer 1 HBD without memo
const xferNoMemo = await aioha.transfer('recipient2', 1, Asset.HBD)
// start recurrent transfer of 1 HIVE every 24 hours for 7 days to 'bob'
const recurrentXfer = await aioha.recurrentTransfer('bob', 1, Asset.HIVE, 24, 7, 'Transferred using Aioha with memo')
// cancel HIVE recurrent transfer to 'bob'
const cancelRecurrentXfer = await aioha.cancelRecurrentTransfer('bob', Asset.HIVE, 'Another memo for cancel recurrent transfer')
HIVE staking
- Stake
- Unstake
- Delegate
// stake 1000 HIVE to itself
const stake = await aioha.stakeHive(1000)
// stake 1000 HIVE to another account
const stakeToOtherAccount = await aioha.stakeHive(1000, 'anotheraccount')
// unstake 1000 HIVE of vesting shares
const unstake = await aioha.unstakeHive(1000)
// unstake 1 million vesting shares
const unstakeByVests = await aioha.unstakeHiveByVests(1000000)
// cancel unstake
const cancelUnstake = await aioha.unstakeHive(0)
// delegate 1000 HIVE of vesting shares
const delegate = await aioha.delegateStakedHive('delegatee', 1000)
// delegate 1 million vesting shares
const delegateByVests = await aioha.delegateVests('delegatee', 1000000)
// undelegate from delegatee
const undelegate = await aioha.delegateStakedHive('delegatee', 0)
Governance
- Vote Witness
- Vote Proposals
- Set/Clear Proxy
const voteWitness = await aioha.voteWitness('thewitness', true)
const unvoteWitness = await aioha.voteWitness('thewitness', false)
const voteProposals = await aioha.voteProposals([0], true)
const unvoteProposals = await aioha.voteProposals([0], false)
const setProxy = await aioha.setProxy('proxiedacc')
const clearProxy = await aioha.clearProxy()
Authority
- Account Auths
- Key Auths
// Add 'aioha' account to posting auths with weight 1
const addAccount = await aioha.addAccountAuthority('aioha', KeyTypes.Posting, 1)
// Remove 'aioha' account from posting auths
const rmAccount = await aioha.removeAccountAuthority('aioha', KeyTypes.Posting)
// Add 'STMxxxx' public key to posting auths with weight 1
const addKey = await aioha.addKeyAuthority('STMxxxx', KeyTypes.Posting, 1)
// Remove 'STMxxxx' public key from posting auths
const rmKey = await aioha.removeKeyAuthority('STMxxxx', KeyTypes.Posting)
Call VSC Contract
Call a VSC contract from L1.
const contractCall = await aioha.vscCallContract('vs41q9c3yg82k4q76nprqk89xzk2n8zhvedrz5prnrrqpy6v9css3seg2q43uvjdc500', 'dumpEnv', 'tx payload', KeyTypes.Active)
JSON-RPC Request
Make an EIP-1193 style JSON-RPC request. For more details, refer to JSON-RPC specs page.
const result = await aioha.request({
method: 'aioha_api.your_method_name',
params: {
foo: 'bar'
}
})