增加钱包登录的签名认证

This commit is contained in:
cebgcontract 2022-02-22 16:23:00 +08:00
parent d88447c5cc
commit 9401c96379
4 changed files with 84 additions and 47 deletions

View File

@ -20,36 +20,36 @@ export const defaultUser: IUser = {
avatar: '',
account: ''
}
export const getUserInfo = (data: any) =>
request({
url: '/api/user/info',
method: 'post',
data
})
// export const getUserInfo = (data: any) =>
// request({
// url: '/api/user/info',
// method: 'post',
// data
// })
export const login = (data: any) =>
request({
url: '/api/user/login',
method: 'post',
data
url: '/webapp/index.php?c=Market&a=auth',
method: 'get',
params: data
})
export const logout = () =>
request({
url: '/api/user/logout',
method: 'post'
})
// export const logout = () =>
// request({
// url: '/api/user/logout',
// method: 'post'
// })
//
// export const changePass = (params: any) =>
// request({
// url: '/api/user/passwd',
// method: 'post',
// data: params
// })
export const changePass = (params: any) =>
export const getNonce = (params: any) =>
request({
url: '/api/user/passwd',
method: 'post',
data: params
})
export const changeInfo = (params: any) =>
request({
url: '/api/user/update_info',
method: 'post',
data: params
url: '/webapp/index.php?c=Market&a=getNonce',
method: 'get',
params: params
})

View File

View File

@ -210,11 +210,8 @@ export class BlockChain {
price: price,
salt: nonce
}
const signCfg = {
name: 'BEBoxMall',
version: '1',
contract: MALL_ADDRESS,
signer: buyerAddress,
const netId = await this.web3.eth.getChainId()
const signObj = {
types: {
EIP712Domain: EIP721_DOMAIN_DATA,
set: [
@ -223,9 +220,17 @@ export class BlockChain {
{ name: 'price', type: 'uint256' },
{ name: 'salt', type: 'uint256' }
]
}
},
primaryType: 'set',
domain: {
name: 'BEBoxMall',
version: '1',
chainId: netId,
verifyingContract: MALL_ADDRESS
},
message: signMsg
}
const signature = await this.signData(signCfg, signMsg)
const signature = await this.signData(signObj, buyerAddress)
return { nonce, signature }
}
@ -241,21 +246,9 @@ export class BlockChain {
return this.web3.utils.toWei(priceStr)
}
public async signData(cfg: any, msg: any) {
const netId = await this.web3.eth.getChainId()
const msgParams = JSON.stringify({
types: cfg.types,
// make sure to replace verifyingContract with address of deployed contract
primaryType: 'set',
domain: {
name: cfg.name,
version: cfg.version,
chainId: netId,
verifyingContract: cfg.contract
},
message: msg
})
const from = cfg.signer
public async signData(signObj: any, signer: string) {
const msgParams = JSON.stringify(signObj)
const from = signer
console.log('clicked, sending personal sign req', 'from', from, msgParams)
const params = [from, msgParams]
const result: any = await this.sendCmd({

44
src/utils/login.ts Normal file
View File

@ -0,0 +1,44 @@
import { getNonce, login } from '@/api/User'
const EIP721_DOMAIN_DATA = [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' }
]
export const loginWithSign = async(bcInstance: any, account: string, chainId: string | number) => {
chainId += ''
const preRequest: any = await getNonce({ account, net_id: chainId })
console.log(`success get nonce: ${preRequest.nonce}`)
const tips = 'This signature is only used for verify your account'
const signMsg = {
tips,
nonce: preRequest.nonce + ''
}
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 bcInstance.signData(signObj, account)
const authData = {
account,
nonce: preRequest.nonce,
signature,
tips,
net_id: chainId
}
console.log('login data: ', authData)
const res = await login(authData)
console.log(res)
}