UNPKG

bigblocks

Version:

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

76 lines (75 loc) 2.43 kB
"use client"; import { useCallback } from "react"; import { useBitcoinAuth } from "../../../hooks/useBitcoinAuth.js"; /** * MetaLens authentication hook integrated with BigBlocks auth * * Based on patterns from: * - BigBlocks authentication system * - MetaLens signing requirements * * @example * ```tsx * const { isAuthenticated, signComment } = useMetaLensAuth(); * * if (!isAuthenticated) { * await authenticate(); * } * * const signed = await signComment('Hello MetaLens!'); * ``` */ export function useMetaLensAuth() { const { user, signIn, signOut } = useBitcoinAuth(); const isAuthenticated = !!user; const authenticate = useCallback(async () => { if (user) return; // Already authenticated try { await signIn("existing"); } catch (error) { console.error("Failed to authenticate:", error); throw new Error("Authentication failed"); } }, [user, signIn]); const signComment = useCallback(async (content) => { if (!user) { throw new Error("User must be authenticated to sign comments"); } // Create timestamp for signing const timestamp = new Date().toISOString(); // Create message to sign (content + timestamp) const _message = `${content}${timestamp}`; // For now, we'll return a placeholder since actual signing // depends on the Bitcoin auth implementation details // In production, this would use the user's signing key const mapData = { app: "bigblocks-metalens", type: "comment", timestamp, }; return { content, signature: "placeholder-signature", // TODO: Implement actual signing publicKey: user.address || "", mapData, }; }, [user]); return { isAuthenticated, authenticate, user: user ? { address: user.address || "", // Note: These fields come from ProfileInfo, not AuthUser directly paymail: user.profiles?.[0]?.paymail, bapId: user.idKey, // Use idKey for BAP ID avatar: user.profiles?.[0]?.image, name: user.profiles?.[0]?.alternateName || user.profiles?.[0]?.name, } : null, signOut, signComment, }; }