增加add account的功能

This commit is contained in:
cebgcontract 2022-06-22 15:29:26 +08:00
parent 3ea26dfbd7
commit c87a6c1706
6 changed files with 43 additions and 21 deletions

View File

@ -4,7 +4,7 @@ import sth = require("./lib/ethSigUtil");
import('./lib/fetch'); import('./lib/fetch');
import { ZError } from "./common/ZError"; import { ZError } from "./common/ZError";
import { AllChains } from "./data/allchain"; 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 { ERC20Standard } from "./standards/ERC20Standard";
import { ERC721Standard } from "./standards/ERC721Standard"; import { ERC721Standard } from "./standards/ERC721Standard";
import { IAccount, initAccount } from "./data/DataModel"; import { IAccount, initAccount } from "./data/DataModel";
@ -35,10 +35,10 @@ export interface IChainData {
@singleton @singleton
export default class JCWallet { export default class JCWallet {
web3: Web3 = null web3: Web3 = null
wallet: any = null private wallet: any = null
password: string = '111111' private password: string = '111111'
chainSet: Set<number> = new Set() private chainSet: Set<number> = new Set()
chainMap: Map<number, IChainData> = new Map() private chainMap: Map<number, IChainData> = new Map()
private _currentChain: IChainData private _currentChain: IChainData
public erc20Standard: ERC20Standard public erc20Standard: ERC20Standard
public erc721Standard: ERC721Standard public erc721Standard: ERC721Standard
@ -46,6 +46,7 @@ export default class JCWallet {
private dataManage = new DataManage() private dataManage = new DataManage()
public data: IAccount[] = [] public data: IAccount[] = []
public iconType = 'jazz' public iconType = 'jazz'
private accountIndex = 0
constructor() { constructor() {
// this.web3 = new Web3('https://rpc-mainnet.kcc.network') // this.web3 = new Web3('https://rpc-mainnet.kcc.network')
@ -89,8 +90,10 @@ export default class JCWallet {
if (data) { if (data) {
return data return data
} }
data = initAccount(address, chain) let accountName = `Account${this.wallet.length}`
data = initAccount(address, chain, accountName)
this.data.push(data) this.data.push(data)
this.dataManage.saveData(this.data)
return data return data
} }
@ -126,14 +129,22 @@ export default class JCWallet {
} }
public currentAccount() { public currentAccount(): IAccount {
return this.wallet[0]; return this.wallet[this.accountIndex];
} }
public accounts() { 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) { public importAccount(privateKey: string) {
} }
@ -148,7 +159,7 @@ export default class JCWallet {
public async getBalance(address?: string) { public async getBalance(address?: string) {
console.log('get balance with address: ', address); console.log('get balance with address: ', address);
if (!address) { if (!address) {
let accountData = this.wallet[0] let accountData = this.wallet[this.accountIndex]
if (!accountData) { if (!accountData) {
throw new ZError(10, 'no account found') throw new ZError(10, 'no account found')
} }

View File

@ -37,10 +37,11 @@ export interface IAccount {
chips: INFT[] chips: INFT[]
} }
export function initAccount(address: string, chain: number): IAccount { export function initAccount(address: string, chain: number, nickname: string): IAccount {
let data: IAccount = { let data: IAccount = {
address, address,
chain, chain,
nickname,
tokens: [], tokens: [],
heros: [], heros: [],
weapons: [], weapons: [],

View File

@ -1,10 +1,11 @@
import { ZError } from "../common/ZError"; import { ZError } from "../common/ZError";
import WalletBase from "./WallerBase";
const {ccclass, property} = cc._decorator; const {ccclass, property} = cc._decorator;
@ccclass @ccclass
export default class MenuPanel extends cc.Component { export default class MenuPanel extends WalletBase {
@property(cc.Prefab) @property(cc.Prefab)
btnPreb: cc.Prefab = null; btnPreb: cc.Prefab = null;
@ -32,6 +33,7 @@ export default class MenuPanel extends cc.Component {
// onLoad () {} // onLoad () {}
start () { start () {
super.start()
this.layoutBtns() this.layoutBtns()
} }
@ -64,8 +66,8 @@ export default class MenuPanel extends cc.Component {
} }
onAddAccount() { onAddAccount() {
console.log(this.inited) this.wallet.createAccount()
console.log('onAddAccount: ') this.onCloseClick()
} }
onImportAccount() { onImportAccount() {
console.log('onImportAccount: ') console.log('onImportAccount: ')

View File

@ -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 { DEFAULT_NFT_TYPES } from "../config/chain_config";
import ButtonGroup, { BTN_SELECT_INDEX_CHANGE } from "./ButtonGroup"; import ButtonGroup, { BTN_SELECT_INDEX_CHANGE } from "./ButtonGroup";
import TextBtn from "./comp/TextBtn"; import TextBtn from "./comp/TextBtn";
@ -31,6 +31,7 @@ export default class TokenTab extends WalletBase {
this.onTokenTypeChange.bind(this)) this.onTokenTypeChange.bind(this))
this.updateAllBtn(); this.updateAllBtn();
this.wallet.mainHandlers.on(WALLET_CHAIN_CHANGE, this.updateAllBtn.bind(this)) this.wallet.mainHandlers.on(WALLET_CHAIN_CHANGE, this.updateAllBtn.bind(this))
this.wallet.mainHandlers.on(WALLET_ACCOUNT_CHANGE, this.updateAllBtn.bind(this))
} }
onDestroy() { onDestroy() {

View File

@ -1,10 +1,6 @@
import { formatPrice } from "../common/chain.util"; import { WALLET_ACCOUNT_CHANGE, WALLET_CHAIN_CHANGE, WALLET_SHOW_QR } from "../common/WalletEvent";
import { WALLET_CHAIN_CHANGE, WALLET_SHOW_QR } from "../common/WalletEvent"; import { IChainData } from "../JCWallet";
import { Bind } from "../decorator/AutoUpdateUI";
import JCWallet, { IChainData } from "../JCWallet";
import { renderFromTokenMinimalUnit } from "../util/number.util"; import { renderFromTokenMinimalUnit } from "../util/number.util";
import HashIcon from "./comp/HashIcon";
import JazzIcon from "./comp/JazzIcon";
import WalletBase from "./WallerBase"; import WalletBase from "./WallerBase";
@ -19,6 +15,9 @@ export default class WalletInfo extends WalletBase {
@property(cc.Label) @property(cc.Label)
balanceLabel: cc.Label = null; balanceLabel: cc.Label = null;
@property(cc.Label)
nameLabel: cc.Label = null;
@property(cc.Node) @property(cc.Node)
avatar: cc.Node = null; avatar: cc.Node = null;
@ -36,11 +35,13 @@ export default class WalletInfo extends WalletBase {
this.updateUI() this.updateUI()
this.updateBalance() this.updateBalance()
this.wallet.mainHandlers.on(WALLET_CHAIN_CHANGE, this.chainChange.bind(this)) this.wallet.mainHandlers.on(WALLET_CHAIN_CHANGE, this.chainChange.bind(this))
this.wallet.mainHandlers.on(WALLET_ACCOUNT_CHANGE, this.chainChange.bind(this))
} }
// update (dt) {} // update (dt) {}
updateUI(): void { updateUI(): void {
super.updateUI() super.updateUI()
this.nameLabel.string = this.wallet.currentAccountData.nickname || 'Account'
this.addressLabel.string = this.formatAddress() this.addressLabel.string = this.formatAddress()
this.balanceLabel.string = this.formatMoney() this.balanceLabel.string = this.formatMoney()
if (this.wallet.iconType === 'jazz') { if (this.wallet.iconType === 'jazz') {
@ -86,6 +87,10 @@ export default class WalletInfo extends WalletBase {
chainChange(data: IChainData) { chainChange(data: IChainData) {
this.updateBalance() this.updateBalance()
} }
onAccountChange() {
this.accountId = this.wallet.currentAccount().address
this.updateBalance()
}
showReceiveNode() { showReceiveNode() {
const address = this.wallet.currentAccount().address const address = this.wallet.currentAccount().address
const chainId = this.wallet.currentChain.id const chainId = this.wallet.currentChain.id

View File

@ -7872,10 +7872,12 @@
"balanceLabel": { "balanceLabel": {
"__id__": 173 "__id__": 173
}, },
"nameLabel": {
"__id__": 179
},
"avatar": { "avatar": {
"__id__": 146 "__id__": 146
}, },
"parentNode": null,
"_id": "8bO0XBvlFDPJ87FOgdOxUH" "_id": "8bO0XBvlFDPJ87FOgdOxUH"
}, },
{ {