@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/LICENSE +21 -0
- package/README.md +17 -0
- package/dist/farcaster.d.ts +19 -0
- package/dist/farcaster.js +96 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +23 -0
- package/dist/jfs.d.ts +21 -0
- package/dist/jfs.js +91 -0
- package/dist/neynar.d.ts +2 -0
- package/dist/neynar.js +17 -0
- package/dist/types.d.ts +23 -0
- package/dist/types.js +12 -0
- package/dist/util.d.ts +2 -0
- package/dist/util.js +10 -0
- package/dist/webhook.d.ts +9 -0
- package/dist/webhook.js +27 -0
- package/esm/farcaster.d.ts +19 -0
- package/esm/farcaster.js +59 -0
- package/esm/index.d.ts +7 -0
- package/esm/index.js +7 -0
- package/esm/jfs.d.ts +21 -0
- package/esm/jfs.js +83 -0
- package/esm/neynar.d.ts +2 -0
- package/esm/neynar.js +13 -0
- package/esm/tsconfig.tsbuildinfo +1 -0
- package/esm/types.d.ts +23 -0
- package/esm/types.js +8 -0
- package/esm/util.d.ts +2 -0
- package/esm/util.js +6 -0
- package/esm/webhook.d.ts +9 -0
- package/esm/webhook.js +22 -0
- package/package.json +44 -0
- package/src/farcaster.ts +82 -0
- package/src/index.ts +7 -0
- package/src/jfs.ts +151 -0
- package/src/neynar.ts +23 -0
- package/src/types.ts +32 -0
- package/src/util.ts +9 -0
- package/src/webhook.ts +50 -0
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
|
+
}
|