This section follows the first step to interact with the TEE, to store and secure private content on the Ternoa chain. We recommend reading thecluster section first, to familiarize yourself with the process of encrypting your content with a public key.
How to generate keys and encrypt content on IPFS
Generate some PGP keys
Here you can use the Ternoa helper
import { generatePGPKeys } from"ternoa-js";constgetPGPKeys=async () => {try {const { privateKey,publicKey } =awaitgeneratePGPKeys();console.log(privateKey, publicKey); // Output expected is two strings under the following fomat:// -----BEGIN PGP PRIVATE KEY BLOCK-----// xVgEZZgHdBYJKwYBBAHaRw8BAQdAeOzyPsxdJ9/s1FiYbE7pziJrING8EGhN// ...// U5n1IAjQkCvJsdrJBNYynTnMTBmrd079dMufBw===afDl// -----END PGP PRIVATE KEY BLOCK-----// -----BEGIN PGP PUBLIC KEY BLOCK-----// xjMEZZgHdBYJKwYBBAHaRw8BAQdAeOzyPsxdJ9/s1FiYbE7pziJrING8EGhN// ...// PjQP/lIzerB6OgD/SvxPeVOZ9SAI0JArybHayQTWMp05zEwZq3dO/XTLnwc==Ap5s// -----END PGP PUBLIC KEY BLOCK-----process.exit(0); } catch (error) {process.exit(1); }};
Encrypt content and store it on IPFS
We assume you are familiar with the Ternoa IPFS client. Use the Ternoa IPFS node endpoint, with a key generated from our key generator, or your own storage provider. Read more about the Ternoa IPFS client. Read more about storage options.
The process of encryption requires a few steps: preparing your file (do not forget to import the File from the ternoa-js library), preparing your metadata, and generating your storage solution (here we use the Ternoa IPFS client).
Example using the secretNftEncryptAndUploadFile() helper, to encrypt the content of a Secret NFT.
import { TernoaIPFS, generatePGPKeys, initializeApi, secretNftEncryptAndUploadFile, File,} from"ternoa-js";import fs from"fs";import dotenv from"dotenv";dotenv.config();constencryptAndStoreContent=async () => {try {awaitinitializeApi();// IPFSconstIPFS_URL=process.env.IPFS_NODE_URLasstring; // we recommend you to store the IPFS endpoint in an .env variable.constIPFS_API_KEY=process.env.IPFS_API_KEYasstring; // we recommend you to store the IPFS key in an .env variable.constipfsClient=newTernoaIPFS(newURL(IPFS_URL),IPFS_API_KEY);// GENERATE YOUR KEYS SETconstkeys=awaitgeneratePGPKeys();// PREPARE YOUR SECRET FILE// Do not forget to import File from the ternoa-js libraryconstsecretNFTFile=newFile( [awaitfs.promises.readFile("SECRET_FILE.jpg")],"SECRET_FILE", { type:"image/jpg",//the FILE_TYPE in this exemple SECRET_FILE.jpg is of type "image/jpg", } );// OPTIONAL - PREPARE YOUR SECRET NFT AND/OR MEDIA METADATA// Optional metadata for the secret NFTconstsecretNftMetadata= { title:"(OPTIONAL) Secret NFT Title.", description:"(OPTIONAL) This is my first Secret NFT on Ternoa.", };// Optional metadata for the media of the secret NFTconstsecretMediaMetadata= { name:"(OPTIONAL) SECRET_FILE_NAME", };const { Hash: secretOffchainDataHash } =awaitsecretNftEncryptAndUploadFile( secretNFTFile,keys.publicKey, ipfsClient, secretNftMetadata, secretMediaMetadata );console.log(`SECRET HASH: https://ipfs-dev.trnnfr.com/ipfs/${secretOffchainDataHash}` );// ...process.exit(0); } catch (error) {process.exit(1); }};
Under the hood the secretNftEncryptAndUploadFile() executes the following code. It utilizes another user-friendly atomic helper,encryptFile(), that you can use at your convenience to encrypt the content of your Capsule, for example. You can simply replace the helper by this piece of code: