Automating Hive with Node Red
I have had, for some time now, an idea in my mind to build some automation for the Hive blockchain using Node-Red. I had - once upon a time - built a quick and dirty Discord bot using Node-Red, which was successful (and which I need to re-enable). Automating the Hive blockchain would be more fun.
Why?
There are many reasons. I'm a fan of technology being used to democratise solutions and bring "power to the people," so to speak. If the pen is mightier than the sword, then being able to use the pen is mightier still. Frankly, the more people who can use it, the better.
Node-Red allows the user the ability to perform automations without the need to worry about running, or coding, or editing, a great deal of code. I am involved in IT, and I hate writing code. I despise editing it. I don't even like to look at it. But I do like the idea of creating a tool simple enough for more people to use.
I also want to run a bot for my own nefarious purpose.
So I wrote a Node-Red module which touches on aspects of the blockchain. The nodes provided will allow for authentication onto the blockchain, reading the stream of new blocks, parsing them, adding comments and, for my purposes, transferring Hive-Engine tokens. The currently included features will serve my purpose right now; undoubtedly there are more features that can be added. For now I'll be able to run my bot.
Read on if you so desire.
(code is available for download at https://victorwiebe.com/harnessing-the-power-of-hive-blockchain-with-node-red/, or a direct link of https://victorwiebe.com/wp-content/uploads/2024/07/node-red-contrib-hive.tar.zip ; I might put it in an s3 bucket at some point)
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
This custom Node-RED module consists of several nodes that enable various interactions with the Hive blockchain:
- hiveAuthenticate: Handles user authentication
- hiveEngineAuthenticate: Manages authentication for Hive Engine tokens
- hiveComment: Allows posting comments on the Hive blockchain
- hivePost: Enables creating new posts on Hive
- hiveEngineTransferToken: Facilitates the transfer of Hive Engine tokens
- hiveParseBlock: Parses a block to easily determine the type of post
Let's explore each of these nodes in detail.
- 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
- 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.
- 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
- 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
- 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:
- A HTML file that defines the node's appearance and configuration options in the Node-RED editor
- 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!
Installation
Download the Module
- Ensure you have downloaded the tar file containing the Hive blockchain module for Node-RED.
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"
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
- Use the cd command to navigate to your Node-RED user directory:
Create a 'nodes' Directory (if it doesn't exist)
- Run the following command:
mkdir -p nodes
- Run the following command:
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/
- Move the downloaded tar file to the 'nodes' directory:
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
- Navigate to the 'nodes' directory:
Install Dependencies
- Navigate back to your Node-RED user directory:
cd ..
- Install the required npm packages:
npm install @hiveio/hive-js sscjs
- Navigate back to your Node-RED user directory:
Restart Node-RED
- If Node-RED is running, stop it.
- Start Node-RED again:
node-red
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.
Update Your Flow
- Drag and drop the new Hive nodes into your flow.
- Configure the nodes as needed for your specific use case.
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.
(c) All images and photographs, unless otherwise specified, are created and owned by me.
(c) Victor Wiebe
Blind Skeleton
Online Radio! 24x7 of OTR classics:
The Skeleton Brew coffee shop is open!
Sounds really neat @wwwiebe I am curious to see more of what you are doing with it in the future.
Thanks. I actually want to use it to run a bot. I have some hive engine tokens that I want to begin to share. Hopefully I can get it set up this weekend!
Good luck!
On my list for having a look at. I have been meaning to look at doing additional things with code andthe blockchain for ages but for one reason and another keep not getting round to it.
I like the sound of Node-red though, on my list for the weekend.
I'm a big fan of node-red. I honestly use it for so many things. I'm thinking my next immediate project with it will be to stand up my Hive Engine bot, and then possibly do something similar with bluesky and mastodon.
Hello @wwwiebe
This is @tengolotodo and I'm part of the Silver Bloggers’ Community Team.
Thank you for sharing your excellent post in the Silver Bloggers community! As a special "token" of appreciation for this contribution to our community, it has been upvoted, reblogged and curated.
seems a bit confusing for me a non super technical person x.x
Well, I won't lie, it is. I swear a lot when I'm working on these types of things, which is why I like to end up dummying it down to as simple terms as possible.
lol u swear a lot i wouldnt be surprised xD. i wont even go near these kinds of things cuz id prob get fuqed. id need a full on video step by step and even then i may mess up somehow
LOL! I know. It might be hard for some to believe, but I have all the range of foul language at my disposal. The bot I want to put in place, in fact, will be representative.
wut do u mean. so the bot will curse like u too?
This looks awesome man, I will have to read this more when I get better sleep LOL I'm a little sleep deprived but I've got this bookmarked :D