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

@aztec-3/react-native-walletconnect

v1.0.0

Published

WalletConnect for React Native

Downloads

3

Readme

@aztec-3/react-native-walletconnect

A drop-in library which helps easily connect your React Native dapps to Ethereum Wallets on Android, iOS and the Web.

Notice: This library assumes you have already enabled prerequisite support for Web3 inside your application. This can be done by creating a new project using npx create-react-native-dapp, or by introducing support for Web3 in an existing project by using npx rn-nodeify --install --hack.

For more details, check out the documentation.

Installing

To get started, install @walletconnect/react-native-dapp:

yarn add @walletconnect/react-native-dapp

If you haven't already, you may also need to install react-native-svg alongside a persistent storage provider such as @react-native-async-storage/async-storage:

yarn add react-native-svg @react-native-async-storage/async-storage

Architecture

This library is implemented using the React Context API, which is used to help make an instance of a connector accessible globally throughout your application. This permits you to use a uniform instance within even deeply nested components, and ensures your rendered application is always synchronized against the connector state.

WalletConnectProvider

At the root of your application, you can declare a WalletConnectProvider which controls access and persistence to a connector instance:

import * as React from 'react';
import WalletConnectProvider from '@walletconnect/react-native-dapp';
import AsyncStorage from '@react-native-async-storage/async-storage';

export default function App(): JSX.Element {
  return (
    <WalletConnectProvider
      redirectUrl={Platform.OS === 'web' ? window.location.origin : 'yourappscheme://'}
      storageOptions= {{
        asyncStorage AsyncStorage,
      }}>
      <>{/* awesome app here */}</>
    </WalletConnectProvider>
  );
}

Above, we pass the WalletConnectProvider two required parameters; redirectUrl and storageOptions:

  • The redirectUrl is used to help control navigation between external wallets and your application. On the web, you only need to specify a valid application route; whereas on mobile platforms, you must specify a deep link URI scheme.
  • The storageOptions prop allows you to specify the storage engine which must be used to persist session data.

Notably, the WalletConnectProvider optionally accepts WalletConnect configuration arguments as defined by the IWalletConnectOptions interface:

import * as React from 'react';
import WalletConnectProvider from '@walletconnect/react-native-dapp';
import AsyncStorage from '@react-native-async-storage/async-storage';

export default function App(): JSX.Element {
  return (
    <WalletConnectProvider
      bridge="https://bridge.walletconnect.org"
      clientMeta={{
        description: 'Connect with WalletConnect',
        url: 'https://walletconnect.org',
        icons: ['https://walletconnect.org/walletconnect-logo.png'],
        name: 'WalletConnect',
      }}
      redirectUrl={Platform.OS === 'web' ? window.location.origin : 'yourappscheme://'}
      storageOptions= {{
        asyncStorage AsyncStorage,
      }}>
      <>{/* awesome app here */}</>
    </WalletConnectProvider>
  );
}

In the snippet above, aside from the required props, we can see the default configuration of the WalletConnectProvider.

Tip: Your custom options are merged deeply against this default configuration. Therefore it's possible to override individual nested properties without being required to define all of them.

withWalletConnect

Alternatively to manually using the WalletConnectProvider, you can use the withWalletConnect higher order component which will wrap your root application in a WalletConnectProvider for you:

import * as React from 'react';
import { withWalletConnect, useWalletConnect } from '@walletconnect/react-native-dapp';
import AsyncStorage from '@react-native-async-storage/async-storage';

function App(): JSX.Element {
  const connector = useWalletConnect(); // valid
  return <>{/* awesome app here */}</>;
}

export default withWalletConnect(App, {
  clientMeta: {
    description: 'Connect with WalletConnect',
  },
  redirectUrl: Platform.OS === 'web' ? window.location.origin : 'yourappscheme://',
  storageOptions: {
    asyncStorage: AsyncStorage,
  },
});

This is almost identical in functionality to the manual implementation of a WalletConnectProvider, with the key difference that we're able to make a call to useWalletConnect directly from the App component. By contrast, in the previous example only child components of the WalletConnectProvider may be able to invoke this hook.

useWalletConnect

The useWalletConnect hook provides access to a WalletConnect connector instance which is accessible on Android, iOS and the Web. This conforms to the original specification:

import AsyncStorage from '@react-native-async-storage/async-storage';
import { useWalletConnect, withWalletConnect } from '@walletconnect/react-native-dapp';
import * as React from 'react';

function App(): JSX.Element {
  const connector = useWalletConnect();
  if (!connector.connected) {
    /**
     *  Connect! 🎉
     */
    return <Button title="Connect" onPress={() => connector.connect())} />;
  }
  return <Button title="Kill Session" onPress={() => connector.killSession()} />;
}

export default withWalletConnect(App, {
  redirectUrl: Platform.OS === 'web' ? window.location.origin : 'yourappscheme://',
  storageOptions: {
    asyncStorage AsyncStorage,
  },
});

Customization

@walletconnect/react-native-dapp also permits you to customize the presentation of the QrcodeModal. This is achieved by passing the Render Callback prop, renderQrcodeModal, to our calls to withWalletConnect or instances of WalletConnectProvider.

For example, you could choose to render a wallet selection using a BottomSheet opposed to a Modal:

import AsyncStorage from '@react-native-async-storage/async-storage';
import BottomSheet from 'react-native-reanimated-bottom-sheet';
import { Image, Text, TouchableOpacity } from 'react-native';
import {
  useWalletConnect,
  withWalletConnect,
  RenderQrcodeModalProps,
  WalletService,
} from '@walletconnect/react-native-dapp';
import * as React from 'react';

function CustomBottomSheet({
  walletServices,
  visible,
  connectToWalletService,
  uri,
}: RenderQrcodeModalProps): JSX.Element {
  const renderContent = React.useCallback(() => {
    return walletServices.map((walletService: WalletService, i: number) => (
      <TouchableOpacity key={`i${i}`} onPress={() => connectToWalletService(walletService, uri)}>
        <Image source={{ uri: walletService.logo }} />
        <Text>{walletService.name}</Text>
      </TouchableOpacity>
    ));
  }, [walletServices, uri]);
  return <BottomSheet renderContent={renderContent} {...etc} />;
};

function App(): JSX.Element {
  const connector = useWalletConnect();
  return <>{/* awesome custom app here */}</>;
}

export default withWalletConnect(App, {
  redirectUrl: Platform.OS === 'web' ? window.location.origin : 'yourappscheme://',
  storageOptions: {
    asyncStorage AsyncStorage,
  },
  renderQrcodeModal: (props: RenderQrcodeModalProps): JSX.Element => (
    <CustomBottomSheet {...props} />
  ),
});