import { sep43Modules, HotWalletModule, StellarWalletsKit, WalletNetwork, ISupportedWallet } from "@creit.tech/stellar-wallets-kit"; import { Transaction } from "@stellar/stellar-base"; import { WalletType } from "../omni/config"; import { HotConnector } from "../HotConnector"; import { ConnectorType, OmniConnector } from "../OmniConnector"; import { isInjected } from "../hot-wallet/hot"; import { OmniWallet } from "../OmniWallet"; import StellarWallet from "./wallet"; class StellarConnector extends OmniConnector { stellarKit: StellarWalletsKit; icon = "https://storage.herewallet.app/upload/1469894e53ca248ac6adceb2194e6950a13a52d972beb378a20bce7815ba01a4.png"; walletTypes = [WalletType.STELLAR, WalletType.OMNI]; type = ConnectorType.WALLET; name = "Stellar Wallet"; id = "stellar"; constructor(wibe3: HotConnector, stellarKit?: StellarWalletsKit) { super(wibe3); this.stellarKit = stellarKit || new StellarWalletsKit({ network: WalletNetwork.PUBLIC, modules: isInjected() ? [new HotWalletModule()] : sep43Modules() }); this.stellarKit.getSupportedWallets().then((wallets) => { const hot = wallets.find((w) => w.id === "hot-wallet"); this.options = wallets.filter((w) => w.id !== "hot-wallet").map((w) => ({ ...w, name: w.name, icon: w.icon, download: w.url, type: "external" as const, id: w.id })); if (hot) this.options.unshift({ ...hot, name: hot.name, icon: hot.icon, download: hot.url, type: "external" as const, id: hot.id }); }); this.getConnectedWallet().then((data) => { if (!data || !this.stellarKit) throw "No wallet"; this.stellarKit.setWallet(data.id!); const signMessage = async (message: string) => this.stellarKit.signMessage(message); const signTransaction = async (transaction: Transaction) => this.stellarKit.signTransaction(transaction.toXDR()); this.setWallet(new StellarWallet(this, { address: data.address!, signMessage, signTransaction })); }); } async createWallet(address: string): Promise { return new StellarWallet(this, { address }); } async getConnectedWallet() { if (isInjected()) { this.stellarKit.setWallet("hot-wallet"); const { address } = await this.stellarKit.getAddress(); return { type: "wallet", id: "hot-wallet", address }; } return await this.getStorage(); } async connect(id: string) { this.stellarKit.setWallet(id); const { address } = await this.stellarKit.getAddress(); const signMessage = async (message: string) => this.stellarKit.signMessage(message); const signTransaction = async (transaction: Transaction) => this.stellarKit.signTransaction(transaction.toXDR()); this.setStorage({ type: "wallet", id, address }); return this.setWallet(new StellarWallet(this, { address, signMessage, signTransaction })); } async disconnect() { super.disconnect(); this.stellarKit.disconnect(); } } export default StellarConnector;