Skip to main content

How to prepare Secret NFT assets

A Ternoa Secret NFT is a unique type of NFT that incorporates encrypted data, making it more secure and exclusive than a Basic NFT. Only the current owner of the Secret NFT has access to the encrypted data at any given time.

There are two ways to create a Secret NFT: either by adding a secret to an existing Basic NFT or by creating a new Secret NFT from scratch. In both cases, you will need to prepare the Basic NFT assets beforehand.

A Ternoa Secret NFT comprises two additional files: a secret asset file (such as an image, video, or music file) and a metadata JSON file for the secret. To ensure maximum security and exclusivity, the secret asset file must be encrypted, and the encrypted file is nested within the metadata file. Both files are stored on IPFS (Interplanetary File Systems) with unique hashes dedicated to each file.

IPFS (Interplanetary File Systems) is one of the solution we recommend to upload NFTs medias and other associated metadatas. Thus offchain datas are stored in a fully decentralized way and only the link to this metadata is stored on-chain as part of the NFT. This link is frequently a fingerprint called a cryptographic ID (e.g. Qmf5RHhnUjSCfCN9d1Ee6sUWxe3Eqvogw1cTsssrxAxtPn). IPFS files are accessible using those hashes.

Ternoa provides its own IPFS public nodes on different HTTP gateways based on the network environement:

  • MAINNET: ipfs-mainnet.trnnfr.com
  • ALPHANET: ipfs-alphanet.trnnfr.com
info

Please note that an api-key is needed to store data on those gateways. Visit IPFS Keymanager to get your API Key.

Off-Chain Metadata

An NFT is an unique ID. On-chain data contains this ID couple with additionnal important information like ownership, royalties and more. However medias and metadatas are stored off-chain for performance and flexibility. These data must be carefully written to guaranty a compatibility accross the tools and dApps of the ecosystem. Ternoa Improvement Proposals (TIPs) propose structures adopted from ERC-1155 to ensure this compatibility.

Here below is the expected format for secret NFT:

Secret NFT

{
"title": "(OPTIONAL) Title of the Secret NFT",
"description": "(OPTIONAL) Description of the Secret NFT",
"properties": {
"encrypted_media": {
"hash": "CID Hash of the encrypted media",
"type": "Type of the encrypted media (file format)",
"size": "Size of the encrypted media",
"name": "(OPTIONAL) Name of the encrypted media",
"description": "(OPTIONAL) Description of the encrypted media"
},
"public_key_of_nft": "Public key associated with the Secret NFT"
}
}

Ternoa IPFS Client

Prerequisites:

An IPFS client is available on Ternoa-JS SDK to make IPFS upload simple with only one line of code.

Place any asset you want to use at the root of your project and use the following code snippet by completing FILE_NAME & FILE_TYPE. A PUBLIC_PGP_KEY is also require to encrypt the secret asset before uploading it to IPFS.

import fs from "fs";
import { encryptFile, TernoaIPFS, File } from "ternoa-js";

const main = async () => {
const file = new File(
[await fs.promises.readFile("FILE_NAME")],
"FILE_NAME",
{
type: "FILE_TYPE",
}
);
const encryptedFile = await encryptFile(file, "PUBLIC_PGP_KEY");

const ipfsClient = new TernoaIPFS(new URL("IPFS_NODE_URL"), "IPFS_API_KEY");

const nftMetadata = {
title: "(OPTIONAL) Something strong.",
description: "(OPTIONAL) This is my first Secret NFT on Ternoa.",
};

const mediaMetadata = {
name: "(OPTIONAL) FILE_NAME",
};

const { Hash } = await ipfsClient.storeSecretNFT(
encryptedFile,
"FILE_TYPE",
"PUBLIC_PGP_KEY",
nftMetadata,
mediaMetadata
);
console.log("The off-chain metadata CID hash is ", Hash);
};

First the asset file is read from the file system and wrapped in a specific File instance imported from the Ternoa-JS library. The encryptFile function is used to encrypt the file contents using a specified public PGP key. The TernoaIPFS class is then used to create an IPFS client that connects to a specified IPFS node using a given API key. The metadatas for the file and the media are then defined in an object and passed to the storeSecretNFT method of the client along with the encrypted file, the file type and the public PGP key. The resulting Hash of the off-chain metadata is logged to the console.

The storeSecretNFT method handles two IPFS uploads under the hood: a first one with the NFT media (e.g. the image) and a second one with the JSON metadata file, including the media's hash from the first upload response. This method also validates the metadata structure to ensure TIPs compatibility.

info

Ternoa-JS provides a convenient helper function named secretNftEncryptAndUploadFile that combines the file encryption and storage of a Secret NFT on IPFS.

info

You can generate a PGP key pair using the generatePGPKeys helper from Ternoa-JS.

Next

Next step will be the on-chain minting using Hash previously generated. Keep it and continue on the "How to mint a Secret NFT on-chain" guide.

Support

If you face any trouble, feel free to reach out to our community engineers in our Discord.