-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
29 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |