@farcaster/frame-node 0.0.0-canary-20250430150627

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts ADDED
@@ -0,0 +1,32 @@
1
+ import type { FrameServerEvent } from '@farcaster/frame-core'
2
+
3
+ export type VerifyAppKeyResult =
4
+ | { valid: true; appFid: number }
5
+ | { valid: false }
6
+
7
+ export type VerifyAppKey = (
8
+ fid: number,
9
+ appKey: string,
10
+ ) => Promise<VerifyAppKeyResult>
11
+
12
+ export type VerifyJfsResult = {
13
+ fid: number
14
+ appFid: number
15
+ payload: Uint8Array
16
+ }
17
+
18
+ export type ParseWebhookEventResult = {
19
+ fid: number
20
+ appFid: number
21
+ event: FrameServerEvent
22
+ }
23
+
24
+ export class BaseError<C extends Error | undefined = undefined> extends Error {
25
+ override name = 'BaseError'
26
+ cause: C
27
+
28
+ constructor(message: string, cause?: C) {
29
+ super(message)
30
+ this.cause = cause as any
31
+ }
32
+ }
package/src/util.ts ADDED
@@ -0,0 +1,9 @@
1
+ export function bytesToHex(bytes: Uint8Array): string {
2
+ return `0x${Buffer.from(bytes).toString('hex')}`
3
+ }
4
+
5
+ export function hexToBytes(hex: string): Uint8Array {
6
+ return Uint8Array.from(
7
+ Buffer.from(hex.startsWith('0x') ? hex.slice(2) : hex, 'hex'),
8
+ )
9
+ }
package/src/webhook.ts ADDED
@@ -0,0 +1,50 @@
1
+ import { serverEventSchema } from '@farcaster/frame-core'
2
+ import {
3
+ type VerifyJsonFarcasterSignature,
4
+ verifyJsonFarcasterSignature,
5
+ } from './jfs'
6
+ import {
7
+ BaseError,
8
+ type ParseWebhookEventResult,
9
+ type VerifyAppKey,
10
+ } from './types'
11
+
12
+ export declare namespace ParseWebhookEvent {
13
+ type ErrorType =
14
+ | VerifyJsonFarcasterSignature.ErrorType
15
+ | InvalidEventDataError
16
+ }
17
+
18
+ export class InvalidEventDataError<
19
+ C extends Error | undefined = undefined,
20
+ > extends BaseError<C> {
21
+ override readonly name = 'VerifyJsonFarcasterSignature.InvalidEventDataError'
22
+ }
23
+
24
+ export async function parseWebhookEvent(
25
+ rawData: unknown,
26
+ verifyAppKey: VerifyAppKey,
27
+ ): Promise<ParseWebhookEventResult> {
28
+ const { fid, appFid, payload } = await verifyJsonFarcasterSignature(
29
+ rawData,
30
+ verifyAppKey,
31
+ )
32
+
33
+ // Pase and validate event payload
34
+ let payloadJson: any
35
+ try {
36
+ payloadJson = JSON.parse(Buffer.from(payload).toString('utf-8'))
37
+ } catch (error: unknown) {
38
+ throw new InvalidEventDataError(
39
+ 'Error decoding and parsing payload',
40
+ error instanceof Error ? error : undefined,
41
+ )
42
+ }
43
+
44
+ const event = serverEventSchema.safeParse(payloadJson)
45
+ if (event.success === false) {
46
+ throw new InvalidEventDataError('Invalid event payload', event.error)
47
+ }
48
+
49
+ return { fid, appFid, event: event.data }
50
+ }