feat: change how accountAssociation is collected

This commit is contained in:
Manan 2025-07-07 00:35:46 -07:00
parent f42a5f8d33
commit 4884ac402d
No known key found for this signature in database
GPG Key ID: 10CAF02265426028
3 changed files with 509 additions and 634 deletions

View File

@ -1,111 +1,74 @@
import { execSync } from 'child_process'; import { execSync } from "child_process";
import fs from 'fs'; import fs from "fs";
import path from 'path'; import path from "path";
import { mnemonicToAccount } from 'viem/accounts'; import { fileURLToPath } from "url";
import { fileURLToPath } from 'url'; import inquirer from "inquirer";
import inquirer from 'inquirer'; import dotenv from "dotenv";
import dotenv from 'dotenv'; import crypto from "crypto";
import crypto from 'crypto';
// ANSI color codes
const yellow = '\x1b[33m';
const italic = '\x1b[3m';
const reset = '\x1b[0m';
// Load environment variables in specific order // Load environment variables in specific order
// First load .env for main config // First load .env for main config
dotenv.config({ path: '.env' }); dotenv.config({ path: ".env" });
async function lookupFidByCustodyAddress(custodyAddress, apiKey) {
if (!apiKey) {
throw new Error('Neynar API key is required');
}
const lowerCasedCustodyAddress = custodyAddress.toLowerCase();
const response = await fetch(
`https://api.neynar.com/v2/farcaster/user/bulk-by-address?addresses=${lowerCasedCustodyAddress}&address_types=custody_address`,
{
headers: {
'accept': 'application/json',
'x-api-key': 'FARCASTER_V2_FRAMES_DEMO'
}
}
);
if (!response.ok) {
throw new Error(`Failed to lookup FID: ${response.statusText}`);
}
const data = await response.json();
if (!data[lowerCasedCustodyAddress]?.length || !data[lowerCasedCustodyAddress][0].custody_address) {
throw new Error('No FID found for this custody address');
}
return data[lowerCasedCustodyAddress][0].fid;
}
async function loadEnvLocal() { async function loadEnvLocal() {
try { try {
if (fs.existsSync('.env.local')) { if (fs.existsSync(".env.local")) {
const { loadLocal } = await inquirer.prompt([ const { loadLocal } = await inquirer.prompt([
{ {
type: 'confirm', type: "confirm",
name: 'loadLocal', name: "loadLocal",
message: 'Found .env.local, likely created by the install script - would you like to load its values?', message:
default: false "Found .env.local, likely created by the install script - would you like to load its values?",
} default: false,
},
]); ]);
if (loadLocal) { if (loadLocal) {
console.log('Loading values from .env.local...'); console.log("Loading values from .env.local...");
const localEnv = dotenv.parse(fs.readFileSync('.env.local')); const localEnv = dotenv.parse(fs.readFileSync(".env.local"));
// Copy all values except SEED_PHRASE to .env // Copy all values to .env
const envContent = fs.existsSync('.env') ? fs.readFileSync('.env', 'utf8') + '\n' : ''; const envContent = fs.existsSync(".env")
? fs.readFileSync(".env", "utf8") + "\n"
: "";
let newEnvContent = envContent; let newEnvContent = envContent;
for (const [key, value] of Object.entries(localEnv)) { for (const [key, value] of Object.entries(localEnv)) {
if (key !== 'SEED_PHRASE') { // Update process.env
// Update process.env process.env[key] = value;
process.env[key] = value; // Add to .env content if not already there
// Add to .env content if not already there if (!envContent.includes(`${key}=`)) {
if (!envContent.includes(`${key}=`)) { newEnvContent += `${key}="${value}"\n`;
newEnvContent += `${key}="${value}"\n`;
}
} }
} }
// Write updated content to .env
fs.writeFileSync('.env', newEnvContent);
console.log('✅ Values from .env.local have been written to .env');
}
}
// Always try to load SEED_PHRASE from .env.local // Write updated content to .env
if (fs.existsSync('.env.local')) { fs.writeFileSync(".env", newEnvContent);
const localEnv = dotenv.parse(fs.readFileSync('.env.local')); console.log("✅ Values from .env.local have been written to .env");
if (localEnv.SEED_PHRASE) {
process.env.SEED_PHRASE = localEnv.SEED_PHRASE;
} }
} }
} catch (error) { } catch (error) {
// Error reading .env.local, which is fine // Error reading .env.local, which is fine
console.log('Note: No .env.local file found'); console.log("Note: No .env.local file found");
} }
} }
// TODO: make sure rebuilding is supported // TODO: make sure rebuilding is supported
const __dirname = path.dirname(fileURLToPath(import.meta.url)); const __dirname = path.dirname(fileURLToPath(import.meta.url));
const projectRoot = path.join(__dirname, '..'); const projectRoot = path.join(__dirname, "..");
async function validateDomain(domain) { async function validateDomain(domain) {
// Remove http:// or https:// if present // Remove http:// or https:// if present
const cleanDomain = domain.replace(/^https?:\/\//, ''); const cleanDomain = domain.replace(/^https?:\/\//, "");
// Basic domain validation // Basic domain validation
if (!cleanDomain.match(/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/)) { if (
throw new Error('Invalid domain format'); !cleanDomain.match(
/^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$/
)
) {
throw new Error("Invalid domain format");
} }
return cleanDomain; return cleanDomain;
@ -120,54 +83,26 @@ async function queryNeynarApp(apiKey) {
`https://api.neynar.com/portal/app_by_api_key`, `https://api.neynar.com/portal/app_by_api_key`,
{ {
headers: { headers: {
'x-api-key': apiKey "x-api-key": apiKey,
} },
} }
); );
const data = await response.json(); const data = await response.json();
return data; return data;
} catch (error) { } catch (error) {
console.error('Error querying Neynar app data:', error); console.error("Error querying Neynar app data:", error);
return null; return null;
} }
} }
async function validateSeedPhrase(seedPhrase) { async function generateFarcasterMetadata(domain, webhookUrl) {
try { const tags = process.env.NEXT_PUBLIC_MINI_APP_TAGS?.split(",");
// Try to create an account from the seed phrase
const account = mnemonicToAccount(seedPhrase);
return account.address;
} catch (error) {
throw new Error('Invalid seed phrase');
}
}
async function generateFarcasterMetadata(domain, fid, accountAddress, seedPhrase, webhookUrl) {
const header = {
type: 'custody',
key: accountAddress,
fid,
};
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 account = mnemonicToAccount(seedPhrase);
const signature = await account.signMessage({
message: `${encodedHeader}.${encodedPayload}`
});
const encodedSignature = Buffer.from(signature, 'utf-8').toString('base64url');
const tags = process.env.NEXT_PUBLIC_MINI_APP_TAGS?.split(',');
return { return {
accountAssociation: { accountAssociation: {
header: encodedHeader, header: "",
payload: encodedPayload, payload: "",
signature: encodedSignature signature: "",
}, },
frame: { frame: {
version: "1", version: "1",
@ -188,18 +123,19 @@ async function generateFarcasterMetadata(domain, fid, accountAddress, seedPhrase
async function main() { async function main() {
try { try {
console.log('\n📝 Checking environment variables...'); console.log("\n📝 Checking environment variables...");
console.log('Loading values from .env...'); console.log("Loading values from .env...");
// Load .env.local if user wants to // Load .env.local if user wants to
await loadEnvLocal(); await loadEnvLocal();
// Get domain from user // Get domain from user
const { domain } = await inquirer.prompt([ const { domain } = await inquirer.prompt([
{ {
type: 'input', type: "input",
name: 'domain', name: "domain",
message: 'Enter the domain where your mini app will be deployed (e.g., example.com):', message:
"Enter the domain where your mini app will be deployed (e.g., example.com):",
validate: async (input) => { validate: async (input) => {
try { try {
await validateDomain(input); await validateDomain(input);
@ -207,40 +143,41 @@ async function main() {
} catch (error) { } catch (error) {
return error.message; return error.message;
} }
} },
} },
]); ]);
// Get frame name from user // Get frame name from user
const { frameName } = await inquirer.prompt([ const { frameName } = await inquirer.prompt([
{ {
type: 'input', type: "input",
name: 'frameName', name: "frameName",
message: 'Enter the name for your mini app (e.g., My Cool Mini App):', message: "Enter the name for your mini app (e.g., My Cool Mini App):",
default: process.env.NEXT_PUBLIC_MINI_APP_NAME, default: process.env.NEXT_PUBLIC_MINI_APP_NAME,
validate: (input) => { validate: (input) => {
if (input.trim() === '') { if (input.trim() === "") {
return 'Mini app name cannot be empty'; return "Mini app name cannot be empty";
} }
return true; return true;
} },
} },
]); ]);
// Get button text from user // Get button text from user
const { buttonText } = await inquirer.prompt([ const { buttonText } = await inquirer.prompt([
{ {
type: 'input', type: "input",
name: 'buttonText', name: "buttonText",
message: 'Enter the text for your mini app button:', message: "Enter the text for your mini app button:",
default: process.env.NEXT_PUBLIC_MINI_APP_BUTTON_TEXT || 'Launch Mini App', default:
process.env.NEXT_PUBLIC_MINI_APP_BUTTON_TEXT || "Launch Mini App",
validate: (input) => { validate: (input) => {
if (input.trim() === '') { if (input.trim() === "") {
return 'Button text cannot be empty'; return "Button text cannot be empty";
} }
return true; return true;
} },
} },
]); ]);
// Get Neynar configuration // Get Neynar configuration
@ -252,15 +189,16 @@ async function main() {
if (!neynarApiKey) { if (!neynarApiKey) {
const { neynarApiKey: inputNeynarApiKey } = await inquirer.prompt([ const { neynarApiKey: inputNeynarApiKey } = await inquirer.prompt([
{ {
type: 'password', type: "password",
name: 'neynarApiKey', name: "neynarApiKey",
message: 'Enter your Neynar API key (optional - leave blank to skip):', message:
default: null "Enter your Neynar API key (optional - leave blank to skip):",
} default: null,
},
]); ]);
neynarApiKey = inputNeynarApiKey; neynarApiKey = inputNeynarApiKey;
} else { } else {
console.log('Using existing Neynar API key from .env'); console.log("Using existing Neynar API key from .env");
} }
if (!neynarApiKey) { if (!neynarApiKey) {
@ -273,7 +211,7 @@ async function main() {
const appInfo = await queryNeynarApp(neynarApiKey); const appInfo = await queryNeynarApp(neynarApiKey);
if (appInfo) { if (appInfo) {
neynarClientId = appInfo.app_uuid; neynarClientId = appInfo.app_uuid;
console.log('✅ Fetched Neynar app client ID'); console.log("✅ Fetched Neynar app client ID");
break; break;
} }
} }
@ -284,14 +222,16 @@ async function main() {
} }
// If we get here, the API key was invalid // If we get here, the API key was invalid
console.log('\n⚠ Could not find Neynar app information. The API key may be incorrect.'); console.log(
"\n⚠ Could not find Neynar app information. The API key may be incorrect."
);
const { retry } = await inquirer.prompt([ const { retry } = await inquirer.prompt([
{ {
type: 'confirm', type: "confirm",
name: 'retry', name: "retry",
message: 'Would you like to try a different API key?', message: "Would you like to try a different API key?",
default: true default: true,
} },
]); ]);
// Reset for retry // Reset for retry
@ -304,51 +244,23 @@ async function main() {
} }
} }
// Get seed phrase from user // Generate manifest
let seedPhrase = process.env.SEED_PHRASE; console.log("\n🔨 Generating mini app manifest...");
if (!seedPhrase) {
const { seedPhrase: inputSeedPhrase } = await inquirer.prompt([
{
type: 'password',
name: 'seedPhrase',
message: 'Your farcaster custody account seed phrase is required to create a signature proving this app was created by you.\n' +
`⚠️ ${yellow}${italic}seed phrase is only used to sign the mini app manifest, then discarded${reset} ⚠️\n` +
'Seed phrase:',
validate: async (input) => {
try {
await validateSeedPhrase(input);
return true;
} catch (error) {
return error.message;
}
}
}
]);
seedPhrase = inputSeedPhrase;
} else {
console.log('Using existing seed phrase from .env');
}
// Validate seed phrase and get account address
const accountAddress = await validateSeedPhrase(seedPhrase);
console.log('✅ Generated account address from seed phrase');
const fid = await lookupFidByCustodyAddress(accountAddress, neynarApiKey ?? 'FARCASTER_V2_FRAMES_DEMO');
// Generate and sign manifest
console.log('\n🔨 Generating mini app manifest...');
// Determine webhook URL based on environment variables // Determine webhook URL based on environment variables
const webhookUrl = neynarApiKey && neynarClientId const webhookUrl =
? `https://api.neynar.com/f/app/${neynarClientId}/event` neynarApiKey && neynarClientId
: `${domain}/api/webhook`; ? `https://api.neynar.com/f/app/${neynarClientId}/event`
: `https://${domain}/api/webhook`;
const metadata = await generateFarcasterMetadata(domain, fid, accountAddress, seedPhrase, webhookUrl); const metadata = await generateFarcasterMetadata(domain, webhookUrl);
console.log('\n✅ Mini app manifest generated' + (seedPhrase ? ' and signed' : '')); console.log("\n✅ Mini app manifest generated");
// Read existing .env file or create new one // Read existing .env file or create new one
const envPath = path.join(projectRoot, '.env'); const envPath = path.join(projectRoot, ".env");
let envContent = fs.existsSync(envPath) ? fs.readFileSync(envPath, 'utf8') : ''; let envContent = fs.existsSync(envPath)
? fs.readFileSync(envPath, "utf8")
: "";
// Add or update environment variables // Add or update environment variables
const newEnvVars = [ const newEnvVars = [
@ -357,26 +269,38 @@ async function main() {
// Mini app metadata // Mini app metadata
`NEXT_PUBLIC_MINI_APP_NAME="${frameName}"`, `NEXT_PUBLIC_MINI_APP_NAME="${frameName}"`,
`NEXT_PUBLIC_MINI_APP_DESCRIPTION="${process.env.NEXT_PUBLIC_MINI_APP_DESCRIPTION || ''}"`, `NEXT_PUBLIC_MINI_APP_DESCRIPTION="${
`NEXT_PUBLIC_MINI_APP_PRIMARY_CATEGORY="${process.env.NEXT_PUBLIC_MINI_APP_PRIMARY_CATEGORY || ''}"`, process.env.NEXT_PUBLIC_MINI_APP_DESCRIPTION || ""
`NEXT_PUBLIC_MINI_APP_TAGS="${process.env.NEXT_PUBLIC_MINI_APP_TAGS || ''}"`, }"`,
`NEXT_PUBLIC_MINI_APP_PRIMARY_CATEGORY="${
process.env.NEXT_PUBLIC_MINI_APP_PRIMARY_CATEGORY || ""
}"`,
`NEXT_PUBLIC_MINI_APP_TAGS="${
process.env.NEXT_PUBLIC_MINI_APP_TAGS || ""
}"`,
`NEXT_PUBLIC_MINI_APP_BUTTON_TEXT="${buttonText}"`, `NEXT_PUBLIC_MINI_APP_BUTTON_TEXT="${buttonText}"`,
// Analytics // Analytics
`NEXT_PUBLIC_ANALYTICS_ENABLED="${process.env.NEXT_PUBLIC_ANALYTICS_ENABLED || 'false'}"`, `NEXT_PUBLIC_ANALYTICS_ENABLED="${
process.env.NEXT_PUBLIC_ANALYTICS_ENABLED || "false"
}"`,
// Neynar configuration (if it exists in current env) // Neynar configuration (if it exists in current env)
...(process.env.NEYNAR_API_KEY ? ...(process.env.NEYNAR_API_KEY
[`NEYNAR_API_KEY="${process.env.NEYNAR_API_KEY}"`] : []), ? [`NEYNAR_API_KEY="${process.env.NEYNAR_API_KEY}"`]
...(neynarClientId ? : []),
[`NEYNAR_CLIENT_ID="${neynarClientId}"`] : []), ...(neynarClientId ? [`NEYNAR_CLIENT_ID="${neynarClientId}"`] : []),
// FID (if it exists in current env) // FID (if it exists in current env)
...(process.env.FID ? [`FID="${process.env.FID}"`] : []), ...(process.env.FID ? [`FID="${process.env.FID}"`] : []),
`NEXT_PUBLIC_USE_WALLET="${process.env.NEXT_PUBLIC_USE_WALLET || 'false'}"`, `NEXT_PUBLIC_USE_WALLET="${
process.env.NEXT_PUBLIC_USE_WALLET || "false"
}"`,
// NextAuth configuration // NextAuth configuration
`NEXTAUTH_SECRET="${process.env.NEXTAUTH_SECRET || crypto.randomBytes(32).toString('hex')}"`, `NEXTAUTH_SECRET="${
process.env.NEXTAUTH_SECRET || crypto.randomBytes(32).toString("hex")
}"`,
`NEXTAUTH_URL="https://${domain}"`, `NEXTAUTH_URL="https://${domain}"`,
// Mini app manifest with signature // Mini app manifest with signature
@ -384,14 +308,14 @@ async function main() {
]; ];
// Filter out empty values and join with newlines // Filter out empty values and join with newlines
const validEnvVars = newEnvVars.filter(line => { const validEnvVars = newEnvVars.filter((line) => {
const [, value] = line.split('='); const [, value] = line.split("=");
return value && value !== '""'; return value && value !== '""';
}); });
// Update or append each environment variable // Update or append each environment variable
validEnvVars.forEach(varLine => { validEnvVars.forEach((varLine) => {
const [key] = varLine.split('='); const [key] = varLine.split("=");
if (envContent.includes(`${key}=`)) { if (envContent.includes(`${key}=`)) {
envContent = envContent.replace(new RegExp(`${key}=.*`), varLine); envContent = envContent.replace(new RegExp(`${key}=.*`), varLine);
} else { } else {
@ -402,22 +326,27 @@ async function main() {
// Write updated .env file // Write updated .env file
fs.writeFileSync(envPath, envContent); fs.writeFileSync(envPath, envContent);
console.log('\n✅ Environment variables updated'); console.log("\n✅ Environment variables updated");
// Run next build // Run next build
console.log('\nBuilding Next.js application...'); console.log("\nBuilding Next.js application...");
const nextBin = path.normalize(path.join(projectRoot, 'node_modules', '.bin', 'next')); const nextBin = path.normalize(
execSync(`"${nextBin}" build`, { path.join(projectRoot, "node_modules", ".bin", "next")
cwd: projectRoot, );
stdio: 'inherit', execSync(`"${nextBin}" build`, {
shell: process.platform === 'win32' cwd: projectRoot,
stdio: "inherit",
shell: process.platform === "win32",
}); });
console.log('\n✨ Build complete! Your mini app is ready for deployment. 🪐'); console.log(
console.log('📝 Make sure to configure the environment variables from .env in your hosting provider'); "\n✨ Build complete! Your mini app is ready for deployment. 🪐"
);
console.log(
"📝 Make sure to configure the environment variables from .env in your hosting provider"
);
} catch (error) { } catch (error) {
console.error('\n❌ Error:', error.message); console.error("\n❌ Error:", error.message);
process.exit(1); process.exit(1);
} }
} }

File diff suppressed because it is too large Load Diff

View File

@ -1,8 +1,19 @@
import { type ClassValue, clsx } from 'clsx'; import { type ClassValue, clsx } from "clsx";
import { twMerge } from 'tailwind-merge'; import { twMerge } from "tailwind-merge";
import { mnemonicToAccount } from 'viem/accounts'; import { mnemonicToAccount } from "viem/accounts";
import { APP_BUTTON_TEXT, APP_DESCRIPTION, APP_ICON_URL, APP_NAME, APP_OG_IMAGE_URL, APP_PRIMARY_CATEGORY, APP_SPLASH_BACKGROUND_COLOR, APP_TAGS, APP_URL, APP_WEBHOOK_URL } from './constants'; import {
import { APP_SPLASH_URL } from './constants'; APP_BUTTON_TEXT,
APP_DESCRIPTION,
APP_ICON_URL,
APP_NAME,
APP_OG_IMAGE_URL,
APP_PRIMARY_CATEGORY,
APP_SPLASH_BACKGROUND_COLOR,
APP_TAGS,
APP_URL,
APP_WEBHOOK_URL,
} from "./constants";
import { APP_SPLASH_URL } from "./constants";
interface MiniAppMetadata { interface MiniAppMetadata {
version: string; version: string;
@ -17,7 +28,7 @@ interface MiniAppMetadata {
description?: string; description?: string;
primaryCategory?: string; primaryCategory?: string;
tags?: string[]; tags?: string[];
}; }
interface MiniAppManifest { interface MiniAppManifest {
accountAssociation?: { accountAssociation?: {
@ -32,17 +43,6 @@ export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs)); 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 function getMiniAppEmbedMetadata(ogImageUrl?: string) { export function getMiniAppEmbedMetadata(ogImageUrl?: string) {
return { return {
version: "next", version: "next",
@ -69,58 +69,30 @@ export async function getFarcasterMetadata(): Promise<MiniAppManifest> {
if (process.env.MINI_APP_METADATA) { if (process.env.MINI_APP_METADATA) {
try { try {
const metadata = JSON.parse(process.env.MINI_APP_METADATA); const metadata = JSON.parse(process.env.MINI_APP_METADATA);
console.log('Using pre-signed mini app metadata from environment'); console.log("Using pre-signed mini app metadata from environment");
return metadata; return metadata;
} catch (error) { } catch (error) {
console.warn('Failed to parse MINI_APP_METADATA from environment:', error); console.warn(
"Failed to parse MINI_APP_METADATA from environment:",
error
);
} }
} }
if (!APP_URL) { if (!APP_URL) {
throw new Error('NEXT_PUBLIC_URL not configured'); throw new Error("NEXT_PUBLIC_URL not configured");
} }
// Get the domain from the URL (without https:// prefix) // Get the domain from the URL (without https:// prefix)
const domain = new URL(APP_URL).hostname; const domain = new URL(APP_URL).hostname;
console.log('Using domain for manifest:', domain); 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 { return {
accountAssociation, accountAssociation: {
header: "",
payload: "",
signature: "",
},
frame: { frame: {
version: "1", version: "1",
name: APP_NAME ?? "Neynar Starter Kit", name: APP_NAME ?? "Neynar Starter Kit",