增加一些walletconnect的方法
This commit is contained in:
parent
46ba09f45b
commit
59cdbd2c0c
@ -5,17 +5,35 @@ import { toHexChainId } from '../util/chain.util'
|
|||||||
|
|
||||||
export class ZWalletConnect {
|
export class ZWalletConnect {
|
||||||
provider: WalletConnectProvider
|
provider: WalletConnectProvider
|
||||||
constructor(rpc: String) {
|
accounts: string[] = []
|
||||||
|
|
||||||
|
constructor(rpc: any) {
|
||||||
|
console.log('ZWalletConnect constructor');
|
||||||
this.provider = new WalletConnectProvider({
|
this.provider = new WalletConnectProvider({
|
||||||
rpc
|
rpc,
|
||||||
|
qrcodeModal: {
|
||||||
|
open(uri: string, cb: any, opts?: any): void {
|
||||||
|
console.log('walletconnect open qrcode modal')
|
||||||
|
//@ts-ignore
|
||||||
|
jumpToWallet(uri);
|
||||||
|
},
|
||||||
|
close(): void {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
this._subscribeToEvents(this.provider)
|
this._subscribeToEvents(this.provider)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async connect() {
|
||||||
|
console.log('wallet connect begin connect')
|
||||||
|
return this.provider.enable()
|
||||||
|
}
|
||||||
|
|
||||||
private _subscribeToEvents(provider: WalletConnectProvider) {
|
private _subscribeToEvents(provider: WalletConnectProvider) {
|
||||||
this.provider.on('accountsChanged', async(accounts: string[]) => {
|
this.provider.on('accountsChanged', async(accounts: string[]) => {
|
||||||
console.log('accountsChanged: ', accounts)
|
console.log('accountsChanged: ', accounts)
|
||||||
|
this.accounts = accounts;
|
||||||
})
|
})
|
||||||
|
|
||||||
// Subscribe to chainId change
|
// Subscribe to chainId change
|
||||||
@ -32,16 +50,15 @@ export class ZWalletConnect {
|
|||||||
/**
|
/**
|
||||||
* @param data
|
* @param data
|
||||||
*/
|
*/
|
||||||
public async addOrChangeChain(data: IChainData, cb?: () => void) {
|
public async addOrChangeChain(data: IChainData) {
|
||||||
const onChainChange = (chainId: string) => {
|
return new Promise((resolve, reject) => {
|
||||||
console.log('switchEthereumChain: ', chainId)
|
const onChainChange = (chainId: string) => {
|
||||||
this.provider.removeListener('chainChanged', onChainChange)
|
console.log('switchEthereumChain: ', chainId)
|
||||||
cb && cb()
|
this.provider.removeListener('chainChanged', onChainChange)
|
||||||
}
|
resolve && resolve(chainId);
|
||||||
this.provider.on('chainChanged', onChainChange)
|
}
|
||||||
|
this.provider.on('chainChanged', onChainChange)
|
||||||
try {
|
this.provider.request({
|
||||||
await this.provider.request({
|
|
||||||
method: 'wallet_addEthereumChain',
|
method: 'wallet_addEthereumChain',
|
||||||
params: [
|
params: [
|
||||||
{
|
{
|
||||||
@ -57,17 +74,24 @@ export class ZWalletConnect {
|
|||||||
}
|
}
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
console.log('add chain success')
|
.then(() => {
|
||||||
} catch (addError) {
|
console.log('add chain success, wait result');
|
||||||
console.error('add chain error: ', addError)
|
})
|
||||||
this.provider.removeListener('chainChanged', onChainChange)
|
.catch(err=>{
|
||||||
}
|
console.error('add chain error: ', err);
|
||||||
|
this.provider.removeListener('chainChanged', onChainChange);
|
||||||
|
reject && reject(err);
|
||||||
|
})
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async signData(signObj: any, signer: string) {
|
public async signData(signObj: any, signer: string) {
|
||||||
const msgParams = JSON.stringify(signObj)
|
const msgParams = JSON.stringify(signObj)
|
||||||
const from = signer
|
const from = signer || this.accounts[0]
|
||||||
|
if (!from) {
|
||||||
|
throw new Error('no account');
|
||||||
|
}
|
||||||
console.log('clicked, sending personal sign req', 'from', from, msgParams)
|
console.log('clicked, sending personal sign req', 'from', from, msgParams)
|
||||||
const params = [from, msgParams]
|
const params = [from, msgParams]
|
||||||
const result: any = await this.sendCmd({
|
const result: any = await this.sendCmd({
|
||||||
|
415
src/index.ts
415
src/index.ts
@ -1,169 +1,198 @@
|
|||||||
import { singleton } from "./decorator/singleton.decorator";
|
import { singleton } from './decorator/singleton.decorator'
|
||||||
import Web3 from "web3";
|
import Web3 from 'web3'
|
||||||
import {
|
import {
|
||||||
recoverTypedSignature,
|
recoverTypedSignature,
|
||||||
signTypedData,
|
signTypedData,
|
||||||
SignTypedDataVersion,
|
SignTypedDataVersion,
|
||||||
} from "@metamask/eth-sig-util";
|
} from '@metamask/eth-sig-util'
|
||||||
import "whatwg-fetch";
|
import 'whatwg-fetch'
|
||||||
import { AllChains } from "./data/allchain";
|
import { AllChains } from './data/allchain'
|
||||||
import {
|
import {
|
||||||
createWalletEvents,
|
createWalletEvents,
|
||||||
WALLET_ACCOUNT_CHANGE,
|
WALLET_ACCOUNT_CHANGE,
|
||||||
WALLET_CHAIN_CHANGE,
|
WALLET_CHAIN_CHANGE,
|
||||||
WALLET_TOKEN_TYPE_CHANGE,
|
WALLET_TOKEN_TYPE_CHANGE,
|
||||||
} from "./common/WalletEvent";
|
} 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'
|
||||||
import {
|
import {
|
||||||
checkPassword,
|
checkPassword,
|
||||||
loadData,
|
loadData,
|
||||||
saveData,
|
saveData,
|
||||||
savePassword,
|
savePassword,
|
||||||
} from "./manage/DataManage";
|
} from './manage/DataManage'
|
||||||
import { WALLET_STORAGE_KEY_NAME } from "./config/constants";
|
import { WALLET_STORAGE_KEY_NAME } from './config/constants'
|
||||||
import { DEFALUT_TOKENS } from "./config/chain_config";
|
import { DEFALUT_TOKENS } from './config/chain_config'
|
||||||
import {
|
import {
|
||||||
newAccount,
|
newAccount,
|
||||||
newMnemonic,
|
newMnemonic,
|
||||||
restoreWalletByMnemonic,
|
restoreWalletByMnemonic,
|
||||||
} from "./manage/WalletManage";
|
} from './manage/WalletManage'
|
||||||
import { signLogin } from "./util/sign.util";
|
import { buildLoginSignMsg, signLogin } from './util/sign.util'
|
||||||
import { JazzIcon } from "./comp/JazzIcon";
|
import { JazzIcon } from './comp/JazzIcon'
|
||||||
import { ERC1155Standard } from "./standards/ERC1155Standard";
|
import { ERC1155Standard } from './standards/ERC1155Standard'
|
||||||
import { ZWalletConnect } from "./comp/ZWalletConnect";
|
import { ZWalletConnect } from './comp/ZWalletConnect'
|
||||||
|
import { rejects } from 'assert'
|
||||||
|
|
||||||
|
|
||||||
var global =
|
var global =
|
||||||
(typeof globalThis !== "undefined" && globalThis) ||
|
(typeof globalThis !== 'undefined' && globalThis) ||
|
||||||
(typeof self !== "undefined" && self) ||
|
(typeof self !== 'undefined' && self) ||
|
||||||
(typeof global !== "undefined" && global) ||
|
(typeof global !== 'undefined' && global) ||
|
||||||
{};
|
{}
|
||||||
declare global {
|
declare global {
|
||||||
interface Window {
|
interface Window {
|
||||||
jc: {
|
jc: {
|
||||||
wallet: JCWallet;
|
wallet: JCWallet
|
||||||
};
|
}
|
||||||
jcwallet: JCWallet;
|
jcwallet: JCWallet
|
||||||
ethSigUtil: any;
|
ethSigUtil: any
|
||||||
cc: any;
|
cc: any
|
||||||
Stream: any;
|
Stream: any
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export interface IChainData {
|
export interface IChainData {
|
||||||
name: string;
|
name: string
|
||||||
type: string;
|
type: string
|
||||||
rpc: string;
|
rpc: string
|
||||||
id: number;
|
id: number
|
||||||
symbol: string;
|
symbol: string
|
||||||
explorerurl: string;
|
explorerurl: string
|
||||||
decimals?: number
|
decimals?: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum WalletType {
|
||||||
|
INTERNAL = 0,
|
||||||
|
THIRD_PATH = 1,
|
||||||
|
}
|
||||||
@singleton
|
@singleton
|
||||||
export default class JCWallet {
|
export default class JCWallet {
|
||||||
web3: Web3 = null;
|
web3: Web3 = null
|
||||||
private wallet: any = null;
|
private wallet: any = null
|
||||||
private password: string = "111111";
|
private password: string = '111111'
|
||||||
private chainSet: Set<number> = new Set();
|
private chainSet: Set<number> = new Set()
|
||||||
private 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
|
||||||
public erc1155Standard: ERC1155Standard;
|
public erc1155Standard: ERC1155Standard
|
||||||
public wConnect: ZWalletConnect;
|
public wConnect: ZWalletConnect
|
||||||
public mainHandlers = createWalletEvents();
|
public mainHandlers = createWalletEvents()
|
||||||
public data: IAccount[] = [];
|
public data: IAccount[] = []
|
||||||
public iconType = "jazz";
|
public iconType = 'jazz'
|
||||||
private accountIndex = 0;
|
private accountIndex = 0
|
||||||
private walletType: number = 0;
|
private walletType: WalletType = WalletType.INTERNAL
|
||||||
|
private rpcUrl: string = ''
|
||||||
|
public rpc: any = {}
|
||||||
|
|
||||||
constructor(type: number, password: string) {
|
|
||||||
// this.web3 = new Web3('https://rpc-mainnet.kcc.network')
|
constructor({ type, chain, password }: { type: number; chain: number, password: string }) {
|
||||||
if (!password) {
|
this.walletType = type
|
||||||
throw new Error("need password");
|
chain = chain || 322
|
||||||
|
let data = AllChains.find((o) => o.id === chain)
|
||||||
|
if (!data) {
|
||||||
|
throw new Error('no current chain data')
|
||||||
}
|
}
|
||||||
if (!checkPassword(password)) {
|
this._currentChain = data;
|
||||||
throw new Error("password error");
|
this.rpcUrl = data.rpc;
|
||||||
|
console.log(`rpc url: ${this.rpcUrl}`);
|
||||||
|
if (this.walletType === WalletType.INTERNAL) {
|
||||||
|
if (!password) {
|
||||||
|
throw new Error('need password')
|
||||||
|
}
|
||||||
|
if (!checkPassword(password)) {
|
||||||
|
throw new Error('password error')
|
||||||
|
}
|
||||||
|
this.password = password
|
||||||
|
savePassword(password)
|
||||||
|
this.initInternalWallet();
|
||||||
|
} else {
|
||||||
|
this.initThirdPartyWallet(chain);
|
||||||
}
|
}
|
||||||
this.walletType = type;
|
window.jc = { wallet: this }
|
||||||
this.password = password;
|
|
||||||
savePassword(password);
|
|
||||||
var start = Date.now();
|
|
||||||
const rpc = "https://rpc-testnet.kcc.network"
|
|
||||||
this.web3 = new Web3(rpc);
|
|
||||||
console.log(`init web3 cost: ${(Date.now() - start) / 1000}`);
|
|
||||||
this.erc20Standard = new ERC20Standard(this.web3);
|
|
||||||
this.erc721Standard = new ERC721Standard(this.web3);
|
|
||||||
this.erc1155Standard = new ERC1155Standard(this.web3);
|
|
||||||
this.wConnect = new ZWalletConnect(rpc);
|
|
||||||
start = Date.now();
|
|
||||||
this.wallet = this.web3.eth.accounts.wallet.load(
|
|
||||||
this.password,
|
|
||||||
WALLET_STORAGE_KEY_NAME
|
|
||||||
);
|
|
||||||
console.log(`load wallet cost: ${(Date.now() - start) / 1000}`);
|
|
||||||
start = Date.now();
|
|
||||||
this.data = loadData();
|
|
||||||
console.log(`init wallet ext data cost: ${(Date.now() - start) / 1000}`);
|
|
||||||
window.jc = { wallet: this };
|
|
||||||
this.init({ chains: [322, 97], password });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public init({ chains, password }: { chains: number[]; password: string }) {
|
private initInternalWallet() {
|
||||||
|
var start = Date.now()
|
||||||
|
this.web3 = new Web3(this.rpcUrl)
|
||||||
|
console.log(`init web3 cost: ${(Date.now() - start) / 1000}`)
|
||||||
|
this.erc20Standard = new ERC20Standard(this.web3)
|
||||||
|
this.erc721Standard = new ERC721Standard(this.web3)
|
||||||
|
this.erc1155Standard = new ERC1155Standard(this.web3)
|
||||||
|
|
||||||
|
start = Date.now()
|
||||||
|
this.wallet = this.web3.eth.accounts.wallet.load(
|
||||||
|
this.password,
|
||||||
|
WALLET_STORAGE_KEY_NAME,
|
||||||
|
)
|
||||||
|
console.log(`load wallet cost: ${(Date.now() - start) / 1000}`)
|
||||||
|
start = Date.now()
|
||||||
|
this.data = loadData()
|
||||||
|
console.log(`init wallet ext data cost: ${(Date.now() - start) / 1000}`)
|
||||||
|
this.init({ chains: [322, 97], password: this.password })
|
||||||
|
}
|
||||||
|
|
||||||
|
private initThirdPartyWallet(chain: number) {
|
||||||
|
for (const d of AllChains) {
|
||||||
|
const id = d.id
|
||||||
|
this.rpc[id] = d.rpc
|
||||||
|
}
|
||||||
|
|
||||||
|
this.wConnect = new ZWalletConnect(this.rpc)
|
||||||
|
}
|
||||||
|
|
||||||
|
private init({ chains, password }: { chains: number[]; password: string }) {
|
||||||
for (let chain of chains) {
|
for (let chain of chains) {
|
||||||
this.chainSet.add(chain);
|
this.chainSet.add(chain)
|
||||||
if (!this.chainMap.has(chain)) {
|
if (!this.chainMap.has(chain)) {
|
||||||
let data = AllChains.find((o) => o.id === chain);
|
let data = AllChains.find((o) => o.id === chain)
|
||||||
if (data) {
|
if (data) {
|
||||||
this.chainMap.set(chain, data);
|
this.chainMap.set(chain, data)
|
||||||
if (!this._currentChain) {
|
if (!this._currentChain) {
|
||||||
this._currentChain = data;
|
this._currentChain = data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!this.wallet || this.wallet.length === 0) {
|
if (!this.wallet || this.wallet.length === 0) {
|
||||||
// this.createAccount();
|
// this.createAccount();
|
||||||
this.newWallet(password);
|
this.newWallet(password)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public newWallet(password: string) {
|
public newWallet(password: string) {
|
||||||
this.password = password;
|
this.password = password
|
||||||
newMnemonic(this.password);
|
newMnemonic(this.password)
|
||||||
this.createAccount();
|
this.createAccount()
|
||||||
}
|
}
|
||||||
|
|
||||||
public restoreFromMnemonic(mnemonic: string, password: string) {
|
public restoreFromMnemonic(mnemonic: string, password: string) {
|
||||||
this.password = password;
|
this.password = password
|
||||||
this.wallet.clear();
|
this.wallet.clear()
|
||||||
restoreWalletByMnemonic(mnemonic, this.password);
|
restoreWalletByMnemonic(mnemonic, this.password)
|
||||||
return this.createAccount();
|
return this.createAccount()
|
||||||
}
|
}
|
||||||
|
|
||||||
get currentChain() {
|
get currentChain() {
|
||||||
return this._currentChain;
|
return this._currentChain
|
||||||
}
|
}
|
||||||
|
|
||||||
updateCurrentChain(chainId: number) {
|
updateCurrentChain(chainId: number) {
|
||||||
const chainData = this.chainMap.get(chainId);
|
const chainData = this.chainMap.get(chainId)
|
||||||
this._currentChain = chainData;
|
this._currentChain = chainData
|
||||||
this.web3.eth.setProvider(chainData.rpc);
|
this.web3.eth.setProvider(chainData.rpc)
|
||||||
this.mainHandlers.emit(WALLET_CHAIN_CHANGE, chainData);
|
this.mainHandlers.emit(WALLET_CHAIN_CHANGE, chainData)
|
||||||
this.updateListType("tokens");
|
this.updateListType('tokens')
|
||||||
return this.currentChain;
|
return this.currentChain
|
||||||
}
|
}
|
||||||
|
|
||||||
updateListType(type: string) {
|
updateListType(type: string) {
|
||||||
this.mainHandlers.emit(WALLET_TOKEN_TYPE_CHANGE, type);
|
this.mainHandlers.emit(WALLET_TOKEN_TYPE_CHANGE, type)
|
||||||
}
|
}
|
||||||
|
|
||||||
get chainList() {
|
get chainList() {
|
||||||
return [...this.chainMap.values()];
|
return [...this.chainMap.values()]
|
||||||
}
|
}
|
||||||
|
|
||||||
public saveLocal() {}
|
public saveLocal() {}
|
||||||
@ -175,153 +204,173 @@ export default class JCWallet {
|
|||||||
public loadRemote() {}
|
public loadRemote() {}
|
||||||
|
|
||||||
public currentAccount() {
|
public currentAccount() {
|
||||||
return this.wallet[this.accountIndex];
|
if (this.walletType === WalletType.INTERNAL) {
|
||||||
|
return this.wallet[this.accountIndex]
|
||||||
|
} else {
|
||||||
|
return this.wConnect.accounts[0]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
get currentAccountData() {
|
get currentAccountData() {
|
||||||
let address = this.currentAccount().address;
|
let address = this.currentAccount().address
|
||||||
const chain = this.currentChain.id;
|
const chain = this.currentChain.id
|
||||||
let data = this.data.find((o) => o.address === address);
|
let data = this.data.find((o) => o.address === address)
|
||||||
if (!data) {
|
if (!data) {
|
||||||
throw new Error("account data not found");
|
throw new Error('account data not found')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data.tokenData[chain]) {
|
if (!data.tokenData[chain]) {
|
||||||
let tokens = DEFALUT_TOKENS[chain];
|
let tokens = DEFALUT_TOKENS[chain]
|
||||||
data.tokenData[chain] = {
|
data.tokenData[chain] = {
|
||||||
tokens,
|
tokens,
|
||||||
heros: [],
|
heros: [],
|
||||||
weapons: [],
|
weapons: [],
|
||||||
chips: [],
|
chips: [],
|
||||||
};
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
saveData(this.data);
|
saveData(this.data)
|
||||||
return data;
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
get accounts() {
|
get accounts() {
|
||||||
return this.data;
|
return this.data
|
||||||
}
|
}
|
||||||
|
|
||||||
public createAccount() {
|
public createAccount() {
|
||||||
// let account = this.web3.eth.accounts.create()
|
// let account = this.web3.eth.accounts.create()
|
||||||
const index = this.getMaxIdexOfType(0);
|
const index = this.getMaxIdexOfType(0)
|
||||||
const accountNew = newAccount(this.password, index);
|
const accountNew = newAccount(this.password, index)
|
||||||
const account = this.wallet.add(accountNew);
|
const account = this.wallet.add(accountNew)
|
||||||
this.wallet.save(this.password, WALLET_STORAGE_KEY_NAME);
|
this.wallet.save(this.password, WALLET_STORAGE_KEY_NAME)
|
||||||
const chain = this.currentChain.id;
|
const chain = this.currentChain.id
|
||||||
let data = this.data.find((o) => o.address === account.address);
|
let data = this.data.find((o) => o.address === account.address)
|
||||||
if (!data) {
|
if (!data) {
|
||||||
const nickname = `Account ${index + 1}`;
|
const nickname = `Account ${index + 1}`
|
||||||
data = initAccount({
|
data = initAccount({
|
||||||
address: account.address,
|
address: account.address,
|
||||||
chain,
|
chain,
|
||||||
nickname,
|
nickname,
|
||||||
type: 0,
|
type: 0,
|
||||||
index,
|
index,
|
||||||
});
|
})
|
||||||
this.data.push(data);
|
this.data.push(data)
|
||||||
saveData(this.data);
|
saveData(this.data)
|
||||||
}
|
}
|
||||||
|
|
||||||
this.accountIndex = this.wallet.length - 1;
|
this.accountIndex = this.wallet.length - 1
|
||||||
this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE, account.address);
|
this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE, account.address)
|
||||||
return account.address;
|
return account.address
|
||||||
}
|
}
|
||||||
|
|
||||||
public importAccount(privateKey: string) {
|
public importAccount(privateKey: string) {
|
||||||
const account = this.wallet.add(privateKey);
|
const account = this.wallet.add(privateKey)
|
||||||
const chain = this.currentChain.id;
|
const chain = this.currentChain.id
|
||||||
let data = this.data.find((o) => o.address === account.address);
|
let data = this.data.find((o) => o.address === account.address)
|
||||||
if (!data) {
|
if (!data) {
|
||||||
const index = this.getMaxIdexOfType(1);
|
const index = this.getMaxIdexOfType(1)
|
||||||
const nickname = `Imported ${index + 1}`;
|
const nickname = `Imported ${index + 1}`
|
||||||
data = initAccount({
|
data = initAccount({
|
||||||
address: account.address,
|
address: account.address,
|
||||||
chain,
|
chain,
|
||||||
nickname,
|
nickname,
|
||||||
type: 1,
|
type: 1,
|
||||||
index,
|
index,
|
||||||
});
|
})
|
||||||
this.data.push(data);
|
this.data.push(data)
|
||||||
saveData(this.data);
|
saveData(this.data)
|
||||||
}
|
}
|
||||||
this.web3.eth.accounts.wallet.save(this.password, WALLET_STORAGE_KEY_NAME);
|
this.web3.eth.accounts.wallet.save(this.password, WALLET_STORAGE_KEY_NAME)
|
||||||
this.accountIndex = this.wallet.length - 1;
|
this.accountIndex = this.wallet.length - 1
|
||||||
this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE, account.address);
|
this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE, account.address)
|
||||||
return account.address;
|
return account.address
|
||||||
}
|
}
|
||||||
|
|
||||||
private getMaxIdexOfType(type: number) {
|
private getMaxIdexOfType(type: number) {
|
||||||
let maxIdx = -1;
|
let maxIdx = -1
|
||||||
for (let i = 0, l = this.data.length; i < l; i++) {
|
for (let i = 0, l = this.data.length; i < l; i++) {
|
||||||
if (this.data[i].type !== type) {
|
if (this.data[i].type !== type) {
|
||||||
continue;
|
continue
|
||||||
}
|
}
|
||||||
maxIdx = Math.max(this.data[i].index, maxIdx);
|
maxIdx = Math.max(this.data[i].index, maxIdx)
|
||||||
}
|
}
|
||||||
return maxIdx + 1;
|
return maxIdx + 1
|
||||||
}
|
}
|
||||||
private getAccountByAddress(address: string) {
|
private getAccountByAddress(address: string) {
|
||||||
let account;
|
let account
|
||||||
let index = 0;
|
let index = 0
|
||||||
for (let i = 0, l = this.wallet.length; i < l; i++) {
|
for (let i = 0, l = this.wallet.length; i < l; i++) {
|
||||||
if (this.wallet[i].address === address) {
|
if (this.wallet[i].address === address) {
|
||||||
account = this.wallet[i];
|
account = this.wallet[i]
|
||||||
index = i;
|
index = i
|
||||||
break;
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return {account, index};
|
return { account, index }
|
||||||
}
|
}
|
||||||
|
|
||||||
public selectAccount(address: string) {
|
public selectAccount(address: string) {
|
||||||
const { index } = this.getAccountByAddress(address);
|
const { index } = this.getAccountByAddress(address)
|
||||||
if (index !== this.accountIndex && index < this.wallet.length) {
|
if (index !== this.accountIndex && index < this.wallet.length) {
|
||||||
this.accountIndex = index;
|
this.accountIndex = index
|
||||||
this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE, this.wallet[index].address);
|
this.mainHandlers.emit(WALLET_ACCOUNT_CHANGE, this.wallet[index].address)
|
||||||
}
|
}
|
||||||
return address;
|
return address
|
||||||
}
|
}
|
||||||
|
|
||||||
public async sendEth(to: string, amount: number | string) {
|
public async sendEth(to: string, amount: number | string) {
|
||||||
let from = this.currentAccount().address;
|
let from = this.currentAccount().address
|
||||||
const amountToSend = this.web3.utils.toWei(amount + "", "ether");
|
const amountToSend = this.web3.utils.toWei(amount + '', 'ether')
|
||||||
let gas = await this.web3.eth.estimateGas({
|
let gas = await this.web3.eth.estimateGas({
|
||||||
from,
|
from,
|
||||||
to,
|
to,
|
||||||
value: amountToSend,
|
value: amountToSend,
|
||||||
});
|
})
|
||||||
this.web3.eth.sendTransaction({ from, to, gas, value: amountToSend });
|
this.web3.eth.sendTransaction({ from, to, gas, value: amountToSend })
|
||||||
}
|
}
|
||||||
|
|
||||||
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[this.accountIndex];
|
let accountData = this.wallet[this.accountIndex]
|
||||||
if (!accountData) {
|
if (!accountData) {
|
||||||
throw new Error("no account found");
|
throw new Error('no account found')
|
||||||
}
|
}
|
||||||
address = accountData.address;
|
address = accountData.address
|
||||||
}
|
}
|
||||||
|
|
||||||
let balance = await this.web3.eth.getBalance(address);
|
let balance = await this.web3.eth.getBalance(address)
|
||||||
return balance;
|
return balance
|
||||||
}
|
}
|
||||||
|
|
||||||
public signTypedDataV4(signObj: any) {
|
public signTypedDataV4(signObj: any) {
|
||||||
const account = this.currentAccount();
|
const account = this.currentAccount()
|
||||||
return signTypedData({
|
return signTypedData({
|
||||||
data: signObj,
|
data: signObj,
|
||||||
privateKey: Buffer.from(account.privateKey.replace("0x", ""), "hex"),
|
privateKey: Buffer.from(account.privateKey.replace('0x', ''), 'hex'),
|
||||||
version: SignTypedDataVersion.V4,
|
version: SignTypedDataVersion.V4,
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public loginSign(nonce: string, tips: string, address?: string) {
|
public loginSign(nonce: string, tips: string, address?: string) {
|
||||||
const account = this.currentAccount();
|
return new Promise((resolve, reject) => {
|
||||||
return signLogin(nonce, tips, account.privateKey);
|
const account = this.currentAccount()
|
||||||
|
if (this.walletType === WalletType.INTERNAL) {
|
||||||
|
const sig = signLogin(nonce, tips, account.privateKey)
|
||||||
|
resolve && resolve(sig);
|
||||||
|
} else {
|
||||||
|
const signObj = buildLoginSignMsg(nonce, tips);
|
||||||
|
this.wConnect.signData(signObj, account)
|
||||||
|
.then(sig => {
|
||||||
|
resolve && resolve(sig);
|
||||||
|
})
|
||||||
|
.catch(err=> {
|
||||||
|
reject && reject(err);
|
||||||
|
})
|
||||||
|
// @ts-ignore
|
||||||
|
jumpToWallet()
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public recoverTypedSignatureV4(signObj: any, signature: string) {
|
public recoverTypedSignatureV4(signObj: any, signature: string) {
|
||||||
@ -329,26 +378,26 @@ export default class JCWallet {
|
|||||||
data: signObj,
|
data: signObj,
|
||||||
signature,
|
signature,
|
||||||
version: SignTypedDataVersion.V4,
|
version: SignTypedDataVersion.V4,
|
||||||
});
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
public generateIconData(msg: string, diameter: number) {
|
public generateIconData(msg: string, diameter: number) {
|
||||||
let icon = new JazzIcon();
|
let icon = new JazzIcon()
|
||||||
return icon.init(msg, diameter);
|
return icon.init(msg, diameter)
|
||||||
}
|
}
|
||||||
|
|
||||||
public async erc20Info(address: string) {
|
public async erc20Info(address: string) {
|
||||||
let symbol = await this.erc20Standard.getTokenSymbol(address);
|
let symbol = await this.erc20Standard.getTokenSymbol(address)
|
||||||
let decimal = await this.erc20Standard.getTokenDecimals(address);
|
let decimal = await this.erc20Standard.getTokenDecimals(address)
|
||||||
return {symbol, decimal};
|
return { symbol, decimal }
|
||||||
}
|
}
|
||||||
|
|
||||||
public async erc20Balance(address: string, account?: string) {
|
public async erc20Balance(address: string, account?: string) {
|
||||||
if (!account) {
|
if (!account) {
|
||||||
account = this.currentAccount().address;
|
account = this.currentAccount().address
|
||||||
}
|
}
|
||||||
let result = await this.erc20Standard.getBalanceOf(address, account);
|
let result = await this.erc20Standard.getBalanceOf(address, account)
|
||||||
return result;
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
public async sendErc20(address: string, to: string, amount: string) {
|
public async sendErc20(address: string, to: string, amount: string) {
|
||||||
@ -357,18 +406,18 @@ export default class JCWallet {
|
|||||||
address,
|
address,
|
||||||
from,
|
from,
|
||||||
to,
|
to,
|
||||||
amount
|
amount,
|
||||||
})
|
})
|
||||||
return result;
|
return result
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// window.jc = window.jc || {wallet: new JCWallet()};
|
// window.jc = window.jc || {wallet: new JCWallet()};
|
||||||
|
|
||||||
export * from "./common/WalletEvent";
|
export * from './common/WalletEvent'
|
||||||
export * from "./common/ZError";
|
export * from './common/ZError'
|
||||||
export * from "./config/chain_config";
|
export * from './config/chain_config'
|
||||||
export * from "./util/number.util";
|
export * from './util/number.util'
|
||||||
export * from "./util/wallet.util";
|
export * from './util/wallet.util'
|
||||||
export * from "./data/DataModel";
|
export * from './data/DataModel'
|
||||||
export * from "./lib/WalletConnect";
|
export * from './lib/WalletConnect'
|
||||||
|
@ -19,6 +19,7 @@ const SubscriptionsSubprovider = require("web3-provider-engine/subproviders/subs
|
|||||||
|
|
||||||
class WalletConnectProvider extends ProviderEngine {
|
class WalletConnectProvider extends ProviderEngine {
|
||||||
public bridge = "https://bridge.walletconnect.org";
|
public bridge = "https://bridge.walletconnect.org";
|
||||||
|
public qrcodeModal;
|
||||||
public rpc: IRPCMap | null = null;
|
public rpc: IRPCMap | null = null;
|
||||||
public infuraId = "";
|
public infuraId = "";
|
||||||
public http: HttpConnection | null = null;
|
public http: HttpConnection | null = null;
|
||||||
@ -35,13 +36,12 @@ class WalletConnectProvider extends ProviderEngine {
|
|||||||
this.bridge = opts.connector
|
this.bridge = opts.connector
|
||||||
? opts.connector.bridge
|
? opts.connector.bridge
|
||||||
: opts.bridge || "https://bridge.walletconnect.org";
|
: opts.bridge || "https://bridge.walletconnect.org";
|
||||||
this.qrcode = typeof opts.qrcode === "undefined" || opts.qrcode !== false;
|
|
||||||
this.qrcodeModal = opts.qrcodeModal || this.qrcodeModal;
|
this.qrcodeModal = opts.qrcodeModal || this.qrcodeModal;
|
||||||
this.qrcodeModalOptions = opts.qrcodeModalOptions;
|
|
||||||
this.wc =
|
this.wc =
|
||||||
opts.connector ||
|
opts.connector ||
|
||||||
new WalletConnect({
|
new WalletConnect({
|
||||||
bridge: this.bridge,
|
bridge: this.bridge,
|
||||||
|
qrcodeModal: this.qrcodeModal,
|
||||||
storageId: opts?.storageId,
|
storageId: opts?.storageId,
|
||||||
signingMethods: opts?.signingMethods,
|
signingMethods: opts?.signingMethods,
|
||||||
clientMeta: opts?.clientMeta,
|
clientMeta: opts?.clientMeta,
|
||||||
@ -314,6 +314,7 @@ class WalletConnectProvider extends ProviderEngine {
|
|||||||
|
|
||||||
updateHttpConnection() {
|
updateHttpConnection() {
|
||||||
if (this.rpcUrl) {
|
if (this.rpcUrl) {
|
||||||
|
console.log(`updateHttpConnection: ${this.rpcUrl}`)
|
||||||
this.http = new HttpConnection(this.rpcUrl);
|
this.http = new HttpConnection(this.rpcUrl);
|
||||||
this.http.on("payload", payload => this.emit("payload", payload));
|
this.http.on("payload", payload => this.emit("payload", payload));
|
||||||
this.http.on("error", error => this.emit("error", error));
|
this.http.on("error", error => this.emit("error", error));
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import { signTypedData, SignTypedDataVersion, TypedMessage } from "@metamask/eth-sig-util"
|
import { signTypedData, SignTypedDataVersion, TypedMessage } from "@metamask/eth-sig-util"
|
||||||
|
|
||||||
|
export function buildLoginSignMsg(nonce: string, tips: string) {
|
||||||
export function signLogin(nonce: string, tips: string, privateKey: string) {
|
|
||||||
const signMsg = {
|
const signMsg = {
|
||||||
tips,
|
tips,
|
||||||
nonce,
|
nonce,
|
||||||
@ -24,7 +23,11 @@ export function signLogin(nonce: string, tips: string, privateKey: string) {
|
|||||||
},
|
},
|
||||||
message: signMsg
|
message: signMsg
|
||||||
}
|
}
|
||||||
|
return signObj
|
||||||
|
}
|
||||||
|
|
||||||
|
export function signLogin(nonce: string, tips: string, privateKey: string) {
|
||||||
|
const signObj = buildLoginSignMsg(nonce, tips);
|
||||||
return signTypedData({
|
return signTypedData({
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
data: signObj,
|
data: signObj,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user