From c87a6c170632f1d69ecd273538dfec9b15e2c53d Mon Sep 17 00:00:00 2001 From: cebgcontract <99630598+cebgcontract@users.noreply.github.com> Date: Wed, 22 Jun 2022 15:29:26 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0add=20account=E7=9A=84?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assets/comp/wallet/scripts/JCWallet.ts | 29 ++++++++++++++------ assets/comp/wallet/scripts/data/DataModel.ts | 3 +- assets/comp/wallet/scripts/ui/MenuPanel.ts | 8 ++++-- assets/comp/wallet/scripts/ui/TokenTab.ts | 3 +- assets/comp/wallet/scripts/ui/WalletInfo.ts | 17 ++++++++---- assets/scenes/main.fire | 4 ++- 6 files changed, 43 insertions(+), 21 deletions(-) diff --git a/assets/comp/wallet/scripts/JCWallet.ts b/assets/comp/wallet/scripts/JCWallet.ts index 346f664..1050cb7 100644 --- a/assets/comp/wallet/scripts/JCWallet.ts +++ b/assets/comp/wallet/scripts/JCWallet.ts @@ -4,7 +4,7 @@ import sth = require("./lib/ethSigUtil"); import('./lib/fetch'); import { ZError } from "./common/ZError"; import { AllChains } from "./data/allchain"; -import { createWalletEvents, WALLET_CHAIN_CHANGE, WALLET_TOKEN_TYPE_CHANGE } from "./common/WalletEvent"; +import { createWalletEvents, WALLET_ACCOUNT_CHANGE, WALLET_CHAIN_CHANGE, WALLET_TOKEN_TYPE_CHANGE } from "./common/WalletEvent"; import { ERC20Standard } from "./standards/ERC20Standard"; import { ERC721Standard } from "./standards/ERC721Standard"; import { IAccount, initAccount } from "./data/DataModel"; @@ -35,10 +35,10 @@ export interface IChainData { @singleton export default class JCWallet { web3: Web3 = null - wallet: any = null - password: string = '111111' - chainSet: Set = new Set() - chainMap: Map = new Map() + private wallet: any = null + private password: string = '111111' + private chainSet: Set = new Set() + private chainMap: Map = new Map() private _currentChain: IChainData public erc20Standard: ERC20Standard public erc721Standard: ERC721Standard @@ -46,6 +46,7 @@ export default class JCWallet { private dataManage = new DataManage() public data: IAccount[] = [] public iconType = 'jazz' + private accountIndex = 0 constructor() { // this.web3 = new Web3('https://rpc-mainnet.kcc.network') @@ -89,8 +90,10 @@ export default class JCWallet { if (data) { return data } - data = initAccount(address, chain) + let accountName = `Account${this.wallet.length}` + data = initAccount(address, chain, accountName) this.data.push(data) + this.dataManage.saveData(this.data) return data } @@ -126,14 +129,22 @@ export default class JCWallet { } - public currentAccount() { - return this.wallet[0]; + public currentAccount(): IAccount { + return this.wallet[this.accountIndex]; } public accounts() { } + public createAccount() { + let account = this.web3.eth.accounts.create() + this.wallet.add(account) + this.wallet.save(this.password) + this.accountIndex = this.wallet.length - 1 + this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE) + } + public importAccount(privateKey: string) { } @@ -148,7 +159,7 @@ export default class JCWallet { public async getBalance(address?: string) { console.log('get balance with address: ', address); if (!address) { - let accountData = this.wallet[0] + let accountData = this.wallet[this.accountIndex] if (!accountData) { throw new ZError(10, 'no account found') } diff --git a/assets/comp/wallet/scripts/data/DataModel.ts b/assets/comp/wallet/scripts/data/DataModel.ts index 52e5e4c..1375ad6 100644 --- a/assets/comp/wallet/scripts/data/DataModel.ts +++ b/assets/comp/wallet/scripts/data/DataModel.ts @@ -37,10 +37,11 @@ export interface IAccount { chips: INFT[] } -export function initAccount(address: string, chain: number): IAccount { +export function initAccount(address: string, chain: number, nickname: string): IAccount { let data: IAccount = { address, chain, + nickname, tokens: [], heros: [], weapons: [], diff --git a/assets/comp/wallet/scripts/ui/MenuPanel.ts b/assets/comp/wallet/scripts/ui/MenuPanel.ts index 1980a4b..1704313 100644 --- a/assets/comp/wallet/scripts/ui/MenuPanel.ts +++ b/assets/comp/wallet/scripts/ui/MenuPanel.ts @@ -1,10 +1,11 @@ import { ZError } from "../common/ZError"; +import WalletBase from "./WallerBase"; const {ccclass, property} = cc._decorator; @ccclass -export default class MenuPanel extends cc.Component { +export default class MenuPanel extends WalletBase { @property(cc.Prefab) btnPreb: cc.Prefab = null; @@ -32,6 +33,7 @@ export default class MenuPanel extends cc.Component { // onLoad () {} start () { + super.start() this.layoutBtns() } @@ -64,8 +66,8 @@ export default class MenuPanel extends cc.Component { } onAddAccount() { - console.log(this.inited) - console.log('onAddAccount: ') + this.wallet.createAccount() + this.onCloseClick() } onImportAccount() { console.log('onImportAccount: ') diff --git a/assets/comp/wallet/scripts/ui/TokenTab.ts b/assets/comp/wallet/scripts/ui/TokenTab.ts index c823f8c..bbec4c8 100644 --- a/assets/comp/wallet/scripts/ui/TokenTab.ts +++ b/assets/comp/wallet/scripts/ui/TokenTab.ts @@ -1,4 +1,4 @@ -import { WALLET_CHAIN_CHANGE } from "../common/WalletEvent"; +import { WALLET_ACCOUNT_CHANGE, WALLET_CHAIN_CHANGE } from "../common/WalletEvent"; import { DEFAULT_NFT_TYPES } from "../config/chain_config"; import ButtonGroup, { BTN_SELECT_INDEX_CHANGE } from "./ButtonGroup"; import TextBtn from "./comp/TextBtn"; @@ -31,6 +31,7 @@ export default class TokenTab extends WalletBase { this.onTokenTypeChange.bind(this)) this.updateAllBtn(); this.wallet.mainHandlers.on(WALLET_CHAIN_CHANGE, this.updateAllBtn.bind(this)) + this.wallet.mainHandlers.on(WALLET_ACCOUNT_CHANGE, this.updateAllBtn.bind(this)) } onDestroy() { diff --git a/assets/comp/wallet/scripts/ui/WalletInfo.ts b/assets/comp/wallet/scripts/ui/WalletInfo.ts index 323a2c5..b852d46 100644 --- a/assets/comp/wallet/scripts/ui/WalletInfo.ts +++ b/assets/comp/wallet/scripts/ui/WalletInfo.ts @@ -1,10 +1,6 @@ -import { formatPrice } from "../common/chain.util"; -import { WALLET_CHAIN_CHANGE, WALLET_SHOW_QR } from "../common/WalletEvent"; -import { Bind } from "../decorator/AutoUpdateUI"; -import JCWallet, { IChainData } from "../JCWallet"; +import { WALLET_ACCOUNT_CHANGE, WALLET_CHAIN_CHANGE, WALLET_SHOW_QR } from "../common/WalletEvent"; +import { IChainData } from "../JCWallet"; import { renderFromTokenMinimalUnit } from "../util/number.util"; -import HashIcon from "./comp/HashIcon"; -import JazzIcon from "./comp/JazzIcon"; import WalletBase from "./WallerBase"; @@ -19,6 +15,9 @@ export default class WalletInfo extends WalletBase { @property(cc.Label) balanceLabel: cc.Label = null; + @property(cc.Label) + nameLabel: cc.Label = null; + @property(cc.Node) avatar: cc.Node = null; @@ -36,11 +35,13 @@ export default class WalletInfo extends WalletBase { this.updateUI() this.updateBalance() this.wallet.mainHandlers.on(WALLET_CHAIN_CHANGE, this.chainChange.bind(this)) + this.wallet.mainHandlers.on(WALLET_ACCOUNT_CHANGE, this.chainChange.bind(this)) } // update (dt) {} updateUI(): void { super.updateUI() + this.nameLabel.string = this.wallet.currentAccountData.nickname || 'Account' this.addressLabel.string = this.formatAddress() this.balanceLabel.string = this.formatMoney() if (this.wallet.iconType === 'jazz') { @@ -86,6 +87,10 @@ export default class WalletInfo extends WalletBase { chainChange(data: IChainData) { this.updateBalance() } + onAccountChange() { + this.accountId = this.wallet.currentAccount().address + this.updateBalance() + } showReceiveNode() { const address = this.wallet.currentAccount().address const chainId = this.wallet.currentChain.id diff --git a/assets/scenes/main.fire b/assets/scenes/main.fire index 8c22f16..95c9da3 100644 --- a/assets/scenes/main.fire +++ b/assets/scenes/main.fire @@ -7872,10 +7872,12 @@ "balanceLabel": { "__id__": 173 }, + "nameLabel": { + "__id__": 179 + }, "avatar": { "__id__": 146 }, - "parentNode": null, "_id": "8bO0XBvlFDPJ87FOgdOxUH" }, {