A Hacky Guide to Hive (part 2.1) YO broadcast

in #dev3 months ago (edited)

Broadcasting

<p dir="auto"><a href="https://peakd.com/dev/@felixxx/a-hacky-guide-to-hive-part-15" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Last post, I talked about how you can <em>get data from the blockchain.<br /> For some applications that might be all you ever need. <p dir="auto">But maybe you want to <em>store something <em>on chain.<br /> For example: you want to make a vote: <p dir="auto"><img src="https://images.hive.blog/768x0/https://files.peakd.com/file/peakd-hive/felixxx/23tRrRPGbYfyCGnXPHV7aVow71dGKnoNVAeJNDbhht5R2ZKNGeyfMk1Ds3XeSNHPgD6zA.png" alt="mouthpiece.png" srcset="https://images.hive.blog/768x0/https://files.peakd.com/file/peakd-hive/felixxx/23tRrRPGbYfyCGnXPHV7aVow71dGKnoNVAeJNDbhht5R2ZKNGeyfMk1Ds3XeSNHPgD6zA.png 1x, https://images.hive.blog/1536x0/https://files.peakd.com/file/peakd-hive/felixxx/23tRrRPGbYfyCGnXPHV7aVow71dGKnoNVAeJNDbhht5R2ZKNGeyfMk1Ds3XeSNHPgD6zA.png 2x" /> <h3>Python broadcast <p dir="auto">Assuming you have <a href="https://github.com/holgern/beem/tree/master" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Beem installed, the easiest way to vote for this post would be: <pre><code>from beem import Hive hive = Hive(keys=['yourpostingkey']) account = 'youraccount' hive.vote(weight=100, account=account, identifier='@felixxx/a-hacky-guide-to-hive-part-21-yo-broadcast') <p dir="auto">While this may be a neat example of how to do get the job done, it's not the best way to go about it.<br /> I just wanted to come in hot and start with a working example. <p dir="auto">Clearly you could improve this code a lot, but before that, the most important here:<br /> For it to work, there must be a private key stored in the code itself. That's not good. That's not safe.<br /> There are many different ways to make this safer and I will adress some, but for now: <h3>System Design <p dir="auto">This may be a <em>hacky guide, but I don't want to write a <em>shitty guide.<br /> The first line of defence against vulnerabilities and errors is to adress them at system design level. <p dir="auto">You could go ahead and write a long script that looks something up on chain and then reacts to events somehow and broadcasts a transaction, and loop over all this somehow...<br /> You could automate something on chain like this. That approach works and I've seen it and done it. <p dir="auto">If you aren't careful, you will end up with a pile of spagetthi code with your keys somewhere inside of it, though. <p dir="auto">I am not the right guy to write a guide on system design, but I'll not just hack away, either.<br /> I don't want to just post code snippets. I will lace in the design principles, that I have settled on. <h3>Python limitations <p dir="auto">Leaving design and safety aside: if you look at this stuff with only your Python hat on, eventually you will run into a wall. There are problems that are very hard or impossible to solve, if you try to do everything in a single script. <h2>user broadcast <p dir="auto">If you want to have <strong>users input something, Python is in most cases the wrong toolbox. <p dir="auto">First of all: if you don't have to, you shouldn't be involved in the signing of other people's transactions. In <a href="https://developers.hive.io/" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">the documentation it says: <blockquote> <p dir="auto">By utilizing Authenticating services, you can eliminate or give more confidence to user, so they are assured their keys are safe. They can securely interact with your application, website or service. <p dir="auto">So, right out of the gate: I don't want to touch anyone's keys.<br /> And if I don't have the keys, this whole Python-side signing stuff is useless. <p dir="auto">The good news: You can just hand over those responsibilities to a third party.<br /> That makes your life easier and the final application safer. <h3>Custom User Client <p dir="auto">I need to be careful here to not overuse the expression <em>client.<br /> In the last post, I talked about writing your own <em>custom client for Hive.<br /> That was in Python, and like I wrote above, this can do a lot of things and get you quite far. <p dir="auto">But you may also want to build your own <em>custom user client, that allows your <strong>users to broadcast to the blockchain, without you being directly involved in signing process. In this case it is easiest to just leave the Python environment and use javascript instead. <p dir="auto">If you want to just stay in Python, and don't plan on making user interfaces, the next part might seem useless at first. I am trying to make a point though. Please bear with me... <h2>YO <p dir="auto">As an example, I created a really simple game.<br /> It only has one move: an empty custom transaction on the Hive blockchain, of the type 'YO'. <h3>Code Example with <a href="https://github.com/hive-keychain/hive-keychain-extension/blob/master/documentation/README.md#requestcustomjson" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Hive Keychain <pre><code><!DOCTYPE html> <html> <style>button{margin:124px;font-size:48px;}</style> <button id="yo_button">YO</button> <script> let yo_button = document.getElementById("yo_button") yo_button.onclick = () => { window.hive_keychain.requestCustomJson( null, 'YO', 'Posting', '{}', 'YO broadcast', (response) => { console.log(response.result); } ) } </script> </html> <ul> <li><p dir="auto">save as <code>yo.html <li><p dir="auto">you <strong>need Keychain browser extension installed<br /> <a href="https://chromewebstore.google.com/detail/hive-keychain/jcacnejopjdphbnjgfaaobbfafkihpep" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">for chrome<br /> <a href="https://addons.mozilla.org/en-US/firefox/addon/hive-keychain/" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">for firefox<br /> you <strong>don't<span> want the SDK or anything else from <a href="https://github.com/hive-keychain" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://github.com/hive-keychain <li><p dir="auto">you will have to <strong>serve this somehow, otherwise keychain won't work.<br /> From <strong>inside the directory, which <code>yo.html is stored in:<br /> <code>python -m http.server -b 127.0.0.1 8080 <li><p dir="auto">Alternatively, in Visual Studio Code, try <a href="https://github.com/ritwickdey/vscode-live-server" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Live Server <li><p dir="auto">For other <em>requests: <a href="https://github.com/hive-keychain/hive-keychain-extension/blob/master/documentation/README.md" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">The part of keychain's documentation that you actually need <p dir="auto">If it all went right, you can now make a <em>move.<br /> Follow this url: <h4><span><a href="http://127.0.0.1:8080/yo.html" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">http://127.0.0.1:8080/yo.html <p dir="auto">Result looks like this: <p dir="auto"><span><a href="https://hivehub.dev/tx/eb025cf797ee5bc81d7399282268079cc29cc66d" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://hivehub.dev/tx/eb025cf797ee5bc81d7399282268079cc29cc66d <h2>Microservices <p dir="auto">Now you have a <em>custom user client in javascript.<br /> It's extremely simple, but it's unique. 🦄<br /> As far as I am aware, only I have ever made a <code>YO <em>on chain.<br /> It's a minimalistic game that only I have played so far, and that has no goal, but ...works.<br /> Maybe you want to join... <p dir="auto">Next episode, I will demonstrate how to <em>observe a <code>YO live.<br /> That will end with a <code>YO client in Python, which I can later build a <code>YO API on. <p dir="auto">Don't worry! I will connect all the services later.<br /> What I want to emphasize: Even though these <em>services and <em>clients can all work as parts of the same <em>system, they each do fundamentally different jobs.<br /> Looking up Hive blockchain data is a different procedure than broadcasting to the chain.<br /> I will keep this stuff apart on the system level side of things. <p dir="auto">This comes with the immediate benefit, that I can work on them separately.<br /> I mean: the broadcasting part of the client already fully works.<br /> Anyone can now <code>YO in 15 lines of code. <p dir="auto">Maybe someone wants to even upload this html to their webserver. 😬 <p dir="auto">Anyways, to build things like this is probably best described as <em>microservice architecture.<br /> <a href="https://www.youtube.com/watch?v=y8OnoxKotPQ" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Here's a video, that I found eye opening and entertaining. Please watch, if you haven't already. <h3>Get and Set <p dir="auto">If you are coming from traditional game- or web-development this might apear unusual or complicated. You might try to view these things as <em>get and <em>set methods; If you don't need to broadcast any other transactions than your own, you could even get away with treating it that way. <p dir="auto">While you can access a lot of blockchain information like you would a REST API or something, A Hive broadcast is much different than a <em>set method, though.<br /> That may make some stuff seem more complicated, but it comes with advantages: <h3>Authentication <p dir="auto">If you want to build anything that requires users to have an <em>account, you will have to enter the world of <em>user authentication. If you've ever touched the topic before, you know how that can be a huge pain. If not, then google it. There are huge libraries dedicated to user auth, password hashing and whatnot. <p dir="auto">Maybe all the above sounded complicated and you don't understand what I am trying to get at. Maybe you can't (at this point) appreciate, how the above demonstration entirely eliminates the need for any authentication measures on your end... <p dir="auto">Let me tell you: It's a big deal. That just solved a big problem with no effort. You don't need any infrastructure, no user tables...<br /> A lot of stuff just happened <em>en passant.<br /> I tried leveraging Keychain and Hive's protocol to do the heavy lifting. <p dir="auto">Maybe <a href="/@brianoflondon/hive-has-the-best-log-in-and-authentication-system-on-the-internet">this post explains it better. <h3>Hive Blockchain <p dir="auto">I will expand on all this in further posts, but for now: <p dir="auto">Trying to build anything directly <em>on chain (for Hive) comes at a price. It has strict limitations.<br /> But if you stay inside this framework, you can build amazingly cheap and tight applications.<br /> <sub><strong>the rhymes! <p dir="auto">I may have left a lot of threads open with this post.<br /> I hope that with a few more posts, it will all come together. <p dir="auto">Maybe all the above wasn't news to you.<br /> Maybe this post was to much talking and to little action.<br /> But I have to go over some fundamental concepts for the rest to make sense.<br /> I'll try sprinklig in useful code examples - maybe that can keep some of you interested, or attract new readers. <h2>Notes <ul> <li>This is a work in progress and subject to change.<br /> With this one, I am really not sure, if I didn't try too much... <li>I appreciate feedback - it could help me a lot, but: <ul> <li><strong>please try to not comment 'That's not right' without further explanation.<br /> At least provide a link or something. <li><strong>please don't just come up with questions just for the sake of it.<br /> I am trying to publish something helpful here, work <strong>with me please.
Sort:  

I was thinking to use Hive SQL instead of the python. Did you use it also?

Like I wrote in the previous post:

This is approach isn't the best for all applications; If you want to do historical stuff, analyzing large amounts of data, statistics and such things, HiveSQL is better.

It depends, on what you are trying to do...

You might try to view these things as get and set methods; If you don't need to broadcast any other transactions than your own, you could even get away with treating it that way.

For this guide, I won't use HiveSQL, no.
You could use this guide to build your own, custom HiveSQL, though...

Hope that helps.

Thanks, yeah, mostly crunching some numbers, so I will check out SQL first.