diff --git a/src/core/getFrameAccountAddress.ts b/src/core/getFrameAccountAddress.ts index 8d5998d884..e3009c8416 100644 --- a/src/core/getFrameAccountAddress.ts +++ b/src/core/getFrameAccountAddress.ts @@ -1,9 +1,4 @@ -import { HubRpcClient, Message, getSSLHubRpcClient } from '@farcaster/hub-nodejs'; - -// URL of the Hub -const HUB_URL = 'nemes.farcaster.xyz:2283'; -// Create a Hub RPC client -const client: HubRpcClient = getSSLHubRpcClient(HUB_URL); +import { parseFrameMessage } from './parseFrameMessage'; type FidResponse = { verifications: string[]; @@ -20,16 +15,7 @@ async function getFrameAccountAddress( { NEYNAR_API_KEY = 'NEYNAR_API_DOCS' }, ) { let farcasterID = 0; - let validatedMessage: Message | undefined = undefined; - // Get the message from the request body - const frameMessage: Message = Message.decode( - Buffer.from(body?.trustedData?.messageBytes ?? '', 'hex'), - ); - // Validate the message - const result = await client.validateMessage(frameMessage); - if (result.isOk() && result.value.valid && result.value.message) { - validatedMessage = result.value.message; - } + const validatedMessage = await parseFrameMessage(body); // Get the Farcaster ID from the message farcasterID = validatedMessage?.data?.fid ?? 0; // Get the user verifications from the Farcaster Indexer diff --git a/src/core/parseFrameMessage.ts b/src/core/parseFrameMessage.ts new file mode 100644 index 0000000000..0fdc7b74cf --- /dev/null +++ b/src/core/parseFrameMessage.ts @@ -0,0 +1,27 @@ +import { HubRpcClient, Message, getSSLHubRpcClient } from '@farcaster/hub-nodejs'; + +// URL of the Hub +const HUB_URL = 'nemes.farcaster.xyz:2283'; +// Create a Hub RPC client +const client: HubRpcClient = getSSLHubRpcClient(HUB_URL); + +/** + * Given a frame message, decode and validate it. If message is valid, + * return the message. Otherwise undefined. + * @param body The JSON received by server on frame callback + */ +async function parseFrameMessage(body: { trustedData?: { messageBytes?: string } }) { + let validatedMessage: Message | undefined = undefined; + // Get the message from the request body + const frameMessage: Message = Message.decode( + Buffer.from(body?.trustedData?.messageBytes ?? '', 'hex'), + ); + // Validate the message + const result = await client.validateMessage(frameMessage); + if (result.isOk() && result.value.valid && result.value.message) { + validatedMessage = result.value.message; + } + return validatedMessage; +} + +export { parseFrameMessage };