2022-06-15 21:50:04 +08:00

82 lines
1.7 KiB
TypeScript

import { singleton } from "./decorator/singleton.decorator";
import Web3 = require('./lib/web3.min');
import sth = require("./lib/ethSigUtil");
import { ZError } from "./common/ZError";
@singleton
export default class JCWallet {
web3: Web3 = null
wallet: any = null
password: string = '111111'
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 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'
})
}
}