|
| 1 | +--- |
| 2 | +title: 'Onchain Vibes Store (Profiles)' |
| 3 | +description: 'How Onchain Vibes Store uses Coinbase Smart Wallet Profiles for secure user data collection during onchain checkout.' |
| 4 | +--- |
| 5 | + |
| 6 | +import {GithubRepoCard} from "/snippets/GithubRepoCard.mdx"; |
| 7 | + |
| 8 | +## Overview |
| 9 | + |
| 10 | +Onchain Vibes Store demonstrates how to use [**Smart Wallet Profiles**](/smart-wallet/concepts/features/optional/profiles) |
| 11 | +to securely collect user information (like email and physical address) |
| 12 | +during an onchain transaction. This feature enables seamless, privacy-first |
| 13 | +data collection for e-commerce and other onchain apps. |
| 14 | + |
| 15 | +## What Are Smart Wallet Profiles? |
| 16 | + |
| 17 | +Smart Wallet Profiles allow your app to request personal information from users as part of a transaction. Supported data types include: |
| 18 | + |
| 19 | +- Email address |
| 20 | +- Phone number |
| 21 | +- Physical address |
| 22 | +- Name |
| 23 | +- Onchain wallet address |
| 24 | + |
| 25 | +Users are always in control: they see exactly what you request and can choose to share or withhold any information. |
| 26 | + |
| 27 | +## How It Works in This App |
| 28 | + |
| 29 | +1. **User clicks Buy**: The checkout UI lets users select what info to share (email, address). |
| 30 | +2. **App requests data**: The app sends a transaction with a `dataCallback` capability, specifying the requested data and a callback URL for validation. |
| 31 | +3. **Smart Wallet prompts the user**: The wallet UI collects the requested info. |
| 32 | +4. **Validation**: The wallet POSTs the data to your callback API for validation. |
| 33 | +5. **Transaction proceeds**: If validation passes, the transaction completes and the app displays the collected info. |
| 34 | + |
| 35 | +<div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}> |
| 36 | + <img |
| 37 | + alt="Onchain Vibes Store" |
| 38 | + src="https://i.imgur.com/rC674pl.gif" |
| 39 | + width="1000" |
| 40 | + loading="lazy" |
| 41 | + /> |
| 42 | +</div> |
| 43 | +<div style={{ textAlign: 'center', marginTop: '0.5rem', fontStyle: 'italic' }}> |
| 44 | + Onchain Vibes Store Quick Demo |
| 45 | + </div> |
| 46 | + |
| 47 | +## Skip ahead |
| 48 | + |
| 49 | +If you want to skip ahead and just get the final code, you can find it here: |
| 50 | + |
| 51 | +<GithubRepoCard title="Onchain Vibes Store" githubUrl="https://github.com/base/demos/tree/master/smart-wallet/onchain-vibes-store" /> |
| 52 | + |
| 53 | + |
| 54 | +## UI Walkthrough: CheckoutButton |
| 55 | + |
| 56 | +The main logic lives in `src/components/CheckoutButton.tsx`: |
| 57 | + |
| 58 | +```tsx src/components/CheckoutButton.tsx |
| 59 | +const requests = []; |
| 60 | +if (dataToRequest.email) requests.push({ type: "email", optional: false }); |
| 61 | +if (dataToRequest.address) requests.push({ type: "physicalAddress", optional: false }); |
| 62 | + |
| 63 | +const response: any = await provider?.request({ |
| 64 | + method: "wallet_sendCalls", |
| 65 | + params: [{ |
| 66 | + version: "1.0", |
| 67 | + chainId: numberToHex(84532), // Base Sepolia |
| 68 | + calls: [ |
| 69 | + { |
| 70 | + to: "0x036CbD53842c5426634e7929541eC2318f3dCF7e", // USDC contract |
| 71 | + data: encodeFunctionData({ |
| 72 | + abi: erc20Abi, |
| 73 | + functionName: "transfer", |
| 74 | + args: [ |
| 75 | + "0xd8da6bf26964af9d7eed9e03e53415d37aa96045", |
| 76 | + parseUnits("0.01", 6), |
| 77 | + ], |
| 78 | + }), |
| 79 | + }, |
| 80 | + ], |
| 81 | + capabilities: { |
| 82 | + dataCallback: { |
| 83 | + requests, |
| 84 | + callbackURL: getCallbackURL(), |
| 85 | + }, |
| 86 | + }, |
| 87 | + }], |
| 88 | +}); |
| 89 | +``` |
| 90 | + |
| 91 | +- The user selects which data to share (checkboxes for email/address). |
| 92 | +- The app sends a transaction with a `dataCallback` capability. |
| 93 | +- The callback URL is set to your API endpoint (must be HTTPS, e.g. via ngrok in dev). |
| 94 | + |
| 95 | +--- |
| 96 | + |
| 97 | +## API Walkthrough: Data Validation |
| 98 | + |
| 99 | +The callback API is implemented in `src/api/data-validation/route.ts`: |
| 100 | + |
| 101 | +```ts src/api/data-validation/route.ts |
| 102 | +export async function POST(request: NextRequest) { |
| 103 | + const requestData = await request.json(); |
| 104 | + try { |
| 105 | + const email = requestData.requestedInfo.email; |
| 106 | + const physicalAddress = requestData.requestedInfo.physicalAddress; |
| 107 | + const errors: any = {}; |
| 108 | + if (email && email.endsWith("@example.com")) { |
| 109 | + errors.email = "Example.com emails are not allowed"; |
| 110 | + } |
| 111 | + if (physicalAddress) { |
| 112 | + if (physicalAddress.postalCode && physicalAddress.postalCode.length < 5) { |
| 113 | + if (!errors.physicalAddress) errors.physicalAddress = {}; |
| 114 | + errors.physicalAddress.postalCode = "Invalid postal code"; |
| 115 | + } |
| 116 | + if (physicalAddress.countryCode === "XY") { |
| 117 | + if (!errors.physicalAddress) errors.physicalAddress = {}; |
| 118 | + errors.physicalAddress.countryCode = "We don't ship to this country"; |
| 119 | + } |
| 120 | + } |
| 121 | + if (Object.keys(errors).length > 0) { |
| 122 | + return NextResponse.json({ errors }); |
| 123 | + } |
| 124 | + return NextResponse.json({ |
| 125 | + calls: requestData.calls, |
| 126 | + chainId: requestData.chainId, |
| 127 | + capabilities: requestData.capabilities |
| 128 | + }); |
| 129 | + } catch (error) { |
| 130 | + return NextResponse.json({ errors: { server: "Server error validating data" } }); |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +- The API receives the user's data, validates it, and returns errors if needed. |
| 136 | +- If validation passes, it must return the original `calls`, `chainId`, and `capabilities`. |
| 137 | +- If errors are returned, the wallet prompts the user to correct their info. |
| 138 | + |
| 139 | +--- |
| 140 | + |
| 141 | +## Wagmi & Wallet Setup |
| 142 | + |
| 143 | +The app uses Wagmi and the Coinbase Wallet connector, configured for Smart Wallet and profiles: |
| 144 | + |
| 145 | +```ts |
| 146 | +import { coinbaseWallet } from "wagmi/connectors"; |
| 147 | +const cbWalletConnector = coinbaseWallet({ |
| 148 | + appName: "Vibes Store", |
| 149 | + preference: { |
| 150 | + keysUrl: "https://keys.coinbase.com/connect", |
| 151 | + options: "smartWalletOnly", |
| 152 | + }, |
| 153 | +}); |
| 154 | +``` |
| 155 | + |
| 156 | +--- |
| 157 | + |
| 158 | +## Testing Locally |
| 159 | + |
| 160 | +1. Start your dev server: `npm run dev` |
| 161 | +2. Start ngrok: `ngrok http 3000` |
| 162 | +3. Set `VITE_NGROK_URL` in your `.env` to your ngrok HTTPS URL |
| 163 | +4. Try a purchase and share profile data |
| 164 | + |
| 165 | +--- |
| 166 | + |
| 167 | +## Best Practices |
| 168 | + |
| 169 | +- **Only request what you need**: Ask for the minimum info required. |
| 170 | +- **Explain why**: Tell users why you need each field. |
| 171 | +- **Validate thoroughly**: Implement robust server-side validation. |
| 172 | +- **Handle errors gracefully**: Show clear error messages. |
| 173 | +- **Use HTTPS**: Your callback URL must be HTTPS (ngrok for local dev). |
| 174 | + |
| 175 | +--- |
| 176 | + |
| 177 | +## Resources |
| 178 | + |
| 179 | +- [Profiles Guide](/smart-wallet/guides/profiles) |
| 180 | +- [Profiles Reference](/smart-wallet/technical-reference/profiles-reference) |
| 181 | +- [Smart Wallet Docs](/smart-wallet/quickstart) |
| 182 | +- [Wagmi Docs](https://wagmi.sh/) |
0 commit comments