
While working on Hive Ledger wallet I needed additional functionalities that were not available in @hiveio/dhive javascript library and decided that it's a good moment to improve it a bit.
A few months ago, when the Splinterlands game got traction and started onboarding masses, witnesses realized that the current implementation of the broadcast method is blocking and may cause network lags. The library was changed to use asynchronous call which helped a lot but projects that rely on a transaction status had to add additional code to handle it. I decided to add a handy method to support Transaction Status API.
const dhive = require("@hiveio/dhive");
const client = new dhive.Client("https://api.hive.blog");
const key = dhive.PrivateKey.fromString('PRIVATE KEY HERE');
const op = ["transfer", {
amount: "0.001 HIVE",
from: "engrave",
to: "engrave.cold",
memo: ""
}];
(async () => {
try {
const {id} = await client.broadcast.sendOperations([op], key);
console.log(`Transaction ID: ${id}`);
let tx = null;
do {
tx = await client.transaction.findTransaction(id);
console.log(`Transaction status: ${tx.status}`);
await wait(500);
} while (tx.status == 'within_mempool')
if (tx.status == 'within_reversible_block') {
console.log('Transaction confirmed');
} else {
throw new Error(`Transaction failed with status: ${tx.status}`);
}
} catch (err) {
console.error(err);
}
})();
const wait = async (ms) => {
return new Promise(resolve => setTimeout(resolve, ms));
};
The main point it so pool the API until we get anything but within_mempool and throw an error when received anything else than within_reversible_block.
This is not a very popular API and I might be the only one using it, but it is very useful for a wallet supporting hardware wallets. The main reason is that you can only get the public key from the device, and you cannot store the information about the account, so every time you connect the hardware wallet, you need to search for associated accounts.
const dhive = require("@hiveio/dhive");
const client = new dhive.Client("https://api.hive.blog");
(async () => {
try {
const result = await client.keys.getKeyReferences(['TST65PUAPA4yC4RgPtGgsPupxT6yJtMhmT5JHFdsT3uoCbR8WJ25s'])
console.log(result) // Will print [["hiveio"]]
} catch (err) {
console.error(err);
}
})();
I also encountered a small bug for update_proposal operation and fixed it.
Support me with your witness vote! Click on the image below: