UNPKG

bigblocks

Version:

Complete Bitcoin UI component library - authentication, social, wallet, market, inscriptions, and blockchain interactions for React

80 lines (79 loc) 2.84 kB
// Market broadcasting utilities // These integrate with BSV network broadcasting services import { ARC, Transaction, WhatsOnChainBroadcaster, } from "@bsv/sdk"; // Default to testnet for safety - should be configurable const DEFAULT_NETWORK = "test"; export async function broadcastTransaction(tx, options) { try { // Parse transaction if it's a string let transaction; if (typeof tx === "string") { // If it's a hex string, parse it as a transaction // If it's just data (like from BAP rotation), we'd need to build a full transaction // For now, we'll assume it's a complete transaction hex transaction = Transaction.fromHex(tx); } else { transaction = tx; } // Select broadcaster based on options const network = options?.network || DEFAULT_NETWORK; const broadcasterType = options?.broadcaster || "whatsonchain"; let broadcaster; if (broadcasterType === "arc") { // ARC requires an endpoint URL broadcaster = new ARC("https://arc.taal.com", { // Example ARC endpoint headers: { Authorization: `Bearer ${process.env.NEXT_PUBLIC_ARC_TOKEN || ""}`, // Would need API key }, }); } else { // Default to WhatsOnChain broadcaster = new WhatsOnChainBroadcaster(network); } // Broadcast the transaction const result = await broadcaster.broadcast(transaction); // Check if broadcast was successful if ("txid" in result) { const broadcastResponse = result; return { success: true, txid: broadcastResponse.txid, error: undefined, }; } const broadcastFailure = result; return { success: false, txid: undefined, error: broadcastFailure.description || "Broadcast failed", }; } catch (error) { console.error("Broadcast error:", error); return { success: false, txid: undefined, error: error instanceof Error ? error.message : "Unknown broadcast error", }; } } export async function broadcastMarketListing(listing) { console.log("Broadcasting market listing:", listing); // This would integrate with actual market protocol return { success: true, listingId: "mock-listing-12345", }; } export async function broadcastSocialTransaction(tx) { console.log("Broadcasting social transaction:", tx); // This would integrate with actual social protocol return { success: true, txid: "mock-social-txid-12345", error: undefined, }; }