|
| 1 | +--- |
| 2 | +title: What's MWA? |
| 3 | +date: 2025-09-06 18:25:14 +0800 |
| 4 | +categories: [android, React Native] |
| 5 | +tags: [android, rn, mwa, dapp, crypto, solana, wallet] # TAG names should always be lowercase |
| 6 | +--- |
| 7 | +Mobile Wallet Adapter (MWA) is a communication protocol that enables secure interactions between decentralized applications (dApps) and wallet applications on mobile devices. It serves as the bridge that makes Solana's mobile ecosystem work seamlessly. |
| 8 | + |
| 9 | +## The Core Problem |
| 10 | + |
| 11 | +On traditional web platforms: |
| 12 | +``` |
| 13 | +Web dApp ←→ Browser Extension Wallet (e.g., MetaMask) |
| 14 | +``` |
| 15 | + |
| 16 | +Browser extensions can inject code into web pages through a shared JavaScript environment. However, mobile applications are isolated from each other, creating a communication gap: |
| 17 | + |
| 18 | +``` |
| 19 | +Mobile dApp ←→ ??? ←→ Standalone Wallet App |
| 20 | +``` |
| 21 | + |
| 22 | +MWA solves this by providing the missing communication layer: |
| 23 | +``` |
| 24 | +Mobile dApp ←→ MWA Protocol ←→ Wallet Application |
| 25 | +``` |
| 26 | + |
| 27 | +## How MWA Works |
| 28 | + |
| 29 | +### Connection Flow |
| 30 | + |
| 31 | +1. **Intent Broadcasting**: dApp sends an MWA intent with `solana-wallet://` scheme |
| 32 | +2. **Wallet Selection**: Android system displays a chooser dialog for installed wallets |
| 33 | +3. **WebSocket Establishment**: Selected wallet app establishes a persistent connection |
| 34 | +4. **Secure Communication**: Encrypted message exchange using AES-128-GCM |
| 35 | + |
| 36 | +### Technical Implementation |
| 37 | + |
| 38 | +**Android Integration:** |
| 39 | +```xml |
| 40 | +<intent-filter> |
| 41 | + <action android:name="android.intent.action.VIEW" /> |
| 42 | + <category android:name="android.intent.category.DEFAULT" /> |
| 43 | + <data android:scheme="solana-wallet" /> |
| 44 | +</intent-filter> |
| 45 | +``` |
| 46 | + |
| 47 | +**JavaScript API:** |
| 48 | +```javascript |
| 49 | +import { transact } from '@solana-mobile/mobile-wallet-adapter-protocol-web3js'; |
| 50 | + |
| 51 | +const result = await transact(async (wallet) => { |
| 52 | + // Authorization |
| 53 | + const authResult = await wallet.authorize({ |
| 54 | + cluster: 'solana:devnet', |
| 55 | + identity: { |
| 56 | + name: 'My dApp', |
| 57 | + uri: 'https://mydapp.com' |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + // Transaction signing |
| 62 | + const signedTx = await wallet.signAndSendTransactions({ |
| 63 | + transactions: [myTransaction] |
| 64 | + }); |
| 65 | + |
| 66 | + return signedTx; |
| 67 | +}); |
| 68 | +``` |
| 69 | + |
| 70 | +## Protocol Messages |
| 71 | + |
| 72 | +MWA uses JSON-RPC 2.0 format: |
| 73 | + |
| 74 | +**Authorization Request:** |
| 75 | +```json |
| 76 | +{ |
| 77 | + "jsonrpc": "2.0", |
| 78 | + "id": 1, |
| 79 | + "method": "authorize", |
| 80 | + "params": { |
| 81 | + "identity": { |
| 82 | + "name": "My dApp", |
| 83 | + "uri": "https://mydapp.com" |
| 84 | + }, |
| 85 | + "cluster": "solana:devnet" |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +**Wallet Response:** |
| 91 | +```json |
| 92 | +{ |
| 93 | + "jsonrpc": "2.0", |
| 94 | + "id": 1, |
| 95 | + "result": { |
| 96 | + "auth_token": "eyJ0eXAiOiJKV1QiLCJhbGc...", |
| 97 | + "accounts": [ |
| 98 | + { |
| 99 | + "address": "DemoKeyPair111111111111111111111111111111111", |
| 100 | + "label": "My Account" |
| 101 | + } |
| 102 | + ] |
| 103 | + } |
| 104 | +} |
| 105 | +``` |
| 106 | + |
| 107 | +## Why iOS Doesn't Support MWA |
| 108 | + |
| 109 | +- **Background Limitations**: iOS suspends WebSocket connections when apps are backgrounded |
| 110 | +- **Intent System**: No equivalent to Android's intent disambiguation |
| 111 | +- **Sandbox Restrictions**: Stricter inter-app communication policies |
| 112 | + |
| 113 | +## MWA Protocol Versions |
| 114 | + |
| 115 | +**MWA 1.0**: Basic authorization and signing functionality |
| 116 | + |
| 117 | +**MWA 2.0**: Current version featuring: |
| 118 | +- Sign-in with Solana (SIWS) support |
| 119 | +- Enhanced security |
| 120 | +- Better error handling |
| 121 | +- Multi-account support |
| 122 | + |
| 123 | +## Example: Before vs After MWA |
| 124 | + |
| 125 | +**Without MWA:** |
| 126 | +```javascript |
| 127 | +if (isPhantomWallet) { |
| 128 | + await phantom.connect(); |
| 129 | +} else if (isSolflareWallet) { |
| 130 | + await solflare.authorize(); |
| 131 | +} else if (isBackpackWallet) { |
| 132 | + await backpack.init(); |
| 133 | +} |
| 134 | +``` |
| 135 | + |
| 136 | +**With MWA:** |
| 137 | +```javascript |
| 138 | +const result = await transact(async (wallet) => { |
| 139 | + // Same API regardless of wallet |
| 140 | + return await wallet.authorize(params); |
| 141 | +}); |
| 142 | +``` |
| 143 | + |
| 144 | +## Benefits and Limitations |
| 145 | + |
| 146 | +### Benefits |
| 147 | +- **Unified API** across different wallet applications |
| 148 | +- **Secure key management** without exposing private keys |
| 149 | +- **User-controlled wallet selection** via Android's intent system |
| 150 | +- **Persistent sessions** with auth token caching |
| 151 | + |
| 152 | +### Limitations |
| 153 | +- **Android-only** (iOS lacks intent disambiguation) |
| 154 | +- **Requires MWA-compatible wallets** to be installed |
| 155 | +- **WebSocket dependency** for persistent connections |
| 156 | + |
| 157 | +## Conclusion |
| 158 | + |
| 159 | +MWA enables mobile Solana dApps to connect with wallets as seamlessly as web applications, but currently only works on Android due to platform limitations. |
0 commit comments