Cross-Chain NFTs with SecretPath and OpenAI

Learn how to mint cross-chain privacy-preserving NFTs with SecretPath

Overview

SecretPath enables EVM developers to use Secret Network as a Confidential Computation Layer (CCL) for all EVM-compatible chains.

In this developer tutorial, you will learn how to use SecretPath to enable cross-chain NFTs on the EVM. You will mint an NFT on Sepolia testnet that uses a sister contract on Secret Network to store private metadata, which can only be accessed by a private password stored in the Secret Contract. And to spice things up, we will generate the NFT image with OpenAI 🔥.

  1. Generate an AI image with OpenAI

  2. Pin the newly generated image with Pinata to create the token URI

  3. Mint the NFT on Sepolia with your token URI

  4. Send the confidential metadata to your Secret Contract with SecretPath

See Cross-Chain NFT demo here.

Getting Started

  1. Git clone the SecretPath tutorials repository:

git clone https://github.com/SecretFoundation/Secretpath-tutorials.git
  1. Get Secret Network testnet tokens from faucet

  2. Get Sepolia testnet tokens from faucet

Deploying Secret Network Sister Contract

cd into secretpath-nft/secret-contract

cd secretpath-nft/secret-contract

Compile the contract:

make build-mainnet

If you are on a Mac and run into compilation error:

error occurred: Command “clang”

Make sure you have the latest version of Xcode installed and then update your clang path by running the following in your terminal:

cargo clean

AR=/opt/homebrew/opt/llvm/bin/llvm-ar CC=/opt/homebrew/opt/llvm/bin/clang cargo build --release --target wasm32-unknown-unknown

See here for instructions on updating your clang path.

Once the contract compiles successfully, install the dependencies to upload it to testnet:

cd node && npm i 

Create an .env file and add your Secret wallet mnemonic:

MNEMONIC="your mnemonic words to go here"

Upload the contract:

node upload

Upon successful upload, your contract address will be returned:

Contract hash: 9391c79a8dcceb8a58334e0a78e357e328eff872b75af6ebc7d267bbdc9b32dc

contract address: secret1cndts3q2shapmkmjsmf74r8v6u6drvwjl52v7p

Before we upload the EVM contract which mints the NFT, let's examine what the Secret Network sister contract does.

Navigate to the state.rs file to the ConfidentialMetadata struct:

pub struct ConfidentialMetadata {
    pub owner: String,
    pub token_id: u64,
    pub uri: String,
    pub private_metadata: String, 
    pub password: String,
}

This is the confidential metadata that you will be storing in the Secret Network smart contract for each NFT that you mint on the EVM. There is an owner, token_id, uri, private_metdata, and password, all of which will be associated with each NFT you mint.

Now open contract.rs and navigate to the execute_store_confidential_metadata function.

Here we have a keymap called CONFIDENTIAL_METADATA, which maps the token id of your EVM NFT to a ConfidentialMetadata struct. So for every NFT you mint on EVM, it has an associated private metadata stored in the Secret Contract. Pretty cool, right!

Note that this is for demo purposes only, if you were doing this in production you should use a more secure way of linking the EVM NFT to the Secret Network sister contract, such as a ZK proof.

Now let's deploy our EVM Minting contract 😊

Deploying EVM NFT Contract

cd into cd secretpath-nft/evm-contract:

cd secretpath-nft/evm-contract

Install the dependencies:

npm i 

Create a .env file and add your EVM PRIVATE_KEY and Infura API Key for Sepolia testnet:

PRIVATE_KEY=5766fdksdfldsfjdlfd84749393j0d
INFURA_KEY=7bb38fdsfjddkfjdfaljf9d82

Once you have your env file configured, upload the minting contract:

npx hardhat run scripts/deployNFT.js --network sepolia

Upon successful upload, a contract address will be returned:

0x0061b1aecAAe02Ddd3ce9De631a2C817c5be18F8

Congrats! You now have a private metadata contract deployed on Secret Network and a minting contract deployed on Sepolia testnet! Now let's learn how to use SecretPath to send data confidentially from the EVM to Secret Network 😎

Sending Cross-Chain Confidential Metadata with SecretPath

It's time to connect your EVM minting contract and your Secret confidential metadata contract to a frontend and SecretPath! You will need to create a Pinata JWT key as well as an OpenAI API key, which is outlined below :D

Setting up the frontend environment

cd into secretpath-nft/frontend and install the dependencies:

cd frontend && npm i 

Create an env and add the following:

# Secret Contract Address and CodeHash
REACT_APP_SECRET_CONTRACT_ADDRESS="your secret contract address"
REACT_APP_SECRET_CODE_HASH="your secret contract codehash"

# EVM Minting Contract Address 
REACT_APP_CONTRACT_ADDRESS="your evm minting contract address"

# SecretPath Address for Sepolia
REACT_APP_SECRETPATH_ADDRESS="0x3879E146140b627a5C858a08e507B171D9E43139"

# ChainID for Sepolia ( hexadecimal)
REACT_APP_CHAIN_ID="0xAA36A7"

# Pinata API Key
REACT_APP_PINATA_JWT="Your Pinata JWT Key"

# OpenAI API Key
REACT_APP_OPENAI_API_KEY="Your OpenAI API Key"
  • Create a Pinata JWT key here

  • Create an OpenAI API key here

Once you have added all of your environment variables, it's time to execute the contracts with our frontend!

Minting your NFT

We will dive into the SecretPath code shortly, but first, let's create your first cross-chain NFT with SecretPath :D

cd into frontend:

cd frontend

Then run the program:

npm run start

The frontend is designed so that the description of your NFT will be sent to OpenAI to as the prompt to generate your NFT image. So choose wisely, or not. 😂

The NFT confidential message and password is stored in your Secret Contract, which is linked to the tokenID of the NFT stored on Ethereum. Only your unique password can reveal the private metadata.

Once you mint your NFT, navigate to opensea testnet to see it in all its glory.

Better yet, view the NFT on your frontend to see it paired with the Secret Network sister contract with confidential metadata:

Congrats on minting your first cross-chain encrypted NFT! 🚀

Putting it all together

Let's walkthrough the minting steps, and then examine the SecretPath code:

  1. Mint the NFT on Sepolia with your token URI

The majority of the SecretPath encryption code is boilerplate; you don't need to change a thing!

The only code you are updating is:

const routing_contract = process.env.REACT_APP_SECRET_CONTRACT_ADDRESS;
const routing_code_hash = process.env.REACT_APP_SECRET_CODE_HASH;
  • the data that you pass to the Secret Network sister contract

  const data = JSON.stringify({
        owner: myAddress,
         token_id: token_id,
            uri: uri,
         private_metadata: secretMessage, 
         password: password
     
       });
  • and the handle function that you are calling in the Secret sister contract

 const _info = {
      user_key: hexlify(userPublicKeyBytes),
      user_pubkey: user_pubkey,
      routing_code_hash: routing_code_hash,
      task_destination_network: "pulsar-3",
      handle: "execute_store_confidential_metadata",
      nonce: hexlify(nonce),
      payload: hexlify(ciphertext),
      payload_signature: payloadSignature,
      callback_gas_limit: callbackGasLimit,
    };

The execute_store_confidential_metadatahandle executes the handle in the Secret Network sister contract

Summary

SecretPath allows EVM developers to leverage Secret Network as a Confidential Computation Layer (CCL) for EVM-compatible chains, enabling cross-chain NFTs with confidential metadata. This tutorial guides developers through minting an NFT on Sepolia testnet, using a sister contract on Secret Network to store private metadata accessible only via a private password in the Secret Contract. The process includes generating an NFT image with OpenAI, pinning it with Pinata, minting the NFT on Sepolia, and sending confidential metadata to Secret Network using SecretPath.

Note: the end user of the application is not exposed to Secret Network and is only working directly in the EVM environment. However, the data is fully protected and cannot be viewed by anyone because it is stored in encrypted Secret contracts 😮‍💨

If you have any questions or run into any issues, post them on the Secret Developer Discord and somebody will assist you shortly.

Last updated