Published: 07 Sept 2020 › Updated: 07 Sept 2020
keychain在前端中的使用 / 网络研习社#69
https://github.com/MattyIce/steem-keychain
较之于MataMask在以太坊中的应用,Keychain就是在Steem中的应用。这两者都是去中心化的钱包,嵌入到浏览器中,帮你存储私钥。
最近因为一个项目的原因,大家也一致认为还是使用keychain比较好,所以花了些力气引用到前端中了。
keychain钱包是长这样的,设计方面有很大改进的余地。
keychain在前端中使用也就转帐,代理这些。把这些总结下,如下:
//等待服务器注入steem_keychain
let waitForGlobal = async () =>{
if(window.steem_keychain) {
// Steem Keychain extension installed...
console.log(123, "steem-keychain", window.steem_keychain)
} else {
// Steem Keychain extension not installed...
console.log(444, "未安装steem-keychain!")
let link = 'Google Chrome: https://chrome.google.com/webstore/detail/steem-keychain/lkcjlnjfpbikmcmbachjpdbijejflpcm\n\n' +
'Firefox: https://addons.mozilla.org/en-US/firefox/addon/steem-keychain'
alert("请安装steem-keychain钱包!\n\n"+link)
}
}
setTimeout(async () => {
await waitForGlobal()
}, 500)
//登录
login(){
let username = this.username
let message = `dice_login-${Math.floor(100000000 + Math.random() * 900000000)}`
let key_type = 'Posting'
steem_keychain.requestSignBuffer(username, message, key_type, response => {
console.log(136, response)
if (response.success === true) {
// If user is already logged in
console.log(' Welcome.', username)
this.$store.commit('saveUser', {username: username, addr: this.addr})
this.$router.push({path:'/wallet'})
} else {
// Verification failed
alert("Steem帐户登录错误!\n"+response.message)
}
})
},
//登录成功结果
data:
key: "posting"
message: "dice_login-785467156"
method: "Posting"
request_id: 5
type: "signBuffer"
username: "timool"
__proto__: Object
error: null
message: "讯息签署已成功。"
publicKey: "STM6Cdxxxxxxxxe6TC4BL"
request_id: 5
result: "1f0ddxxxxxxxxf9"
success: true
注:res.success === true 可以做为判断结果。
//转帐
async steemTransfer() {
try{
this.isLoading = true
this.exchangeFlag = false
//steem转帐
let addr = this.$store.state.addr
let from = this.$store.state.username
let to = process.env.VUE_APP_STEEM
let enforce = process.env.VUE_APP_STEEM
let ss = parseFloat(this.steemvalue).toFixed(3)
let amount = ss
let currency = 'STEEM'
let memo = addr+" +"+ss+' TSTEEM'
function steemTransfer(){
return new Promise(resolve => {
steem_keychain.requestTransfer(from, to, amount, memo, currency, function(response) {
console.log(123654, response)
resolve(response)
},enforce)
})
}
let res = await steemTransfer()
if(res.success === true){
//转帐成功
console.log(123, "转帐成功")
this.flag = false
await this.getBalance()
await this.getSteemStates()
this.steemvalue = ''
this.isLoading = false
this.flag = true
}else{
this.steemvalue = ''
this.isLoading = false
this.flag = true
alert("转帐错误!\n"+res.message)
}
}
catch(e){
this.isLoading = false
alert("错误\n" + e)
}
},
//代理
async delegate(){
try {
this.flag = false
this.isLoading = true
let username = this.$store.state.username
let addr = this.$store.state.addr
//steem代理
let delegator = this.$store.state.username
let delegatee = process.env.VUE_APP_STEEM
let b = this.delegatevalue * this.spToVests
let amount = b.toFixed(6)
let unit = 'VESTS'
function steemDelegate(){
return new Promise(resolve => {
steem_keychain.requestDelegation(delegator, delegatee, amount, unit, function(response) {
console.log(369, response)
resolve(response)
})
})
}
let res = await steemDelegate()
if(res.success === true) {
//代理成功
//直接刷新当前页面
this.$router.go(0)
}else{
this.delegatevalue = ''
this.isLoading = false
this.flag = true
alert("代理错误!\n"+res.message)
}
}
catch(e){
this.isLoading = false
alert("错误\n" + e)
}
},
这是几个常见的函数,还有几个就不多举例了,使用方法都差不多。
Leave keychain在前端中的使用 / 网络研习社#69 to:
Read more #cn posts
Best Posts From lemooljiang
We have not curated any of lemooljiang's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From lemooljiang
- DeepTutor:AI个性化辅导平台 / ai #57
- ComfyUI:可编程和自动化的PS / ai #56
- 将Neo4j封装进Agent / ai #55
- 闲鱼省钱大法:极限0.5折!
- 对Neo4j批量导入结构化数据 / ai #54
- langchain对neo4j进行交互,写入和查询数据 / ai #53
- 知识图谱和图数据库Neo4j的作用和使用场景 / ai #52
- SpaceX、美股和比特币
- 又是币圈血洗日!
- 链股合流,当币安也开始卖股票啰
- 天涯社区重启,爷青回?
- 诺基亚在AI时代重新杀回来啦
- 《2049:未来10000天的可能》书中的镜像世界为什么不叫元宇宙
- 今天适合吃披萨
- 开通即被封,别再给Claude送人头了!
- 两根内存条引发的“官司”
- AI·Joe V12更新, 更新GPT-5.5和DeepSeek V4 等
- 中医进校园活动反响热烈
- 猪价低伤农,猪价贵伤民
- Solidity开发指南