This guide will focus on integrating with the Metaplex JS SDK in a React Native app with Mobile Wallet Adapter.
Installation
Install the Metaplex JS package to your project.
Polyfill installation
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.
1. Install polyfills
2. Add polyfills to resolver in metro.config.js
Adding the resolver property lets the Metro know which packages to substitute with when seeing a require.
3. Add imports to index.js
Usage
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.
Using MWA as an Identity Driver
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.
Create an MWA Identity Signer:
Using the Identity Driver
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.
Storage Drivers
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.
Viewing owned NFTs
Now with the Metaplex instance, you can access the nfts() module that provides easy interaction and signing with onchain programs.
Minting an NFT
For example, to mint an NFT:
The nfts() module provides plenty of other APIs. Look through the Metaplex docs for full details.