@hiveio/dhive
    Preparing search index...

    @hiveio/dhive

    dhive

    Robust hive client library that runs in both node.js and the browser.

    Needs test net urls, chain id


    note As of version 0.7.0 WebSocket support has been removed. The only transport provided now is HTTP(2). For most users the only change required is to swap wss:// to https:// in the address. If you run your own full node make sure to set the proper CORS headers if you plan to access it from a browser.


    Build Status

    For node.js or the browser with browserify or webpack.

    npm install @hiveio/dhive
    

    Grab dist/dhive.js from git and include in your html:

    <script src="@hiveio/dhive.js"></script>
    

    Or from the unpkg cdn:

    <script src="https://unpkg.com/@hiveio/dhive@latest/dist/dhive.js"></script>
    

    Make sure to set the version you want when including from the cdn, you can also use dhive@latest but that is not always desirable. See unpkg.com for more information.

    <script src="https://unpkg.com/@hiveio/dhive@latest/dist/dhive.js"></script>
    <script>
    var client = new dhive.Client(["https://api.hive.blog", "https://api.hivekings.com", "https://anyx.io", "https://api.openhive.network"]);
    client.database
    .getDiscussions("trending", { tag: "writing", limit: 1 })
    .then(function(discussions) {
    document.body.innerHTML += "<h1>" + discussions[0].title + "</h1>";
    document.body.innerHTML += "<h2>by " + discussions[0].author + "</h2>";
    document.body.innerHTML +=
    '<pre style="white-space: pre-wrap">' + discussions[0].body + "</pre>";
    });
    </script>

    See the demo source for an example on how to setup a livereloading TypeScript pipeline with wintersmith and browserify.

    With TypeScript:

    import { Client } from "@hiveio/dhive";

    const client = new Client(["https://api.hive.blog", "https://api.hivekings.com", "https://anyx.io", "https://api.openhive.network"]);

    for await (const block of client.blockchain.getBlocks()) {
    console.log(`New block, id: ${block.block_id}`);
    }

    With JavaScript:

    var dhive = require("@hiveio/dhive");

    var client = new dhive.Client(["https://api.hive.blog", "https://api.hivekings.com", "https://anyx.io", "https://api.openhive.network"]);
    var key = dhive.PrivateKey.fromLogin("username", "password", "posting");

    client.broadcast
    .vote(
    {
    voter: "username",
    author: "almost-digital",
    permlink: "dhive-is-the-best",
    weight: 10000
    },
    key
    )
    .then(
    function(result) {
    console.log("Included in block: " + result.block_num);
    },
    function(error) {
    console.error(error);
    }
    );

    With ES2016 (node.js 7+):

    const { Client } = require("@hiveio/dhive");

    const client = new Client(["https://api.hive.blog", "https://api.hivekings.com", "https://anyx.io", "https://api.openhive.network"]);

    async function main() {
    const props = await client.database.getChainProperties();
    console.log(`Maximum blocksize consensus: ${props.maximum_block_size} bytes`);
    client.disconnect();
    }

    main().catch(console.error);

    With node.js streams:

    var dhive = require("@hiveio/dhive");
    var es = require("event-stream"); // npm install event-stream
    var util = require("util");

    var client = new dhive.Client(["https://api.hive.blog", "https://api.hivekings.com", "https://anyx.io", "https://api.openhive.network"]);

    var stream = client.blockchain.getBlockStream();

    stream
    .pipe(
    es.map(function(block, callback) {
    callback(null, util.inspect(block, { colors: true, depth: null }) + "\n");
    })
    )
    .pipe(process.stdout);

    Use client.block in new code. It calls block_api, which HAF API nodes serve from HAfAH rather than hived, and plain hived nodes serve as well. Blocks come in the block_api format: operations are {type: "vote_operation", value: {...}} objects, assets are NAI objects such as {nai: "@@000000013", amount: "1000", precision: 3}, and transactions carry no transaction_id, block_num or transaction_num (the ids are in the block's transaction_ids). A missing block is null.

    const block = await client.block.getBlock(90000000); // BlockApiSignedBlock | null
    const header = await client.block.getBlockHeader(90000000);
    const blocks = await client.block.getBlockRange(90000000, 10); // up to 1000 per call

    client.database.getBlock and getBlockHeader are deprecated but unchanged: they call condenser_api and return the legacy format (["vote", {...}] operations, "1.000 HBD" asset strings). Keep them only for code that depends on that format. client.blockchain.getBlocks() and getBlockStream() still use the condenser path.

    The easiest way to bundle dhive (with browserify, webpack etc.) is to just npm install @hiveio/dhive and require('@hiveio/dhive') which will give you well-tested (see browser compatibility matrix above) pre-bundled code guaranteed to JustWork™. However, that is not always desirable since it will not allow your bundler to de-duplicate any shared dependencies dhive and your app might have.

    To allow for deduplication you can require('@hiveio/dhive/lib/index-browser'), or if you plan to provide your own polyfills: require('@hiveio/dhive/lib/index'). See src/index-browser.ts for a list of polyfills expected.

    The package also ships an ES-module build in lib/esm/ and declares "sideEffects" (only the polyfill entry lib/index-browser.js, its ESM twin and dist/ have any), so a bundler can drop the parts of dhive an app does not import. package.json's exports picks the build by condition:

    Condition Gets
    types lib/index.d.ts
    browser dist/dhive.js, the prebuilt bundle, as before
    module (esbuild, webpack, rollup) lib/esm/index.js
    node (both require and import) lib/index-node.js (CommonJS, one copy of the classes either way)
    import / require otherwise lib/esm/index.js / lib/index-node.js

    Bundles for Node (SSR, serverless, esbuild --platform=node) therefore tree-shake without any configuration. A browser build keeps receiving dist/dhive.js unless it leaves the browser condition out (webpack's resolve.conditionNames, Vite's resolve.conditions); the ES-module build then needs the bundler to provide the Node built-ins it imports (crypto, assert, events, stream) and the Buffer global, which dist/dhive.js brings with it.


    Share and Enjoy!