How To Verify A Private Posting Key Using Steem-Python

learn_reward_pool.jpg

A tutorial that teaches a Python 3 programmer how to use the Steem-Python library to authenticate and validate a Steemit.com Private Posting WIF Key.

Repository
https://github.com/steemit/steem-python

You will learn

  • How to convert a private key to a public key
  • How to compare this to the public key on steemd

Requirements

Difficulty
Intermediate

There you are on steem.readthedocs.io,

scrolling through method definitions, and you come upon this image:

A9urMG9.png

You've now entered the "Low Level" utilities section.

You'll find an amazing number of very helpful class methods here, two of which we are going to be using from the steembase class, but they're not well documented on readthedocs.io so we'll just have to use the source, Luke.

This is actually much easier than One might think

and is really only a matter of a few simple steps. However, I assume you already know Python 3, and have a rudimentary understanding of the Steem-Python library on GitHub, so this is somewhat of an intermediate tutorial. I include some fantastic resources for learning Python 3 at the end of this tutorial.

Import the PrivateKey and InvalidWifError Class

We import steem as usual. The sys module is simply used to help us exit the program. We also import the PrivateKey class and the InvalidWifError class from the steembase package. Steembase is included in the Steem-Python library.

#!/usr/bin/python

import sys
from steem import Steem
from steembase.account import PrivateKey
from steembase.exceptions import InvalidWifError

Check to See If the Key Exists and is Valid

This part is pretty basic. Just start up an instance of Steem using a Private Posting Key and check for an exception. Obviously, replace the "XXXXX" with the private posting key. The InvalidWifError class will throw an exception specific to WIF keys, giving you more information about why the key did not validate.

private_posting_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

try:
    steem = Steem(keys=[private_posting_key])
except InvalidWifError as e:
    print("Invalid key: " + str(e))
    sys.exit()

Convert The Private Key to a Public Key

Next, take the Private Posting Key and convert it to a Public Key using the PrivateKey class and the pubkey method. We then save this to a variable called "pubkey".

try:
    pubkey = PrivateKey(private_posting_key).pubkey
except:
    print ("Could not convert to public key")
    sys.exit()

Get the Account for Which the Key is to Be Tested Against

Using the instance of steem we created at the beginning, we use the get_account method and fetch all account information and store it in the dict named "account".

account = steem.get_account("learnelectronics")

Get The Account's Public Posting Key From Steem

The account's public key is stored as "key_auths" in the returned dict.

pubkey2 = account['posting']['key_auths'][0][0]

Compare the Two Keys To See if They Match

A simple string comparison is all it takes to see if there's a match.

if str(pubkey) != str(pubkey2):
    print ("The private key and account name provided do not match.")
else:
    print ("Private key is valid.")

And there you go! Easy.

Further Reading

Watch

Written by "@Artopium" Mike

Visit ArtoLabs on GitHub
Visit: https://mike.artopium.com
Contact: https://discord.gg/97GKVFC

Sort:  

if only the upvote window was larger, I was looking for this a few months before you posted lol

The content of the tutorial is too narrow and lacking instructiveness, you might improve the quality by covering larger subjects and providing greater details and explanations.

Instead of just simply giving the necessary code for something, focusing on teaching the concept in any of your future tutorials will significantly improve the quality.

While choosing the subject to cover, first you can research uniqueness and the need of the tutorial, instructions which can be found easily on the web or functions/features which are already well documented in the project documentation, don't really add value to the open source community.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.

Thank you for your feedback but I must wholeheartedly disagree. Nowhere online can you find instructions on how to use the the low level section of the steem.readthedocs.io to convert a private key to a public key. I had to read the source code to figure this out on my own. If you feel there is already sufficient documentation on this subject can you link to it? I specifically wrote this tutorial because I could find nothing equivalent online and wanted to provide further clarification on how to go about doing this. You state:

Instead of just simply giving the necessary code for something, focusing on teaching the concept in any of your future tutorials will significantly improve the quality.

Can you please point out where I "give out the code" without explaining it? I state at the beginning of the tutorial that you should understand Python3 and the Steem-Python library, so I did not spend time in this tutorial explaining those things. Instead I "focused" entirely on the subject at hand: how to convert a private key to a public key and to compare it to the key provided by steemd. Can you please explain more explicitly how I did not accomplish this?

You state:

While choosing the subject to cover, first you can research uniqueness and the need of the tutorial, instructions which can be found easily on the web or functions/features which are already well documented in the project documentation, don't really add value to the open source community.

As I explained earlier, I wrote this specifically because I researched this and could not find a readily made tutorial to teach this concept. It took me hours to figure this out whereas the person to find my tutorial will only have to spend minutes. How is that not added value?

Furthermore, you speak of adding value yet you have made no contribution yourself. Instead it seems this comment you left on my hard work is cut and pasted, mostly because it lacks any specific reference to my work.

Look, I understand you guys are busy, but this really seems like a subjective judgment call made based on personal opinion rather than an informed and unbiased decision.

Before anything else, I must assure you that my comment is not copy-pasted. I never copy-paste my comments I only used to reference the rules. We no more directly reject and approve, and comment that way. And before I score your contribution, I asked the opinion of the community manager of the category, we both agreed on the decision.

About the uniqueness, I didn't check that if there is a tutorial explaining the exactly same concept. I assumed that, since relatively narrow/easy/minimalist tutorials are more likely to be found on the web easily.

But about the wording, you are absolutely right. It was not specific and clear enough. I honestly accept that I made it look like it has the general lacks the contributions in the category have, as I made it about being well-documented, and adding value part in my comment.

I really value giving detailed feedbacks and providing clarifications and suggestions in my reviews, you can check it out from my prior comments on other contributions.

And score of your contribution is not affected by any subjective calls, because our scoring system is working with a questionnaire which asks objective questions to the moderator and the resulting score is calculated by the algorithm.

To summarize, the comment was not clear but the my actual point I tried to clarify is that the content of the tutorial is too narrow or minimalist, which usually causes the explanation quality and volume to decrease. But in Utopian, we score the contributions and only high quality/scored contributions get upvoted, so volume of the content is important in the whole review process because categories like tutorials focus on teaching part in the content, if there is not much to teach/explain it naturally gets low score.

Coin Marketplace

STEEM 0.35
TRX 0.12
JST 0.040
BTC 70734.57
ETH 3561.52
USDT 1.00
SBD 4.75