- React
- React Native
npm i @solana-mobile/wallet-standard-mobile
import {
createDefaultAuthorizationCache,
createDefaultChainSelector,
createDefaultWalletNotFoundHandler,
registerMwa
} from '@solana-mobile/wallet-standard-mobile';
registerMwa({
appIdentity: {
name: 'My app',
uri: 'https://myapp.io',
icon: 'relative/path/to/icon.png' // resolves to https://myapp.io/relative/path/to/icon.png
},
authorizationCache: createDefaultAuthorizationCache(),
chains: ['solana:mainnet'],
chainSelector: createDefaultChainSelector(),
onWalletNotFound: createDefaultWalletNotFoundHandler()
});
npm install @solana-mobile/mobile-wallet-adapter-protocol-web3js @solana-mobile/mobile-wallet-adapter-protocol
transact and authorize methods from the @solana-mobile/mobile-wallet-adapter-protocol-web3js package, you’ll then need to log in the user with Privy using the useLoginWithSiws hook.You can do this in a custom login button component.import {transact} from '@solana-mobile/mobile-wallet-adapter-protocol-web3js';
import {useLoginWithSiws} from '@privy-io/expo';
import {toByteArray} from 'react-native-quick-base64';
import {PublicKey} from '@solana/web3.js';
import {Buffer} from 'buffer';
const MwaLoginButton = () => {
const {generateMessage, login} = useLoginWithSiws();
const handleLogin = async () => {
try {
await transact(async (wallet) => {
// 1. Create a MWA session
const authorizationResult = await wallet.authorize({
chain: 'mainnet-beta',
identity: {
name: 'My app',
uri: 'https://myapp.io',
icon: 'https://myapp.io/icon.png'
}
});
const desiredAccount = authorizationResult.accounts[0];
// 2. Convert base64 address to base58 (Privy expects base58)
const addressBytes = toByteArray(desiredAccount.address);
const publicKey = new PublicKey(addressBytes);
const base58Address = publicKey.toBase58();
// 3. Generate a Privy SIWS message for the wallet address
const siwsMessage = await generateMessage({
wallet: {address: base58Address}, // Use base58 for Privy
from: {domain: 'com.myapp.app', uri: 'https://myapp.io'}
});
// 4. Convert the SIWS message to Uint8Array for signing
const encodedSiwsMessage = new TextEncoder().encode(siwsMessage.message);
// 5. Request user to sign the SIWS message with their wallet
const [signatureBytes] = await wallet.signMessages({
addresses: [desiredAccount.address], // Use base64 for MWA
payloads: [encodedSiwsMessage]
});
const signatureBase64 = Buffer.from(signatureBytes).toString('base64');
// 6. Authenticate with Privy using the signed message
const user = await login({
signature: signatureBase64,
message: siwsMessage.message
});
return {mwaResult: authResult, user};
});
} catch (error) {
console.error('Login failed', error);
}
};
return <Button title="Login with MWA" onPress={handleLogin} />;
};
Was this page helpful?