import { singleton } from "./decorator/singleton.decorator"; import Web3 = require('./lib/web3.min'); import sth = require("./lib/ethSigUtil"); import { ZError } from "./common/ZError"; import { AllChains } from "./data/allchain"; export interface IChainData { name: string, type: string, rpc: string, id: number, symbol: string, explorerurl: string } @singleton export default class JCWallet { web3: Web3 = null wallet: any = null password: string = '111111' chainSet: Set = new Set() chainMap: Map = new Map() private _currentChain: IChainData constructor() { this.web3 = new Web3('https://rpc-testnet.kcc.network') this.wallet = this.web3.eth.accounts.wallet.load(this.password) if (!this.wallet) { this.wallet = this.web3.eth.accounts.wallet; let key = '0xa6c4354fb93a55fb67117969a12465209395ec31089fea9e6e061f873b87a473' this.wallet.add(key); this.web3.eth.accounts.wallet.save(this.password); } } public init({chains}: {chains: number[]}) { for (let chain of chains) { this.chainSet.add(chain) if (!this.chainMap.has(chain)) { let data = AllChains.find(o => o.id === chain) if (data) { this.chainMap.set(chain, data); if (!this._currentChain) { this._currentChain = data } } } } } get currentChain() { return this._currentChain } get chainList() { return [...this.chainMap.values()] } public saveLocal() { } public loadLocal() { } public saveRemove() { } public loadRemote() { } public currentAccount() { return this.wallet[0]; } public accounts() { } public importAccount(privateKey: string) { } public async getBalance(address?: string) { console.log('get balance: ', address); if (!address) { let accountData = this.wallet[0] if (!accountData) { throw new ZError(10, 'no account found') } address = accountData.address } let balance = await this.web3.eth.getBalance(address); return balance } public signTypedData(signObj: any) { const account = this.currentAccount() return ethSigUtil.signTypedData({ data: signObj, privateKey: Buffer.from(account.privateKey.replace('0x', ''), 'hex'), version: 'V4' }) } public recoverTypedSignature(signObj: any, signature: string) { return ethSigUtil.recoverTypedSignature({ data: signObj, signature, version: 'V4' }) } }