diff --git a/src/data/DataModel.ts b/src/data/DataModel.ts index ea98c50..0cbde81 100644 --- a/src/data/DataModel.ts +++ b/src/data/DataModel.ts @@ -1,62 +1,77 @@ -import { DEFALUT_TOKENS } from "../config/chain_config" +import { DEFALUT_TOKENS } from "../config/chain_config"; export interface IToken { - address: string - type: 'eth'|'erc20' - default: boolean - symbol?: string - balance?: string - decimal: number - image?: string - last?: number + address: string; + type: "eth" | "erc20"; + default: boolean; + symbol?: string; + balance?: string; + decimal: number; + image?: string; + last?: number; } export interface INFT { - address: string - index: number - tokenId?: string - image?: string - name?: string - desc?: string - last?: number + address: string; + index: number; + tokenId?: string; + image?: string; + name?: string; + desc?: string; + last?: number; } export function initNFT(address: string, index: number) { return { address, - index - } + index, + }; } export interface ITokenData { - tokens: IToken[] - heros: INFT[] - weapons: INFT[] - chips: INFT[] + tokens: IToken[]; + heros: INFT[]; + weapons: INFT[]; + chips: INFT[]; } export interface IAccount { - address: string - nickname?: string - avatar?: string + address: string; + type: number; + index: number; + nickname?: string; + avatar?: string; tokenData: { - [key: number]: ITokenData - } + [key: number]: ITokenData; + }; } -export function initAccount(address: string, chain: number, nickname: string): IAccount { - let chainData = {} +export function initAccount({ + address, + chain, + nickname, + type, + index +}: { + address: string; + chain: number; + nickname: string; + type: number, + index: number +}): IAccount { + let chainData = {}; let data: IAccount = { address, nickname, - tokenData: chainData - } - let tokens = DEFALUT_TOKENS[chain] + type, + index, + tokenData: chainData, + }; + let tokens = DEFALUT_TOKENS[chain]; chainData[chain] = { tokens, heros: [], weapons: [], - chips: [] - } - //TODO: add default tokens - return data + chips: [], + }; + return data; } diff --git a/src/index.ts b/src/index.ts index e6c29b0..19af80f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -63,17 +63,14 @@ export default class JCWallet { this.erc20Standard = new ERC20Standard(this.web3); this.erc721Standard = new ERC721Standard(this.web3); this.wallet = this.web3.eth.accounts.wallet.load(this.password, WALLET_STORAGE_KEY_NAME) - if (!this.wallet || this.wallet.length === 0) { - let key = '0xa6c4354fb93a55fb67117969a12465209395ec31089fea9e6e061f873b87a473' - this.wallet.add(key); - this.web3.eth.accounts.wallet.save(this.password, WALLET_STORAGE_KEY_NAME); - } this.data = this.dataManage.loadData() + window.jc = { wallet: this }; window.cc = window.cc || {}; window.cc.walletCallback = (dataStr: string) => { console.log('[Native CB]::' + dataStr) } + this.init({chains: [322, 97]}) } @@ -90,6 +87,12 @@ export default class JCWallet { } } } + if (!this.wallet || this.wallet.length === 0) { + // let key = '0xa6c4354fb93a55fb67117969a12465209395ec31089fea9e6e061f873b87a473' + // this.wallet.add(key); + // this.web3.eth.accounts.wallet.save(this.password, WALLET_STORAGE_KEY_NAME); + this.createAccount(); + } } get currentChain() { @@ -139,20 +142,19 @@ export default class JCWallet { const chain = this.currentChain.id let data = this.data.find(o => o.address === address) if (!data) { - let accountName = `Account${this.wallet.length}` - data = initAccount(address, chain, accountName) - this.data.push(data) - } else { - if (!data.tokenData[chain]) { - let tokens = DEFALUT_TOKENS[chain] - data.tokenData[chain] = { - tokens, - heros: [], - weapons: [], - chips: [] - } + throw new Error('account data not found'); + } + + if (!data.tokenData[chain]) { + let tokens = DEFALUT_TOKENS[chain] + data.tokenData[chain] = { + tokens, + heros: [], + weapons: [], + chips: [] } } + this.dataManage.saveData(this.data) return data } @@ -165,10 +167,64 @@ export default class JCWallet { let account = this.web3.eth.accounts.create() this.wallet.add(account) this.wallet.save(this.password, WALLET_STORAGE_KEY_NAME) + const chain = this.currentChain.id + let data = this.data.find(o => o.address === account.address) + if (!data) { + const index = this.getMaxIdexOfType(0); + const nickname = `Account ${ index + 1 }` + data = initAccount({ + address: account.address, + chain, + nickname, + type: 0, + index + }) + this.data.push(data); + this.dataManage.saveData(this.data) + } + this.accountIndex = this.wallet.length - 1 this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE, account.address) } + public importAccount(privateKey: string) { + let account = this.web3.eth.accounts.privateKeyToAccount(privateKey) + if (this.wallet[account.address]) { + return false + } + this.wallet.add(account); + const chain = this.currentChain.id + let data = this.data.find(o => o.address === account.address) + if (!data) { + const index = this.getMaxIdexOfType(1); + const nickname = `Imported ${ index + 1 }` + data = initAccount({ + address: account.address, + chain, + nickname, + type: 1, + index + }) + this.data.push(data); + this.dataManage.saveData(this.data) + } + this.web3.eth.accounts.wallet.save(this.password, WALLET_STORAGE_KEY_NAME); + this.accountIndex = this.wallet.length - 1 + this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE, account.address) + return true + } + + private getMaxIdexOfType(type: number) { + let maxIdx = -1; + for (let i = 0, l = this.data.length; i < l; i++) { + if (this.data[i].type !== type) { + continue; + } + maxIdx = Math.max(this.data[i].index, maxIdx); + } + return maxIdx + 1 + } + public selectAccount(address: string) { let index = 0 for (let i = 0, l = this.wallet.length; i < l ; i ++) { @@ -183,18 +239,6 @@ export default class JCWallet { } } - public importAccount(privateKey: string) { - let account = this.web3.eth.accounts.privateKeyToAccount(privateKey) - if (this.wallet[account.address]) { - return false - } - this.wallet.add(account); - this.web3.eth.accounts.wallet.save(this.password, WALLET_STORAGE_KEY_NAME); - this.accountIndex = this.wallet.length - 1 - this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE, account.address) - return true - } - public async sendEth(to: string, amount: number | string) { let from = this.currentAccount().address; const amountToSend = this.web3.utils.toWei(amount+'', "ether");