Harnessing the Power of Hive Blockchain with Node-RED

In the ever-evolving world of blockchain technology, the ability to interact with decentralized networks efficiently is crucial. Today, I’m excited to introduce a powerful new tool that bridges the gap between the Hive blockchain and Node-RED, a popular flow-based development tool for visual programming. My custom Node-RED module for Hive blockchain interactions opens up a world of possibilities for developers and Hive users alike.

What is Node-RED?

Node-RED is an open-source, browser-based programming tool that allows users to wire together hardware devices, APIs, and online services using a visual flow editor. It’s particularly popular in IoT applications but is versatile enough for a wide range of uses.

The Hive Blockchain Module: An Overview

Our custom Node-RED module consists of several nodes that enable various interactions with the Hive blockchain:

  1. hiveAuthenticate: Handles user authentication
  2. hiveEngineAuthenticate: Manages authentication for Hive Engine tokens
  3. hiveComment: Allows posting comments on the Hive blockchain
  4. hivePost: Enables creating new posts on Hive
  5. hiveEngineTransferToken: Facilitates the transfer of Hive Engine tokens
  6. hiveParseBlock: Parses a block to easily determine the type of post

Let’s explore each of these nodes in detail.

  1. hiveAuthenticate Node

This node is responsible for authenticating users on the Hive blockchain. It takes a username and private key as input and verifies the credentials against the Hive blockchain. If authentication is successful, it stores the credentials in the global context for use by other nodes.

Key Features:

  • Input fields for username and private key in the node configuration
  • Ability to override configured values with input message payload
  • Error handling for missing credentials or failed authentication
  1. hiveEngineAuthenticate Node

Similar to the hiveAuthenticate node, this node handles authentication specifically for Hive Engine operations. It also stores the credentials in the global context for use by other Hive Engine related nodes.

  1. hiveComment Node

This node allows users to post comments on the Hive blockchain. It includes fields for the parent post’s author and permlink, as well as the comment’s body and permlink.

Key Features:

  • Configurable fields for all comment parameters
  • Option to override configured values with input message payload
  • Automatic use of authenticated user’s credentials from global context
  1. hivePost Node

The hivePost node enables users to create new posts on the Hive blockchain. It includes fields for the post title, body, tags, and various posting options like maximum accepted payout and beneficiaries.

Key Features:

  • Comprehensive configuration options for all post parameters
  • Flexibility to override any parameter via input message
  • Integration with Hive authentication for seamless posting
  1. hiveEngineTransferToken Node

This node facilitates the transfer of Hive Engine tokens. Users can specify the recipient, token symbol, amount, and an optional memo.

Key Features

  • Configuration fields for all transfer parameters
  • Ability to override configured values with input message payload
  • Automatic use of Hive Engine credentials from global context

Implementation and Usage

Each node in our module follows a similar pattern:

  1. A HTML file that defines the node’s appearance and configuration options in the Node-RED editor
  2. A JavaScript file that implements the node’s functionality

The nodes are designed to be flexible, allowing users to either configure default values in the node itself or pass values dynamically through the input message payload. This design choice makes the nodes suitable for both static configurations and dynamic, programmatically-driven flows.

Error Handling and Security

I’ve implemented robust error handling throughout the module. Each node checks for the presence of required parameters and provides meaningful error messages if any are missing. Additionally, sensitive information like private keys is handled securely, being stored in the global context rather than being passed around in messages. No one but you gets to see your private keys.

Summing Up

This custom Node-RED module for Hive blockchain interactions provides a powerful, flexible, and user-friendly way to interact with the Hive blockchain. Whether you’re looking to automate posting, manage comments, or handle token transfers, this module offers a comprehensive solution. It does not yet support all functions and capabilities that the Hive blockchain provides, but it will support the drag-and-drop, low-code/no-code creation of bots!

Sample Flow

Streaming updates on the blockchain

[
    {
        "id": "a1b2c3d4.e5f6",
        "type": "tab",
        "label": "Hive Blockchain Monitor",
        "disabled": false,
        "info": ""
    },
    {
        "id": "12345678.abcd",
        "type": "inject",
        "z": "a1b2c3d4.e5f6",
        "name": "Start Fetching Blocks",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "start",
        "payloadType": "str",
        "x": 150,
        "y": 80,
        "wires": [
            [
                "9abcdef0.1234"
            ]
        ]
    },
    {
        "id": "23456789.bcde",
        "type": "inject",
        "z": "a1b2c3d4.e5f6",
        "name": "Stop Fetching Blocks",
        "props": [
            {
                "p": "payload"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "stop",
        "payloadType": "str",
        "x": 150,
        "y": 140,
        "wires": [
            [
                "9abcdef0.1234"
            ]
        ]
    },
    {
        "id": "9abcdef0.1234",
        "type": "hive",
        "z": "a1b2c3d4.e5f6",
        "name": "Hive Node",
        "x": 370,
        "y": 100,
        "wires": [
            [
                "5e00c4ed04fd1602"
            ]
        ]
    },
    {
        "id": "7890abcd.ef12",
        "type": "switch",
        "z": "a1b2c3d4.e5f6",
        "name": "Check Operation Type",
        "property": "operationType",
        "propertyType": "msg",
        "rules": [
            {
                "t": "eq",
                "v": "comment",
                "vt": "str"
            },
            {
                "t": "else"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 2,
        "x": 780,
        "y": 100,
        "wires": [
            [
                "34567890.cdef"
            ],
            [
                "def12345.6789"
            ]
        ]
    },
    {
        "id": "34567890.cdef",
        "type": "debug",
        "z": "a1b2c3d4.e5f6",
        "name": "Debug Comment",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "statusVal": "",
        "statusType": "auto",
        "x": 1030,
        "y": 80,
        "wires": []
    },
    {
        "id": "def12345.6789",
        "type": "debug",
        "z": "a1b2c3d4.e5f6",
        "name": "Debug Other Operations",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "true",
        "statusVal": "",
        "statusType": "auto",
        "x": 1050,
        "y": 140,
        "wires": []
    },
    {
        "id": "5e00c4ed04fd1602",
        "type": "hiveParseBlock",
        "z": "a1b2c3d4.e5f6",
        "name": "",
        "x": 560,
        "y": 100,
        "wires": [
            [
                "7890abcd.ef12"
            ]
        ]
    }
]

Installation

  1. Download the Module
  • Ensure you have downloaded the tar file containing the Hive blockchain module for Node-RED.
  1. Locate Your Node-RED User Directory
  • Open your terminal or command prompt.
  • Run Node-RED with the -v flag to find your user directory:
    node-red -v
  • Look for a line that says “User directory : /path/to/your/.node-red”
  1. Navigate to the Node-RED User Directory
  • Use the cd command to navigate to your Node-RED user directory:
    cd /path/to/your/.node-red
  1. Create a ‘nodes’ Directory (if it doesn’t exist)
  • Run the following command:
    mkdir -p nodes
  1. Move the Downloaded Tar File
  • Move the downloaded tar file to the ‘nodes’ directory:
    mv /path/to/downloaded/hive-node-red-module.tar.gz nodes/
  1. Extract the Tar File
  • Navigate to the ‘nodes’ directory:
    cd nodes
  • Extract the contents of the tar file:
    tar -xzvf hive-node-red-module.tar.gz
  1. Install Dependencies
  • Navigate back to your Node-RED user directory:
    cd ..
  • Install the required npm packages:
    npm install @hiveio/hive-js sscjs
  1. Restart Node-RED
  • If Node-RED is running, stop it.
  • Start Node-RED again:
    node-red
  1. Verify Installation
  • Open the Node-RED editor in your web browser (usually at http://localhost:1880).
  • Check the palette on the left side of the editor. You should see new nodes for Hive blockchain interactions.
  1. Update Your Flow
    • Drag and drop the new Hive nodes into your flow.
    • Configure the nodes as needed for your specific use case.
  2. Deploy Your Updated Flow
    • Click the ‘Deploy’ button in the Node-RED editor to apply your changes.

Troubleshooting:

  • If you don’t see the new nodes, check the Node-RED console for any error messages during startup.
  • Ensure that all files were correctly extracted from the tar file.
  • Verify that the required npm packages (@hiveio/hive-js and sscjs) were installed successfully.

Note: Depending on your system configuration, you might need to use sudo for some of these commands, especially when installing npm packages globally.

That’s it! You should now have the Hive blockchain module installed and ready to use in your Node-RED environment. Happy automating!

Leave a Comment