Mobile Wallet Adapter
repository·main·Indexed 43 days ago
https://github.com/solana-mobile/mobile-wallet-adapterA protocol and set of libraries enabling interaction between Solana dApps and mobile wallets on Android and web platforms. Includes the @solana-mobile/mobile-wallet-adapter-protocol for low-level API calls, convenience wrappers for @solana/kit and @solana/web3.js, and walletlib for implementing MWA event listeners and session management in React Native wallets.
What's inside MWA
- The Mobile Wallet Adapter (MWA) is a protocol designed to expose Solana wallet functionality (authorization and transaction signing) to dapps on mobile (iOS/Android) and desktop platforms. It aims to provide a standard way for native and web-based dapps to connect to native wallet apps, ensuring secure, private, and simple user experiences.
Overview of the Mobile Wallet Adapter specification
mainThe Mobile Wallet Adapter (MWA) is a protocol designed to expose Solana wallet functionality—specifically authorization and transaction signing—to dapps. It is optimized for mobile environments (iOS and Android) but is compatible with desktop OSs.
Key goals include:
- Enabling a single mobile device to act as a universal wallet for transactions across different devices.
- Ensuring dapp transactions remain private and secure.
- Providing a one-time authorization mechanism for persistent dapp-to-wallet connections.
- Supporting both native dapps and web-based dapps (via mobile browsers) through standard web technologies.
Overview of Mobile Wallet Adapter
mainThe Mobile Wallet Adapter (MWA) provides a standardized way for Solana mobile dApps to interact with Solana mobile wallets. It includes a protocol specification and reference implementations for both Android and JavaScript environments.
Core Components
For dApp Developers
- Android Client Library: An Android library (
clientlib) used by dApps to consume transaction signing services from a wallet. - JavaScript Protocol & Wrappers:
@solana-mobile/mobile-wallet-adapter-protocol: A reference implementation of the MWA protocol.@solana-mobile/mobile-wallet-adapter-protocol-web3js: A convenience wrapper that allows using@solana/web3.jsdatatypes as inputs to the protocol.@solana-mobile/wallet-adapter-mobile: A plugin for use with the standard Solana wallet adapter.
- React Native Support: An example React Native app is provided to demonstrate protocol interaction.
For Wallet Developers
- Android Wallet Library: An Android library (
walletlib) that allows wallets to provide MWA transaction signing services to dApps.
- Android Client Library: An Android library (
Use @solana-mobile/wallet-standard-mobile to enable native wallet support in web apps
mainThe
@solana-mobile/wallet-standard-mobilepackage is a plugin designed to register a@wallet-standard/wallet-standardimplementation specifically for mobile environments.By using this plugin, web applications running on mobile devices can interface with native wallet applications to:
- Sign messages: Request the user to sign arbitrary messages via their native wallet.
- Sign transactions: Request the user to sign Solana transactions.
- Send transactions: If the native wallet supports it, the web app can trigger the sending of transactions directly through the wallet.
Use @solana-mobile/mobile-wallet-adapter-protocol-web3js for web3.js integration
mainThe
@solana-mobile/mobile-wallet-adapter-protocol-web3jspackage is a convenience wrapper designed to bridge the gap between the Mobile Wallet Adapter (MWA) protocol and the standard@solana/web3.jslibrary.It allows you to use common
@solana/web3.jsprimitives—specificallyTransactionandUint8Array—directly with the@solana-mobile/mobile-wallet-adapter-protocol. This simplifies the process of preparing transactions in a standard web3.js format and passing them through the MWA protocol to a mobile wallet.Use @solana-mobile/mobile-wallet-adapter-protocol-kit for convenience
mainThe@solana-mobile/mobile-wallet-adapter-protocol-kitpackage is a convenience wrapper designed to bridge the gap between the Mobile Wallet Adapter (MWA) protocol and the@solana/kitecosystem. It allows you to use common@solana/kitprimitives—specificallyTransactionandSignature—directly with the@solana-mobile/mobile-wallet-adapter-protocol.Understand the Reflector protocol
mainThe Reflector protocol is a simple mechanism used to reflect communications between two endpoints. It acts as a relay, transmitting all received communications from one endpoint to the other, with a maximum frame size of 4KB.
Connection Endpoints
The reflector listens for WebSocket secure (WSS) connections at:
wss://<host_authority>/reflectwss://<host_authority>/reflect?id=<reflector_unique_id>
Where
<reflector_unique_id>is the base64url encoded reflector ID byte sequence provided by the server during the association process.Connection Lifecycle
- Half-open reflections: Connections waiting for a counterparty to connect. If no
reflector_unique_idis provided during connection, the reflector generates one, adds it to this set, and sends aREFLECTOR_IDmessage to the endpoint. Incoming data is discarded until the pair is complete. - Fully open reflections: Connections where both endpoints have successfully connected. The reflector sends an
APP_PINGmessage to both, and then begins bidirectional transmission.
Timeouts and Disconnection
- Half-open timeout: Connections in the half-open set are closed if a counterparty does not connect within 30 seconds.
- Fully open timeout: Connections in the fully open set are closed if they remain inactive for 90 seconds.
- Disconnection: If a connection in a fully open pair drops, the reflector closes the corresponding connection in the pair.
- Maintenance: The reflector sends periodic
PINGframes to maintain active connections.
Terminate a session using SESSION_END in Nostr
mainBecause Nostr relays do not notify subscribers when a counterparty disconnects, the dapp endpoint can explicitly send a
SESSION_ENDmessage to notify the wallet that the session is terminating.Wallet Endpoint Actions upon receiving SESSION_END:
- Immediately close the Nostr relay connection.
- End the MWA session cleanly.
SESSION_END Event Structure:
{ "kind": 20012, "pubkey": "<dapp_nostr_pubkey>", "tags": [ ["d", "<session_identifier>"], ["p", "<wallet_nostr_pubkey>"], ["msg", "SESSION_END"] ], "content": "" }Understand the Association process
mainAssociation is the ephemeral process of establishing a shared identifier between a dapp endpoint and a wallet endpoint. A new association must be performed every time a dapp seeks to connect to a wallet.
To perform an association, the dapp endpoint must generate an ephemeral EC keypair on the P-256 curve. The public keypoint $Q_a$ is encoded using the X9.62 format
(0x04 || x || y), base64url-encoded, and used as theassociation_token.Extending Mobile Wallet Adapter to other chains
mainThe Mobile Wallet Adapter specification is currently designed for the Solana network, but it is architected to support other blockchain networks in the future. Support for additional chains would involve three primary modifications:
- Association URIs: Using new schemes such as
newchain-wallet://instead of the currentsolana-wallet://. - RPC Method Parameters:
authorize: Utilizing a different set of chain identifiers.get_capabilities: Expressing domain-specific capabilities relevant to the new chain.sign_and_send_transactions: Using chain-specific shapes for theoptionsobject.
Specific implementation details for new chains will be provided via updates to the core specification or through accompanying extensions.
- Association URIs: Using new schemes such as
When to use `@solana-mobile/mobile-wallet-adapter-protocol`
mainThis package is a reference implementation of the Mobile Wallet Adapter specification in JavaScript.
- Use this package if: You are implementing the protocol itself or need to interact directly with the low-level specification to issue API calls like
sign_messages. - Use
@solana-mobile/wallet-adapter-mobileinstead if: You are simply looking to integrate a standard JavaScript application (like a dApp) with mobile wallets. The latter provides a higher-level abstraction for common integration tasks.
- Use this package if: You are implementing the protocol itself or need to interact directly with the low-level specification to issue API calls like
How Association works in Mobile Wallet Adapter
mainAssociation is the process of establishing a shared identifier between a dapp endpoint and a wallet endpoint. This process is ephemeral and must be performed every time a dapp seeks to connect to a wallet.
To initiate association, the dapp endpoint must generate an ephemeral EC keypair on the P-256 curve. The public keypoint
Qais encoded using the X9.62 format(0x04 || x || y), base64url-encoded, and used as theassociation_token.# No runnable code provided for the raw key generation logic, but the token format is: # association_token = base64url(X9.62(Qa))