Implement Phantom React Native SDK integration for Solana mobile apps with the following requirements:
INSTALLATION:
1. Install SDK: npm install @phantom/react-native-sdk
2. Install Solana dependency: npm install @solana/web3.js
3. Install peer dependencies:
- For Expo: npx expo install expo-secure-store expo-web-browser expo-auth-session expo-router
- Required polyfill: npm install react-native-get-random-values
POLYFILL SETUP (CRITICAL):
Add this import at the VERY TOP of your app entry point (index.js, App.tsx, or _layout.tsx):
// MUST be the first import - before any other imports
import "react-native-get-random-values";
import { PhantomProvider } from "@phantom/react-native-sdk";
// ... other imports
APP CONFIGURATION:
Configure app.json with your custom URL scheme:
{
"expo": {
"name": "My Solana Wallet App",
"slug": "my-solana-wallet-app",
"scheme": "[YOUR_SCHEME]",
"plugins": ["expo-router", "expo-secure-store", "expo-web-browser", "expo-auth-session"]
}
}
PROVIDER SETUP:
Wrap your app with PhantomProvider in App.tsx or _layout.tsx:
import { PhantomProvider, AddressType } from "@phantom/react-native-sdk";
export default function App() {
return (
<PhantomProvider
config={{
providerType: "embedded",
appId: "[YOUR_APP_ID]",
scheme: "[YOUR_SCHEME]",
addressTypes: [AddressType.solana],
authOptions: {
redirectUrl: "[YOUR_SCHEME]://phantom-auth-callback",
},
appName: "My Solana Wallet App",
}}
>
<YourAppContent />
</PhantomProvider>
);
}
WALLET SCREEN COMPONENT:
Create a component using hooks from "@phantom/react-native-sdk":
1. Import: useConnect, useAccounts, useSolana, useDisconnect
2. Use React Native components: View, Button, Text, Alert, StyleSheet from "react-native"
3. Connect flow:
- const { connect, isConnecting, error } = useConnect()
- await connect({ provider: "google" }) // or "apple"
- const { addresses, isConnected } = useAccounts()
4. Display Solana address when connected (truncate for mobile: show first 4 and last 4 chars)
5. Add disconnect button using useDisconnect()
6. Show wallet balance if needed
MESSAGE SIGNING:
Implement message signing for authentication:
- const { solana } = useSolana()
- const signature = await solana.signMessage("Sign in to MyApp")
- signature.signature contains the signature string
- Show success Alert with truncated signature
- Use for login or authentication flows
TRANSACTION SCREEN:
Build a transfer screen with @solana/web3.js:
1. Import Transaction, SystemProgram, PublicKey from @solana/web3.js
2. Create TextInput fields for:
- Recipient address (validate it's a valid Solana address)
- Amount in SOL (convert to lamports: amount * 1,000,000,000)
3. Build transaction:
- Create transfer with SystemProgram.transfer()
- Set fromPubkey, toPubkey, lamports
4. Send transaction:
- await solana.signAndSendTransaction(transaction)
- Returns { hash } with transaction signature
5. Show success Alert with link to Solana Explorer
6. Handle errors with user-friendly messages
REQUIREMENTS:
- Use TypeScript with proper types
- Handle all errors with try-catch and Alert.alert
- Show loading states during connection and transactions
- Use StyleSheet for component styling
- Add pull-to-refresh for balance updates
- Format SOL amounts properly (show 2-4 decimal places)
- Truncate addresses for mobile display
- Add haptic feedback for actions
- Test thoroughly on both iOS and Android
- Implement proper null checks for addresses
- Add helpful comments explaining Solana concepts
The SDK handles OAuth flow automatically with system browser and deep link redirect back to your app.