135 lines
4.6 KiB
TypeScript
135 lines
4.6 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, GET_JSON } 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]
|
|
let chains = [321]
|
|
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 obj: any = await GET_JSON(nonceUrl);
|
|
const nonce = obj.nonce;
|
|
console.log(obj);
|
|
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_JSON(loginUrl);
|
|
console.log(resLogin);
|
|
}
|
|
|
|
async testERC20() {
|
|
// let address = '0x67f6a7BbE0da067A747C6b2bEdF8aBBF7D6f60dc'
|
|
let address = '0xdb6D4bB22E2C12686Efff25a79EC78f9f078fe7D'
|
|
let symbol = await this.wallet.erc20Standard.getTokenSymbol(address)
|
|
console.log(symbol)
|
|
let decimals = await this.wallet.erc20Standard.getTokenDecimals(address)
|
|
console.log(decimals)
|
|
let result3 = await this.wallet.erc20Standard
|
|
.getBalanceOf(address,
|
|
'0x565edA4ef351EB78F03B8AfCb6dCF02E29cAD62e')
|
|
console.log(`balance: ${renderFromTokenMinimalUnit(result3, decimals, 4)}`)
|
|
let resultAll = await this.wallet.erc20Standard.getDetails(
|
|
address,
|
|
'0x565edA4ef351EB78F03B8AfCb6dCF02E29cAD62e'
|
|
)
|
|
console.log(resultAll)
|
|
}
|
|
|
|
async testSendMoney() {
|
|
let address = '0xdb6D4bB22E2C12686Efff25a79EC78f9f078fe7D'
|
|
let amount = 1
|
|
let from = '0x565edA4ef351EB78F03B8AfCb6dCF02E29cAD62e'
|
|
let to = '0x42448C6a38c08637218D8327b748F213fC2c0231'
|
|
let result = await this.wallet.erc20Standard.transfer({address, from, to, amount})
|
|
console.log(result)
|
|
}
|
|
|
|
async testGetHero() {
|
|
let address = '0x52917087cd4E48bDb5f336012E677f471f9E1C2D'
|
|
let info = await this.wallet.erc721Standard.getDetails(address, '', '101')
|
|
console.log(info)
|
|
}
|
|
|
|
async testSendEth() {
|
|
let amount = 0.1
|
|
let to = '0x42448C6a38c08637218D8327b748F213fC2c0231'
|
|
let result = await this.wallet.sendEth(to, amount)
|
|
console.log(result)
|
|
}
|
|
}
|