This is how you can write JSON to the Hive blockchain

in voilk •  3 months ago

    This Sunday's task is to start taming the technicals of the Hive blockchain.

    It is a necessity to learn how to write custom JSON directly on the blockchain. Geekgirl's post says it all: https://ecency.com/hive-167922/@geekgirl/custom-json-the-magical-power-of-hive-blockchain#@geekgirl/r6nnfr

    I am convinced and so I started to do so using some Python magic.

    No dramas here, anyone can do it even if you have limited experience with Python. The easiest is to work it through using Google Colab https://colab.google/. You can find out how to use it elsewhere later while I go through the codes quickly.

    i was struggling and had to ask for some help.

    Thanks to @ben.haase who suggested lighthive https://lighthive.readthedocs.io/en/latest/, I have it tamed finally.

    This works after editing the code he shared. You can check out our thread conversation for the code he shared.

    So here is me sharing my understanding to pay it forward.

    First install lighthive

    !pip install lighthive

    This is important to get thing started.

    Then the codes below did the trick.

    Image file for aesthetic.

    Raw codes in italics in case you want to copy and paste:

    import json
    from lighthive.client import Client
    from lighthive.datastructures import Operation

    This is where you include your account name and active key info between the quotation marks. Of course, you take care of the security!

    accountname = ' '
    active_key = ' '

    This line below activates the client, which uses the active key defined earlier.

    lighthiveClient = Client(keys=[active_key])

    Here this is the actual json data which you want to store. Nothing special here, you should at least know about the json format. No biggie here. I have two things here, a and b, whatever you type in the quotation after the colon will be the value.

    jsonData = {"a":"","b":""}

    Now, note also that to write to the Hive blockchain, there are other required fields (presented in a json format too). Here the account name is called, and of course the jsonData defined above is included.

    pushData = {'required_auths': [accountname] ,'required_posting_auths': [] ,'id':'' ,'json': json.dumps(jsonData) }

    Lastly, the few operations will do the rest of the magic.

    op = Operation(type='custom_json', value=pushData)
    r = lighthiveClient.broadcast_sync(op)
    print(r)

    I ran the above with my passcode and got these printed out:
    {'id': '32242c2431dea95dfe438eb9b9e891d095a1a01c', 'block_num': 84724827, 'trx_num': 7, 'rc_cost': 69576410, 'expired': False}

    You can check out the record here: https://hivexplorer.com/tx/32242c2431dea95dfe438eb9b9e891d095a1a01c

    Notice where the id (32242c2431dea95dfe438eb9b9e891d095a1a01c) is located within the URL? Pretty neat huh?

    I have successfully written a json of nothing but a and b with nothing to the Hive blockchain.

    I guess next weekend I have to try another code to extract these json data or even think of way to encrypt them.

    Thereafter I can use it for fanciful stuffs. Perhaps building something for Hive.

    Out of a sudden, I think the Hive price at the moment is cheap! It gives me this extra database facility and I think it is so undervalued! Not financial advice, but Shhhh!

    Posted Using InLeo Alpha

      Authors get paid when people like you upvote their post.
      If you enjoyed what you read here, create your account today and start earning FREE VOILK!