[Steem-js] How to validate Username and Private Keys.

in #dev7 years ago

how.png

Hello, my friends.

Steem-js is official Javascript library for Steem.
you can read more about steem-js here

Today in Steemdevs Discord Server Someone asked a question:

Screenshot_2.png

Now I'm going to Answer This question.

Note: You can find another solution for this.

First we need to add steem-js to our js file.

in browser:

<script src="https://cdn.steemjs.com/lib/latest/steem.min.js"></script>

and in node.js:

first, you must install steem by npm: npm install steem --save
then, add this line to your js fildes.
var steem = require('steem');

We can get accounts info by:
var name = 'mahdiyari';
steem.api.getAccounts([name], function(err, result) { console.log(err,result); });

but we need only public posting key:
var name = 'mahdiyari';
steem.api.getAccounts([name], function(err, result) {
var pubWif = result[0].posting.key_auths[0][0];
});
pubWif will be Posting Public Key.

Asume private posting key is like below:
var privWif = 'mylongprivatepostingkey';

We can Check if privWif is valid for that pubWif or not.
steem.auth.wifIsValid(privWif, pubWif);

It will return true or false.
so we can use it like this:
var isvalid = steem.auth.wifIsValid(privWif, pubWif);
if(isvalid == true){
console.log(name+' Welcome.');
}else{
console.log('Wrong! Check your Private key.');
}

If user private posting key is valid, it will say mahdiyari Welcome.

Full Code:
var steem = require('steem');
var name = 'mahdiyari';
steem.api.getAccounts([name], function(err, result) {
var pubWif = result[0].posting.key_auths[0][0];
var privWif = 'mylongprivatepostingkey';
var isvalid = steem.auth.wifIsValid(privWif, pubWif);
if(isvalid == true){
console.log(name+' Welcome.');
}else{
console.log('Wrong! Check your Private key.');
}
});

this code is working fine for true Private Keys.
if you try with another character as privWif in above example, you will get an error.
we can remove this error by adding try{}catch(){}

Full code with try{}catch(){}:

var steem = require('steem');
var name = 'mahdiyari';
steem.api.getAccounts([name], function(err, result) {
var pubWif = result[0].posting.key_auths[0][0];
var privWif = 'mylongprivatepostingkey';
var isvalid;
try{ isvalid = steem.auth.wifIsValid(privWif, pubWif); }
catch(e){ isvalid = 'false'; }
if(isvalid == true){
console.log(name+' Welcome.');
}else{
console.log('Wrong! Check your Private key.');
}
});

If you have any question, ask in comments or join Steemdevs Discord Server.
first image source: pixabay.com


Support me by voting me as a witness. It is Important for me.
Voting for witnesses is free! And you can vote up to 30 witnesses. Also, Unvoting is possible at any time.

1- open https://steemit.com/~witnesses
2- scroll down.
3- type mahdiyari and once click on the vote button.


If you want to Receive an upvote from me, Join our discord server and Send your post link in #post-promotion Channel.
You can get an upvote from my account every 15 Hour.
Join Our Discord Channel


If you are a witness, Include my seed node in your config.ini File.
seed-node = node.mahdiyari.info:2001


Regards,
2017-09-04

Sort:  

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by MahdiYari from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews/crimsonclad, and netuoso. The goal is to help Steemit grow by supporting Minnows and creating a social network. Please find us in the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

Thank you for this. Keep them coming. It's hard to find help on these topics.

Thank you very much for this! This is more than I expected. Resteemed

nice post thanks for help.

Good info following you.

This post has received a 1.04 % upvote from @drotto thanks to: @banjo.

This post has been ranked within the top 80 most undervalued posts in the second half of Sep 04. We estimate that this post is undervalued by $16.06 as compared to a scenario in which every voter had an equal say.

See the full rankings and details in The Daily Tribune: Sep 04 - Part II. You can also read about some of our methodology, data analysis and technical details in our initial post.

If you are the author and would prefer not to receive these comments, simply reply "Stop" to this comment.

Thanks for this post, it's awesome to see the community giving detailed examples of how to use the steem JS API. I did, however, notice that running this example with an invalid WIF on my machine returns a warning for a node.js unhandled promise rejection. This is due to the fact that I replaced your console.log with a thrown Error. I will go ahead and post my code in case anyone else plans to throw errors from invalid WIFs

    steem.api.getAccountsAsync([username])
      .then(result => {
        var publicWif = result[0].posting.key_auths[0][0];
        let validWif = true;
        try {
          validWif = steem.auth.wifIsValid(wif, publicWif);
        }
        catch(e) {
          validWif = false;
        }
        if(!validWif)
          throw new Error('Invalid Wif provided');
      })
      .catch(error => console.log('Sign in failed: ' + error.message));

    this.wif = wif;

I'm also currently working on using the steem.auth.toWif(username, password, 'posting') call to retrieve the wif instead and could post that solution if there is any interest.

Of course, you can modify that code according to your needs. That was just an example.

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.029
BTC 57971.70
ETH 2448.51
USDT 1.00
SBD 2.34