import { Transaction, Connection } from '@solana/web3.js'; import type { WalletAdapter } from '../wallet/types'; /** * Deserialize a base64-encoded transaction, sign via wallet adapter, send to Solana. * Prefers signAndSendTransaction if available (MWA), otherwise falls back to * signTransaction + sendRawTransaction via RPC (Phantom deeplinks). * Returns the transaction signature. */ export async function signAndSendBase64Transaction( base64Tx: string, wallet: WalletAdapter, connection: Connection, ): Promise { if (!wallet.publicKey) throw new Error('Wallet not connected'); const binaryStr = atob(base64Tx); const bytes = new Uint8Array(binaryStr.length); for (let i = 0; i < binaryStr.length; i++) { bytes[i] = binaryStr.charCodeAt(i); } const transaction = Transaction.from(bytes); // Prefer signAndSendTransaction if wallet supports it (e.g. MWA) if (wallet.signAndSendTransaction) { return wallet.signAndSendTransaction(transaction); } // Fallback: sign via wallet, then send via RPC const signed = await wallet.signTransaction(transaction); const signature = await connection.sendRawTransaction(signed.serialize()); return signature; }