$npx -y skills add basezh/agent-skills-on-base --skill farcasterAutonomous Farcaster account creation and casting without human intervention.
| 1 | # Farcaster Agent |
| 2 | |
| 3 | Autonomous Farcaster account creation and casting without human intervention. |
| 4 | |
| 5 | **OpenClaw Skill:** This repository includes an OpenClaw-compatible skill in the `skill/` directory. Install it with: |
| 6 | ```bash |
| 7 | npx clawhub@latest install farcaster-agent |
| 8 | ``` |
| 9 | Or copy `skill/` to `~/.openclaw/skills/farcaster-agent/`. |
| 10 | |
| 11 | This toolkit allows an AI agent (or script) to: |
| 12 | 1. Create a new Farcaster account (register an FID) |
| 13 | 2. Add a signer key for posting |
| 14 | 3. Post casts to the network |
| 15 | |
| 16 | All operations are fully programmatic - no Farcaster app or manual steps required. |
| 17 | |
| 18 | **For AI agents:** See [AGENT_GUIDE.md](./AGENT_GUIDE.md) for detailed implementation instructions. |
| 19 | |
| 20 | ## Prerequisites |
| 21 | |
| 22 | - Node.js 18+ |
| 23 | - **$1 of ETH or USDC** on any major chain (Ethereum, Optimism, Base, Arbitrum, Polygon) |
| 24 | |
| 25 | The toolkit handles bridging and swapping automatically. |
| 26 | |
| 27 | ## Installation |
| 28 | |
| 29 | ```bash |
| 30 | npm install |
| 31 | ``` |
| 32 | |
| 33 | ## Quick Start (Fully Automatic) |
| 34 | |
| 35 | Send $1 of ETH or USDC to your wallet on any supported chain, then: |
| 36 | |
| 37 | ```bash |
| 38 | PRIVATE_KEY=0x... npm run auto |
| 39 | # or |
| 40 | PRIVATE_KEY=0x... node src/auto-setup.js "Your first cast text" |
| 41 | ``` |
| 42 | |
| 43 | This will: |
| 44 | 1. Detect your funds across all chains |
| 45 | 2. Bridge/swap to get ETH on Optimism and USDC on Base |
| 46 | 3. Register your FID |
| 47 | 4. Add a signer key |
| 48 | 5. Post your first cast |
| 49 | 6. Save credentials to `~/.openclaw/farcaster-credentials.json` or `./credentials.json` |
| 50 | |
| 51 | **Security Note:** Credentials are stored as plain text JSON with restricted file permissions. Anyone with access to these files can control both the wallet funds and the Farcaster account. For production use, implement your own secure storage solution. |
| 52 | |
| 53 | ## Manual Step-by-Step |
| 54 | |
| 55 | ### 1. Generate a Wallet |
| 56 | |
| 57 | ```javascript |
| 58 | const { Wallet } = require('ethers'); |
| 59 | const wallet = Wallet.createRandom(); |
| 60 | console.log('Address:', wallet.address); |
| 61 | console.log('Private Key:', wallet.privateKey); |
| 62 | console.log('Mnemonic:', wallet.mnemonic.phrase); |
| 63 | ``` |
| 64 | |
| 65 | ### 2. Fund the Wallet |
| 66 | |
| 67 | - Send ~0.005 ETH to the address on **Optimism** (for FID registration) |
| 68 | - Send ~0.001 ETH to the same address on **Base** (for USDC swap) |
| 69 | |
| 70 | ### 3. Register FID |
| 71 | |
| 72 | ```bash |
| 73 | PRIVATE_KEY=0x... node src/register-fid.js |
| 74 | ``` |
| 75 | |
| 76 | ### 4. Add Signer Key |
| 77 | |
| 78 | ```bash |
| 79 | PRIVATE_KEY=0x... node src/add-signer.js |
| 80 | ``` |
| 81 | |
| 82 | Save the signer private key that's output - you need it to post casts. |
| 83 | |
| 84 | ### 5. Swap ETH to USDC (for x402 payments) |
| 85 | |
| 86 | ```bash |
| 87 | PRIVATE_KEY=0x... node src/swap-to-usdc.js |
| 88 | ``` |
| 89 | |
| 90 | ### 6. Post a Cast |
| 91 | |
| 92 | ```bash |
| 93 | PRIVATE_KEY=0x... SIGNER_PRIVATE_KEY=... FID=123 node src/post-cast.js "Hello Farcaster!" |
| 94 | ``` |
| 95 | |
| 96 | ### 7. Set Up Profile (Optional) |
| 97 | |
| 98 | ```bash |
| 99 | # Set username, display name, bio, and profile picture |
| 100 | PRIVATE_KEY=0x... SIGNER_PRIVATE_KEY=... FID=123 npm run profile myusername "Display Name" "My bio" "https://example.com/pfp.png" |
| 101 | ``` |
| 102 | |
| 103 | ## Detailed Walkthrough |
| 104 | |
| 105 | ### Step 1: FID Registration (Optimism) |
| 106 | |
| 107 | Farcaster IDs are registered on Optimism via the `IdGateway` contract. |
| 108 | |
| 109 | ``` |
| 110 | IdGateway: 0x00000000Fc25870C6eD6b6c7E41Fb078b7656f69 |
| 111 | ``` |
| 112 | |
| 113 | The registration costs ~0.00008 ETH (storage fee) plus gas. |
| 114 | |
| 115 | **Key function:** `register(address recovery)` - registers a new FID with the caller as owner. |
| 116 | |
| 117 | ### Step 2: Adding a Signer Key (Optimism) |
| 118 | |
| 119 | This is the tricky part. Farcaster requires a "Signed Key Request" to add signer keys - it's an anti-spam measure. |
| 120 | |
| 121 | **The key insight:** You can use your own FID as the "app" that signs the key request. Since you control the custody address, you can self-sign. |
| 122 | |
| 123 | The process: |
| 124 | 1. Generate an Ed25519 keypair (the signer key) |
| 125 | 2. Create an EIP-712 `SignedKeyRequest` message |
| 126 | 3. Sign it with your custody wallet |
| 127 | 4. Use the `SignedKeyRequestValidator` contract to encode the metadata |
| 128 | 5. Call `KeyGateway.add()` with the encoded metadata |
| 129 | |
| 130 | **Critical:** You MUST use the validator contract's `encodeMetadata()` function. Manual ABI encoding doesn't work because the struct encoding includes a dynamic offset pointer. |
| 131 | |
| 132 | ``` |
| 133 | KeyGateway: 0x00000000fC56947c7E7183f8Ca4B62398CaAdf0B |
| 134 | SignedKeyRequestValidator: 0x00000000FC700472606ED4fA22623Acf62c60553 |
| 135 | ``` |
| 136 | |
| 137 | ### Step 3: Posting Casts (via Neynar Hub) |
| 138 | |
| 139 | Casts are submitted to Farcaster hubs as protobuf-encoded messages. |
| 140 | |
| 141 | **Problem:** Most public hubs (like hub.pinata.cloud) lag behind on syncing on-chain events. They may not recognize your new FID for hours or days. |
| 142 | |
| 143 | **Solution:** Use Neynar's hub (`hub-api.neynar.com`) which requires x402 micropayments but is well-synced. |
| 144 | |
| 145 | ### Step 4: x402 Payments |
| 146 | |
| 147 | Neynar uses the x402 payment protocol. Each API call costs 0.001 USDC on Base. |
| 148 | |
| 149 | The payment uses EIP-3009 (`transferWithAuthorization`) - a gasless signature-based USDC transfer: |
| 150 | |
| 151 | 1. Sign an EIP-712 message authorizing Neynar to pull USDC |
| 152 | 2. Base64-encode the payment payload |
| 153 | 3. Include it in the `X-PAYMENT` header |
| 154 | |
| 155 | **Payment payload structure:** |
| 156 | ```json |
| 157 | { |
| 158 | "x402Version": 1, |
| 159 | "scheme": "exact", |
| 160 | "network": "base", |