diff --git a/src/api/WalletApi.ts b/src/api/WalletApi.ts index 0545ac1..1669f73 100644 --- a/src/api/WalletApi.ts +++ b/src/api/WalletApi.ts @@ -50,3 +50,13 @@ export function uploadInfoForWebLogin(data) { const url = `${WALLET_API_HOST}/bridge/upload`; 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, {}); +} diff --git a/src/index.ts b/src/index.ts index d191f02..48aa108 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,7 +14,14 @@ import { import { AllChains } from './data/allchain'; import { singleton } from './decorator/singleton.decorator'; 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 { NativeSvr } from './services/NativeSvr'; import { PaySvr } from './services/PaySvr'; @@ -29,6 +36,7 @@ import { IChainData } from './types/data.types'; import { universalChainCb } from './util/chain.util'; import { fromTokenMinimalUnit, renderFromTokenMinimalUnit, safeNumberToBN } from './util/number.util'; import { buildLoginSignMsg, signLogin } from './util/sign.util'; +import { resetAccount, resetWallet } from './api/WalletApi'; var global = (typeof globalThis !== 'undefined' && globalThis) || @@ -481,6 +489,17 @@ export default class JCWallet { } return result; } + + public async deleteAccount() { + await resetAccount(); + return {}; + } + + public async resetWalletAddress() { + await resetWallet(); + await syncWalletEnv(); + return {}; + } } // window.jc = window.jc || {wallet: new JCWallet()}; diff --git a/src/manage/WalletManage.ts b/src/manage/WalletManage.ts index 2976ad8..8a95078 100644 --- a/src/manage/WalletManage.ts +++ b/src/manage/WalletManage.ts @@ -1,7 +1,7 @@ -import { hdkey } from "ethereumjs-wallet"; -import { generateMnemonic, mnemonicToSeedSync } from "bip39"; -import { loadMnemonic, saveMnemonic } from "./DataManage"; -import { NativeSvr } from "../services/NativeSvr"; +import { hdkey } from 'ethereumjs-wallet'; +import { generateMnemonic, mnemonicToSeedSync } from 'bip39'; +import { loadMnemonic, saveMnemonic } from './DataManage'; +import { NativeSvr } from '../services/NativeSvr'; import { appleAuth, facebookAuth, @@ -10,12 +10,12 @@ import { tikTokAuth, twitterAuth, uploadWalletInfo, -} from "../api/WalletApi"; -import { WalletEnv } from "../config/WalletEnv"; -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"; +} from '../api/WalletApi'; +import { WalletEnv } from '../config/WalletEnv'; +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); @@ -39,7 +39,7 @@ export function restoreWalletByMnemonic(mnemonic: string, password: string) { saveMnemonic(mnemonic, password); } -async function syncWalletEnv() { +export async function syncWalletEnv() { let walletEnv = new WalletEnv(); let infoRes = await retry(() => getWalletInfo(), MAX_TRY_COUNT); if (infoRes.errcode) { @@ -55,27 +55,27 @@ export async function walletPreLogin(channel: number) { let tokenRes: any; if (channel == 1) { 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); } else if (channel == 2) { 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); } else if (channel == 3) { 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); } else if (channel == 4) { 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); } else { 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); } 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) { throw new ZError(tokenRes.errcode, tokenRes.errmsg); } @@ -100,7 +100,7 @@ export async function loadInternalWallet(pass: string) { let walletEnv = new WalletEnv(); let address = await prepareInternalWallet(pass); 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) { retry(() => uploadWalletInfo({ address }), MAX_UPLOAD_COUNT); @@ -116,13 +116,7 @@ async function prepareInternalWallet(pass: string) { } let { id, openid } = walletEnv.tokenData; - let address = jsb.prepareWallet( - id, - openid, - walletEnv.key, - walletEnv.salt, - pass - ); + let address = jsb.prepareWallet(id, openid, walletEnv.key, walletEnv.salt, pass); return address; } /** @@ -138,19 +132,13 @@ export async function verifyPassword(pass: string) { export function exportSecKey(pass: string) { let walletEnv = new WalletEnv(); 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 resultStr = jsb.walletSecKey( - id, - openid, - walletEnv.key, - walletEnv.salt, - pass - ); + let resultStr = jsb.walletSecKey(id, openid, walletEnv.key, walletEnv.salt, pass); let result = JSON.parse(resultStr); 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; } @@ -161,7 +149,7 @@ export function walletSign(str: 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) { // return; // }