Mobile Wallet Adapter

repository·main·Indexed 43 days ago

https://github.com/solana-mobile/mobile-wallet-adapter

A 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.

Tokens
20.9K
Snippets
50
Records
106
Agent score
50%

What's inside MWA

  1. Overview of the Mobile Wallet Adapter (MWA) specification

    main
    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.
  2. Overview of the Mobile Wallet Adapter specification

    main

    The 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.
  3. Overview of Mobile Wallet Adapter

    main

    The 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.js datatypes 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.
  4. Use @solana-mobile/wallet-standard-mobile to enable native wallet support in web apps

    main

    The @solana-mobile/wallet-standard-mobile package is a plugin designed to register a @wallet-standard/wallet-standard implementation specifically for mobile environments.

    By using this plugin, web applications running on mobile devices can interface with native wallet applications to:

    1. Sign messages: Request the user to sign arbitrary messages via their native wallet.
    2. Sign transactions: Request the user to sign Solana transactions.
    3. Send transactions: If the native wallet supports it, the web app can trigger the sending of transactions directly through the wallet.
  5. Use @solana-mobile/mobile-wallet-adapter-protocol-web3js for web3.js integration

    main

    The @solana-mobile/mobile-wallet-adapter-protocol-web3js package is a convenience wrapper designed to bridge the gap between the Mobile Wallet Adapter (MWA) protocol and the standard @solana/web3.js library.

    It allows you to use common @solana/web3.js primitives—specifically Transaction and Uint8Array—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.

  6. Use @solana-mobile/mobile-wallet-adapter-protocol-kit for convenience

    main
    The @solana-mobile/mobile-wallet-adapter-protocol-kit package is a convenience wrapper designed to bridge the gap between the Mobile Wallet Adapter (MWA) protocol and the @solana/kit ecosystem. It allows you to use common @solana/kit primitives—specifically Transaction and Signature—directly with the @solana-mobile/mobile-wallet-adapter-protocol.
  7. Understand the Reflector protocol

    main

    The 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>/reflect
    • wss://<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

    1. Half-open reflections: Connections waiting for a counterparty to connect. If no reflector_unique_id is provided during connection, the reflector generates one, adds it to this set, and sends a REFLECTOR_ID message to the endpoint. Incoming data is discarded until the pair is complete.
    2. Fully open reflections: Connections where both endpoints have successfully connected. The reflector sends an APP_PING message 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 PING frames to maintain active connections.
  8. Terminate a session using SESSION_END in Nostr

    main

    Because Nostr relays do not notify subscribers when a counterparty disconnects, the dapp endpoint can explicitly send a SESSION_END message 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": ""
    }
  9. Understand the Association process

    main

    Association 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 the association_token.

  10. Extending Mobile Wallet Adapter to other chains

    main

    The 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:

    1. Association URIs: Using new schemes such as newchain-wallet:// instead of the current solana-wallet://.
    2. 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 the options object.

    Specific implementation details for new chains will be provided via updates to the core specification or through accompanying extensions.

  11. When to use `@solana-mobile/mobile-wallet-adapter-protocol`

    main

    This 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-mobile instead 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.
  12. How Association works in Mobile Wallet Adapter

    main

    Association 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 Qa is encoded using the X9.62 format (0x04 || x || y), base64url-encoded, and used as the association_token.

    # No runnable code provided for the raw key generation logic, but the token format is:
    # association_token = base64url(X9.62(Qa))