Metaplex provides a collection of on-chain tools/programs for creating and managing NFTs on Solana. In addition, Metaplex provides multiple Client SDKs that makes it easier for developers to interact and use their on chain programs.
This guide will focus on integrating with the Metaplex JS SDK in a React Native app with Mobile Wallet Adapter.
The Metaplex JS SDK was originally written for a Browser/Node environment, so certain dependencies aren’t immediately available on React Native. These polyfill libraries will fill in the missing libraries and enable React Native compatibility.
import 'react-native-url-polyfill/auto'; // Add this before the 'App' import!import {AppRegistry} from 'react-native';import App from './App';import {name as appName} from './app.json';AppRegistry.registerComponent(appName, () => App);
The entry point to the JavaScript SDK is a Metaplex instance that will give you access to its API. It provides a convenient API to interact with on-chain programs, simplifying actions like minting an NFT.It accepts a Connection instance from @solana/web3.js that will be used to communicate with the cluster.
Report incorrect code
Copy
Ask AI
import {Metaplex} from '@metaplex-foundation/js';import {Connection} from '@solana/web3.js';const metaplex = Metaplex.make(connection);
Metaplex also allows you to further customize who the SDK should interact on behalf of, by providing an “Identity Driver”. You can use the Mobile Wallet Adapter methods to implement an Identity Driver. With this provided, Metaplex SDK can request signing for transactions/messages when needed.
Then you need to wrap the implemented IdentitySigner within a MetaplexPlugin. Now you can call the use method on the Metaplex instance and supply the identity driver plugin.The Metaplex instance requires passing in a MetaplexPlugin, so you need to first create a plugin that wraps the identity signer. Lastly, you can call the use method on the Metaplex instance and supply the identity driver plugin.
Report incorrect code
Copy
Ask AI
// Create a Metaplex Plugin for the identity driverconst mobileWalletAdapterIdentity = ( mwaIdentitySigner: IdentitySigner,): MetaplexPlugin => ({ install(metaplex: Metaplex) { metaplex.identity().setDriver(mwaIdentitySigner); },});// Finally, create the Metaplex instance with the identity driver.const metaplex = Metaplex.make(connection).use( mobileWalletAdapterIdentity(mwaIdentitySigner),);
On Browser/Node environments, the SDK also allows you to specify a “Storage Driver” that conveniently integrates various de/centralized storage options (e.g: Bundlr, IPFS, AWS), for uploading media assets and metadata.For React Native, the usual storage driver plugins will not work. Read the note below!
WARNINGThe existing 3rd party storage drivers provided by Metaplex are not compatible with React Native, due to reliance on Node libraries! For example, the bundlrStorage() and nftStorage() plugins will not work and throw an error.
TIPThe workaround solution is to manually interact with the storage providers directly, without relying on a prebuilt Metaplex SDK/plugin.
For example, you can upload directly to IPFS using NFT.storage’s REST API. You can see an code example of this in the example app.