209 lines
5.7 KiB
TypeScript
209 lines
5.7 KiB
TypeScript
import { singleton } from "./decorator/singleton.decorator";
|
|
import Web3 = require('./lib/web3.min');
|
|
import sth = require("./lib/ethSigUtil");
|
|
import('./lib/fetch');
|
|
import { ZError } from "./common/ZError";
|
|
import { AllChains } from "./data/allchain";
|
|
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";
|
|
import { DataManage } from "./manage/DataManage";
|
|
import { WALLET_STORAGE_KEY_NAME } from "./config/constants";
|
|
|
|
var global =
|
|
(typeof globalThis !== 'undefined' && globalThis) ||
|
|
(typeof self !== 'undefined' && self) ||
|
|
(typeof global !== 'undefined' && global) ||
|
|
{}
|
|
declare global {
|
|
interface Window {
|
|
jc: {
|
|
wallet: JCWallet;
|
|
},
|
|
ethSigUtil: any
|
|
}
|
|
}
|
|
|
|
export interface IChainData {
|
|
name: string,
|
|
type: string,
|
|
rpc: string,
|
|
id: number,
|
|
symbol: string,
|
|
explorerurl: string
|
|
}
|
|
@singleton
|
|
export default class JCWallet {
|
|
web3: Web3 = null
|
|
private wallet: any = null
|
|
private password: string = '111111'
|
|
private chainSet: Set<number> = new Set()
|
|
private chainMap: Map<number, IChainData> = new Map()
|
|
private _currentChain: IChainData
|
|
public erc20Standard: ERC20Standard
|
|
public erc721Standard: ERC721Standard
|
|
public mainHandlers = createWalletEvents()
|
|
public uiHandlers = createWalletEvents()
|
|
private dataManage = new DataManage()
|
|
public data: IAccount[] = []
|
|
public iconType = 'jazz'
|
|
private accountIndex = 0
|
|
|
|
constructor() {
|
|
// this.web3 = new Web3('https://rpc-mainnet.kcc.network')
|
|
this.web3 = new Web3('https://rpc-testnet.kcc.network')
|
|
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);
|
|
}
|
|
window.jc = window.jc || {wallet: this};
|
|
this.data = this.dataManage.loadData()
|
|
}
|
|
|
|
|
|
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
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
this.updateListType('tokens')
|
|
}
|
|
|
|
updateListType(type: string) {
|
|
this.mainHandlers.emit(WALLET_TOKEN_TYPE_CHANGE, type)
|
|
}
|
|
|
|
get chainList() {
|
|
return [...this.chainMap.values()]
|
|
}
|
|
|
|
public saveLocal() {
|
|
|
|
}
|
|
|
|
public loadLocal() {
|
|
|
|
}
|
|
|
|
public saveRemove() {
|
|
|
|
}
|
|
|
|
public loadRemote() {
|
|
|
|
}
|
|
|
|
public currentAccount(): IAccount {
|
|
return this.wallet[this.accountIndex];
|
|
}
|
|
|
|
get currentAccountData() {
|
|
let address = this.currentAccount().address;
|
|
const chain = this.currentChain.id
|
|
let data = this.data.find(o => o.address === address && o.chain === chain)
|
|
if (data) {
|
|
return data
|
|
}
|
|
let accountName = `Account${this.wallet.length}`
|
|
data = initAccount(address, chain, accountName)
|
|
this.data.push(data)
|
|
this.dataManage.saveData(this.data)
|
|
return data
|
|
}
|
|
|
|
get accounts() {
|
|
return this.data
|
|
}
|
|
|
|
public createAccount() {
|
|
let account = this.web3.eth.accounts.create()
|
|
this.wallet.add(account)
|
|
this.wallet.save(this.password, WALLET_STORAGE_KEY_NAME)
|
|
this.accountIndex = this.wallet.length - 1
|
|
this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE, account.address)
|
|
}
|
|
|
|
public selectAccount(address: string) {
|
|
let index = 0
|
|
for (let i = 0, l = this.wallet.length; i < l ; i ++) {
|
|
if (this.wallet[i].address === address) {
|
|
index = i
|
|
break
|
|
}
|
|
}
|
|
if (index !== this.accountIndex && index < this.wallet.length) {
|
|
this.accountIndex = index
|
|
this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE, this.wallet[index].address)
|
|
}
|
|
}
|
|
|
|
public importAccount(privateKey: string) {
|
|
|
|
}
|
|
|
|
public async sendEth(to: string, amount: number | string) {
|
|
let from = this.currentAccount().address;
|
|
const amountToSend = this.web3.utils.toWei(amount+'', "ether");
|
|
let gas = await this.web3.eth.estimateGas({ from, to, value: amountToSend })
|
|
this.web3.eth.sendTransaction({ from, to, gas, value: amountToSend });
|
|
}
|
|
|
|
public async getBalance(address?: string) {
|
|
console.log('get balance with address: ', address);
|
|
if (!address) {
|
|
let accountData = this.wallet[this.accountIndex]
|
|
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'
|
|
})
|
|
}
|
|
}
|
|
|
|
// window.jc = window.jc || {wallet: new JCWallet};
|