SteemJs登录的逻辑  / 网络研习社#57

SteemJs登录的逻辑 / 网络研习社#57

login.jpg

Steem的登录有个麻烦事:Steem为了安全,设置了多层密码,什么posting key , active key……之类的,对于大部分人来讲,都是相当复杂的。而基于Steem设计的应用要想实现登录功能也绕不开这个麻烦事。

以前为了省事,做的SteemJiang是限定只能posting key登录!不过有些朋友反映SteemJiang登录不上去,也不知怎么查posting key。好吧,你就折腾着去查吧!

后来做链课,也充分考虑了多项因素,还是决定任意密码都可登录!

如上图所示,用户可以使用posting key,也可以使用密码,都可以正常使用,无非是在登录时多做了一层逻辑判断。

在用密码登录时要多做一步生成密钥的动作。在js中是这个函数steem.auth.toWif(account, password, role) ,以用户名和密码来生成私钥。这个是固定算法,生成的结果会一致。然后,再用这个私钥登录就可以啰。

具体代码如下,有兴趣的朋友可以参考下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
steem-login(){
//用私钥或密码登录
this.isLoading = true
let account = this.username
this.steem.api.getAccounts([account], (err, result) => {
if(result.length === 0){
this.isLoading = false
alert("用户不存在!")
}else{
//用户存在
let pubWif = result[0].posting.key_auths[0][0]
let privWif = this.password
let isvalid
//判断是私钥还是密码
let res = this.steem.auth.isWif(privWif)
if(res === true){
//是私钥
console.log(456, res, '是私钥!')
try{
isvalid = this.steem.auth.wifIsValid(privWif, pubWif)
}
catch(e){
isvalid = 'false'
}
if(isvalid === true){
this.isLoading = false
console.log(' Welcome.', account)
this.$store.commit('saveUser', {username: account, password: privWif})
this.$router.push({path:'/'})
}else{
this.isLoading = false
alert(`错误!请检查用户名和发贴密钥!`)
}
}else{
//是密码
console.log(458, res, '是密码')
//获取发贴私钥
let role = ["posting"]
let Wif = this.steem.auth.toWif(account, privWif, role)
try{
isvalid = this.steem.auth.wifIsValid(Wif, pubWif)
}
catch(e){
isvalid = 'false'
}
if(isvalid === true){
this.isLoading = false
console.log(' Welcome.', account)
this.$store.commit('saveUser', {username: account, password: Wif})
this.$router.push({path:'/'})
}else{
this.isLoading = false
alert(`错误!请检查用户名和密码!`)
}
}
}
})
}

This page is synchronized from the post: ‘SteemJs登录的逻辑 / 网络研习社#57’

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×