import { CLAWBOOK_API_URL } from "./constants"; import { createIdentity, getIdKey } from "./identity"; let _cached: { idKey: string; name: string } | undefined | null = null; /** * Derive bot identity from SIGMA_MEMBER_WIF. * Fetches display name from on-chain BAP identity via profile API. * Cached after first successful call. Returns undefined if WIF is missing or invalid. */ export async function getBotIdentity(): Promise< { idKey: string; name: string } | undefined > { if (_cached !== null) return _cached; const wif = process.env.SIGMA_MEMBER_WIF; if (!wif) { _cached = undefined; return undefined; } try { const identity = createIdentity(wif); const idKey = getIdKey(identity); let name = idKey.slice(0, 8); try { const res = await fetch( `${CLAWBOOK_API_URL}/api/profiles/${encodeURIComponent(idKey)}`, ); const data = await res.json(); if (data?.data?.identity?.alternateName) { name = data.data.identity.alternateName; } } catch { /* use truncated idKey as fallback */ } _cached = { idKey, name }; return _cached; } catch { _cached = undefined; return undefined; } }