import { TonConnectUI, TonConnect } from "@tonconnect/ui"; import { runInAction } from "mobx"; import { WalletType } from "../omni/config"; import { HotConnector } from "../HotConnector"; import { ConnectorType, OmniConnector } from "../OmniConnector"; import { isInjected } from "../hot-wallet/hot"; import TonWallet from "./wallet"; export interface TonConnectorOptions { tonConnect?: TonConnectUI; tonWalletsUrl?: string; tonManifestUrl?: string; } const hotWallet = { app_name: "hot", name: "HOT", image: "https://raw.githubusercontent.com/hot-dao/media/main/logo.png", about_url: "https://hot-labs.org/", universal_url: "https://t.me/herewalletbot?attach=wallet", bridge: [ { type: "sse", url: "https://sse-bridge.hot-labs.org", }, { type: "js", key: "hotWallet", }, ], platforms: ["ios", "android", "chrome", "firefox", "macos", "windows", "linux"], features: [ { name: "SendTransaction", maxMessages: 4, extraCurrencySupported: false, }, ], }; class TonConnector extends OmniConnector { type = ConnectorType.WALLET; walletTypes = [WalletType.TON, WalletType.OMNI]; icon = "https://storage.herewallet.app/upload/3ffa61e237f8e38d390abd60200db8edff3ec2b20aad0cc0a8c7a8ba9c318124.png"; name = "TON Wallet"; id = "ton"; private tonConnect = this.initializeConnector(); constructor(wibe3: HotConnector, readonly args?: TonConnectorOptions) { super(wibe3); } async initializeConnector() { if (typeof window === "undefined") throw "TonConnector can only be initialized in the browser"; const url = await fetch(this.args?.tonWalletsUrl || "https://config.ton.org/wallets-v2.json") .then((res) => res.json()) .then((data) => { const list = data.filter((t: any) => t.app_name !== "hot"); list.unshift(hotWallet); const myBlob = new Blob([JSON.stringify(list)], { type: "text/plain" }); return URL.createObjectURL(myBlob); }); const tonConnect = this.args?.tonConnect || new TonConnectUI({ connector: new TonConnect({ walletsListSource: url, manifestUrl: this.args?.tonManifestUrl, }), }); tonConnect.onStatusChange(async (wallet) => { if (!wallet) return this.removeWallet(); this.setWallet( new TonWallet(this, { sendTransaction: (params) => tonConnect.sendTransaction(params), signData: (params) => tonConnect.signData(params), account: wallet.account, }) ); }); tonConnect.connector.restoreConnection(); tonConnect.getWallets().then((wallets) => { runInAction(() => { this.options = wallets.map((w) => ({ name: w.name, icon: w.imageUrl, id: w.appName, download: w.aboutUrl, type: "external" as const, })); }); }); const tcRoot = document.querySelector("#tc-widget-root"); if (tcRoot instanceof HTMLElement) { tcRoot.style.zIndex = "10000000000"; tcRoot.style.position = "fixed"; } if (isInjected()) { tonConnect.getWallets().then((wallets) => { const wallet = wallets.find((w) => w.appName === "hot"); if (wallet) tonConnect.connector.connect(wallet, { tonProof: "wibe3" }); }); } return tonConnect; } async createWallet(address: string) { return new TonWallet(this, { account: { address } }); } async connect(id: string) { const connector = await this.tonConnect; connector.openSingleWalletModal(id); return new Promise((resolve) => { const dispose = this.onConnect(({ wallet }) => { if (!wallet) return; resolve(wallet); dispose(); }); }); } async disconnect() { super.disconnect(); const connector = await this.tonConnect; connector.disconnect(); } } export default TonConnector;