Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: factor out parseFrameMessage #29

Merged
merged 2 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we want to make this a param? That way if you're parsing the message to get other data anyway you can just pass the result in to this function rather than loading same data twice. Could do this with an overload

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';
Loading