Skip to content

Commit

Permalink
feat: factor out parseFrameMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasc committed Jan 28, 2024
1 parent 54dac6e commit 8bf7a83
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
18 changes: 2 additions & 16 deletions src/core/getFrameAccountAddress.ts
Original file line number Diff line number Diff line change
@@ -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[];
Expand All @@ -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
Expand Down
27 changes: 27 additions & 0 deletions src/core/parseFrameMessage.ts
Original file line number Diff line number Diff line change
@@ -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 };

0 comments on commit 8bf7a83

Please sign in to comment.