import { Account, createWalletClient, http } from "viem"; import { Envs, FV_IDENTIFIER_MSG2 } from "../constants"; async function signMessageWithViem( account: Account, message: string, ): Promise { const walletClient = createWalletClient({ account, transport: http(), }); return walletClient.signMessage({ message }); } export interface ParsedBody { ok?: number; success?: boolean; error?: string; [key: string]: any; } type SafeParseResult = { body: ParsedBody; error?: Error; }; const safeParse = async (response: Response): Promise => { try { const body = await response.json(); return { body }; } catch (error) { return { error: new Error("Failed to parse JSON response"), body: {} }; } }; export const g$Response = async (response: Response) => { const { body, error } = await safeParse(response); if ( !response.ok || error || ("ok" in body && !body.ok) || ("success" in body && !body.success) ) { throw error || new Error(body.error || "Unknown server error"); } return body; }; export const g$Request = ( json: any, method: string = "POST", headers: Record = {}, ) => ({ method, headers: { "Content-Type": "application/json", ...headers }, body: JSON.stringify(json), }); export const g$AuthRequest = ( token: string, json: any, method: string = "POST", ) => g$Request(json, method, { Authorization: `Bearer ${token}` }); /** * Authenticates a user with the GoodDollar server. */ export async function fvAuth( env: string, signerOrAddress: string | Account, fvSig?: string, ): Promise<{ token: string; fvsig: string }> { const { backend } = Envs[env]; const authEndpoint = `${backend}/auth/fv2`; let account: Account; let fvsig: string; if (typeof signerOrAddress === "string") { if (!fvSig) { throw new Error("fvSig is required when an address is provided."); } account = { address: signerOrAddress } as Account; fvsig = fvSig; } else { account = signerOrAddress; const message = FV_IDENTIFIER_MSG2.replace("", account.address); fvsig = await signMessageWithViem(account, message); } const response = await fetch( authEndpoint, g$Request({ fvsig, account: account.address }), ); const { token } = await g$Response(response); return { token, fvsig }; }