-
-
Notifications
You must be signed in to change notification settings - Fork 98
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
10 changed files
with
243 additions
and
10 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,58 @@ | ||
import { Button, Frog } from 'frog' | ||
import { type NeynarVariables, neynar } from 'frog/middlewares' | ||
|
||
export const app = new Frog<{ | ||
Variables: NeynarVariables | ||
}>() | ||
|
||
app.use( | ||
neynar({ | ||
apiKey: 'NEYNAR_FROG_FM', | ||
features: ['interactor', 'cast'], | ||
}), | ||
) | ||
|
||
app.frame('/', (c) => { | ||
return c.res({ | ||
action: '/guess', | ||
image: ( | ||
<div | ||
style={{ | ||
alignItems: 'center', | ||
color: 'white', | ||
display: 'flex', | ||
justifyContent: 'center', | ||
fontSize: 48, | ||
height: '100%', | ||
width: '100%', | ||
}} | ||
> | ||
I can guess your name and follower count. | ||
</div> | ||
), | ||
intents: [<Button>Go on</Button>], | ||
}) | ||
}) | ||
|
||
app.frame('/guess', (c) => { | ||
const { displayName, followerCount } = c.var.interactor || {} | ||
console.log('interactor: ', c.var.interactor) | ||
console.log('cast: ', c.var.cast) | ||
return c.res({ | ||
image: ( | ||
<div | ||
style={{ | ||
alignItems: 'center', | ||
color: 'white', | ||
display: 'flex', | ||
justifyContent: 'center', | ||
fontSize: 48, | ||
height: '100%', | ||
width: '100%', | ||
}} | ||
> | ||
Greetings {displayName}, you have {followerCount} followers. | ||
</div> | ||
), | ||
}) | ||
}) |
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 @@ | ||
export { neynar, type NeynarVariables } from './neynar.js' |
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,170 @@ | ||
import type { MiddlewareHandler } from 'hono' | ||
import { hexToBytes } from 'viem' | ||
import { Message } from '../protobufs/generated/message_pb.js' | ||
import { messageToFrameData } from '../utils/verifyFrame.js' | ||
|
||
export type NeynarVariables = { | ||
cast?: Cast | undefined | ||
interactor?: User | undefined | ||
} | ||
|
||
export type NeynarMiddlewareParameters = { | ||
apiKey: string | ||
features: ('interactor' | 'cast')[] | ||
} | ||
|
||
export function neynar( | ||
parameters: NeynarMiddlewareParameters, | ||
): MiddlewareHandler<{ | ||
Variables: NeynarVariables | ||
}> { | ||
const { apiKey, features } = parameters | ||
return async (c, next) => { | ||
const { trustedData } = (await c.req.json().catch(() => {})) || {} | ||
if (!trustedData) return await next() | ||
|
||
// Note: We are not verifying here as we verify downstream (internal Frog handler). | ||
const body = hexToBytes(`0x${trustedData.messageBytes}`) | ||
const message = Message.fromBinary(body) | ||
const frameData = messageToFrameData(message) | ||
|
||
const { | ||
castId: { fid: castFid, hash }, | ||
fid, | ||
} = frameData | ||
|
||
const [castResponse, usersResponse] = await Promise.all([ | ||
features.includes('cast') | ||
? getCast({ | ||
apiKey, | ||
hash, | ||
}) | ||
: Promise.resolve(undefined), | ||
features.includes('interactor') | ||
? getUsers({ apiKey, castFid, fids: [fid] }) | ||
: Promise.resolve(undefined), | ||
]) | ||
|
||
if (castResponse) c.set('cast', castResponse.cast) | ||
if (usersResponse) { | ||
const [user] = usersResponse.users | ||
if (user) c.set('interactor', user) | ||
} | ||
|
||
await next() | ||
} | ||
} | ||
|
||
/////////////////////////////////////////////////////////////////////////// | ||
// Utilities | ||
|
||
const neynarApiUrl = 'https://api.neynar.com' | ||
|
||
type GetCastParameters = { apiKey: string; hash: string } | ||
type GetCastReturnType = { | ||
cast: Cast | ||
} | ||
|
||
async function getCast({ | ||
apiKey, | ||
hash, | ||
}: GetCastParameters): Promise<GetCastReturnType> { | ||
const response = await fetch( | ||
`${neynarApiUrl}/v2/farcaster/cast?type=hash&identifier=${hash}`, | ||
{ | ||
headers: { | ||
api_key: apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
).then((res) => res.json()) | ||
return camelCaseKeys(response) as GetCastReturnType | ||
} | ||
|
||
type GetUsersParameters = { apiKey: string; castFid: number; fids: number[] } | ||
type GetUsersReturnType = { | ||
users: User[] | ||
} | ||
|
||
async function getUsers({ | ||
apiKey, | ||
castFid, | ||
fids, | ||
}: GetUsersParameters): Promise<GetUsersReturnType> { | ||
const response = await fetch( | ||
`${neynarApiUrl}/v2/farcaster/user/bulk?fids=${fids.join( | ||
',', | ||
)}&viewer_fid=${castFid}`, | ||
{ | ||
headers: { | ||
api_key: apiKey, | ||
'Content-Type': 'application/json', | ||
}, | ||
}, | ||
).then((res) => res.json()) | ||
return camelCaseKeys(response) as GetUsersReturnType | ||
} | ||
|
||
function camelCaseKeys(response: object): object { | ||
if (!response) return response | ||
if (typeof response !== 'object') return response | ||
if (Array.isArray(response)) return response.map(camelCaseKeys) | ||
return Object.fromEntries( | ||
Object.entries(response).map(([key, value]) => [ | ||
key.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase()), | ||
camelCaseKeys(value), | ||
]), | ||
) | ||
} | ||
|
||
/////////////////////////////////////////////////////////////////////////// | ||
// Types | ||
|
||
export type Cast = { | ||
author: User | ||
embeds: { url: string }[] | ||
// TODO: populate with real type. | ||
frames: unknown | ||
hash: string | ||
mentionedProfiles: User[] | ||
object: 'cast' | ||
parentAuthor: { fid: number | null } | ||
parentHash: string | null | ||
parentUrl: string | ||
reactions: { | ||
likes: { fid: number; fname: string }[] | ||
recasts: { fid: number; fname: string }[] | ||
} | ||
replies: { count: number } | ||
rootParentUrl: string | ||
text: string | ||
threadHash: string | ||
timestamp: string | ||
} | ||
|
||
export type User = { | ||
activeStatus: 'active' | 'inactive' | ||
custodyAddress: string | ||
displayName: string | ||
fid: number | ||
followerCount: number | ||
followingCount: number | ||
object: 'user' | ||
pfpUrl: string | ||
profile: { | ||
bio: { | ||
text: string | ||
mentionedProfiles: string[] | ||
} | ||
} | ||
username: string | ||
verifications: string[] | ||
verifiedAddresses: { | ||
ethAddresses: string[] | ||
solAddresses: string[] | ||
} | ||
viewerContext?: { | ||
following: boolean | ||
followedBy: boolean | ||
} | ||
} |
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,5 @@ | ||
{ | ||
"type": "module", | ||
"types": "../_lib/middlewares/index.d.ts", | ||
"module": "../_lib/middlewares/index.js" | ||
} |
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
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 |
---|---|---|
|
@@ -27,6 +27,5 @@ export function requestQueryToContext< | |
return { | ||
...queryContext, | ||
req: c.req, | ||
var: c.var, | ||
} | ||
} |
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