create account

My contributions to dhive library | How to wait for transaction status by engrave

View this thread on: hive.blogpeakd.comecency.com
· @engrave · (edited)
$425.34
My contributions to dhive library | How to wait for transaction status
![](https://images.hive.blog/0x0/https://files.peakd.com/file/peakd-hive/engrave/T5pMYYyz-image.png)

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.

## Transaction Status API

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](https://developers.hive.io/apidefinitions/#apidefinitions-transaction-status-api).

 * [feat: add transaction status API](https://gitlab.syncad.com/hive/dhive/-/commit/bdb9b083dc7e9e4bd45b646226d1142fe5f8af88)

### How to wait for a transaction until it's included in a block

```javascript=
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`.

## Account By Key API

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.

 * [feat: add "account_by_key_api"](https://gitlab.syncad.com/hive/dhive/-/commit/816bf781ab89265c22c3a2289db52a28ba0ace8e)

### How to search for an account by the **Bold**public key

```javascript=
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);
    }
})();
```

# Serializer fix

I also encountered a small bug for `update_proposal` operation and fixed it.

 * [fix: update_proposal serializator](https://gitlab.syncad.com/hive/dhive/-/commit/10faa356ff741e9c078f2555391ec4500f4c64ef)

***

**Support me with your witness vote! Click on the image below:**

[![banner_engrave 100.png](https://images.hive.blog/DQmUghvic5TCPPvVkB4iB7eXmgQ3RQ4L8jkVizvuCBW8RMT/banner_engrave%20100.png)](https://hivesigner.com/sign/account-witness-vote?witness=engrave&approve=1)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 207 others
properties (23)
authorengrave
permlinkmy-contributions-to-dhive-library
categoryhive-139531
json_metadata{"app":"peakd/2022.01.2","format":"markdown","tags":["development","opensource","hive","dhive","programming","engrave","witness-update"],"users":["hiveio"],"image":["https://files.peakd.com/file/peakd-hive/engrave/T5pMYYyz-image.png","https://images.hive.blog/DQmUghvic5TCPPvVkB4iB7eXmgQ3RQ4L8jkVizvuCBW8RMT/banner_engrave%20100.png"]}
created2022-02-20 23:56:42
last_update2022-02-21 00:09:09
depth0
children6
last_payout2022-02-27 23:56:42
cashout_time1969-12-31 23:59:59
total_payout_value212.728 HBD
curator_payout_value212.609 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,658
author_reputation318,720,972,858,296
root_title"My contributions to dhive library | How to wait for transaction status"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id110,657,919
net_rshares367,277,378,084,643
author_curate_reward""
vote details (271)
@emeka4 ·
$0.26
Awesome keep up the good work, this is really amazing and a work well-done in the blockchain
👍  
properties (23)
authoremeka4
permlinkr7n5wx
categoryhive-139531
json_metadata{"app":"hiveblog/0.1"}
created2022-02-21 06:27:03
last_update2022-02-21 06:27:03
depth1
children1
last_payout2022-02-28 06:27:03
cashout_time1969-12-31 23:59:59
total_payout_value0.130 HBD
curator_payout_value0.129 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length92
author_reputation236,422,948,284,406
root_title"My contributions to dhive library | How to wait for transaction status"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id110,666,241
net_rshares227,326,904,732
author_curate_reward""
vote details (1)
@engrave ·
Thanks :)
properties (22)
authorengrave
permlinkre-emeka4-r7njsm
categoryhive-139531
json_metadata{"tags":["hive-139531"],"app":"peakd/2022.01.2"}
created2022-02-21 11:26:45
last_update2022-02-21 11:26:45
depth2
children0
last_payout2022-02-28 11:26:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length9
author_reputation318,720,972,858,296
root_title"My contributions to dhive library | How to wait for transaction status"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id110,672,539
net_rshares0
@empoderat ·
Can't wait for that Ledger integration! Keep up with that bugfixing.

Good Job! 
👎  
properties (23)
authorempoderat
permlinkre-engrave-r7ynsn
categoryhive-139531
json_metadata{"tags":["hive-139531"],"app":"peakd/2022.02.6"}
created2022-02-27 11:26:48
last_update2022-02-27 11:26:48
depth1
children0
last_payout2022-03-06 11:26:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length80
author_reputation263,017,255,426,544
root_title"My contributions to dhive library | How to wait for transaction status"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id110,851,697
net_rshares-758,923,076
author_curate_reward""
vote details (1)
@stoodkev ·
Great contribution as always! 
What's the status of the Ledger App review ?
properties (22)
authorstoodkev
permlinkre-engrave-r7nxk7
categoryhive-139531
json_metadata{"tags":["hive-139531"],"app":"peakd/2022.01.2"}
created2022-02-21 16:24:09
last_update2022-02-21 16:24:09
depth1
children2
last_payout2022-02-28 16:24:09
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length75
author_reputation190,283,772,273,558
root_title"My contributions to dhive library | How to wait for transaction status"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id110,680,238
net_rshares0
@engrave ·
It passed the functional review and I'm waiting for security review. It is possible to install it via the Ledger Live already (by enabling developer mode) and hopefully, it will be officially released soon :)
properties (22)
authorengrave
permlinkre-stoodkev-r7nxse
categoryhive-139531
json_metadata{"tags":["hive-139531"],"app":"peakd/2022.01.2"}
created2022-02-21 16:29:03
last_update2022-02-21 16:29:03
depth2
children1
last_payout2022-02-28 16:29:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length208
author_reputation318,720,972,858,296
root_title"My contributions to dhive library | How to wait for transaction status"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id110,680,391
net_rshares0
@stoodkev ·
$0.26
Awesome, that will be a game changer!
👍  
properties (23)
authorstoodkev
permlinkre-engrave-r7ny39
categoryhive-139531
json_metadata{"tags":["hive-139531"],"app":"peakd/2022.01.2"}
created2022-02-21 16:35:36
last_update2022-02-21 16:35:36
depth3
children0
last_payout2022-02-28 16:35:36
cashout_time1969-12-31 23:59:59
total_payout_value0.132 HBD
curator_payout_value0.132 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length37
author_reputation190,283,772,273,558
root_title"My contributions to dhive library | How to wait for transaction status"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id110,680,586
net_rshares228,858,707,411
author_curate_reward""
vote details (1)