123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- import ConfigM, { Config } from "../api/ConfigM";
- import { Loading } from "../Loading";
- import PogHttp, { PogLoginResp, PogSignMessageResp } from "../net/PogHttp";
- import Utils from "../utils/Utils";
- import { WalletListener, WalletManager } from "../web3/WalletManager";
- import TgM from "./TgM";
- import { Tips } from "./Tips";
- export default class LoginM {
- private static _ins: LoginM;
- public static get ins(): LoginM {
- return (LoginM._ins ??= new LoginM());
- }
- retryLogin() {
- WalletManager.ins.open();
- }
- async login(): Promise<PogLoginResp | null> {
- let abcd = Utils.getQueryString("abcd");
- if (abcd != null && abcd.length > 0) {
- return await PogHttp.ins.loginByWalletAddress(abcd);
- }
- if (await TgM.ins.isTG()) {
- await Loading.ins.U("Telegram Login...", 0.3);
- return await TgM.ins.login();
- }
- setTimeout(async () => {
- let isConnected = await WalletManager.ins.isConnected();
- console.log("isConnected", isConnected);
- if (isConnected) {
- return;
- }
- Loading.ins.showLoginByWalletButton();
- WalletManager.ins.open();
- }, 10000);
- const walletLoginResult: PogLoginResp | null = await new Promise(
- (resolve) => {
- this.checkWalletConnect((res) => {
- resolve(res);
- });
- }
- );
- return walletLoginResult;
- }
- async beginSignMessage(address: string) {
- let signResult = await PogHttp.ins.getSignMessage(address);
- if (
- !signResult ||
- !signResult.fullMessage ||
- signResult.fullMessage.length <= 0
- ) {
- console.error("sign message error", "signResult is null");
- return;
- }
- let signature = await WalletManager.ins.signMessage(signResult.fullMessage);
- return signature;
- }
- async beginLogin(address: string): Promise<PogLoginResp | null> {
- if (!address) {
- return null;
- }
- if (ConfigM.ins.ignoreSignMessage) {
- return PogHttp.ins.loginByWalletAddress(address);
- // .then(async (res: PogLoginResp) => {
- // cb(res);
- // });
- }
- let signature = await LoginM.ins.beginSignMessage(address);
- if (signature) {
- let loginResult = await PogHttp.ins.loginByWalletAddressAndSignature(
- address,
- signature
- );
- return loginResult;
- }
- return null;
- }
- async checkWalletConnect(cb: (res: PogLoginResp) => void) {
- let self = this;
- await Loading.ins.U("Connecting to wallet...", 0.3);
- let walletListener: WalletListener = {
- onLoginSuccess: async (address: string, balance: number) => {
- await Loading.ins.U("Wallet connected...", 0.4);
- await new Promise((resolve) => setTimeout(resolve, 30));
- await Loading.ins.U("Logging in...", 0.5);
- let loginResult: PogLoginResp = await LoginM.ins.beginLogin(address);
- cb(loginResult);
- // PogHttp.ins
- // .loginByWalletAddress(account)
- // .then(async (res: PogLoginResp) => {
- // cb(res);
- // });
- },
- OnWalletDisconnected: () => {
- //
- },
- };
- WalletManager.ins.init(walletListener);
- }
- startWalletLogin() {
- WalletManager.ins.open();
- }
- }
|