npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@solana-mobile/mobile-wallet-adapter-walletlib

v1.4.0-beta2

Published

A React Native wrapper of the Solana Mobile, Mobile Wallet Adapter Wallet Library. Wallet apps can use this to handle dapp requests for signing and sending.

Downloads

35

Readme

@solana-mobile/mobile-wallet-adapter-walletlib

This is a package that provides React Native bridge for the native mobile-wallet-adapter-walletlib library and it is designed for Wallet apps built in React Native. It provides an API to implement the wallet endpoint of the mobile wallet adapter protocol.

Deep dive and read the full Mobile Wallet Adapter protocol specification.

Note

This package is still in alpha and is not production ready. However, the API is stable and will not change drastically, so you can begin integration with your wallet.

Quickstart

1. Initialize the MWA event listener

Use the following API to start listening for MWA requests and events, and register request handlers.

import {
  initializeMWAEventListener,
  MWARequest,
  MWASessionEvent,
} from '@solana-mobile/mobile-wallet-adapter-walletlib';

const listener: EmitterSubscription = initializeMWAEventListener(
  (request: MWARequest) => { /* ... */ },
  (sessionEvent: MWASessionEvent) => { /* ... */ },
);

/* ... */

// Clean up the listener when it is out of scope
listener.remove()

You should ensure the listener is cleaned up with listener.remove() when it goes out of scope (e.g listener.remove() on component lifecycle unmount).

2. Initialize the MWA session

Define your wallet config and use initializeMWASession to establish a session with the dApp endpoint and begin emission of MWA requests/events.

Note: This should be called after initializeMWAEventListener is called, to ensure no events are missed.

const config: MobileWalletAdapterConfig = {
  supportsSignAndSendTransactions: true,
  maxTransactionsPerSigningRequest: 10,
  maxMessagesPerSigningRequest: 10,
  supportedTransactionVersions: [0, 'legacy'],
  noConnectionWarningTimeoutMs: 3000,
  optionalFeatures: ['solana:signInWithSolana']
};

try {
  const sessionId = await initializeMobileWalletAdapterSession(
    'Wallet Name',
    config,
  );
  console.log('sessionId: ' + sessionId);
} catch (e: any) {
    if (e instanceof SolanaMWAWalletLibError) {
      console.error(e.name, e.code, e.message);
    } else {
      console.error(e);
    }   
}

Note: Although, the initializeMobileWalletAdapterSession method returns a sessionId, this library only supports one active session for now.

Example usage:

// When your MWA entrypoint is loaded, call a `useEffect` to kick off the listener and session.
useEffect(() => {
  async function initializeMWASession() {
    const config: MobileWalletAdapterConfig = {
      supportsSignAndSendTransactions: true,
      maxTransactionsPerSigningRequest: 10,
      maxMessagesPerSigningRequest: 10,
      supportedTransactionVersions: [0, 'legacy'],
      noConnectionWarningTimeoutMs: 3000,
    };
    try {
      const sessionId = await initializeMobileWalletAdapterSession(
        'Wallet Name',
        config,
      );
      console.log('sessionId: ' + sessionId);
    } catch (e: any) {
        if (e instanceof SolanaMWAWalletLibError) {
          console.error(e.name, e.code, e.message);
        } else {
          console.error(e);
        }   
    }
  }
  const listener = initializeMWAEventListener(
    (request: MWARequest) => { /* ... */ },
    (sessionEvent: MWASessionEvent) => { /* ... */ },
  );
  initializeMWASession();

  // When the component is unmounted, clean up the listener.
  return () => listener.remove();
}, []);

3. Handling requests and events

A MWARequest is handled by calling resolve(request, response) and each request have their appropriate response types.

An example of handling an AuthorizationRequest:

import {
  AuthorizeDappResponse
} from '@solana-mobile/mobile-wallet-adapter-walletlib';

const response = {
  publicKey: Keypair.generate().publicKey.toBytes(),
  label: 'Wallet Label',
} as AuthorizeDappResponse;

resolve(authorizationRequest, response)

There are a a selection of "fail" responses that you can return to the dApp. These are for cases where the user declines, or an error occurs during signing, etc.

import {
  UserDeclinedResponse
} from '@solana-mobile/mobile-wallet-adapter-walletlib';

const response = {
  failReason: MWARequestFailReason.UserDeclined,
} as UserDeclinedResponse;

// Tells the dApp user has declined the authorization request
resolve(authorizationRequest, response)

Properties of an MWA Request

Each MWA Request is defined in resolve.ts. Each come with their own properties and completion response structures.

If you want to understand the dApp perspective and how a dApp would send these requests, see MWA API Documentation for dAppstypescript/mobile-wallet-adapter.

MWARequest Interfaces

IMWARequest

This is the base interface that all MWARequsts inherit from. The fields defined here are used in the package's internal implementation and the package consumer will generally not use them.

Fields:

  • __type: An enum defining the type of MWA Request it is.
  • requestId: A unique identifier of this specific MWA Request
  • sessionId: A unique identifier of the MWA Session this request belongs to.

IVerifiableIdentityRequest

This an interface that describes MWA Requests that come with a verifiable identity and the following 3 fields.

Fields:

  • authorizationScope: A byte representation of the authorization token granted to the dApp.
  • cluster: The Solana RPC cluster that the dApp intends to use.
  • appIdentity: An object containing 3 optional identity fields about the dApp:
    • Note: The iconRelativeUri is a relative path, relative to identityUri.
{
  identityName: 'dApp Name',
  identityUri:  'https://yourdapp.com'
  iconRelativeUri: "favicon.ico", // Full path resolves to https://yourdapp.com/favicon.ico
}

MWARequest Types

  • AuthorizeDappRequest

    • Spec
    • Interfaces: IMWARequest
  • ReauthorizeDappRequest

    • Spec
    • Interfaces: IMWARequest, IVerifiableIdentityRequest
  • DeauthorizeDappRequest

    • Spec
    • Interfaces: IMWARequest, IVerifiableIdentityRequest
  • SignMessagesRequest

    • Spec
    • Interfaces: IMWARequest, IVerifiableIdentityRequest
  • SignTransactionsRequest

    • Spec
    • Interfaces: IMWARequest, IVerifiableIdentityRequest
  • SignAndSendTransactionsRequest

    • Spec
    • Interfaces: IMWARequest, IVerifiableIdentityRequest