375 lines
7.5 KiB
Markdown
375 lines
7.5 KiB
Markdown
# ๐ญ Complete Farcaster Registration Guide
|
|
|
|
## ๐ Two Registration Methods
|
|
|
|
### 1. **Bundler** (Recommended)
|
|
Register FID + Add Keys + Rent Storage in **one transaction**
|
|
|
|
### 2. **ID Gateway**
|
|
Register FID only (keys added separately)
|
|
|
|
---
|
|
|
|
## ๐ Method 1: Bundler (All-in-One)
|
|
|
|
### When to Use
|
|
- โ
New user signup
|
|
- โ
Need account keys immediately
|
|
- โ
Want single transaction
|
|
- โ
Best user experience
|
|
|
|
### Implementation
|
|
```typescript
|
|
import { registerFarcasterAccount } from '@/lib/farcaster/register'
|
|
|
|
const result = await registerFarcasterAccount(userPrivateKey)
|
|
// Returns: { fid, accountPrivateKey, txHash, price }
|
|
```
|
|
|
|
### What It Does
|
|
1. Registers FID
|
|
2. Adds account key
|
|
3. Rents 1 storage unit
|
|
4. Returns everything needed
|
|
|
|
### Cost
|
|
~$5 USD (includes FID + key + storage)
|
|
|
|
---
|
|
|
|
## ๐ง Method 2: ID Gateway (FID Only)
|
|
|
|
### When to Use
|
|
- โ
Just need FID
|
|
- โ
Will add keys later
|
|
- โ
Custom key management
|
|
- โ
Lower initial cost
|
|
|
|
### Implementation
|
|
|
|
#### Option A: Direct Registration
|
|
```typescript
|
|
import { registerFID } from '@/lib/farcaster/id-gateway'
|
|
|
|
const result = await registerFID(privateKey, 0n)
|
|
// Returns: { fid, txHash, price }
|
|
```
|
|
|
|
#### Option B: Register For Another Address
|
|
```typescript
|
|
import { registerFor, generateRegisterSignature } from '@/lib/farcaster/id-gateway'
|
|
|
|
// 1. User generates signature
|
|
const deadline = BigInt(Math.floor(Date.now() / 1000) + 3600)
|
|
const signature = await generateRegisterSignature(userPrivateKey, deadline)
|
|
|
|
// 2. App pays for registration
|
|
const result = await registerFor(
|
|
appPrivateKey,
|
|
userAddress,
|
|
signature,
|
|
deadline,
|
|
0n
|
|
)
|
|
```
|
|
|
|
### Cost
|
|
~$3 USD (FID + 1 storage unit only)
|
|
|
|
---
|
|
|
|
## ๐ Comparison
|
|
|
|
| Feature | Bundler | ID Gateway |
|
|
|---------|---------|------------|
|
|
| FID Registration | โ
| โ
|
|
|
| Account Keys | โ
| โ (separate) |
|
|
| Storage | โ
| โ
|
|
|
| Transactions | 1 | 2+ |
|
|
| Cost | ~$5 | ~$3 + key cost |
|
|
| Complexity | Low | Medium |
|
|
| Best For | New users | Advanced use |
|
|
|
|
---
|
|
|
|
## ๐ฏ Recommended Flow for PollCelo
|
|
|
|
### For New Users (Bundler)
|
|
```typescript
|
|
// 1. User clicks "Sign Up"
|
|
// 2. Generate wallet
|
|
const privateKey = generatePrivateKey()
|
|
|
|
// 3. Register with Bundler
|
|
const { fid, accountPrivateKey } = await registerFarcasterAccount(privateKey)
|
|
|
|
// 4. Store credentials
|
|
localStorage.setItem('farcaster_fid', fid)
|
|
localStorage.setItem('farcaster_key', accountPrivateKey)
|
|
|
|
// 5. User can vote immediately
|
|
```
|
|
|
|
### For Sponsored Registration (ID Gateway)
|
|
```typescript
|
|
// 1. User generates signature
|
|
const signature = await generateRegisterSignature(userPrivateKey, deadline)
|
|
|
|
// 2. App pays for registration
|
|
const { fid } = await registerFor(
|
|
APP_PRIVATE_KEY,
|
|
userAddress,
|
|
signature,
|
|
deadline
|
|
)
|
|
|
|
// 3. Add keys later when needed
|
|
```
|
|
|
|
---
|
|
|
|
## ๐ฐ Cost Breakdown
|
|
|
|
### Bundler
|
|
- FID registration: ~$2
|
|
- Account key: ~$2
|
|
- Storage (1 unit): ~$1
|
|
- **Total: ~$5**
|
|
|
|
### ID Gateway
|
|
- FID registration: ~$2
|
|
- Storage (1 unit): ~$1
|
|
- **Total: ~$3**
|
|
- Keys added separately: ~$2 each
|
|
|
|
### Extra Storage
|
|
```typescript
|
|
// Get price with extra storage
|
|
const price = await getRegistrationPrice(10n) // 10 extra units
|
|
```
|
|
|
|
---
|
|
|
|
## ๐ Security Best Practices
|
|
|
|
### Private Key Management
|
|
```typescript
|
|
// โ DON'T: Store in plain text
|
|
localStorage.setItem('key', privateKey)
|
|
|
|
// โ
DO: Encrypt before storing
|
|
const encrypted = await encrypt(privateKey, userPassword)
|
|
localStorage.setItem('key', encrypted)
|
|
```
|
|
|
|
### Signature Deadlines
|
|
```typescript
|
|
// โ
Good: 1 hour
|
|
const deadline = BigInt(Math.floor(Date.now() / 1000) + 3600)
|
|
|
|
// โ Too long: 1 week
|
|
const deadline = BigInt(Math.floor(Date.now() / 1000) + 604800)
|
|
```
|
|
|
|
### Recovery Address
|
|
```typescript
|
|
// โ
Use Farcaster's recovery proxy
|
|
const RECOVERY = '0x00000000FcB080a4D6c39a9354dA9EB9bC104cd7'
|
|
|
|
// โ Don't use user's address (can't recover if lost)
|
|
const RECOVERY = userAddress
|
|
```
|
|
|
|
---
|
|
|
|
## ๐งช Testing
|
|
|
|
### Test on OP Sepolia
|
|
```typescript
|
|
// Update chain
|
|
import { optimismSepolia } from 'viem/chains'
|
|
|
|
const publicClient = createPublicClient({
|
|
chain: optimismSepolia,
|
|
transport: http(process.env.OP_SEPOLIA_RPC_URL)
|
|
})
|
|
```
|
|
|
|
### Get Testnet ETH
|
|
- [OP Sepolia Faucet](https://app.optimism.io/faucet)
|
|
- [Superchain Faucet](https://app.optimism.io/faucet)
|
|
|
|
### Verify Registration
|
|
```typescript
|
|
// Check FID
|
|
const fid = await idRegistry.idOf(address)
|
|
console.log('FID:', fid.toString())
|
|
|
|
// Check if address has FID
|
|
if (fid > 0n) {
|
|
console.log('โ
Registration successful')
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## ๐ File Structure
|
|
|
|
```
|
|
lib/farcaster/
|
|
โโโ register.ts # Bundler implementation
|
|
โโโ register-app.ts # App FID registration
|
|
โโโ id-gateway.ts # ID Gateway implementation
|
|
|
|
app/api/farcaster/
|
|
โโโ register/
|
|
โ โโโ route.ts # Bundler API
|
|
โโโ id-gateway/
|
|
โโโ route.ts # ID Gateway API
|
|
|
|
app/
|
|
โโโ register/
|
|
โโโ page.tsx # Registration UI
|
|
|
|
scripts/
|
|
โโโ register-app.ts # Setup script
|
|
```
|
|
|
|
---
|
|
|
|
## ๐ Quick Start
|
|
|
|
### 1. Install Dependencies
|
|
```bash
|
|
npm install @farcaster/hub-nodejs @noble/ed25519
|
|
```
|
|
|
|
### 2. Setup Environment
|
|
```env
|
|
OP_MAINNET_RPC_URL=https://mainnet.optimism.io
|
|
APP_PRIVATE_KEY=0x...
|
|
```
|
|
|
|
### 3. Register App FID
|
|
```bash
|
|
npx tsx scripts/register-app.ts
|
|
```
|
|
|
|
### 4. Use in Your App
|
|
```typescript
|
|
// Bundler (recommended)
|
|
import { registerFarcasterAccount } from '@/lib/farcaster/register'
|
|
|
|
// ID Gateway (advanced)
|
|
import { registerFID } from '@/lib/farcaster/id-gateway'
|
|
```
|
|
|
|
---
|
|
|
|
## ๐ฏ API Endpoints
|
|
|
|
### Bundler
|
|
```bash
|
|
POST /api/farcaster/register
|
|
{
|
|
"privateKey": "0x..."
|
|
}
|
|
```
|
|
|
|
### ID Gateway
|
|
```bash
|
|
# Get price
|
|
GET /api/farcaster/id-gateway?extraStorage=0
|
|
|
|
# Register
|
|
POST /api/farcaster/id-gateway
|
|
{
|
|
"method": "register",
|
|
"privateKey": "0x...",
|
|
"extraStorage": 0
|
|
}
|
|
|
|
# Register for another address
|
|
POST /api/farcaster/id-gateway
|
|
{
|
|
"method": "registerFor",
|
|
"privateKey": "0x...",
|
|
"recipientAddress": "0x...",
|
|
"signature": "0x...",
|
|
"deadline": "1234567890",
|
|
"extraStorage": 0
|
|
}
|
|
```
|
|
|
|
---
|
|
|
|
## โ ๏ธ Common Errors
|
|
|
|
### InvalidPayment (0x3c6b4b28)
|
|
**Cause:** Insufficient ETH sent
|
|
**Fix:** Get exact price with `price()` function
|
|
|
|
### InvalidSignature (0x8baa579f)
|
|
**Cause:** Wrong signer or malformed signature
|
|
**Fix:** Ensure signature is from correct address
|
|
|
|
### SignatureExpired (0x0819bdcd)
|
|
**Cause:** Deadline passed
|
|
**Fix:** Generate new signature with later deadline
|
|
|
|
### InvalidMetadata (0xbcecb64a)
|
|
**Cause:** Invalid signed key request
|
|
**Fix:** Ensure app FID is registered
|
|
|
|
---
|
|
|
|
## ๐ Resources
|
|
|
|
- [Bundler Docs](https://docs.farcaster.xyz/reference/contracts/reference/bundler)
|
|
- [ID Gateway Docs](https://docs.farcaster.xyz/reference/contracts/reference/id-gateway)
|
|
- [Contract Addresses](https://docs.farcaster.xyz/reference/contracts/reference/addresses)
|
|
- [Signup Demo](https://github.com/farcasterxyz/auth-monorepo)
|
|
|
|
---
|
|
|
|
## โ
Production Checklist
|
|
|
|
### Setup
|
|
- [ ] Dependencies installed
|
|
- [ ] OP Mainnet RPC configured
|
|
- [ ] App private key generated
|
|
- [ ] App wallet funded (~0.01 ETH)
|
|
- [ ] App FID registered
|
|
|
|
### Testing
|
|
- [ ] Tested on OP Sepolia
|
|
- [ ] Verified FID creation
|
|
- [ ] Tested error handling
|
|
- [ ] Checked gas costs
|
|
|
|
### Security
|
|
- [ ] Private keys encrypted
|
|
- [ ] Recovery address set
|
|
- [ ] Rate limiting added
|
|
- [ ] Monitoring enabled
|
|
|
|
### Deployment
|
|
- [ ] Environment variables set
|
|
- [ ] APIs deployed
|
|
- [ ] UI tested
|
|
- [ ] Documentation updated
|
|
|
|
---
|
|
|
|
## ๐ Summary
|
|
|
|
**Two Methods:**
|
|
1. **Bundler** - All-in-one (recommended)
|
|
2. **ID Gateway** - FID only (advanced)
|
|
|
|
**Cost:** $3-5 per registration
|
|
**Time:** ~30 seconds
|
|
**Complexity:** Low (Bundler) / Medium (ID Gateway)
|
|
|
|
**Choose Bundler for best user experience! ๐**
|