How to Distinguish a Fly from a Bee?

in HiveDevs11 months ago
Authored by @small.minion

Hello everyone ❗❗

Something very unexpected happened to us recently (actually, last night). An elderly man got stuck in a chimney at our office and we found him in this very unfavorable situation:


Santa Claus

We helped him, of course, and he decided to fulfill our wish - we asked him to make for us an application operating on Hive Blockchain HIVE

He was a bit surprised and said: "I have heard about beehives, bees 🐝 and other insects, such as flies... I also know how to distinguish them from each other: flies are attracted to stee 💩, and bees like flowers 🌹. But how can I deal with a program running on some Hive blockchain❓"

We told him that good computer programs used types to distinguish things. We also told him that to perform actions on the Hive platform he must use specially prepared operations known as Hive Protocol 📜.

He thought for a moment and said: "I see that you publish a lot of articles in this Hive and vote for them - I will give you a program that will vote for you".

So he made a bot for us with some helper libraries and tools which would significantly simplify the development of such programs.

We looked at the code examples and saw a really beautiful thing 😍 - a tiny piece of specialized code designed for a specific usecase - please take a look:

import WorkerBee from "@hive-staging/workerbee";

const bot = new WorkerBee();
bot.on("error", console.error);

await bot.start();

for await(const { block, number } of bot)
  console.info(`Got block #${block.block_id} (${number})`);

Yes - just few lines of code to get data from Hive Blockchain... He also gave us more, a little bigger examples. Here is a snippet which you can use to observe some account activity:

import WorkerBee from "@hive-staging/workerbee";

const bot = new WorkerBee();
bot.on("error", console.error);

await bot.start();

const observer = bot.observe.accountOperations("thebeedevs");

observer.subscribe({
  next(op) {
    console.info(op);
  }
});

Finally, we asked him how to perform some actions on Hive - we heard that it was difficult, especially directly on the web client inside a browser. Here is his answer 👇:

import WorkerBee from "@hive-staging/workerbee";

const bot = new WorkerBee({ postingKey: "5JkFnXrLM2ap9t3AmAxBJvQHF7xSKtnTrCTginQCkhzU5S7ecPT" });
bot.on("error", console.error);

await bot.start();

// Build transaction
const builder = await bot.chain.getTransactionBuilder();
builder.push({
  vote: {
    voter: "santaclaus",
    author: "thebeedevs",
    permlink: "IsItaFlyOraBee",
    weight: 2200
  }
});

// Broadcast our transaction with custom internal expiration time
const observer = await bot.signAndBroadcast(builder.build());

// Observe if our transaction has been applied
observer.subscribe({
  next(tx) {
    console.info(tx, "applied in blockchain");
  },
  error() {
    console.error("Transaction observation time expired");
  }
});

One of our guys (a little frightened 😱), who likes to be very secure 🔒 asked: "Why we should hardcode our keys inside some code⁈". Santa Claus 🎅 replied: "you don't need that - I prepared for you some special tool where you can store them safely like also dedicated library providing necessary blockchain functionality, but sharing directly internal chain code and hiding all that awful details":

  1. Beekeeper is a tool which allows you to import your private keys to its internal (encrypted and password protected) storage. Moreover, it allows you to generate a signature from provided sigDigest, but does not provide any access to your keys directly.
    You can find more details related to this tool here: @hive-staging/beekeeper and check definition of defined Beekeeper interfaces.
  2. Wax is a Python & Typescript/JS library which glues chain code to your own (like a beewax) and gives you access to low level Hive blockchain code like:
    • well commented Python & Typescript definitions of all operations handled by Hive. Their definitions are generated from language independent code directly defined in the Hive Protocol
    • high level object oriented interface to interact with a blockchain: IHiveChainInterface which allows you to specify a node you would like to interact with, and use predefined set of API descriptions (or extend it easily) to finally build a transaction using a dedicated ITransactionBuilder and broadcast it using a network_broadcast_api like in this example.

At the end, we got bot creation supporting library: workerbee and working bot app which waits for some posts matching predefined criteria (posted by specied author and having defined matching beneficiary account) and votes for them. Its code can be found here. The workerbee library allows to specify any Hive chain endpoint (like also a chain-id to work with), what can be passed as chainOptions to the WorkerBee class constructor, i.e.:

bot = new WorkerBee({
  postingKey: document.getElementById('key').value,
  chainOptions: {
    apiEndpoint: 'https://some.api.hive.endpoint',
    chainId: '18dcf0a285365fc58b71f18b3d3fec954aa0c141c44e4e5cb4cf777b9eab274e'
  }

To allow you to start this code on your own side, some elves prepared a docker compose scripts to run this example together with small Hive testnet instance and watch how it works...

The compose scripts will automatically point this testnet endpoint to your app origin at port 3000 like also publish bot application on port 31234, so assuming you exposed it on some hive-app-sandbox hostname, opening: http://hive-app-sandbox:31234 in your browser should load the bot code.

You can find compose stack setup documentation here.

But please remember - elves are not so smart and magic as Santa Claus, so they could break something...

We will try to discover more of such goodies he left for us and prepare next posts describing them in more details.

Thanks for reading and 🎄 Happy coding 🎄

TheBeeDevs

Sort:  

Congratulations @thebeedevs! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

You distributed more than 600 upvotes.
Your next target is to reach 700 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP