163 lines
4.1 KiB
TypeScript
163 lines
4.1 KiB
TypeScript
import store from "@/views/home/store/index";
|
|
import Web3 from 'web3'
|
|
import { Message } from 'element-ui';
|
|
import axios from 'axios';
|
|
import {Http_getNonce,Http_login} from "@/utils/login-request"
|
|
// import Cookie from "@u/cookie";
|
|
interface Window {
|
|
ethereum: any
|
|
web3: any
|
|
celo: any
|
|
}
|
|
declare let window: Window
|
|
|
|
export default class ChainManager {
|
|
public provider:any
|
|
public web3:Web3
|
|
public chainId:number
|
|
public account:string
|
|
public nonce:string
|
|
|
|
private async login( account, chainId, nonce ) {
|
|
nonce += ''
|
|
// console.log(nonce);return
|
|
const tips = 'This signature is only used for verify your account'
|
|
const signMsg = {
|
|
tips,
|
|
nonce
|
|
}
|
|
const EIP721_DOMAIN_DATA = [
|
|
{ name: 'name', type: 'string' },
|
|
{ name: 'version', type: 'string' }
|
|
]
|
|
|
|
const signObj = {
|
|
types: {
|
|
EIP712Domain: EIP721_DOMAIN_DATA,
|
|
set: [
|
|
{ name: 'tips', type: 'string' },
|
|
{ name: 'nonce', type: 'string' }
|
|
]
|
|
},
|
|
primaryType: 'set',
|
|
domain: {
|
|
name: 'Auth',
|
|
version: '1'
|
|
},
|
|
message: signMsg
|
|
}
|
|
const signature = await this.signData(signObj, account)
|
|
const authData = {
|
|
account,
|
|
nonce,
|
|
signature,
|
|
tips,
|
|
net_id: chainId
|
|
}
|
|
// console.log('login data: ', authData)
|
|
const res = await Http_login(authData)
|
|
console.log(res)
|
|
// if (!res.errcode && res.token) {
|
|
// store.commit('set_token',res.token)
|
|
//
|
|
// }
|
|
}
|
|
public async checkNance() {
|
|
try {
|
|
let nonce = store.getters.nonce
|
|
// console.log(store.getters);return
|
|
if (!nonce) {
|
|
let params = {account:this.account, net_id:this.chainId}
|
|
const res = await Http_getNonce(params);
|
|
// store.commit('set_nonce',res.nonce)
|
|
// this.nonce = res.nonce
|
|
}
|
|
// console.log(this.account,this.chainId,this.nonce);return
|
|
this.login(this.account,this.chainId,this.nonce)
|
|
} catch (err) {
|
|
console.log(err)
|
|
Promise.reject(err)
|
|
}
|
|
}
|
|
public async connect(){
|
|
if (!this.hasMetamask()) {
|
|
Message({
|
|
message: '请先安装MetaMask插件',
|
|
type: 'error',
|
|
duration: 5 * 1000
|
|
})
|
|
return
|
|
}
|
|
this.provider = await this.connectMetaMask()
|
|
if (!this.provider) {
|
|
return
|
|
}
|
|
console.log(this.provider);return
|
|
this.web3 = new Web3(this.provider)
|
|
this.chainId = await this.web3.eth.getChainId()
|
|
const accounts = await this.web3.eth.getAccounts()
|
|
if (accounts && accounts.length > 0) {
|
|
this.account =accounts[0];
|
|
}
|
|
}
|
|
private async connectMetaMask() {
|
|
let provider = window.ethereum
|
|
try {
|
|
await provider.request({ method: 'eth_requestAccounts' })
|
|
} catch (error) {
|
|
if (error.code === -32002) {
|
|
throw new Error('MeatMask not login, Open MeatMask and login first')
|
|
} else {
|
|
throw new Error('User Rejected')
|
|
}
|
|
}
|
|
return provider
|
|
}
|
|
private verifyInjectedProvider(check) {
|
|
return window.ethereum
|
|
? window.ethereum[check]
|
|
: window.web3 &&
|
|
window.web3.currentProvider &&
|
|
window.web3.currentProvider[check]
|
|
}
|
|
private hasMetamask() {
|
|
if (typeof window.ethereum !== 'undefined') {
|
|
return true;
|
|
}else {
|
|
return false;
|
|
}
|
|
}
|
|
private async signData(signObj, signer) {
|
|
const msgParams = JSON.stringify(signObj)
|
|
const from = signer
|
|
// console.log('clicked, sending personal sign req', 'from', from, msgParams)
|
|
const params = [from, msgParams]
|
|
const result = await this.sendCmd({
|
|
method: 'eth_signTypedData_v4',
|
|
params,
|
|
from
|
|
})
|
|
console.log(result)
|
|
// return result.result
|
|
}
|
|
private async sendCmd({ method, params, from }) {
|
|
return new Promise((resolve, reject) => {
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
// @ts-ignore
|
|
this.web3.currentProvider.sendAsync({
|
|
method,
|
|
params,
|
|
from
|
|
}, async function(err, result) {
|
|
if (err) {
|
|
reject && reject(err)
|
|
return
|
|
}
|
|
resolve && resolve(result)
|
|
})
|
|
})
|
|
}
|
|
|
|
|
|
}
|