TestChain/assets/scripts/wallet/WalletController.ts
2022-06-17 15:00:55 +08:00

114 lines
4.0 KiB
TypeScript

import { WALLET_CHAIN_CHANGE } from "../../comp/wallet/scripts/common/WalletEvent";
import JCWallet, { IChainData } from "../../comp/wallet/scripts/JCWallet";
import sth = require("../../comp/wallet/scripts/lib/ethSigUtil");
import('../../comp/wallet/scripts/lib/fetch');
import { GET } from "../../comp/wallet/scripts/lib/Http";
import { renderFromTokenMinimalUnit, renderFromWei } from "../../comp/wallet/scripts/util/number.util";
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
// private apiBase = 'https://market.cebg.games'
private apiBase = 'https://game2006api-test.kingsome.cn'
// LIFE-CYCLE CALLBACKS:
// onLoad () {}
start () {
this.walletNode.active = false;
this.wallet = new JCWallet();
let chains = CC_DEBUG ? [322, 97] : [321, 56]
this.wallet.init({
chains
})
this.wallet.mainHandlers.on(WALLET_CHAIN_CHANGE, this.chainChange.bind(this))
console.log(this.wallet.currentAccount());
}
chainChange(data: IChainData) {
}
// update (dt) {}
testShowWallet() {
this.walletNode.active = !this.walletNode.active
}
async testSign() {
// const nonce = Math.random() * 100000 | 0
let account = this.wallet.currentAccount()
let nonceUrlBase = this.apiBase + '/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)
console.log(`sign result: ${result}`)
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 = this.apiBase + '/webapp/index.php?c=Market&a=auth'
let loginUrl = `${loginUrlBase}&account=${account.address}&nonce=${nonce}&signature=${result}&tips=${tips.replace(/\ /g, '+')}&net_id=322`
let resLogin = await GET(loginUrl);
console.log(resLogin);
}
async testERC20() {
let symbol = await this.wallet.erc20Standard.getTokenSymbol('0x67f6a7BbE0da067A747C6b2bEdF8aBBF7D6f60dc')
console.log(symbol)
let decimals = await this.wallet.erc20Standard.getTokenDecimals('0x67f6a7BbE0da067A747C6b2bEdF8aBBF7D6f60dc')
console.log(decimals)
let result3 = await this.wallet.erc20Standard
.getBalanceOf('0x67f6a7BbE0da067A747C6b2bEdF8aBBF7D6f60dc',
'0x565edA4ef351EB78F03B8AfCb6dCF02E29cAD62e')
console.log(`balance: ${renderFromTokenMinimalUnit(result3, decimals, 4)}`)
console.log(`balance: ${renderFromTokenMinimalUnit('10000000000000', decimals, 6)}`)
console.log(`renderFromWei: ${renderFromWei('100000000000000')}`)
let resultAll = await this.wallet.erc20Standard.getDetails(
'0x67f6a7BbE0da067A747C6b2bEdF8aBBF7D6f60dc',
'0x565edA4ef351EB78F03B8AfCb6dCF02E29cAD62e'
)
console.log(resultAll)
}
}