Skip to content

Commit

Permalink
feat: factor out parseFrameMessage (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasc authored Jan 28, 2024
1 parent 0c8caba commit ef014d4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
28 changes: 3 additions & 25 deletions src/core/getFrameAccountAddress.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { HubRpcClient, Message, getSSLHubRpcClient } from '@farcaster/hub-nodejs';

/**
* Farcaster Hub for signature verification, consider using a private hub if needed:
* https://docs.farcaster.xyz/hubble/hubble
*/
const HUB_URL = 'nemes.farcaster.xyz:2283';
import { parseFrameMessage } from './parseFrameMessage';

type FidResponse = {
verifications: string[];
};

function getHubClient(): HubRpcClient {
return getSSLHubRpcClient(HUB_URL);
}
/**
* Get the Account Address from the Farcaster ID using the Frame. This uses a Neynar api
* to get verified addresses belonging to the user wht that FID. This is using a demo api
Expand All @@ -25,22 +16,9 @@ async function getFrameAccountAddress(
body: { trustedData?: { messageBytes?: string } },
{ NEYNAR_API_KEY = 'NEYNAR_API_DOCS' },
): Promise<string | undefined> {
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 client = getHubClient();
const result = await client.validateMessage(frameMessage);
if (result.isOk() && result.value.valid && result.value.message) {
validatedMessage = result.value.message;
} else {
return;
}
const validatedMessage = await parseFrameMessage(body);
// Get the Farcaster ID from the message
farcasterID = validatedMessage?.data?.fid ?? 0;
const farcasterID = validatedMessage?.data?.fid ?? 0;
// Get the user verifications from the Farcaster Indexer
const options = {
method: 'GET',
Expand Down
33 changes: 33 additions & 0 deletions src/core/parseFrameMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { HubRpcClient, Message, getSSLHubRpcClient } from '@farcaster/hub-nodejs';

/**
* Farcaster Hub for signature verification, consider using a private hub if needed:
* https://docs.farcaster.xyz/hubble/hubble
*/
const HUB_URL = 'nemes.farcaster.xyz:2283';

function getHubClient(): HubRpcClient {
return 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 client = getHubClient();
const result = await client.validateMessage(frameMessage);
if (result.isOk() && result.value.valid && result.value.message) {
validatedMessage = result.value.message;
}
return validatedMessage;
}

export { parseFrameMessage };
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ const version = '0.1.5';
export { version };
export { getFrameAccountAddress } from './core/getFrameAccountAddress';
export { getFrameMetadata } from './core/getFrameMetadata';
export { parseFrameMessage } from './core/parseFrameMessage';

0 comments on commit ef014d4

Please sign in to comment.