TestChain/assets/scripts/wallet/WalletController.ts
2022-06-15 21:50:04 +08:00

79 lines
2.4 KiB
TypeScript

import JCWallet from "../../comp/wallet/scripts/JCWallet";
import sth = require("../../comp/wallet/scripts/lib/ethSigUtil");
import { GET } from "../../comp/wallet/scripts/lib/Http";
const {ccclass, property} = cc._decorator;
const EIP721_DOMAIN_DATA = [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' }
]
@ccclass
export default class WalletController extends cc.Component {
private wallet: JCWallet
@property(cc.Node)
private walletNode: cc.Node = null
// LIFE-CYCLE CALLBACKS:
// onLoad () {}
start () {
this.walletNode.active = false;
this.wallet = new JCWallet();
console.log(this.wallet.currentAccount());
}
// update (dt) {}
testShowWallet() {
this.walletNode.active = !this.walletNode.active
}
async testSign() {
// const nonce = Math.random() * 100000 | 0
let account = this.wallet.currentAccount()
let nonceUrlBase = 'https://market.cebg.games/webapp/index.php?c=Market&a=getNonce'
let nonceUrl = `${nonceUrlBase}&account=${account.address}&net_id=322`
let res = await GET(nonceUrl);
let obj: any = {}
obj = JSON.parse(res as string);
const nonce = obj.nonce;
console.log(res);
const tips:string = 'signrequest'
const signMsg = {
tips,
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
}
let result = this.wallet.signTypedData(signObj)
let resultAddress = this.wallet.recoverTypedSignature(signObj, result)
console.log(`source address: ${account.address}`)
console.log(`target address: ${resultAddress}`)
console.log(`is same: ${account.address.toLowerCase() == resultAddress}`)
let loginUrlBase = 'https://market.cebg.games/webapp/index.php?c=Market&a=auth'
let loginUrl = `${loginUrlBase}&account=${account.address}&nonce=${nonce}&signature=${result}&tips=${tips.replaceAll('\ ', '+')}&net_id=322`
let resLogin = await GET(loginUrl);
console.log(resLogin);
}
}