#
Finalization
When the work with the transaction is ready, you now need to decide what you want to do with it next.
#
Conversion to api form
The simple toApi
method returns the transaction in the Hive API-JSON form:
import { createHiveChain } from '@hiveio/wax';
// Initialize hive chain interface
const chain = await createHiveChain();
// Initialize a transaction object
const tx = await chain.createTransaction();
// Declare example operation
const operation = {
vote: {
voter: "voter",
author: "test-author",
permlink: "test-permlink",
weight: 2200
}
};
// Push operation into the transction
tx.pushOperation(operation);
// Log to the console the transaction which is **not signed yet** in the api form
console.log(tx.toApi());
// Delete the created wax proto_protocol instance
chain.delete();
{
"ref_block_num": 1960,
"ref_block_prefix": 3915120327,
"expiration": "2023-11-09T21:51:27",
"operations": [
{
"type": "vote_operation",
"value": {
"voter": "voter",
"author": "test-author",
"permlink": "test-permlink",
"weight": 2200
}
}
]
}
You can also represent your transaction in the API form, with your signature added to the internal signatures array (it will also apply the transaction expiration time):
import { createHiveChain } from '@hiveio/wax';
// Initialize hive chain interface
const chain = await createHiveChain();
// Initialize a transaction object
const tx = await chain.createTransaction();
// Declare example operation
const operation = {
vote: {
voter: "voter",
author: "author",
permlink: "test-permlink",
weight: 2200
}
};
// Push operation into the transction
tx.pushOperation(operation);
// Supplement a transaction with an externally generated signature.
tx.sign('signature...');
console.log(tx.toApi());
// Delete the created wax proto_protocol instance
chain.delete();
{
"ref_block_num": 1960,
"ref_block_prefix": 3915120327,
"expiration": "2023-11-09T21:51:27",
"operations": [
{
"type": "vote_operation",
"value": {
"voter": "voter",
"author": "author",
"permlink": "test-permlink",
"weight": 2200
}
}
],
"signatures": [
"signature..."
]
}
Multiple signatures
Also remember that you can add more than one signature while signing your transaction.
If you want to sign your transaction in traditional way and return it in the API form, you can use this sample (it will also apply the transaction expiration time):
import { createHiveChain } from '@hiveio/wax';
// Initialize hive chain interface
const chain = await createHiveChain();
const { wallet, publicKey1 } = globalThis.snippetsBeekeeperData;
// Initialize a transaction object
const tx = await chain.createTransaction();
// Declare example operation
const operation = {
vote: {
voter: "voter",
author: "author",
permlink: "test-permlink",
weight: 2200
}
};
// Push operation into the transction
tx.pushOperation(operation);
// Build transaction with signature provided.
tx.sign(wallet, publicKey1);
console.log(tx.toApi());
// Delete the created wax proto_protocol instance
chain.delete();
{
"ref_block_num": 1960,
"ref_block_prefix": 3915120327,
"expiration": "2023-11-09T21:51:27",
"operations": [
{
"type": "vote_operation",
"value": {
"voter": "voter",
"author": "author",
"permlink": "test-permlink",
"weight": 2200
}
}
],
"signatures": [
"1fe8647a82f131671997ce26250bf5a1cb7a18609cbc69b3b2fd7fcaefc848c7fc308abfb0992c1ce9a805715f102416d85c4313a8a00527fa1500ac93898b418a"
]
}
You can also sign the transaction without converting it to the API form (which will return the signatures):
import { createHiveChain } from '@hiveio/wax';
// Initialize hive chain interface
const chain = await createHiveChain();
const { wallet, publicKey1 } = globalThis.snippetsBeekeeperData;
// Initialize a transaction object
const tx = await chain.createTransaction();
// Declare example operation
const operation = {
vote: {
voter: "voter",
author: "author",
permlink: "test-permlink",
weight: 2200
}
};
// Push operation into the transction
tx.pushOperation(operation);
// Sign transaction.
const signature = tx.sign(wallet, publicKey1);
console.log(signature);
// Delete the created wax proto_protocol instance
chain.delete();
1fe8647a82f131671997ce26250bf5a1cb7a18609cbc69b3b2fd7fcaefc848c7fc308abfb0992c1ce9a805715f102416d85c4313a8a00527fa1500ac93898b418a
Beekeeper Information
If you want to sign the transaction, you have to initialize beekeeper library. If you want to sign the transaction after creating it, using our Transaction
interface, you have to initialize our beekeeper library. Remember to import keys into Beekeeper. It ensures that you can use them securely for transactions and other operations without exposing the raw keys.
Imported key and the one you want to use for signing must be the same!
#
Convertions
At the end you can also just convert your transaction into the Hive API-form JSON:
import { createHiveChain } from '@hiveio/wax';
// Initialize hive chain interface
const chain = await createHiveChain();
// Initialize a transaction object
const tx = await chain.createTransaction();
// Declare example operation
const operation = {
vote: {
voter: "voter",
author: "test-author",
permlink: "test-permlink",
weight: 2200
}
};
// Push operation into the transction
tx.pushOperation(operation);
// Convert transaction into the Hive API-form JSON.
const apiTx = tx.toApi();
console.log(apiTx);
// Delete the created wax proto_protocol instance
chain.delete();
{
"ref_block_num": 1960,
"ref_block_prefix": 3915120327,
"expiration": "2023-11-09T21:51:27",
"operations": [
{
"type": "vote_operation",
"value": {
"voter": "voter",
"author": "test-author",
"permlink": "test-permlink",
"weight": 2200
}
}
]
}
Or you can just convert transction to legacy API form:
import { createHiveChain } from '@hiveio/wax';
// Initialize hive chain interface
const chain = await createHiveChain();
// Initialize a transaction object
const tx = await chain.createTransaction();
// Declare example operation
const operation = {
vote: {
voter: "voter",
author: "test-author",
permlink: "test-permlink",
weight: 2200
}
};
// Push operation into the transction
tx.pushOperation(operation);
// Convert transaction into the Hive API-legacy form JSON string
const legacyApiTx = tx.toLegacyApi();
console.log(legacyApiTx);
// Delete the created wax proto_protocol instance
chain.delete();
{
"ref_block_num": 1960,
"ref_block_prefix": 3915120327,
"expiration": "2023-11-09T21:51:27",
"operations": [
[
"vote",
{
"voter": "voter",
"author": "test-author",
"permlink": "test-permlink",
"weight": 2200
}
]
],
"extensions": [],
"signatures": []
}