import { hdkey } from "ethereumjs-wallet"; import { generateMnemonic, mnemonicToSeedSync } from "bip39"; import { loadMnemonic, saveMnemonic } from "./DataManage"; import { NativeSvr } from "../services/NativeSvr"; import { appleAuth, facebookAuth, getWalletInfo, googleAuth, tikTokAuth, twitterAuth, uploadInfoForWebLogin, uploadWalletInfo, } from "../api/WalletApi"; import { WalletEnv } from "../config/WalletEnv"; import { md5Hash, sha1Hash } from "../util/crypto.util"; import { retry } from "../util/promise.util"; import { MAX_TRY_COUNT, MAX_UPLOAD_COUNT } from "../config/constants"; export function newAccount(password: string, index: number) { const mnemonic = loadMnemonic(password); const seed = mnemonicToSeedSync(mnemonic); const hdWallet = hdkey.fromMasterSeed(seed); const keyPair1 = hdWallet.derivePath(`m/44'/60'/0'/0/${index}`); const w1 = keyPair1.getWallet(); return { address: w1.getAddressString(), privateKey: w1.getPrivateKeyString(), }; } export function newMnemonic(password: string) { let mnemonic = generateMnemonic(); saveMnemonic(mnemonic, password); return mnemonic; } export function restoreWalletByMnemonic(mnemonic: string, password: string) { saveMnemonic(mnemonic, password); } export async function loadInternalWallet(channel: number) { let tokenRes: any; if (channel == 1) { let res: any = await new NativeSvr().signWithApple(); 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); tokenRes = await tikTokAuth(res); } else if (channel == 3) { let res: any = await new NativeSvr().signWithFacebook(); 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); tokenRes = await twitterAuth(res); } else if (channel == 6) { 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); if (tokenRes.errcode || !tokenRes.data?.token) { return; } new WalletEnv().token = tokenRes.data.token; let infoRes = await retry(() => getWalletInfo(), MAX_TRY_COUNT); if (infoRes.errcode) { return; } let seed = infoRes.data.oid + infoRes.data.is + infoRes.data.salt; let seedHash = md5Hash(seed); let address; let idHash = md5Hash(infoRes.data.oid); if (!infoRes.data.key) { let time = Date.now(); let strWallet = jsb.generateWallet(idHash, seedHash); console.log("generate wallet cost: " + (Date.now() - time) / 1000); window.debug && console.log("native wallet info " + strWallet); let walletInfo = JSON.parse(strWallet); address = walletInfo.address; // setImmediate(function () { // }); retry( () => uploadWalletInfo({ key: walletInfo.master, address }), MAX_UPLOAD_COUNT ); } else { let localSKey, localBKey; if (jsb.loadLocalStorage) { localSKey = jsb.loadLocalStorage("cebg_wallet_s_" + idHash); localBKey = jsb.loadLocalStorage("cebg_wallet_b_" + idHash); } else { localSKey = localStorage.getItem("cebg_wallet_s_" + idHash); localBKey = localStorage.getItem("cebg_wallet_b_" + idHash); } if (localSKey || localBKey) { let strWallet = jsb.prepareWallet(idHash, seedHash, infoRes.data.key); let walletInfo = JSON.parse(strWallet); address = walletInfo.address; retry(() => uploadWalletInfo({ address }), MAX_UPLOAD_COUNT); } else { let qrResult = await new NativeSvr().restoreKey(idHash); let strWallet = jsb.restoreWallet( idHash, seedHash, infoRes.data.key, qrResult ); window.debug && console.log("restore native wallet info " + strWallet); let walletInfo = JSON.parse(strWallet); address = walletInfo.address; retry(() => uploadWalletInfo({ address }), MAX_UPLOAD_COUNT); } } return address; } export function walletSign(str: string) { let result = jsb.walletSign(str); return result; } export async function parseWebLogin(dataStr: string) { console.log("found web login scheme, begin login"); if (dataStr.indexOf("|") < 0) { return; } let datas = dataStr.split("|"); let webtoken = datas[0]; let pk64 = datas[1]; let pk = jsb.hexInflate(pk64); let keyEncrypt = jsb.encryptedLocalKey(pk); console.log("webtoken: " + webtoken); console.log("local key: " + keyEncrypt); let result = await uploadInfoForWebLogin({ key: keyEncrypt, webtoken }); console.log("login result: " + result); }