Create a React Native App
This guide will walk you through adding support for Smart Wallet into a React Native app by integrating the Mobile Wallet Protocol Client.
Prerequisites
Deeplink handling
Your app needs to be able to handle Universal Links (iOS) and App Links (Android). Opening https://your-app.example.com/coinbase-wallet-sdk should open up your app. You can see more detailed instructions on setting up deeplinks in the Expo docs.
Install peer dependencies
The Mobile Wallet Protocol Client library requires the Expo WebBrowser and Async Storage packages to be installed. Follow the instructions on the respective pages for any additional setup.
npm i expo-web-browser @react-native-async-storage/async-storage
Polyfills
Mobile Wallet Protocol Client requires crypto.randomUUID
, crypto.getRandomValues
, and URL
to be polyfilled globally since they are not available in the React Native environment.
npm i expo-crypto expo-standard-web-crypto react-native-url-polyfill
import 'react-native-url-polyfill/auto'
import { polyfillWebCrypto } from 'expo-standard-web-crypto'
import { randomUUID } from "expo-crypto"
polyfillWebCrypto()
crypto.randomUUID = randomUUID
Setup
Install Mobile Wallet Protocol Client
Add the latest version of Mobile Wallet Protocol Client to your project.
npm i @mobile-wallet-protocol/client@latest
Configuration
Next, set up your app to pass in deeplinks coming from Smart Wallet to Mobile Wallet Protocol Client so that it can handle responses to the requests you make. In your app, this might look something like the following.
import { handleResponse } from '@mobile-wallet-protocol/client/dist/core/communicator/handleResponse'
import * as Linking from 'expo-linking'
// ...
useEffect(() => {
const subscription = Linking.addEventListener('url', ({ url }) => {
const handled = handleResponse(url)
if (!handled) {
// handle other deeplinks
}
})
return () => subscription.remove()
}, [])
Usage
Note that we are iterating on the Mobile Wallet Protocol Client and the API may change.
Create a new CoinbaseWalletSDK
instance and use the makeWeb3Provider
function to create an EIP-1193 compliant provider.
The appDeeplinkUrl
is required and should match the deeplink URL you set up earlier.
import { CoinbaseWalletSDK } from '@mobile-wallet-protocol/client'
// 1. Create SDK instance
const sdk = new CoinbaseWalletSDK({
appDeeplinkUrl: 'https://your-app.example.com', // required
appName: 'My App Name',
appChainIds: [8453],
appLogoUrl: 'https://example.com/logo.png'
})
// 2. Create EIP-1193 provider
const provider = sdk.makeWeb3Provider()
// ...
// 3. Use the provider
const addresses = await provider.request({method: 'eth_requestAccounts'})
const signedData = await provider.request({
method: "personal_sign",
params: ["0x48656c6c6f20776f726c6421", addresses[0]],
})
Give feedback!
Send us feedback on the Coinbase Developer Platform Discord or create a new issue on the MobileWalletProtocol/react-native-client repository.