增加删除账号和重置wallet的接口

This commit is contained in:
CounterFire2023 2023-08-10 17:44:23 +08:00
parent 0ed31320cb
commit 86e71e6d73
3 changed files with 53 additions and 36 deletions

View File

@ -50,3 +50,13 @@ export function uploadInfoForWebLogin(data) {
const url = `${WALLET_API_HOST}/bridge/upload`; const url = `${WALLET_API_HOST}/bridge/upload`;
return POST_JSON(url, data); return POST_JSON(url, data);
} }
export function resetWallet() {
const url = `${WALLET_API_HOST}/wallet/reset`;
return POST_JSON(url, {});
}
export function resetAccount() {
const url = `${WALLET_API_HOST}/wallet/account/reset`;
return POST_JSON(url, {});
}

View File

@ -14,7 +14,14 @@ import {
import { AllChains } from './data/allchain'; import { AllChains } from './data/allchain';
import { singleton } from './decorator/singleton.decorator'; import { singleton } from './decorator/singleton.decorator';
import { parseUrl } from './manage/SchemeManage'; import { parseUrl } from './manage/SchemeManage';
import { exportSecKey, loadInternalWallet, loginByEmail, verifyPassword, walletPreLogin } from './manage/WalletManage'; import {
exportSecKey,
loadInternalWallet,
loginByEmail,
syncWalletEnv,
verifyPassword,
walletPreLogin,
} from './manage/WalletManage';
import { EmailVerifySvr } from './services/EmailVerifySvr'; import { EmailVerifySvr } from './services/EmailVerifySvr';
import { NativeSvr } from './services/NativeSvr'; import { NativeSvr } from './services/NativeSvr';
import { PaySvr } from './services/PaySvr'; import { PaySvr } from './services/PaySvr';
@ -29,6 +36,7 @@ import { IChainData } from './types/data.types';
import { universalChainCb } from './util/chain.util'; import { universalChainCb } from './util/chain.util';
import { fromTokenMinimalUnit, renderFromTokenMinimalUnit, safeNumberToBN } from './util/number.util'; import { fromTokenMinimalUnit, renderFromTokenMinimalUnit, safeNumberToBN } from './util/number.util';
import { buildLoginSignMsg, signLogin } from './util/sign.util'; import { buildLoginSignMsg, signLogin } from './util/sign.util';
import { resetAccount, resetWallet } from './api/WalletApi';
var global = var global =
(typeof globalThis !== 'undefined' && globalThis) || (typeof globalThis !== 'undefined' && globalThis) ||
@ -481,6 +489,17 @@ export default class JCWallet {
} }
return result; return result;
} }
public async deleteAccount() {
await resetAccount();
return {};
}
public async resetWalletAddress() {
await resetWallet();
await syncWalletEnv();
return {};
}
} }
// window.jc = window.jc || {wallet: new JCWallet()}; // window.jc = window.jc || {wallet: new JCWallet()};

View File

@ -1,7 +1,7 @@
import { hdkey } from "ethereumjs-wallet"; import { hdkey } from 'ethereumjs-wallet';
import { generateMnemonic, mnemonicToSeedSync } from "bip39"; import { generateMnemonic, mnemonicToSeedSync } from 'bip39';
import { loadMnemonic, saveMnemonic } from "./DataManage"; import { loadMnemonic, saveMnemonic } from './DataManage';
import { NativeSvr } from "../services/NativeSvr"; import { NativeSvr } from '../services/NativeSvr';
import { import {
appleAuth, appleAuth,
facebookAuth, facebookAuth,
@ -10,12 +10,12 @@ import {
tikTokAuth, tikTokAuth,
twitterAuth, twitterAuth,
uploadWalletInfo, uploadWalletInfo,
} from "../api/WalletApi"; } from '../api/WalletApi';
import { WalletEnv } from "../config/WalletEnv"; import { WalletEnv } from '../config/WalletEnv';
import { retry } from "../util/promise.util"; import { retry } from '../util/promise.util';
import { MAX_TRY_COUNT, MAX_UPLOAD_COUNT } from "../config/constants"; import { MAX_TRY_COUNT, MAX_UPLOAD_COUNT } from '../config/constants';
import { ZError } from "../common/ZError"; import { ZError } from '../common/ZError';
import { emailLogin } from "../api/EmailApi"; import { emailLogin } from '../api/EmailApi';
export function newAccount(password: string, index: number) { export function newAccount(password: string, index: number) {
const mnemonic = loadMnemonic(password); const mnemonic = loadMnemonic(password);
@ -39,7 +39,7 @@ export function restoreWalletByMnemonic(mnemonic: string, password: string) {
saveMnemonic(mnemonic, password); saveMnemonic(mnemonic, password);
} }
async function syncWalletEnv() { export async function syncWalletEnv() {
let walletEnv = new WalletEnv(); let walletEnv = new WalletEnv();
let infoRes = await retry(() => getWalletInfo(), MAX_TRY_COUNT); let infoRes = await retry(() => getWalletInfo(), MAX_TRY_COUNT);
if (infoRes.errcode) { if (infoRes.errcode) {
@ -55,27 +55,27 @@ export async function walletPreLogin(channel: number) {
let tokenRes: any; let tokenRes: any;
if (channel == 1) { if (channel == 1) {
let res: any = await new NativeSvr().signWithApple(); let res: any = await new NativeSvr().signWithApple();
window.debug && console.log("native apple res: " + res); window.debug && console.log('native apple res: ' + res);
tokenRes = await appleAuth(res); tokenRes = await appleAuth(res);
} else if (channel == 2) { } else if (channel == 2) {
let res: any = await new NativeSvr().signWithTikTok(); let res: any = await new NativeSvr().signWithTikTok();
window.debug && console.log("native tiktok res: " + res); window.debug && console.log('native tiktok res: ' + res);
tokenRes = await tikTokAuth(res); tokenRes = await tikTokAuth(res);
} else if (channel == 3) { } else if (channel == 3) {
let res: any = await new NativeSvr().signWithFacebook(); let res: any = await new NativeSvr().signWithFacebook();
window.debug && console.log("native facebook res: " + res); window.debug && console.log('native facebook res: ' + res);
tokenRes = await facebookAuth(res); tokenRes = await facebookAuth(res);
} else if (channel == 4) { } else if (channel == 4) {
let res: any = await new NativeSvr().signWithTwitter(); let res: any = await new NativeSvr().signWithTwitter();
window.debug && console.log("native twitter res: " + res); window.debug && console.log('native twitter res: ' + res);
tokenRes = await twitterAuth(res); tokenRes = await twitterAuth(res);
} else { } else {
let res: any = await new NativeSvr().signWithGoogle(); let res: any = await new NativeSvr().signWithGoogle();
window.debug && console.log("native google res: " + res); window.debug && console.log('native google res: ' + res);
tokenRes = await googleAuth(res); tokenRes = await googleAuth(res);
} }
window.debug && console.log(tokenRes); window.debug && console.log(tokenRes);
window.debug && console.log("wallet token: " + tokenRes.data?.token); window.debug && console.log('wallet token: ' + tokenRes.data?.token);
if (tokenRes.errcode || !tokenRes.data?.token) { if (tokenRes.errcode || !tokenRes.data?.token) {
throw new ZError(tokenRes.errcode, tokenRes.errmsg); throw new ZError(tokenRes.errcode, tokenRes.errmsg);
} }
@ -100,7 +100,7 @@ export async function loadInternalWallet(pass: string) {
let walletEnv = new WalletEnv(); let walletEnv = new WalletEnv();
let address = await prepareInternalWallet(pass); let address = await prepareInternalWallet(pass);
if (walletEnv.address && walletEnv.address !== address) { if (walletEnv.address && walletEnv.address !== address) {
throw new ZError(10, "address not match, perhaps wrong password"); throw new ZError(10, 'address not match, perhaps wrong password');
} }
if (!walletEnv.address) { if (!walletEnv.address) {
retry(() => uploadWalletInfo({ address }), MAX_UPLOAD_COUNT); retry(() => uploadWalletInfo({ address }), MAX_UPLOAD_COUNT);
@ -116,13 +116,7 @@ async function prepareInternalWallet(pass: string) {
} }
let { id, openid } = walletEnv.tokenData; let { id, openid } = walletEnv.tokenData;
let address = jsb.prepareWallet( let address = jsb.prepareWallet(id, openid, walletEnv.key, walletEnv.salt, pass);
id,
openid,
walletEnv.key,
walletEnv.salt,
pass
);
return address; return address;
} }
/** /**
@ -138,19 +132,13 @@ export async function verifyPassword(pass: string) {
export function exportSecKey(pass: string) { export function exportSecKey(pass: string) {
let walletEnv = new WalletEnv(); let walletEnv = new WalletEnv();
if (!walletEnv.address || !walletEnv.key) { if (!walletEnv.address || !walletEnv.key) {
throw new ZError(10, "wallet not found"); throw new ZError(10, 'wallet not found');
} }
let { id, openid } = walletEnv.tokenData; let { id, openid } = walletEnv.tokenData;
let resultStr = jsb.walletSecKey( let resultStr = jsb.walletSecKey(id, openid, walletEnv.key, walletEnv.salt, pass);
id,
openid,
walletEnv.key,
walletEnv.salt,
pass
);
let result = JSON.parse(resultStr); let result = JSON.parse(resultStr);
if (result.address !== walletEnv.address) { if (result.address !== walletEnv.address) {
throw new ZError(11, "address not match, perhaps wrong password"); throw new ZError(11, 'address not match, perhaps wrong password');
} }
return result.key; return result.key;
} }
@ -161,7 +149,7 @@ export function walletSign(str: string) {
} }
export async function parseWebLogin(dataStr: string) { export async function parseWebLogin(dataStr: string) {
console.log("found web login scheme, begin login"); console.log('found web login scheme, begin login');
// if (dataStr.indexOf("|") < 0) { // if (dataStr.indexOf("|") < 0) {
// return; // return;
// } // }