Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@metamask/sdk-react-native

Package Overview
Dependencies
Maintainers
11
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@metamask/sdk-react-native

MetaMask SDK for React Native applications, enabling seamless integration with MetaMask for blockchain interactions.

  • 0.3.8
  • Source
  • npm
  • Socket score

Version published
Maintainers
11
Created
Source

MetaMask SDK React Native

The MetaMask SDK React Native allows developers to integrate MetaMask seamlessly into React Native applications.

Getting Started

Installation

Install the SDK:

yarn add @metamask/sdk-react-native

iOS Setup

  1. Add the following imports to your AppDelegate.m:
#import <React/RCTBundleURLProvider.h>
#import <React/RCTBridge.h>
#import <React/RCTLinkingManager.h>
  1. Implement the following method in AppDelegate.m to handle deep links:
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
  return [RCTLinkingManager application:app openURL:url options:options];
}
  1. Add the URL scheme to your Xcode project:

    • Open your project in Xcode.
    • Go to the Info tab.
    • Under URL Types, click the + button to add a new URL type.
    • In the URL Schemes field, add your custom scheme (e.g., yourappscheme).
  2. Install CocoaPods dependencies:

cd ios && pod install

Wrap Your Application in the Provider

import React from 'react';
import { AppRegistry } from 'react-native';
import App from './App';
import { MetaMaskProvider } from '@metamask/sdk-react-native';

const sdkOptions = {
  dappMetadata: {
    name: 'Demo React Native App',
    url: 'https://yourdapp.com',
    iconUrl: 'https://yourdapp.com/icon.png',
    scheme: 'yourappscheme',
  },
  infuraAPIKey: 'YOUR_INFURA_API_KEY', // Optional, but highly recommended for a better user experience
};

const Root = () => (
  <MetaMaskProvider sdkOptions={sdkOptions}>
    <App />
  </MetaMaskProvider>
);

AppRegistry.registerComponent('YourAppName', () => Root);

Use the SDK in Your Components

import { useSDK } from '@metamask/sdk-react-native';
import React from 'react';
import { View, Button, Text } from 'react-native';

const App = () => {
  const { sdk, connected, connecting, provider, chainId, account } = useSDK();

  const connect = async () => {
    try {
      await sdk?.connect();
    } catch (err) {
      console.warn('Failed to connect..', err);
    }
  };

  return (
    <View style={{ padding: 20 }}>
      <Button title="Connect" onPress={connect} disabled={connecting} />
      {connected && (
        <View>
          {chainId && <Text>Connected chain: {chainId}</Text>}
          {account && <Text>Connected account: {account}</Text>}
        </View>
      )}
    </View>
  );
};

export default App;

SDK Options

OptionTypeDescriptionMandatory
dappMetadata.namestringName of your dAppYes
dappMetadata.urlstringURL of your dAppYes
dappMetadata.iconUrlstringURL of the icon of your dAppNo
dappMetadata.schemestringCustom scheme for your React Native appYes
infuraAPIKeystringYour Infura API keyNo

SDK Methods

  • connect(): Connect to MetaMask.
  • connectAndSign({ msg }): Connect to MetaMask and sign a message.
  • connectWith(req): Connect to MetaMask with a specific request.
  • terminate(): Terminate the MetaMask connection.

Provider Methods

  • request(req): Make a request to MetaMask.
  • batchRequest(requests): Make batch requests to MetaMask.
  • getChainId(): Get the current chain ID.
  • getSelectedAddress(): Get the selected address.

Example

Refer to the example folder for more info on how to use the SDK.

Contacts

For additional support, open an issue on our GitHub repository.

Keywords

FAQs

Package last updated on 28 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc