From 22abde882150985084862f77107733b6ac4180df Mon Sep 17 00:00:00 2001 From: zhl Date: Tue, 16 May 2023 19:45:49 +0800 Subject: [PATCH] fix some bug --- src/api/PayApi.ts | 2 +- src/config/chain_config.ts | 2 ++ src/config/constants.ts | 2 +- src/index.ts | 17 +++++++++-------- src/lib/Http.ts | 2 +- src/manage/WalletManage.ts | 1 - src/services/EmailVerifySvr.ts | 29 ++++++++++++++++++++++++----- src/services/PaySvr.ts | 4 ++-- 8 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/api/PayApi.ts b/src/api/PayApi.ts index 00f0101..2f16894 100644 --- a/src/api/PayApi.ts +++ b/src/api/PayApi.ts @@ -1,7 +1,7 @@ import { WALLET_API_HOST } from "../config/constants"; import { POST_JSON } from "../lib/Http"; -export function alchemyPrePay(data: any) { +export function reqAlchemyPrePay(data: any) { const url = `${WALLET_API_HOST}/pay/alchemy/buy`; return POST_JSON(url, data); } diff --git a/src/config/chain_config.ts b/src/config/chain_config.ts index c4edf0b..41b3735 100644 --- a/src/config/chain_config.ts +++ b/src/config/chain_config.ts @@ -1,5 +1,7 @@ export const BASE_TOKEN_URI = "https://market.cebg.games/api/nft/info/"; +export const AVAILABLE_CHAINS = [80001, 421613, 137, 42161]; + export const DEFALUT_TOKENS = { 321: [ { diff --git a/src/config/constants.ts b/src/config/constants.ts index 673ab51..ee23421 100644 --- a/src/config/constants.ts +++ b/src/config/constants.ts @@ -1,7 +1,7 @@ export const WALLET_STORAGE_KEY_NAME = "jc_wallet_data"; // export const WALLET_API_HOST = "https://wallet.cebggame.com"; -export const WALLET_API_HOST = "http://192.168.100.183:3007"; +export const WALLET_API_HOST = "http://192.168.100.184:3007"; export const MAX_TRY_COUNT = 6; diff --git a/src/index.ts b/src/index.ts index 47fc1cb..b7003ae 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,7 +13,7 @@ import { } from "./common/WalletEvent"; import { JazzIcon } from "./comp/JazzIcon"; import { ZWalletConnect } from "./comp/ZWalletConnect"; -import { DEFALUT_TOKENS } from "./config/chain_config"; +import { AVAILABLE_CHAINS, DEFALUT_TOKENS } from "./config/chain_config"; import { NATIVE_PK_PREFIX, TX_CONFIRM_BLOCKS, @@ -93,18 +93,18 @@ export default class JCWallet { this.paySvr = new PaySvr(); this.walletType = type; window.jc = { wallet: this }; + this.init(); } private updateChain(chain: number) { chain = chain || 80001; - let data = AllChains.find((o) => o.id === chain); + let data = this.chainMap.get(chain); if (!data) { throw new Error("no current chain data"); } this._currentChain = data; this.rpcUrl = data.rpc; console.log(`rpc url: ${this.rpcUrl}`); - this.init({ chains: [chain], password: this.password }); } public get isInternal() { @@ -198,16 +198,13 @@ export default class JCWallet { * create local wallet data if there is no local wallet data * @returns */ - private init({ chains, password }: { chains: number[]; password: string }) { - for (let chain of chains) { + private init() { + for (let chain of AVAILABLE_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; - } } } } @@ -225,6 +222,10 @@ export default class JCWallet { return new Promise((resolve, reject) => { if (this.walletType === WalletType.INTERNAL) { const chainData = this.chainMap.get(chainId); + if (!chainData) { + reject && reject("chain data not found"); + return; + } this._currentChain = chainData; this.web3.eth.setProvider(chainData.rpc); this.mainHandlers.emit(WALLET_CHAIN_CHANGE, chainData); diff --git a/src/lib/Http.ts b/src/lib/Http.ts index 3b6d632..a7d1c16 100644 --- a/src/lib/Http.ts +++ b/src/lib/Http.ts @@ -1,4 +1,4 @@ -import "whatwg-fetch"; +import { fetch } from "whatwg-fetch"; import { WalletEnv } from "../config/WalletEnv"; export function request(url, option) { diff --git a/src/manage/WalletManage.ts b/src/manage/WalletManage.ts index fd4a6a5..a46a175 100644 --- a/src/manage/WalletManage.ts +++ b/src/manage/WalletManage.ts @@ -105,7 +105,6 @@ export async function loadInternalWallet(pass: string) { } let { id, openid } = walletEnv.tokenData; - console.log("data from token: " + JSON.stringify(walletEnv.tokenData)); let address = jsb.prepareWallet( id, openid, diff --git a/src/services/EmailVerifySvr.ts b/src/services/EmailVerifySvr.ts index feb96cc..ed53b0b 100644 --- a/src/services/EmailVerifySvr.ts +++ b/src/services/EmailVerifySvr.ts @@ -5,6 +5,7 @@ import { sendCode, verifyEmailByCode, } from "../api/EmailApi"; +import { ZError } from "../common/ZError"; import { singleton } from "../decorator/singleton.decorator"; @@ -20,7 +21,7 @@ export class EmailVerifySvr { }> { let res = await isEmailVerified(); if (res.errcode) { - throw new Error("Failed to fetch email verification status"); //Throw error if call to the checking function fails + throw new ZError(res.errcode, res.errmsg); //Throw error if call to the checking function fails } const { data } = res; @@ -42,18 +43,36 @@ export class EmailVerifySvr { */ public async sendEmailCode(email: string, type: number) { - return sendCode({ email, type }); + let result = await sendCode({ email, type }); + console.log(JSON.stringify(result)); + if (result.errcode) { + throw new ZError(result.errcode, result.errmsg); + } + return result.data; } public async updateEmailVerify(email: string, code: string) { - return verifyEmailByCode({ email, code }); + let result = await verifyEmailByCode({ email, code }); + if (result.errcode) { + throw new ZError(result.errcode, result.errmsg); + } + return result.data; } public async isEmailRegister(email: string) { - return checkEmailRegister({ email }); + let result = await checkEmailRegister({ email }); + if (result.errcode) { + throw new ZError(result.errcode, result.errmsg); + } + return result.data; } public async registByEmail(email: string, password: string, code: string) { - return emailRegister({ email, password, code }); + password = jsb.hashSvrPass(password); + let result = await emailRegister({ email, password, code }); + if (result.errcode) { + throw new ZError(result.errcode, result.errmsg); + } + return result.data; } } diff --git a/src/services/PaySvr.ts b/src/services/PaySvr.ts index c41283b..f72e7d6 100644 --- a/src/services/PaySvr.ts +++ b/src/services/PaySvr.ts @@ -1,4 +1,4 @@ -import { alchemyPrePay } from "../api/PayApi"; +import { reqAlchemyPrePay } from "../api/PayApi"; import { singleton } from "../decorator/singleton.decorator"; import { IPayData } from "../types/data.types"; @@ -10,7 +10,7 @@ export class PaySvr { * @returns The result of the alchemyPrePay function. */ public async alchemyPrePay(data: IPayData) { - let res = await alchemyPrePay(data); + let res = await reqAlchemyPrePay(data); if (res.errcode) { throw new Error(res.errmsg); }