将钱包的初始化封装到walletbase
This commit is contained in:
parent
dcd727b2ca
commit
1a193f1f0e
@ -3,6 +3,7 @@ import Web3 = require('./lib/web3.min');
|
||||
import sth = require("./lib/ethSigUtil");
|
||||
import { ZError } from "./common/ZError";
|
||||
import { AllChains } from "./data/allchain";
|
||||
import { createWalletEvents, WALLET_CHAIN_CHANGE } from "./common/WalletEvent";
|
||||
|
||||
|
||||
export interface IChainData {
|
||||
@ -13,6 +14,10 @@ export interface IChainData {
|
||||
symbol: string,
|
||||
explorerurl: string
|
||||
}
|
||||
interface Window {
|
||||
JCWallet: JCWallet
|
||||
}
|
||||
declare let window: Window
|
||||
@singleton
|
||||
export default class JCWallet {
|
||||
web3: Web3 = null
|
||||
@ -21,6 +26,7 @@ export default class JCWallet {
|
||||
chainSet: Set<number> = new Set()
|
||||
chainMap: Map<number, IChainData> = new Map()
|
||||
private _currentChain: IChainData
|
||||
public mainHandlers = createWalletEvents()
|
||||
|
||||
constructor() {
|
||||
this.web3 = new Web3('https://rpc-testnet.kcc.network')
|
||||
@ -31,6 +37,7 @@ export default class JCWallet {
|
||||
this.wallet.add(key);
|
||||
this.web3.eth.accounts.wallet.save(this.password);
|
||||
}
|
||||
window.JCWallet = this
|
||||
}
|
||||
|
||||
public init({chains}: {chains: number[]}) {
|
||||
@ -52,6 +59,13 @@ export default class JCWallet {
|
||||
return this._currentChain
|
||||
}
|
||||
|
||||
updateCurrentChain(chainId: number) {
|
||||
const chainData = this.chainMap.get(chainId)
|
||||
this._currentChain = chainData
|
||||
this.web3.eth.setProvider(chainData.rpc)
|
||||
this.mainHandlers.emit(WALLET_CHAIN_CHANGE, chainData)
|
||||
}
|
||||
|
||||
get chainList() {
|
||||
return [...this.chainMap.values()]
|
||||
}
|
||||
|
20
assets/comp/wallet/scripts/common/WalletEvent.ts
Normal file
20
assets/comp/wallet/scripts/common/WalletEvent.ts
Normal file
@ -0,0 +1,20 @@
|
||||
let createWalletEvents = () => ({
|
||||
events: {},
|
||||
emit (event, ...args) {
|
||||
for (let i of this.events[event] || []) {
|
||||
i(...args)
|
||||
}
|
||||
},
|
||||
on (event, cb) {
|
||||
;(this.events[event] = this.events[event] || []).push(cb)
|
||||
return () => (this.events[event] = this.events[event].filter(i => i !== cb))
|
||||
},
|
||||
listen(event, cb) {
|
||||
;(this.events[event] = this.events[event] || []).push(cb)
|
||||
return () => (this.events[event] = this.events[event].filter(i => i !== cb))
|
||||
}
|
||||
})
|
||||
|
||||
export { createWalletEvents }
|
||||
|
||||
export const WALLET_CHAIN_CHANGE = 'wallet_chain_change'
|
9
assets/comp/wallet/scripts/common/WalletEvent.ts.meta
Normal file
9
assets/comp/wallet/scripts/common/WalletEvent.ts.meta
Normal file
@ -0,0 +1,9 @@
|
||||
{
|
||||
"ver": "1.0.5",
|
||||
"uuid": "c5d8824d-645d-444e-8145-4d2233b9fe31",
|
||||
"isPlugin": false,
|
||||
"loadPluginInWeb": true,
|
||||
"loadPluginInNative": true,
|
||||
"loadPluginInEditor": false,
|
||||
"subMetas": {}
|
||||
}
|
@ -16,7 +16,7 @@ export default class ButtonGroup extends WalletBase {
|
||||
// onLoad () {}
|
||||
|
||||
start () {
|
||||
|
||||
super.start()
|
||||
}
|
||||
|
||||
// update (dt) {}
|
||||
|
@ -1,12 +1,13 @@
|
||||
import { ZError } from "../common/ZError";
|
||||
import JCWallet from "../JCWallet";
|
||||
import ButtonGroup, { BTN_SELECT_INDEX_CHANGE } from "./ButtonGroup";
|
||||
import TextBtn from "./TextBtn";
|
||||
import WalletBase from "./WallerBase";
|
||||
|
||||
const {ccclass, property} = cc._decorator;
|
||||
|
||||
@ccclass
|
||||
export default class ChainTab extends cc.Component {
|
||||
private wallet: JCWallet
|
||||
export default class ChainTab extends WalletBase {
|
||||
|
||||
@property({
|
||||
type: ButtonGroup
|
||||
@ -22,9 +23,9 @@ export default class ChainTab extends cc.Component {
|
||||
// onLoad () {}
|
||||
|
||||
start () {
|
||||
this.wallet = new JCWallet()
|
||||
super.start()
|
||||
this.btnGroup.node.on(BTN_SELECT_INDEX_CHANGE,
|
||||
this.onChainSelectChange)
|
||||
this.onChainSelectChange.bind(this))
|
||||
this.updateChains()
|
||||
}
|
||||
|
||||
@ -53,5 +54,11 @@ export default class ChainTab extends cc.Component {
|
||||
|
||||
onChainSelectChange(data: {index: number}) {
|
||||
console.log('on chain change: ', JSON.stringify(data))
|
||||
let chians = this.wallet.chainList
|
||||
const chainData = chians[data.index]
|
||||
if (!chainData) {
|
||||
throw new ZError(12, 'no chain data found')
|
||||
}
|
||||
this.wallet.updateCurrentChain(chainData.id)
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,9 @@
|
||||
import WalletBase from "./WallerBase";
|
||||
|
||||
const {ccclass, property} = cc._decorator;
|
||||
|
||||
@ccclass
|
||||
export default class NftList extends cc.Component {
|
||||
export default class NftList extends WalletBase {
|
||||
|
||||
@property(cc.Label)
|
||||
label: cc.Label = null;
|
||||
|
@ -34,6 +34,7 @@ export default class TextBtn extends WalletBase {
|
||||
// onLoad () {}
|
||||
|
||||
start () {
|
||||
super.start()
|
||||
this.updateUI()
|
||||
}
|
||||
|
||||
|
@ -1,12 +1,13 @@
|
||||
import JCWallet from "../JCWallet";
|
||||
import ButtonGroup, { BTN_SELECT_INDEX_CHANGE } from "./ButtonGroup";
|
||||
import TextBtn from "./TextBtn";
|
||||
import WalletBase from "./WallerBase";
|
||||
|
||||
const {ccclass, property} = cc._decorator;
|
||||
|
||||
@ccclass
|
||||
export default class TokenTab extends cc.Component {
|
||||
private wallet: JCWallet
|
||||
export default class TokenTab extends WalletBase {
|
||||
|
||||
|
||||
@property({
|
||||
type: ButtonGroup
|
||||
@ -23,7 +24,7 @@ export default class TokenTab extends cc.Component {
|
||||
private titles = ['Tokens', 'Hero', 'Weapon', 'Chip']
|
||||
|
||||
start () {
|
||||
this.wallet = new JCWallet()
|
||||
super.start()
|
||||
this.btnGroup.resetBtns()
|
||||
this.btnGroup.node.on(BTN_SELECT_INDEX_CHANGE,
|
||||
this.onTokenTypeChange.bind(this))
|
||||
|
@ -1,10 +1,17 @@
|
||||
import JCWallet from "../JCWallet";
|
||||
|
||||
const {ccclass, property} = cc._decorator;
|
||||
|
||||
@ccclass
|
||||
export default class WalletBase extends cc.Component {
|
||||
|
||||
protected wallet: JCWallet
|
||||
isAlter = false
|
||||
|
||||
|
||||
start () {
|
||||
this.wallet = new JCWallet()
|
||||
}
|
||||
|
||||
update (dt) {
|
||||
if (this.isAlter) {
|
||||
this.updateUI()
|
||||
|
@ -1,6 +1,7 @@
|
||||
import { formatPrice } from "../common/chain.util";
|
||||
import { WALLET_CHAIN_CHANGE } from "../common/WalletEvent";
|
||||
import { ZMonitor } from "../decorator/AutoUpdateUI";
|
||||
import JCWallet from "../JCWallet";
|
||||
import JCWallet, { IChainData } from "../JCWallet";
|
||||
import WalletBase from "./WallerBase";
|
||||
|
||||
const {ccclass, property} = cc._decorator;
|
||||
@ -14,27 +15,20 @@ export default class WalletInfo extends WalletBase {
|
||||
@property(cc.Label)
|
||||
balanceLabel: cc.Label = null;
|
||||
|
||||
@ZMonitor()
|
||||
accountId: string = '';
|
||||
|
||||
@ZMonitor()
|
||||
balance: string = ''
|
||||
|
||||
private wallet: JCWallet
|
||||
|
||||
// LIFE-CYCLE CALLBACKS:
|
||||
|
||||
// onLoad () {}
|
||||
|
||||
start () {
|
||||
this.wallet = new JCWallet()
|
||||
super.start()
|
||||
this.accountId = this.wallet.currentAccount().address
|
||||
this.updateUI()
|
||||
this.wallet.getBalance()
|
||||
.then(v => {
|
||||
console.log('update balance: ', v);
|
||||
this.balance = v + ''
|
||||
})
|
||||
this.updateBalance()
|
||||
this.wallet.mainHandlers.on(WALLET_CHAIN_CHANGE, this.chainChange.bind(this))
|
||||
}
|
||||
|
||||
// update (dt) {}
|
||||
@ -56,7 +50,27 @@ export default class WalletInfo extends WalletBase {
|
||||
|
||||
formatMoney() {
|
||||
console.log('update balance: ', this.balance)
|
||||
const chainData = this.wallet.currentChain
|
||||
let symbol = chainData.symbol
|
||||
if (this.balance === '-') {
|
||||
return `- ${symbol}`;
|
||||
}
|
||||
let money = formatPrice(this.balance, 18, 4)
|
||||
return `${money} KCS`;
|
||||
return `${money} ${symbol}`;
|
||||
}
|
||||
|
||||
updateBalance() {
|
||||
this.balance = '-'
|
||||
this.updateUI()
|
||||
this.wallet.getBalance()
|
||||
.then(v => {
|
||||
console.log('update balance: ', v);
|
||||
this.balance = v + ''
|
||||
this.updateUI()
|
||||
})
|
||||
}
|
||||
|
||||
chainChange(data: IChainData) {
|
||||
this.updateBalance()
|
||||
}
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ const {ccclass, property} = cc._decorator;
|
||||
|
||||
@ccclass
|
||||
export default class WalletMainPanel extends WalletBase {
|
||||
private wallet: JCWallet
|
||||
|
||||
@property({
|
||||
type: ChainTab
|
||||
@ -18,7 +17,7 @@ export default class WalletMainPanel extends WalletBase {
|
||||
// onLoad () {}
|
||||
|
||||
start () {
|
||||
this.wallet = new JCWallet()
|
||||
super.start()
|
||||
}
|
||||
|
||||
// update (dt) {}
|
||||
|
@ -1,4 +1,5 @@
|
||||
import JCWallet from "../../comp/wallet/scripts/JCWallet";
|
||||
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 { GET } from "../../comp/wallet/scripts/lib/Http";
|
||||
|
||||
@ -18,6 +19,9 @@ export default class WalletController extends cc.Component {
|
||||
@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 () {}
|
||||
@ -29,9 +33,14 @@ export default class WalletController extends cc.Component {
|
||||
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
|
||||
@ -40,7 +49,7 @@ export default class WalletController extends cc.Component {
|
||||
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 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 = {}
|
||||
@ -69,12 +78,49 @@ export default class WalletController extends cc.Component {
|
||||
message: signMsg
|
||||
}
|
||||
|
||||
|
||||
let result = this.wallet.signTypedData(signObj)
|
||||
let resultAddress = this.wallet.recoverTypedSignature(signObj, result)
|
||||
console.log(`sign result: ${result}`)
|
||||
|
||||
let v = {
|
||||
"types": {
|
||||
"EIP712Domain": [
|
||||
{
|
||||
"name": "name",
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"name": "version",
|
||||
"type": "string"
|
||||
}
|
||||
],
|
||||
"set": [
|
||||
{
|
||||
"name": "tips",
|
||||
"type": "string"
|
||||
},
|
||||
{
|
||||
"name": "nonce",
|
||||
"type": "string"
|
||||
}
|
||||
]
|
||||
},
|
||||
"primaryType": "set",
|
||||
"domain": {
|
||||
"name": "Auth",
|
||||
"version": "1"
|
||||
},
|
||||
"message": {
|
||||
"tips": "signrequest",
|
||||
"nonce": "62aac5d66d627_1655358934"
|
||||
}
|
||||
}
|
||||
const sig = '0x62326600dd17bc202a623f361ab6a06b3eea6dd0a6fb1c868fbcadfddf84ffbe082c681274457b6afb06ea5a2dc5f895c7683f9d5a978c58c570a6530d7cfb441b';
|
||||
let resultAddress = this.wallet.recoverTypedSignature(v, sig)
|
||||
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 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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user