Update a Capsule NFT content
Prerequisites
Before getting started, make sure you have the following ready:
Create a Ternoa account with Alphanet CAPS
Install and set up your editor of choice (we will use Visual Studio Code [VSC] in this tutorial)
Install NodeJS v.14+ & NPM
Update a Capsule NFT using Ternoa-JS: Add a new asset to the Capsule
If you want to update the content of a Capsule NFT on the Ternoa chain by adding a new asset, follow these steps:
Retrieve the capsule off-chain data CID hash using Ternoa Indexer.
Prepare new asset: encryption and storage on IPFS.
Prepare new capsule metadata with the new encrypted media added to the content array.
Update the capsule off-chain data CID hash on-chain.
Step 1: Retrieve the capsule off-chain data CID hash using Ternoa Indexer
Ternoa indexer is a record of the Ternoa Chain data. You can query data for some specific entities (NFT, Collection, Marketplace(...)) using GraphQL.
You first need to prepare a stringified query to get NFT data from a specific Capsule NFT id.
The data we will use in step 2 is capsuleOffchainData
the IPFS CID hash of the Capsule NFT (e.g. "QmNM7fDadTjoLKqMi8i4Lyru4FUiKyQYpRmSoXnDjtgm6N").
Learn more about the NFTEntity and how to fetch data from the Ternoa Indexer here.
Step 2: Prepare new asset - encryption and stored on IPFS
Before adding a new asset to an existing Capsule NFT you have to encrypt the asset and store it on IPFS. Ternoa-JS provides you with an encryptFile
helper to do so. It uses a specified public PGP key to encrypt the asset. You can generate a PGP key pair using the generatePGPKeys
helper from Ternoa-JS. To store the encrypted asset on IPFS you can use the storeFile
method from the TernoaIPFS
client. Here is an example of a code snippet:
Learn more about preparing the Capsule NFT assets.
Step 3: Prepare new capsule metadata with the new encrypted media added to the content array
A Ternoa Capsule NFT comprises additional files: several asset files (such as an image, video, or music file) and a metadata JSON file for the capsule. To ensure maximum security and exclusivity, asset files must be encrypted, and the encrypted files are nested within the metadata file. Both files are stored on IPFS (Interplanetary File Systems) with unique hashes dedicated to each file.
This function retrieves first the current capsule metadata from IPFS using the getFile
method from the TernoaIPFS
client. Next, a new asset is added to the encrypted_media array within the metadata object using object and array spreading. The new metadata is stored as a JSON-formatted Blob object then as a File instance. The updated metadata JSON File is then stored on IPFS using the storeFile
method from the TernoaIPFS
client, which returns a hash value for the new metadata object. This hash is saved for use in the final Step 4. If any errors occur during this process, they are logged to the console using "console.error".
Learn more about preparing the Capsule NFT assets.
Step 4: Update the capsule off-chain data CID hash on-chain
This program uses the setCapsuleOffchaindata
function to update the off-chain data associated with the NFT capsule. This function takes several parameters including the NFT ID, the new off-chain data hash value retrieved in Step 3, the keyring associated with the capsule owner, and a WaitUntil block inclusion option. If any errors occur during this process, they will be logged to the console using "console.error()".
Use your own account by updating the //TernoaTestAccount
with your account seed when retrieving the keyring from the example below.
Support
If you face any trouble, feel free to reach out to our community engineers in our Discord.
Last updated