-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: factor out parseFrameMessage (#29)
- Loading branch information
Showing
3 changed files
with
37 additions
and
25 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,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 }; |
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