增加email相关方法

This commit is contained in:
zhl 2023-05-12 17:31:50 +08:00
parent c2a36696b2
commit 222f84307a
9 changed files with 103 additions and 40 deletions

1
src/JCWallet.d.ts vendored
View File

@ -29,6 +29,7 @@ declare namespace jsb {
export function hexDeflate(str: string): string;
export function hexInflate(str: string): string;
export function storeLocalPass(pass: string): void;
export function hashSvrPass(pass: string): string;
export function verifyLocalPass(pass: string): boolean;
export function walletEncrypt(str: string): string;
export function walletDecrypt(str: string): string;

37
src/api/EmailApi.ts Normal file
View File

@ -0,0 +1,37 @@
import { WALLET_API_HOST } from "../config/constants";
import { GET_JSON, POST_JSON } from "../lib/Http";
// request /wallet/info/email to get if email is verified
export function isEmailVerified() {
const url = `${WALLET_API_HOST}/wallet/info/email`;
return GET_JSON(url);
}
export function sendCode(data) {
const url = `${WALLET_API_HOST}/email/send_code`;
return POST_JSON(url, data);
}
export function verifyEmailByCode(data) {
const url = `${WALLET_API_HOST}/email/verify_by_code`;
return POST_JSON(url, data);
}
export function checkEmailRegister(data) {
const url = `${WALLET_API_HOST}/email/check`;
return POST_JSON(url, data);
}
export function emailRegister(data) {
const url = `${WALLET_API_HOST}/email/regist`;
return POST_JSON(url, data);
}
/**
*
* @param data {email, pass}
* @returns
*/
export function emailLogin(data) {
const url = `${WALLET_API_HOST}/wallet/login/email`;
return POST_JSON(url, data);
}

View File

@ -5,8 +5,3 @@ export function alchemyPrePay(data: any) {
const url = `${WALLET_API_HOST}/pay/alchemy/buy`;
return POST_JSON(url, data);
}
// update email info of logined user
export function updateEmailVerify(data: { email: string }) {
const url = `${WALLET_API_HOST}/email/verify`;
return POST_JSON(url, data);
}

View File

@ -50,9 +50,3 @@ export function uploadInfoForWebLogin(data) {
const url = `${WALLET_API_HOST}/bridge/upload`;
return POST_JSON(url, data);
}
// request /wallet/info/email to get if email is verified
export function isEmailVerified() {
const url = `${WALLET_API_HOST}/wallet/info/email`;
return GET_JSON(url);
}

View File

@ -1,6 +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 = "https://wallet.cebggame.com";
export const WALLET_API_HOST = "http://192.168.100.184:3007";
export const MAX_TRY_COUNT = 6;

View File

@ -26,6 +26,7 @@ import { saveData } from "./manage/DataManage";
import { parseUrl } from "./manage/SchemeManage";
import {
loadInternalWallet,
loginByEmail,
restoreWalletByMnemonic,
walletPreLogin,
} from "./manage/WalletManage";
@ -48,7 +49,6 @@ import {
} from "./util/chain.util";
import { fromTokenMinimalUnit } from "./util/number.util";
import { buildLoginSignMsg, signLogin } from "./util/sign.util";
import { findUrlScheme } from "./util/string.util";
var global =
(typeof globalThis !== "undefined" && globalThis) ||
@ -56,7 +56,7 @@ var global =
(typeof global !== "undefined" && global) ||
{};
window.debug = false;
window.debug = true;
@singleton
export default class JCWallet {
@ -86,12 +86,16 @@ export default class JCWallet {
public nativeAccount = "";
public feeAddressMap: Map<string, string> = new Map();
constructor({ type, chain }: { type: number; chain: number }) {
constructor({ type }: { type: number }) {
this.nativeSvr = new NativeSvr();
this.historySvr = new TranHistorySvr();
this.emailVerifySvr = new EmailVerifySvr();
this.paySvr = new PaySvr();
this.walletType = type;
window.jc = { wallet: this };
}
private updateChain(chain: number) {
chain = chain || 80001;
let data = AllChains.find((o) => o.id === chain);
if (!data) {
@ -101,20 +105,23 @@ export default class JCWallet {
this.rpcUrl = data.rpc;
console.log(`rpc url: ${this.rpcUrl}`);
this.init({ chains: [chain], password: this.password });
window.jc = { wallet: this };
}
public get isInternal() {
return this.walletType === WalletType.INTERNAL;
}
public async preLogin(channel: number) {
let token = await walletPreLogin(channel);
return token;
public preLogin(channel: number) {
return walletPreLogin(channel);
}
public async initInternalWallet(pass: string) {
public emailLogin(email: string, password: string) {
return loginByEmail(email, password);
}
public async initInternalWallet(chain: number, pass: string) {
this.walletType = WalletType.INTERNAL;
this.updateChain(chain);
let address: string = await loadInternalWallet(pass);
this.nativeAccount = address;
console.log("native wallet address: " + address);
@ -142,8 +149,9 @@ export default class JCWallet {
* init wallet connect
* @returns
*/
public async initThirdPartyWallet() {
public async initThirdPartyWallet(chain: number) {
this.walletType = WalletType.THIRD_PATH;
this.updateChain(chain);
return new Promise(async (resolve, reject) => {
for (const d of AllChains) {
const id = d.id;

View File

@ -1,7 +1,7 @@
import "whatwg-fetch";
import { WalletEnv } from "../config/WalletEnv";
export async function request(url, option) {
export function request(url, option) {
let headers = new Headers();
headers.append("Content-Type", "application/json");
let walletEnv = new WalletEnv();
@ -19,17 +19,17 @@ export async function request(url, option) {
return fetch(url, optionInt);
}
export async function GET(url: string) {
export function GET(url: string) {
return request(url, {});
}
export async function GET_JSON(url: string) {
export function GET_JSON(url: string) {
return GET(url).then((res) => {
return res.json();
});
}
export async function POST(url, data) {
export function POST(url, data) {
let option = {
method: "POST",
body: JSON.stringify(data),
@ -37,7 +37,7 @@ export async function POST(url, data) {
return request(url, option);
}
export async function DELETE(url, data) {
export function DELETE(url, data) {
let option = {
method: "DELETE",
body: JSON.stringify(data),
@ -45,7 +45,7 @@ export async function DELETE(url, data) {
return request(url, option);
}
export async function PUT(url, data) {
export function PUT(url, data) {
let option = {
method: "PUT",
body: JSON.stringify(data),
@ -53,19 +53,19 @@ export async function PUT(url, data) {
return request(url, option);
}
export async function POST_JSON(url, data) {
export function POST_JSON(url, data) {
return POST(url, data).then((res) => {
return res.json();
});
}
export async function DELETE_JSON(url, data) {
export function DELETE_JSON(url, data) {
return DELETE(url, data).then((res) => {
return res.json();
});
}
export async function PUT_JSON(url, data) {
export function PUT_JSON(url, data) {
return PUT(url, data).then((res) => {
return res.json();
});

View File

@ -17,6 +17,7 @@ import { md5Hash, sha1Hash } from "../util/crypto.util";
import { retry } from "../util/promise.util";
import { MAX_TRY_COUNT, MAX_UPLOAD_COUNT } from "../config/constants";
import { ZError } from "../common/ZError";
import { emailLogin } from "../api/EmailApi";
export function newAccount(password: string, index: number) {
const mnemonic = loadMnemonic(password);
@ -49,6 +50,7 @@ async function syncWalletEnv() {
walletEnv.address = infoRes.data.address;
walletEnv.key = infoRes.data.key;
walletEnv.salt = infoRes.data.salt;
console.log(JSON.stringify(walletEnv));
}
export async function walletPreLogin(channel: number) {
@ -70,17 +72,13 @@ export async function walletPreLogin(channel: number) {
let res: any = await new NativeSvr().signWithTwitter();
window.debug && console.log("native twitter res: " + res);
tokenRes = await twitterAuth(res);
} else if (channel == 5) {
let res: any = await new NativeSvr().signWithEmail();
window.debug && console.log("native twitter res: " + res);
tokenRes = await twitterAuth(res);
} else {
let res: any = await new NativeSvr().signWithGoogle();
window.debug && console.log("native google res: " + res);
tokenRes = await googleAuth(res);
}
window.debug && console.log("wallet token: " + tokenRes.data?.token);
window.debug && console.log(tokenRes);
window.debug && console.log("wallet token: " + tokenRes.data?.token);
if (tokenRes.errcode || !tokenRes.data?.token) {
throw new ZError(tokenRes.errcode, tokenRes.errmsg);
}
@ -89,6 +87,18 @@ export async function walletPreLogin(channel: number) {
return { token: walletEnv.token, address: walletEnv.address };
}
export async function loginByEmail(email: string, password: string) {
password = jsb.hashSvrPass(password);
let tokenRes = await emailLogin({ email, password });
if (tokenRes.errcode || !tokenRes.data?.token) {
throw new ZError(tokenRes.errcode, tokenRes.errmsg);
}
let walletEnv = new WalletEnv();
walletEnv.token = tokenRes.data.token;
await syncWalletEnv();
return { token: walletEnv.token, address: walletEnv.address };
}
export async function loadInternalWallet(pass: string) {
let walletEnv = new WalletEnv();
if (!walletEnv.key) {

View File

@ -1,5 +1,11 @@
import { updateEmailVerify } from "../api/PayApi";
import { isEmailVerified } from "../api/WalletApi";
import {
checkEmailRegister,
emailRegister,
isEmailVerified,
sendCode,
verifyEmailByCode,
} from "../api/EmailApi";
import { singleton } from "../decorator/singleton.decorator";
@singleton
@ -33,10 +39,21 @@ export class EmailVerifySvr {
/**
* Begins the process of verifying the user's email
* @param email - the email to be verified
* @returns a Promise that resolves to the result of the updateEmailVerify API call
*/
public async beginVerifyEmail(email: string) {
return updateEmailVerify({ email });
public async sendEmailCode(email: string, type: number) {
return sendCode({ email, type });
}
public async updateEmailVerify(email: string, code: string) {
return verifyEmailByCode({ email, code });
}
public async isEmailRegister(email: string) {
return checkEmailRegister({ email });
}
public async registByEmail(email: string, password: string, code: string) {
return emailRegister({ email, password, code });
}
}