Solana NFT Minting with Lighthouse Storage and Metaplex
Solana NFT Minting with Lighthouse Storage and Metaplex
In this tutorial, we'll demonstrate how to mint an NFT on Solana using the Metaplex library and store associated data on the Lighthouse storage platform. Let's dive in.
What is Lighthouse?
Lighthouse stands out in the realm of digital storage, offering a perpetual file storage protocol. Traditional models often burden users with recurrent payments and ongoing maintenance, but Lighthouse reshapes this narrative, enabling users to pay once and secure their files indefinitely. Built atop the decentralized capabilities of IPFS and Filecoin, Lighthouse combines the strengths of both to offer features like genuine perpetual storage, robust encryption, a dedicated IPFS gateway for swift file retrieval, payment flexibility across various tokens, and efficient image optimization. Particularly for NFTs on Solana, Lighthouse ensures that digital assets associated with tokens remain accessible and unchanged, offering an optimal solution for projects prioritizing long-term, reliable storage.
Choose a login method and complete the verification steps.
Click on "API Key" on the dashboard's left side panel.
Name your API key.
Copy the provided API key for later use.
3. Wallet Setup:
Create a Solana wallet and acquire some Devnet SOL for transaction fees. Save the wallet's secret to solanawallet.json in your project directory. Use the Solana File System Wallet (Keypair written to the solanawallet.json) and airdrop some SOL to it. To create a Solana wallet and fund it:
Install the Solana CLI Tools: The Solana command-line tools provide several utilities to help you create and manage your Solana wallet.
Create a Solana Wallet: Once the installation is complete, you can create your Solana wallet.
solana-keygennew--outfilesolanawallet.json
This command will generate a new keypair and save it into solanawallet.json. Make sure to keep your seed phrase in a secure place; it's the only way to recover your wallet. You will also find pubkey, this is also your WALLET_ADDRESS.
Airdrop SOL to Your Wallet: Before you can start minting or making transactions, you need some SOL in your wallet for transaction fees.
Ensure to include .env in your .gitignore to avoid exposing your key.
Minting your NFT
The process of minting your NFT involves preparing your asset files, uploading them to Lighthouse, creating metadata for your NFT, and then using the Metaplex library to mint the NFT on the Solana blockchain.
1. Initialize Environment and Imports:
Set up your script with the necessary imports and load your environment variables.
This orchestrator function uses the above helper functions to upload an image and metadata to Lighthouse, then mints the NFT with the uploaded metadata URL.
asyncfunctionuploadAndMintNFT() {constapiKey=process.env.API_KEY;constimagePath="uploads/lighthouse.png"; // Ensure this path is correctconstnftMetadata= {// Replace with your NFT's metadata name:"Lighthouse Storage NFT #1", symbol:"LSNFT", description:"A description about my lighthouse NFT #1", sellerFeeBasisPoints:500, externalUrl:"", creators: [{ address:process.env.WALLET_ADDRSS, share:100 }], };try {constimageUrl=awaituploadImageToLighthouse(imagePath, apiKey);console.log("Image URL:", imageUrl);nftMetadata.image = imageUrl;nftMetadata.properties = { files: [{ uri: imageUrl, type:"image/png" }], category:"image", creators:nftMetadata.creators, };constmetadataUrl=awaituploadMetadataToLighthouse(nftMetadata, apiKey);console.log("Metadata URL:", metadataUrl);awaitmintNFT(metadataUrl, nftMetadata); } catch (error) {console.error("An error occurred during the NFT creation process:", error);throw error; }}
5. Running the Script:
After defining all functions, call the uploadAndMintNFT() function to start the process. Handle any errors appropriately.
uploadAndMintNFT().catch(console.error);
Complete Script:
Here is the entire script combining all the functions:
Lighthouse offers a robust decentralized storage solution that integrates seamlessly with blockchain platforms like Solana. When minting NFTs, it's essential to have a reliable storage mechanism for the digital assets, and Lighthouse fits the bill perfectly, ensuring the assets' permanence, integrity, and authenticity.