0.0.5 • Published 2 years ago

dbeowulf v0.0.5

Weekly downloads
-
License
BSD-3-Clause
Repository
-
Last release
2 years ago

dbeowulf

Robust beowulf 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.


Browser compatibility

Installation

Via npm

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

npm install dbeowulf

From cdn or self-hosted script

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

<script src="dbeowulf.js"></script>

Usage

In node.js

With TypeScript:

import { Client } from "dbeowulf";

const client = new Client(["https://bw.beowulfchain.com"]);

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

With JavaScript:

var dbeowulf = require("dbeowulf");

var client = new dbeowulf.Client(["https://bw.beowulfchain.com"]);
var key = dbeowulf.PrivateKey.fromLogin("username", "password", "posting");

client.broadcast
  .vote(
    {https://bw.beowulfchain.com
      voter: "username",
      author: "almost-digital",
      permlink: "",
      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("dbeowulf");

const client = new Client(["https://bw.beowulfchain.com"]);

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 dbeowulf = require("dbeowulf");
var es = require("event-stream"); // npm install event-stream
var util = require("util");

var client = new dbeowulf.Client(["https://bw.beowulfchain.com"]);

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);

Bundling

The easiest way to bundle dbeowulf (with browserify, webpack etc.) is to just npm install dbeowulf and require('dbeowulf') 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 dbeowulf and your app might have.

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


Share and Enjoy!