Continuation of this post: - https://hive.blog/blockchain/@the.journal/how-to-encrypt-information-in-customjson-episode-1 Now we are going to retrieve the encrypted message from the blockchain, and decrypt it for our usage. The script I'm going to use is in `python` like the previous one, and it will parse each single block to check if there is a custom_json with our id in order to get the message. so let's start to import the libraries ```python from beem.blockchain import Blockchain from beem import Steem from beem.nodelist import NodeList import re from cryptography.fernet import Fernet nodelist = NodeList() nodelist.update_nodes() ``` After that we need to load the `Fernet` key we saved before in order to decrypt the message. ```python def load_key(): """ Loads the key named `secret.key` from the current directory. """ k=open("secret.key", "rb").read() return k key = load_key() f = Fernet(key) ``` Let's start the blockchain instance for this example we will use the `steem` blockchain, but it works as well for the `hive` one. ```python stm = Steem(node=nodelist.get_steem_nodes()) b = Blockchain(blockchain_instance=stm) ``` Now let's start an infinite loop to keep check the blocks, and print out the message once found in the blockchain. ```python for i in b.blocks(start=b.get_current_block_num()): print (i['id']) for t in i['transactions']: for o in t['operations']: if o['type']=='custom_json_operation': if o['value']['id']=='test_DM': enc= re.findall('"type":"(.+?)"',o['value']['json'])[0] ms=enc.encode('utf_8') decrypted_message = str(f.decrypt(ms)) print (decrypted_message.replace("b'",'').replace("'",'')) ``` Normally it takes around a minute from the custom_json publication to get discovered in the blockchain.
author | the.journal | ||||||
---|---|---|---|---|---|---|---|
permlink | how-to-encrypt-information-in-customjson-episode-2 | ||||||
category | hive | ||||||
json_metadata | {"app":"peakd/2020.07.1","format":"markdown","tags":["hive","steem","blockchain","development","code","python","tutorial"],"users":["the.journal"],"links":["https://hive.blog/blockchain/@the.journal/how-to-encrypt-information-in-customjson-episode-1"]} | ||||||
created | 2020-07-07 22:08:33 | ||||||
last_update | 2020-07-07 22:08:33 | ||||||
depth | 0 | ||||||
children | 1 | ||||||
last_payout | 2020-07-14 22:08:33 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 0.000 HBD | ||||||
curator_payout_value | 3.484 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 1,903 | ||||||
author_reputation | 5,799,246,878,950 | ||||||
root_title | "How to encrypt information in custom_json [episode 2]" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 98,397,259 | ||||||
net_rshares | 18,320,880,092,028 | ||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
tykee | 0 | 2,936,498,014 | 50% | ||
kamchore | 0 | 145,556,616,272 | 100% | ||
bala41288 | 0 | 40,081,916,680 | 20% | ||
moeenali | 0 | 19,598,233,623 | 10% | ||
digital.mine | 0 | 18,018,641,444,449 | 100% | ||
laissez-faire | 0 | 12,832,448 | 100% | ||
p3d1 | 0 | 24,707,582,974 | 100% | ||
gitplait | 0 | 69,344,967,568 | 100% |
Thanks for sharing anorher tutorial nice tutorial about emcryption in python. Love to see that you share the link of part 1. It will be easier for python enthusiast to explore your nice efforts. <sub> Your post has been submitted to be curated with @gitplait community account because this is the kind of publications we like to see in our community. </sub> Join our [Community on Hive](https://hive.blog)
author | gitplait-mod1 |
---|---|
permlink | qd5b2o |
category | hive |
json_metadata | {"users":["gitplait"],"links":["https://hive.blog"],"app":"hiveblog/0.1"} |
created | 2020-07-08 10:13:36 |
last_update | 2020-07-08 10:13:36 |
depth | 1 |
children | 0 |
last_payout | 2020-07-15 10:13:36 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 407 |
author_reputation | 64,455,719,431 |
root_title | "How to encrypt information in custom_json [episode 2]" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 98,405,078 |
net_rshares | 0 |