@farcaster/frame-node 0.0.10 → 0.0.11
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/dist/farcaster.d.ts +1 -1
- package/dist/farcaster.js +17 -17
- package/dist/index.d.ts +7 -7
- package/dist/jfs.d.ts +3 -3
- package/dist/jfs.js +19 -19
- package/dist/neynar.d.ts +1 -1
- package/dist/neynar.js +4 -4
- package/dist/types.d.ts +1 -1
- package/dist/types.js +1 -1
- package/dist/util.js +2 -2
- package/dist/webhook.d.ts +2 -2
- package/dist/webhook.js +4 -4
- package/esm/farcaster.d.ts +29 -19
- package/esm/farcaster.js +57 -49
- package/esm/index.d.ts +7 -7
- package/esm/index.js +7 -7
- package/esm/jfs.d.ts +33 -16
- package/esm/jfs.js +93 -74
- package/esm/neynar.d.ts +2 -2
- package/esm/neynar.js +14 -12
- package/esm/tsconfig.tsbuildinfo +1 -1
- package/esm/types.d.ts +27 -20
- package/esm/types.js +6 -6
- package/esm/util.d.ts +2 -2
- package/esm/util.js +4 -2
- package/esm/webhook.d.ts +17 -6
- package/esm/webhook.js +23 -18
- package/package.json +7 -5
- package/src/farcaster.ts +34 -36
- package/src/index.ts +7 -7
- package/src/jfs.ts +55 -50
- package/src/neynar.ts +11 -11
- package/src/types.ts +15 -15
- package/src/util.ts +3 -3
- package/src/webhook.ts +18 -14
package/esm/util.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export function bytesToHex(bytes) {
|
|
2
|
-
|
|
2
|
+
return `0x${Buffer.from(bytes).toString('hex')}`
|
|
3
3
|
}
|
|
4
4
|
export function hexToBytes(hex) {
|
|
5
|
-
|
|
5
|
+
return Uint8Array.from(
|
|
6
|
+
Buffer.from(hex.startsWith('0x') ? hex.slice(2) : hex, 'hex'),
|
|
7
|
+
)
|
|
6
8
|
}
|
package/esm/webhook.d.ts
CHANGED
|
@@ -1,9 +1,20 @@
|
|
|
1
|
-
import { VerifyJsonFarcasterSignature } from
|
|
2
|
-
import {
|
|
1
|
+
import type { VerifyJsonFarcasterSignature } from './jfs'
|
|
2
|
+
import {
|
|
3
|
+
BaseError,
|
|
4
|
+
type ParseWebhookEventResult,
|
|
5
|
+
type VerifyAppKey,
|
|
6
|
+
} from './types'
|
|
3
7
|
export declare namespace ParseWebhookEvent {
|
|
4
|
-
|
|
8
|
+
type ErrorType =
|
|
9
|
+
| VerifyJsonFarcasterSignature.ErrorType
|
|
10
|
+
| InvalidEventDataError
|
|
5
11
|
}
|
|
6
|
-
export declare class InvalidEventDataError<
|
|
7
|
-
|
|
12
|
+
export declare class InvalidEventDataError<
|
|
13
|
+
C extends Error | undefined = undefined,
|
|
14
|
+
> extends BaseError<C> {
|
|
15
|
+
readonly name = 'VerifyJsonFarcasterSignature.InvalidEventDataError'
|
|
8
16
|
}
|
|
9
|
-
export declare function parseWebhookEvent(
|
|
17
|
+
export declare function parseWebhookEvent(
|
|
18
|
+
rawData: unknown,
|
|
19
|
+
verifyAppKey: VerifyAppKey,
|
|
20
|
+
): Promise<ParseWebhookEventResult>
|
package/esm/webhook.js
CHANGED
|
@@ -1,22 +1,27 @@
|
|
|
1
|
-
import { serverEventSchema } from
|
|
2
|
-
import { verifyJsonFarcasterSignature
|
|
3
|
-
import { BaseError } from
|
|
1
|
+
import { serverEventSchema } from '@farcaster/frame-core'
|
|
2
|
+
import { verifyJsonFarcasterSignature } from './jfs'
|
|
3
|
+
import { BaseError } from './types'
|
|
4
4
|
export class InvalidEventDataError extends BaseError {
|
|
5
|
-
|
|
5
|
+
name = 'VerifyJsonFarcasterSignature.InvalidEventDataError'
|
|
6
6
|
}
|
|
7
7
|
export async function parseWebhookEvent(rawData, verifyAppKey) {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
8
|
+
const { fid, appFid, payload } = await verifyJsonFarcasterSignature(
|
|
9
|
+
rawData,
|
|
10
|
+
verifyAppKey,
|
|
11
|
+
)
|
|
12
|
+
// Pase and validate event payload
|
|
13
|
+
let payloadJson
|
|
14
|
+
try {
|
|
15
|
+
payloadJson = JSON.parse(Buffer.from(payload).toString('utf-8'))
|
|
16
|
+
} catch (error) {
|
|
17
|
+
throw new InvalidEventDataError(
|
|
18
|
+
'Error decoding and parsing payload',
|
|
19
|
+
error instanceof Error ? error : undefined,
|
|
20
|
+
)
|
|
21
|
+
}
|
|
22
|
+
const event = serverEventSchema.safeParse(payloadJson)
|
|
23
|
+
if (event.success === false) {
|
|
24
|
+
throw new InvalidEventDataError('Invalid event payload', event.error)
|
|
25
|
+
}
|
|
26
|
+
return { fid, appFid, event: event.data }
|
|
22
27
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@farcaster/frame-node",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.11",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "esm/index.js",
|
|
6
6
|
"files": [
|
|
@@ -9,15 +9,17 @@
|
|
|
9
9
|
"src"
|
|
10
10
|
],
|
|
11
11
|
"devDependencies": {
|
|
12
|
+
"@types/node": "^22.10.2",
|
|
12
13
|
"@vitest/coverage-v8": "^2.1.8",
|
|
13
|
-
"
|
|
14
|
-
"typescript": "^5.6.3",
|
|
14
|
+
"typescript": "^5.7.2",
|
|
15
15
|
"vitest": "^2.1.8",
|
|
16
16
|
"@farcaster/tsconfig": "0.0.2"
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"
|
|
20
|
-
"
|
|
19
|
+
"@noble/curves": "^1.7.0",
|
|
20
|
+
"ox": "^0.4.4",
|
|
21
|
+
"zod": "^3.24.1",
|
|
22
|
+
"@farcaster/frame-core": "0.0.22"
|
|
21
23
|
},
|
|
22
24
|
"publishConfig": {
|
|
23
25
|
"access": "public"
|
package/src/farcaster.ts
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { z } from
|
|
3
|
-
import {
|
|
1
|
+
import { AbiParameters } from 'ox'
|
|
2
|
+
import { z } from 'zod'
|
|
3
|
+
import { BaseError, type VerifyAppKey, type VerifyAppKeyResult } from './types'
|
|
4
4
|
|
|
5
5
|
export const signedKeyRequestAbi = [
|
|
6
6
|
{
|
|
7
7
|
components: [
|
|
8
8
|
{
|
|
9
|
-
name:
|
|
10
|
-
type:
|
|
9
|
+
name: 'requestFid',
|
|
10
|
+
type: 'uint256',
|
|
11
11
|
},
|
|
12
12
|
{
|
|
13
|
-
name:
|
|
14
|
-
type:
|
|
13
|
+
name: 'requestSigner',
|
|
14
|
+
type: 'address',
|
|
15
15
|
},
|
|
16
16
|
{
|
|
17
|
-
name:
|
|
18
|
-
type:
|
|
17
|
+
name: 'signature',
|
|
18
|
+
type: 'bytes',
|
|
19
19
|
},
|
|
20
20
|
{
|
|
21
|
-
name:
|
|
22
|
-
type:
|
|
21
|
+
name: 'deadline',
|
|
22
|
+
type: 'uint256',
|
|
23
23
|
},
|
|
24
24
|
],
|
|
25
|
-
name:
|
|
26
|
-
type:
|
|
25
|
+
name: 'SignedKeyRequest',
|
|
26
|
+
type: 'tuple',
|
|
27
27
|
},
|
|
28
|
-
] as const
|
|
28
|
+
] as const
|
|
29
29
|
|
|
30
30
|
const hubResponseSchema = z.object({
|
|
31
31
|
events: z.array(
|
|
@@ -34,51 +34,49 @@ const hubResponseSchema = z.object({
|
|
|
34
34
|
key: z.string(),
|
|
35
35
|
metadata: z.string(),
|
|
36
36
|
}),
|
|
37
|
-
})
|
|
37
|
+
}),
|
|
38
38
|
),
|
|
39
|
-
})
|
|
39
|
+
})
|
|
40
40
|
|
|
41
41
|
export const createVerifyAppKeyWithHub: (
|
|
42
42
|
hubUrl: string,
|
|
43
|
-
requestOptions?: RequestInit
|
|
43
|
+
requestOptions?: RequestInit,
|
|
44
44
|
) => VerifyAppKey =
|
|
45
45
|
(hubUrl, requestOptions) =>
|
|
46
46
|
async (fid: number, appKey: string): Promise<VerifyAppKeyResult> => {
|
|
47
|
-
const url = new URL(
|
|
48
|
-
url.searchParams.append(
|
|
47
|
+
const url = new URL('/v1/onChainSignersByFid', hubUrl)
|
|
48
|
+
url.searchParams.append('fid', fid.toString())
|
|
49
49
|
|
|
50
|
-
const response = await fetch(url, requestOptions)
|
|
50
|
+
const response = await fetch(url, requestOptions)
|
|
51
51
|
|
|
52
52
|
if (response.status !== 200) {
|
|
53
|
-
throw new BaseError(
|
|
54
|
-
`Non-200 response received: ${await response.text()}`
|
|
55
|
-
);
|
|
53
|
+
throw new BaseError(`Non-200 response received: ${await response.text()}`)
|
|
56
54
|
}
|
|
57
55
|
|
|
58
|
-
const responseJson = await response.json()
|
|
59
|
-
const parsedResponse = hubResponseSchema.safeParse(responseJson)
|
|
56
|
+
const responseJson = await response.json()
|
|
57
|
+
const parsedResponse = hubResponseSchema.safeParse(responseJson)
|
|
60
58
|
if (parsedResponse.error) {
|
|
61
|
-
throw new BaseError(
|
|
59
|
+
throw new BaseError('Error parsing Hub response', parsedResponse.error)
|
|
62
60
|
}
|
|
63
61
|
|
|
64
|
-
const appKeyLower = appKey.toLowerCase()
|
|
62
|
+
const appKeyLower = appKey.toLowerCase()
|
|
65
63
|
|
|
66
64
|
const signerEvent = parsedResponse.data.events.find(
|
|
67
|
-
(event) => event.signerEventBody.key.toLowerCase() === appKeyLower
|
|
68
|
-
)
|
|
65
|
+
(event) => event.signerEventBody.key.toLowerCase() === appKeyLower,
|
|
66
|
+
)
|
|
69
67
|
if (!signerEvent) {
|
|
70
|
-
return { valid: false }
|
|
68
|
+
return { valid: false }
|
|
71
69
|
}
|
|
72
70
|
|
|
73
71
|
const decoded = AbiParameters.decode(
|
|
74
72
|
signedKeyRequestAbi,
|
|
75
|
-
Buffer.from(signerEvent.signerEventBody.metadata,
|
|
76
|
-
)
|
|
73
|
+
Buffer.from(signerEvent.signerEventBody.metadata, 'base64'),
|
|
74
|
+
)
|
|
77
75
|
if (decoded.length !== 1) {
|
|
78
|
-
throw new BaseError(
|
|
76
|
+
throw new BaseError('Error decoding metadata')
|
|
79
77
|
}
|
|
80
78
|
|
|
81
|
-
const appFid = Number(decoded[0].requestFid)
|
|
79
|
+
const appFid = Number(decoded[0].requestFid)
|
|
82
80
|
|
|
83
|
-
return { valid: true, appFid }
|
|
84
|
-
}
|
|
81
|
+
return { valid: true, appFid }
|
|
82
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
1
|
+
export * from '@farcaster/frame-core'
|
|
2
|
+
export * from './jfs'
|
|
3
|
+
export * from './neynar'
|
|
4
|
+
export * from './farcaster'
|
|
5
|
+
export * from './types'
|
|
6
|
+
export * from './webhook'
|
|
7
|
+
export * from './util'
|
package/src/jfs.ts
CHANGED
|
@@ -1,35 +1,40 @@
|
|
|
1
1
|
import {
|
|
2
|
-
EncodedJsonFarcasterSignatureSchema,
|
|
2
|
+
type EncodedJsonFarcasterSignatureSchema,
|
|
3
3
|
encodedJsonFarcasterSignatureSchema,
|
|
4
4
|
jsonFarcasterSignatureHeaderSchema,
|
|
5
|
-
} from
|
|
6
|
-
import { ed25519 } from
|
|
7
|
-
import {
|
|
8
|
-
|
|
5
|
+
} from '@farcaster/frame-core'
|
|
6
|
+
import { ed25519 } from '@noble/curves/ed25519'
|
|
7
|
+
import {
|
|
8
|
+
BaseError,
|
|
9
|
+
type VerifyAppKey,
|
|
10
|
+
type VerifyAppKeyResult,
|
|
11
|
+
type VerifyJfsResult,
|
|
12
|
+
} from './types'
|
|
13
|
+
import { bytesToHex, hexToBytes } from './util'
|
|
9
14
|
|
|
10
15
|
export declare namespace VerifyJsonFarcasterSignature {
|
|
11
16
|
type ErrorType =
|
|
12
17
|
| InvalidJfsDataError
|
|
13
18
|
| InvalidJfsAppKeyError
|
|
14
|
-
| VerifyAppKeyError
|
|
19
|
+
| VerifyAppKeyError
|
|
15
20
|
}
|
|
16
21
|
|
|
17
22
|
export class InvalidJfsDataError<
|
|
18
23
|
C extends Error | undefined = undefined,
|
|
19
24
|
> extends BaseError<C> {
|
|
20
|
-
override readonly name =
|
|
25
|
+
override readonly name = 'VerifyJsonFarcasterSignature.InvalidDataError'
|
|
21
26
|
}
|
|
22
27
|
|
|
23
28
|
export class InvalidJfsAppKeyError<
|
|
24
29
|
C extends Error | undefined = undefined,
|
|
25
30
|
> extends BaseError<C> {
|
|
26
|
-
override readonly name =
|
|
31
|
+
override readonly name = 'VerifyJsonFarcasterSignature.InvalidAppKeyError'
|
|
27
32
|
}
|
|
28
33
|
|
|
29
34
|
export class VerifyAppKeyError<
|
|
30
35
|
C extends Error | undefined = undefined,
|
|
31
36
|
> extends BaseError<C> {
|
|
32
|
-
override readonly name =
|
|
37
|
+
override readonly name = 'VerifyJsonFarcasterSignature.VerifyAppKeyError'
|
|
33
38
|
}
|
|
34
39
|
|
|
35
40
|
export async function verifyJsonFarcasterSignature(
|
|
@@ -40,77 +45,77 @@ export async function verifyJsonFarcasterSignature(
|
|
|
40
45
|
// Parse, decode and validate data
|
|
41
46
|
//
|
|
42
47
|
|
|
43
|
-
const body = encodedJsonFarcasterSignatureSchema.safeParse(data)
|
|
48
|
+
const body = encodedJsonFarcasterSignatureSchema.safeParse(data)
|
|
44
49
|
if (body.success === false) {
|
|
45
|
-
throw new InvalidJfsDataError(
|
|
50
|
+
throw new InvalidJfsDataError('Error parsing data', body.error)
|
|
46
51
|
}
|
|
47
52
|
|
|
48
|
-
let headerData
|
|
53
|
+
let headerData: any
|
|
49
54
|
try {
|
|
50
55
|
headerData = JSON.parse(
|
|
51
|
-
Buffer.from(body.data.header,
|
|
52
|
-
)
|
|
56
|
+
Buffer.from(body.data.header, 'base64url').toString('utf-8'),
|
|
57
|
+
)
|
|
53
58
|
} catch (error: unknown) {
|
|
54
|
-
throw new InvalidJfsDataError(
|
|
59
|
+
throw new InvalidJfsDataError('Error decoding and parsing header')
|
|
55
60
|
}
|
|
56
61
|
|
|
57
|
-
const header = jsonFarcasterSignatureHeaderSchema.safeParse(headerData)
|
|
62
|
+
const header = jsonFarcasterSignatureHeaderSchema.safeParse(headerData)
|
|
58
63
|
if (header.success === false) {
|
|
59
|
-
throw new InvalidJfsDataError(
|
|
64
|
+
throw new InvalidJfsDataError('Error parsing header', header.error)
|
|
60
65
|
}
|
|
61
66
|
|
|
62
|
-
const payload = Buffer.from(body.data.payload,
|
|
67
|
+
const payload = Buffer.from(body.data.payload, 'base64url')
|
|
63
68
|
|
|
64
|
-
const signature = Buffer.from(body.data.signature,
|
|
69
|
+
const signature = Buffer.from(body.data.signature, 'base64url')
|
|
65
70
|
if (signature.byteLength !== 64) {
|
|
66
|
-
throw new InvalidJfsDataError(
|
|
71
|
+
throw new InvalidJfsDataError('Invalid signature length')
|
|
67
72
|
}
|
|
68
73
|
|
|
69
74
|
//
|
|
70
75
|
// Verify the signature
|
|
71
76
|
//
|
|
72
77
|
|
|
73
|
-
const fid = header.data.fid
|
|
74
|
-
const appKey = header.data.key
|
|
75
|
-
const appKeyBytes = hexToBytes(appKey)
|
|
78
|
+
const fid = header.data.fid
|
|
79
|
+
const appKey = header.data.key
|
|
80
|
+
const appKeyBytes = hexToBytes(appKey)
|
|
76
81
|
|
|
77
82
|
const signedInput = new Uint8Array(
|
|
78
|
-
Buffer.from(body.data.header +
|
|
79
|
-
)
|
|
83
|
+
Buffer.from(body.data.header + '.' + body.data.payload),
|
|
84
|
+
)
|
|
80
85
|
|
|
81
|
-
let verifyResult
|
|
86
|
+
let verifyResult: boolean
|
|
82
87
|
try {
|
|
83
|
-
verifyResult = ed25519.verify(signature, signedInput, appKeyBytes)
|
|
88
|
+
verifyResult = ed25519.verify(signature, signedInput, appKeyBytes)
|
|
84
89
|
} catch (e) {
|
|
85
90
|
throw new InvalidJfsDataError(
|
|
86
|
-
|
|
91
|
+
'Error checking signature',
|
|
87
92
|
e instanceof Error ? e : undefined,
|
|
88
|
-
)
|
|
93
|
+
)
|
|
89
94
|
}
|
|
90
95
|
|
|
91
96
|
if (!verifyResult) {
|
|
92
|
-
throw new InvalidJfsDataError(
|
|
97
|
+
throw new InvalidJfsDataError('Invalid signature')
|
|
93
98
|
}
|
|
94
99
|
|
|
95
100
|
//
|
|
96
101
|
// Verify that the app key belongs to the FID
|
|
97
102
|
//
|
|
98
103
|
|
|
99
|
-
let appKeyResult
|
|
104
|
+
let appKeyResult: VerifyAppKeyResult
|
|
100
105
|
try {
|
|
101
|
-
appKeyResult = await verifyAppKey(fid, appKey)
|
|
106
|
+
appKeyResult = await verifyAppKey(fid, appKey)
|
|
102
107
|
} catch (error: unknown) {
|
|
103
108
|
throw new VerifyAppKeyError(
|
|
104
|
-
|
|
109
|
+
'Error verifying app key',
|
|
105
110
|
error instanceof Error ? error : undefined,
|
|
106
|
-
)
|
|
111
|
+
)
|
|
107
112
|
}
|
|
108
113
|
|
|
109
114
|
if (!appKeyResult.valid) {
|
|
110
|
-
throw new InvalidJfsAppKeyError(
|
|
115
|
+
throw new InvalidJfsAppKeyError('App key not valid for FID')
|
|
111
116
|
}
|
|
112
117
|
|
|
113
|
-
return { fid, appFid: appKeyResult.appFid, payload }
|
|
118
|
+
return { fid, appFid: appKeyResult.appFid, payload }
|
|
114
119
|
}
|
|
115
120
|
|
|
116
121
|
export function createJsonFarcasterSignature({
|
|
@@ -119,28 +124,28 @@ export function createJsonFarcasterSignature({
|
|
|
119
124
|
privateKey,
|
|
120
125
|
payload,
|
|
121
126
|
}: {
|
|
122
|
-
fid: number
|
|
123
|
-
type:
|
|
124
|
-
privateKey: Uint8Array
|
|
125
|
-
payload: Uint8Array
|
|
127
|
+
fid: number
|
|
128
|
+
type: 'app_key'
|
|
129
|
+
privateKey: Uint8Array
|
|
130
|
+
payload: Uint8Array
|
|
126
131
|
}): EncodedJsonFarcasterSignatureSchema {
|
|
127
|
-
const publicKey = ed25519.getPublicKey(privateKey)
|
|
132
|
+
const publicKey = ed25519.getPublicKey(privateKey)
|
|
128
133
|
|
|
129
|
-
const header = { fid, type, key: bytesToHex(publicKey) }
|
|
134
|
+
const header = { fid, type, key: bytesToHex(publicKey) }
|
|
130
135
|
const encodedHeader = Buffer.from(JSON.stringify(header)).toString(
|
|
131
|
-
|
|
132
|
-
)
|
|
133
|
-
const encodedPayload = Buffer.from(payload).toString(
|
|
136
|
+
'base64url',
|
|
137
|
+
)
|
|
138
|
+
const encodedPayload = Buffer.from(payload).toString('base64url')
|
|
134
139
|
const signatureInput = new Uint8Array(
|
|
135
|
-
Buffer.from(encodedHeader +
|
|
136
|
-
)
|
|
140
|
+
Buffer.from(encodedHeader + '.' + encodedPayload, 'utf-8'),
|
|
141
|
+
)
|
|
137
142
|
|
|
138
|
-
const signature = ed25519.sign(signatureInput, privateKey)
|
|
139
|
-
const encodedSignature = Buffer.from(signature).toString(
|
|
143
|
+
const signature = ed25519.sign(signatureInput, privateKey)
|
|
144
|
+
const encodedSignature = Buffer.from(signature).toString('base64url')
|
|
140
145
|
|
|
141
146
|
return {
|
|
142
147
|
header: encodedHeader,
|
|
143
148
|
payload: encodedPayload,
|
|
144
149
|
signature: encodedSignature,
|
|
145
|
-
}
|
|
150
|
+
}
|
|
146
151
|
}
|
package/src/neynar.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { createVerifyAppKeyWithHub } from
|
|
2
|
-
import { VerifyAppKey, VerifyAppKeyResult } from
|
|
1
|
+
import { createVerifyAppKeyWithHub } from './farcaster'
|
|
2
|
+
import type { VerifyAppKey, VerifyAppKeyResult } from './types'
|
|
3
3
|
|
|
4
|
-
const apiKey = process.env.NEYNAR_API_KEY ||
|
|
4
|
+
const apiKey = process.env.NEYNAR_API_KEY || ''
|
|
5
5
|
|
|
6
6
|
export const verifyAppKeyWithNeynar: VerifyAppKey = async (
|
|
7
7
|
fid: number,
|
|
8
|
-
appKey: string
|
|
8
|
+
appKey: string,
|
|
9
9
|
): Promise<VerifyAppKeyResult> => {
|
|
10
10
|
if (!apiKey) {
|
|
11
11
|
throw new Error(
|
|
12
|
-
|
|
13
|
-
)
|
|
12
|
+
'Environment variable NEYNAR_API_KEY needs to be set to use Neynar for app key verification',
|
|
13
|
+
)
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
const verifier = createVerifyAppKeyWithHub(
|
|
16
|
+
const verifier = createVerifyAppKeyWithHub('https://hub-api.neynar.com', {
|
|
17
17
|
headers: {
|
|
18
|
-
|
|
18
|
+
'x-api-key': apiKey,
|
|
19
19
|
},
|
|
20
|
-
})
|
|
20
|
+
})
|
|
21
21
|
|
|
22
|
-
return verifier(fid, appKey)
|
|
23
|
-
}
|
|
22
|
+
return verifier(fid, appKey)
|
|
23
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
import { FrameServerEvent } from
|
|
1
|
+
import type { FrameServerEvent } from '@farcaster/frame-core'
|
|
2
2
|
|
|
3
3
|
export type VerifyAppKeyResult =
|
|
4
4
|
| { valid: true; appFid: number }
|
|
5
|
-
| { valid: false }
|
|
5
|
+
| { valid: false }
|
|
6
6
|
|
|
7
7
|
export type VerifyAppKey = (
|
|
8
8
|
fid: number,
|
|
9
9
|
appKey: string,
|
|
10
|
-
) => Promise<VerifyAppKeyResult
|
|
10
|
+
) => Promise<VerifyAppKeyResult>
|
|
11
11
|
|
|
12
12
|
export type VerifyJfsResult = {
|
|
13
|
-
fid: number
|
|
14
|
-
appFid: number
|
|
15
|
-
payload: Uint8Array
|
|
16
|
-
}
|
|
13
|
+
fid: number
|
|
14
|
+
appFid: number
|
|
15
|
+
payload: Uint8Array
|
|
16
|
+
}
|
|
17
17
|
|
|
18
18
|
export type ParseWebhookEventResult = {
|
|
19
|
-
fid: number
|
|
20
|
-
appFid: number
|
|
21
|
-
event: FrameServerEvent
|
|
22
|
-
}
|
|
19
|
+
fid: number
|
|
20
|
+
appFid: number
|
|
21
|
+
event: FrameServerEvent
|
|
22
|
+
}
|
|
23
23
|
|
|
24
24
|
export class BaseError<C extends Error | undefined = undefined> extends Error {
|
|
25
|
-
override name =
|
|
26
|
-
cause: C
|
|
25
|
+
override name = 'BaseError'
|
|
26
|
+
cause: C
|
|
27
27
|
|
|
28
28
|
constructor(message: string, cause?: C) {
|
|
29
|
-
super(message)
|
|
30
|
-
this.cause = cause as any
|
|
29
|
+
super(message)
|
|
30
|
+
this.cause = cause as any
|
|
31
31
|
}
|
|
32
32
|
}
|
package/src/util.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export function bytesToHex(bytes: Uint8Array): string {
|
|
2
|
-
return `0x${Buffer.from(bytes).toString(
|
|
2
|
+
return `0x${Buffer.from(bytes).toString('hex')}`
|
|
3
3
|
}
|
|
4
4
|
|
|
5
5
|
export function hexToBytes(hex: string): Uint8Array {
|
|
6
6
|
return Uint8Array.from(
|
|
7
|
-
Buffer.from(hex.startsWith(
|
|
8
|
-
)
|
|
7
|
+
Buffer.from(hex.startsWith('0x') ? hex.slice(2) : hex, 'hex'),
|
|
8
|
+
)
|
|
9
9
|
}
|
package/src/webhook.ts
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
|
-
import { serverEventSchema } from
|
|
1
|
+
import { serverEventSchema } from '@farcaster/frame-core'
|
|
2
2
|
import {
|
|
3
|
-
VerifyJsonFarcasterSignature,
|
|
3
|
+
type VerifyJsonFarcasterSignature,
|
|
4
4
|
verifyJsonFarcasterSignature,
|
|
5
|
-
} from
|
|
6
|
-
import {
|
|
5
|
+
} from './jfs'
|
|
6
|
+
import {
|
|
7
|
+
BaseError,
|
|
8
|
+
type ParseWebhookEventResult,
|
|
9
|
+
type VerifyAppKey,
|
|
10
|
+
} from './types'
|
|
7
11
|
|
|
8
12
|
export declare namespace ParseWebhookEvent {
|
|
9
13
|
type ErrorType =
|
|
10
14
|
| VerifyJsonFarcasterSignature.ErrorType
|
|
11
|
-
| InvalidEventDataError
|
|
15
|
+
| InvalidEventDataError
|
|
12
16
|
}
|
|
13
17
|
|
|
14
18
|
export class InvalidEventDataError<
|
|
15
19
|
C extends Error | undefined = undefined,
|
|
16
20
|
> extends BaseError<C> {
|
|
17
|
-
override readonly name =
|
|
21
|
+
override readonly name = 'VerifyJsonFarcasterSignature.InvalidEventDataError'
|
|
18
22
|
}
|
|
19
23
|
|
|
20
24
|
export async function parseWebhookEvent(
|
|
@@ -24,23 +28,23 @@ export async function parseWebhookEvent(
|
|
|
24
28
|
const { fid, appFid, payload } = await verifyJsonFarcasterSignature(
|
|
25
29
|
rawData,
|
|
26
30
|
verifyAppKey,
|
|
27
|
-
)
|
|
31
|
+
)
|
|
28
32
|
|
|
29
33
|
// Pase and validate event payload
|
|
30
|
-
let payloadJson
|
|
34
|
+
let payloadJson: any
|
|
31
35
|
try {
|
|
32
|
-
payloadJson = JSON.parse(Buffer.from(payload).toString(
|
|
36
|
+
payloadJson = JSON.parse(Buffer.from(payload).toString('utf-8'))
|
|
33
37
|
} catch (error: unknown) {
|
|
34
38
|
throw new InvalidEventDataError(
|
|
35
|
-
|
|
39
|
+
'Error decoding and parsing payload',
|
|
36
40
|
error instanceof Error ? error : undefined,
|
|
37
|
-
)
|
|
41
|
+
)
|
|
38
42
|
}
|
|
39
43
|
|
|
40
|
-
const event = serverEventSchema.safeParse(payloadJson)
|
|
44
|
+
const event = serverEventSchema.safeParse(payloadJson)
|
|
41
45
|
if (event.success === false) {
|
|
42
|
-
throw new InvalidEventDataError(
|
|
46
|
+
throw new InvalidEventDataError('Invalid event payload', event.error)
|
|
43
47
|
}
|
|
44
48
|
|
|
45
|
-
return { fid, appFid, event: event.data }
|
|
49
|
+
return { fid, appFid, event: event.data }
|
|
46
50
|
}
|