|
| 1 | +//扫描二维码登陆 |
| 2 | +/** |
| 3 | + * 导入钱包界面 |
| 4 | + */ |
| 5 | +<template> |
| 6 | +<div class="page" v-bind:class="{hidebackground: showScanner}"> |
| 7 | + <toolbar :title="$t(title)" :showbackicon="showbackicon"> |
| 8 | + <v-btn icon style="visibility: hidden;" slot="left-tool"> |
| 9 | + <v-icon class="back-icon"/> |
| 10 | + </v-btn> |
| 11 | + <div class="right" slot="right-tool"> |
| 12 | + <span class="toolbar-ico" @click="scan"> |
| 13 | + <v-icon class="font28" v-if="showScanner"></v-icon> |
| 14 | + <i v-else class="iconfont icon-erweima1 font28"></i> |
| 15 | + </span> |
| 16 | + </div> |
| 17 | + </toolbar> |
| 18 | + |
| 19 | + <q-r-scan |
| 20 | + @finish="qrfinish" |
| 21 | + @close="qrclose" |
| 22 | + :validator="qrvalidator" |
| 23 | + v-if="showScanner"></q-r-scan> |
| 24 | + |
| 25 | + <div class="content" v-if="!showScanner"> |
| 26 | + |
| 27 | + <div>是否登陆网站:{{domain}}?</div> |
| 28 | + <v-btn block @click="logon">{{$t('OK')}}</v-btn> |
| 29 | + </div> |
| 30 | +</div> |
| 31 | +</template> |
| 32 | + |
| 33 | +<script> |
| 34 | +import Toolbar from '@/components/Toolbar' |
| 35 | +import QRScan from '@/components/QRScan' |
| 36 | +import SecretKeyInput from '@/components/SecretKeyInput' |
| 37 | +import {importAccount,isValidSeed,fromMnemonic, validateMnemonic} from '@/api/account' |
| 38 | +import { getMnemonicFromData } from '@/api/qr' |
| 39 | +import { mapState, mapActions} from 'vuex' |
| 40 | +import { isEnglishMnemonic, isChineseMnemonic } from '../api/account'; |
| 41 | +
|
| 42 | +const axios = require('axios'); |
| 43 | +import { signToBase64, verifyByBase64 } from '@/api/keypair' |
| 44 | +
|
| 45 | +export default { |
| 46 | + data(){ |
| 47 | + return { |
| 48 | + title: 'Login', |
| 49 | + showbackicon: false, |
| 50 | + showScanner: true, |
| 51 | + url: null, |
| 52 | +
|
| 53 | + } |
| 54 | + }, |
| 55 | + computed:{ |
| 56 | + ...mapState({ |
| 57 | + account: state => state.accounts.selectedAccount, |
| 58 | + accountData: state => state.accounts.accountData, |
| 59 | + }), |
| 60 | + domain(){ |
| 61 | + if(!this.url)return; |
| 62 | + let key = '&origin_domain=' |
| 63 | + let index = this.url.indexOf(key) + key.length |
| 64 | + key = '&signature=' |
| 65 | + return decodeURIComponent(this.url.substring(index, this.url.indexOf(key))) |
| 66 | + } |
| 67 | + |
| 68 | + }, |
| 69 | + mounted () { |
| 70 | + }, |
| 71 | + methods: { |
| 72 | + ...mapActions({ |
| 73 | + }), |
| 74 | + |
| 75 | + goback(){ |
| 76 | + this.$router.back() |
| 77 | + }, |
| 78 | + // inputSeed(value){ |
| 79 | + // this.setNewSeed({seed:value}) |
| 80 | + // }, |
| 81 | + scan(){ |
| 82 | + //只能识别stargazer类似的格式数据 |
| 83 | + if(this.showScanner){ |
| 84 | + this.showScanner = false |
| 85 | + this.title = 'Login' |
| 86 | + }else{ |
| 87 | + this.showScanner = true |
| 88 | + this.title = 'Title.Scan' |
| 89 | + this.scanSuccess = false |
| 90 | + } |
| 91 | + }, |
| 92 | + qrvalidator(text){ |
| 93 | + if(text.startsWith('web+stellar:login')){ |
| 94 | + this.$toasted.show('---qrcode is right--') |
| 95 | + this.url = text; |
| 96 | + return true; |
| 97 | + } |
| 98 | + this.$toasted.error('-----qrtext valid false----') |
| 99 | + return false; |
| 100 | + }, |
| 101 | + qrfinish(result){ |
| 102 | + this.showScanner = false |
| 103 | + this.title = 'Login' |
| 104 | + //校验签名是否正确 |
| 105 | + let key = '&signature=' |
| 106 | + let text = this.url; |
| 107 | + let index = text.indexOf(key) |
| 108 | + let plainText = text.substring(0,index) |
| 109 | + let signature = text.substring(index+key.length, text.length) |
| 110 | + let accountid = 'GDFETGX4ZOZUDOGMYHW2IN2WXWQLJ5V7VRJIDW5GCYMZ4X5O6ULV5GZI'//后续从stellar.toml中获取 |
| 111 | + let chk = verifyByBase64(accountid, plainText, signature) |
| 112 | + if(chk){ |
| 113 | + this.$toasted.show(`checked true`) |
| 114 | + } |
| 115 | + this.$toasted.error(`checked fail.`) |
| 116 | + |
| 117 | + }, |
| 118 | + qrclose(){ |
| 119 | + //this.showScanner = false |
| 120 | + //this.title = 'Login' |
| 121 | + }, |
| 122 | + logon(){ |
| 123 | + //生成签名 |
| 124 | + let cbkey = '&callback=' |
| 125 | + let cbindex = this.url.indexOf(cbkey) |
| 126 | + let cb = this.url.substring(cbindex+cbkey.length, this.url.indexOf('&origin_domain=')) |
| 127 | + cb = decodeURIComponent(cb) |
| 128 | + let path = this.url.substring(0, cbindex) |
| 129 | + path += '&accountid='+this.account.address |
| 130 | + let signature = signToBase64(this.accountData.seed, path) |
| 131 | + // path += '&signature' + encodeURIComponent(signature) |
| 132 | + this.$toasted.show('cb='+cb) |
| 133 | + try{ |
| 134 | + axios.post(cb, { |
| 135 | + r: this.url.substring(this.url.indexOf('?r=')+3, cbindex), |
| 136 | + accountid: this.account.address, |
| 137 | + signature: encodeURIComponent(signature) |
| 138 | + }) |
| 139 | + .then(response=>{ |
| 140 | + this.$toasted.show('logon success') |
| 141 | + |
| 142 | + }) |
| 143 | + .catch(err=>{ |
| 144 | + console.error(err) |
| 145 | + this.$toasted.error('logon failed!'+err.message + ',' + err.response.data.error) |
| 146 | + }) |
| 147 | + |
| 148 | + }catch(err){ |
| 149 | + alert('发生错误:'+err.message) |
| 150 | + } |
| 151 | + |
| 152 | +
|
| 153 | + } |
| 154 | +
|
| 155 | + }, |
| 156 | + components: { |
| 157 | + Toolbar, |
| 158 | + QRScan, |
| 159 | + } |
| 160 | +} |
| 161 | +</script> |
| 162 | + |
| 163 | +<style lang="stylus" scoped> |
| 164 | +@require '../stylus/color.styl' |
| 165 | +
|
| 166 | +</style> |
0 commit comments