mirror of
https://github.com/neynarxyz/create-farcaster-mini-app.git
synced 2025-12-06 01:12:34 -05:00
make seed phrase optional
This commit is contained in:
@@ -1,34 +1,12 @@
|
||||
import { readFileSync } from 'fs';
|
||||
import { join } from 'path';
|
||||
import { NextResponse } from 'next/server';
|
||||
import { generateFarcasterMetadata } from '../../../lib/utils';
|
||||
|
||||
export async function GET() {
|
||||
const appUrl = process.env.NEXT_PUBLIC_URL;
|
||||
const splashImageUrl = process.env.NEXT_PUBLIC_FRAME_SPLASH_IMAGE_URL || `${appUrl}/splash.png`;
|
||||
|
||||
let accountAssociation; // TODO: add type
|
||||
try {
|
||||
const manifestPath = join(process.cwd(), 'public/manifest.json');
|
||||
const manifest = JSON.parse(readFileSync(manifestPath, 'utf-8'));
|
||||
accountAssociation = manifest;
|
||||
const config = await generateFarcasterMetadata();
|
||||
return NextResponse.json(config);
|
||||
} catch (error) {
|
||||
console.warn('Warning: manifest.json not found or invalid. Frame will not be associated with an account.');
|
||||
accountAssociation = null;
|
||||
console.error('Error generating metadata:', error);
|
||||
return NextResponse.json({ error: error.message }, { status: 500 });
|
||||
}
|
||||
|
||||
const config = {
|
||||
...(accountAssociation && { accountAssociation }),
|
||||
frame: {
|
||||
version: "1",
|
||||
name: process.env.NEXT_PUBLIC_FRAME_NAME || "Frames v2 Demo",
|
||||
iconUrl: `${appUrl}/icon.png`,
|
||||
homeUrl: appUrl,
|
||||
imageUrl: `${appUrl}/frames/hello/opengraph-image`,
|
||||
buttonTitle: process.env.NEXT_PUBLIC_FRAME_BUTTON_TEXT || "Launch Frame",
|
||||
splashImageUrl,
|
||||
splashBackgroundColor: "#f7f7f7",
|
||||
webhookUrl: `${appUrl}/api/webhook`,
|
||||
},
|
||||
};
|
||||
|
||||
return Response.json(config);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ const Demo = dynamic(() => import("~/components/Demo"), {
|
||||
});
|
||||
|
||||
export default function App(
|
||||
{ title }: { title?: string } = { title: "Frames v2 Demo" }
|
||||
{ title }: { title?: string } = { title: process.env.NEXT_PUBLIC_FRAME_NAME || "Frames v2 Demo" }
|
||||
) {
|
||||
return <Demo title={title} />;
|
||||
}
|
||||
|
||||
18
src/lib/neynar.ts
Normal file
18
src/lib/neynar.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { NeynarAPIClient } from '@neynar/nodejs-sdk';
|
||||
|
||||
let neynarClient: NeynarAPIClient | null = null;
|
||||
|
||||
export function getNeynarClient() {
|
||||
if (!neynarClient) {
|
||||
const apiKey = process.env.NEYNAR_API_KEY;
|
||||
if (!apiKey) {
|
||||
throw new Error('NEYNAR_API_KEY not configured');
|
||||
}
|
||||
neynarClient = new NeynarAPIClient(apiKey);
|
||||
}
|
||||
return neynarClient;
|
||||
}
|
||||
|
||||
// Example usage:
|
||||
// const client = getNeynarClient();
|
||||
// const user = await client.lookupUserByFid(fid);
|
||||
@@ -1,6 +1,79 @@
|
||||
import { clsx, type ClassValue } from "clsx"
|
||||
import { twMerge } from "tailwind-merge"
|
||||
import { mnemonicToAccount } from 'viem/accounts';
|
||||
|
||||
export function cn(...inputs: ClassValue[]) {
|
||||
return twMerge(clsx(inputs))
|
||||
}
|
||||
|
||||
export function getSecretEnvVars() {
|
||||
const seedPhrase = process.env.SEED_PHRASE;
|
||||
const fid = process.env.FID;
|
||||
|
||||
if (!seedPhrase || !fid) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return { seedPhrase, fid };
|
||||
}
|
||||
|
||||
export async function generateFarcasterMetadata() {
|
||||
const appUrl = process.env.NEXT_PUBLIC_URL;
|
||||
if (!appUrl) {
|
||||
throw new Error('NEXT_PUBLIC_URL not configured');
|
||||
}
|
||||
|
||||
// Get the domain from the URL (without https:// prefix)
|
||||
const domain = new URL(appUrl).hostname;
|
||||
console.log('Using domain for manifest:', domain);
|
||||
|
||||
const secretEnvVars = getSecretEnvVars();
|
||||
if (!secretEnvVars) {
|
||||
console.warn('No seed phrase or FID found in environment variables -- generating unsigned metadata');
|
||||
}
|
||||
|
||||
let accountAssociation;
|
||||
if (secretEnvVars) {
|
||||
// Generate account from seed phrase
|
||||
const account = mnemonicToAccount(secretEnvVars.seedPhrase);
|
||||
const custodyAddress = account.address;
|
||||
|
||||
const header = {
|
||||
fid: parseInt(secretEnvVars.fid),
|
||||
type: 'custody',
|
||||
key: custodyAddress,
|
||||
};
|
||||
const encodedHeader = Buffer.from(JSON.stringify(header), 'utf-8').toString('base64');
|
||||
|
||||
const payload = {
|
||||
domain
|
||||
};
|
||||
const encodedPayload = Buffer.from(JSON.stringify(payload), 'utf-8').toString('base64url');
|
||||
|
||||
const signature = await account.signMessage({
|
||||
message: `${encodedHeader}.${encodedPayload}`
|
||||
});
|
||||
const encodedSignature = Buffer.from(signature, 'utf-8').toString('base64url');
|
||||
|
||||
accountAssociation = {
|
||||
header: encodedHeader,
|
||||
payload: encodedPayload,
|
||||
signature: encodedSignature
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
accountAssociation,
|
||||
frame: {
|
||||
version: "1",
|
||||
name: process.env.NEXT_PUBLIC_FRAME_NAME || "Frames v2 Demo",
|
||||
iconUrl: `${appUrl}/icon.png`,
|
||||
homeUrl: appUrl,
|
||||
imageUrl: `${appUrl}/opengraph-image`,
|
||||
buttonTitle: process.env.NEXT_PUBLIC_FRAME_BUTTON_TEXT || "Launch Frame",
|
||||
splashImageUrl: process.env.NEXT_PUBLIC_FRAME_SPLASH_IMAGE_URL || `${appUrl}/splash.png`,
|
||||
splashBackgroundColor: "#f7f7f7",
|
||||
webhookUrl: `${appUrl}/api/webhook`,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user