Integrate Embedded Wallets with the Solana Blockchain in React Native
After a user signs in on a Solana chain, the React Native SDK v10 exposes a
Wallet Standard wallet as
useWeb3Auth().connection.solanaWallet. The @web3auth/react-native-sdk/solana entry point wraps
it in hooks for reading accounts, signing messages, and signing and sending transactions with
@solana/kit.
If your app uses web3Auth.signer, SolanaWallet, SolanaPrivateKeyProvider, or
@web3auth/solana-provider, follow the
React Native migration guide.
Prerequisites
Complete Get started: install the SDK, configure Metro,
Babel, and the entry file, and wrap your app in Web3AuthProvider.
Chain details for Solana
- Mainnet
- Testnet
- Devnet
- Chain namespace:
SOLANA - Chain ID:
0x65 - Public RPC URL:
https://api.mainnet-beta.solana.com(use a managed RPC provider in production) - Display name: Solana Mainnet
- Block explorer:
https://explorer.solana.com - Ticker: SOL
- Ticker name: Solana
- Chain namespace:
SOLANA - Chain ID:
0x66 - Public RPC URL:
https://api.testnet.solana.com - Display name: Solana Testnet
- Block explorer:
https://explorer.solana.com?cluster=testnet - Ticker: SOL
- Ticker name: Solana
- Chain namespace:
SOLANA - Chain ID:
0x67 - Public RPC URL:
https://api.devnet.solana.com - Display name: Solana Devnet
- Block explorer:
https://explorer.solana.com?cluster=devnet - Ticker: SOL
- Ticker name: Solana
The SDK maps these chain IDs to the Wallet Standard chains solana:mainnet, solana:testnet, and
solana:devnet. Other chain IDs don't work with the Solana wallet.
Set up
Enable a Solana chain on the dashboard, or add it to web3AuthOptions.chains:
import {
CHAIN_NAMESPACES,
WEB3AUTH_NETWORK,
type Web3AuthContextConfig,
} from '@web3auth/react-native-sdk'
const web3AuthConfig: Web3AuthContextConfig = {
web3AuthOptions: {
clientId: '<YOUR_CLIENT_ID>',
redirectUrl: 'yourapp://auth',
network: WEB3AUTH_NETWORK.SAPPHIRE_MAINNET,
chains: [
{
chainNamespace: CHAIN_NAMESPACES.SOLANA,
chainId: '0x67',
rpcTarget: 'https://api.devnet.solana.com',
displayName: 'Solana Devnet',
blockExplorerUrl: 'https://explorer.solana.com?cluster=devnet',
ticker: 'SOL',
tickerName: 'Solana',
},
],
defaultChainId: '0x67',
},
}
export default web3AuthConfig
Then install the Solana packages, add the polyfills and the Metro shim, and wrap your app in
SolanaProvider, as described in
Solana hooks: Set up.
Get the account and balance
import { useSolanaWallet } from '@web3auth/react-native-sdk/solana'
import { address } from '@solana/kit'
function SolanaAccount() {
const { accounts, rpc } = useSolanaWallet()
const getBalance = async () => {
if (!rpc || !accounts?.[0]) return null
const { value: lamports } = await rpc.getBalance(address(accounts[0])).send()
return Number(lamports) / 1_000_000_000 // SOL
}
// accounts?.[0] is the user's Solana address
}
Sign a message
import { useSignMessage } from '@web3auth/react-native-sdk/solana'
const { signMessage } = useSignMessage()
const signature = await signMessage('Hello from Embedded Wallets')
Send SOL
Build the transaction with @solana/kit, then sign and send it with useSignAndSendTransaction:
import {
address,
appendTransactionMessageInstruction,
compileTransaction,
createNoopSigner,
createTransactionMessage,
lamports,
pipe,
setTransactionMessageFeePayer,
setTransactionMessageLifetimeUsingBlockhash,
} from '@solana/kit'
import { getTransferSolInstruction } from '@solana-program/system'
import { useSignAndSendTransaction, useSolanaWallet } from '@web3auth/react-native-sdk/solana'
function SendSol() {
const { accounts, rpc } = useSolanaWallet()
const { signAndSendTransaction } = useSignAndSendTransaction()
const send = async (to: string) => {
if (!rpc || !accounts?.[0]) return
const from = address(accounts[0])
const { value: latestBlockhash } = await rpc.getLatestBlockhash().send()
const transaction = pipe(
createTransactionMessage({ version: 0 }),
message => setTransactionMessageFeePayer(from, message),
message => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, message),
message =>
appendTransactionMessageInstruction(
getTransferSolInstruction({
source: createNoopSigner(from),
destination: address(to),
amount: lamports(1_000_000n), // 0.001 SOL
}),
message
),
compileTransaction
)
return signAndSendTransaction(transaction)
}
// ...
}
This example also uses @solana-program/system
for the transfer instruction. SDK v10.0.0 uses @solana/kit 6, so install a matching version:
- npm
- Yarn
- pnpm
- Bun
npm install @solana-program/system@^0.12.0
yarn add @solana-program/system@^0.12.0
pnpm add @solana-program/system@^0.12.0
bun add @solana-program/system@^0.12.0