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
Requirements
Difficulty
Intermediate
scrolling through method definitions, and you come upon this image:
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.
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.
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
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()
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()
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")
The account's public key is stored as "key_auths" in the returned dict.
pubkey2 = account['posting']['key_auths'][0][0]
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.
https://steemit.com/utopian-io/@abhi3700/learn-steem-python-1-installation-guide by @abhi3700 - This is a new tutorial series on accessing Steem blockchain using python programming language.
https://steemit.com/utopian-io/@scipio/learn-python-series-intro by @scipio - This series is intended for anybody interested in Python programming
Visit ArtoLabs on GitHub
Visit: https://mike.artopium.com
Contact: https://discord.gg/97GKVFC