Let’s say you generate an image using Stability AI. Without adding a proper license to your image, others could use it freely. In this tutorial, you will learn how to add a license to your Stability AI-Generated image so that if others want to use it, they must properly license it from you.
There are a few steps you have to complete before you can start the tutorial.
You will need to install Node.js and npm. If you’ve coded before, you likely have these.
Add your Story Network Testnet wallet’s private key to .env file:
.env
WALLET_PRIVATE_KEY=
Go to the Pinata dashboard and create a new API key and a gateway. Add the JWT along with the gateway to your .env file:
.env
PINATA_JWT=PINATA_GATEWAY=
Go to Stability and create a new API key. Add the new key to your .env file:
Stability Credits
In order to generate an image, you’ll need Stability credits. If you just created an account, you will probably have a free trial that will give you a few credits to start with.
.env
STABILITY_API_KEY=
Add your preferred RPC URL to your .env file. You can just use the public default one we provide:
You can follow the Stability API Reference to use the model of your choice. For this tutorial, we’ll be using Stability’s Stable Image Core generate endpoint to generate an image. The below is taken directly from their documentation.
Create a main.ts file and add the following code:
main.ts
import fs from "fs";import axios from "axios";import FormData from "form-data";async function main() { const payload = { prompt: "Lighthouse on a cliff overlooking the ocean", output_format: "png", }; const response = await axios.postForm( `https://api.stability.ai/v2beta/stable-image/generate/core`, axios.toFormData(payload, new FormData()), { validateStatus: undefined, responseType: "arraybuffer", headers: { Authorization: `Bearer ${process.env.STABILITY_API_KEY}`, Accept: "image/*", }, } );}main();
Stability generates images that are heavy in size, and therefore expensive to store. Optionally, we can condense the produced image for faster loading speeds and less expensive storage costs.
main.ts
import fs from "fs";import axios from "axios";import FormData from "form-data";async function main() { // previous code here ... const condensedImgBuffer = await sharp(response.data) .png({ quality: 10 }) // Adjust the quality value as needed (between 0 and 100) .toBuffer();}main();
Now that we have our image, we need to store it on IPFS so we can get a URL back to access it. In this tutorial we’ll be using Pinata, a decentralized storage solution that makes storing images easy.
In a separate file uploadToIpfs.ts, create a function uploadBlobToIPFS that uploads our buffer to IPFS:
uploadToIpfs.ts
import { PinataSDK } from "pinata-web3";const pinata = new PinataSDK({ pinataJwt: process.env.PINATA_JWT, // you can put your pinata gateway here, or leave it empty pinataGateway: process.env.PINATA_GATEWAY,});// upload our image to ipfs by putting it in a public groupexport async function uploadBlobToIPFS( blob: Blob, fileName: string): Promise<string> { const file = new File([blob], fileName, { type: "image/png" }); const { IpfsHash } = await pinata.upload.file(file); return IpfsHash;}
Back in the main file, call the uploadBlobToIPFS function to store our image:
main.ts
import fs from "fs";import axios from "axios";import FormData from "form-data";import { uploadBlobToIPFS } from "./uploadToIpfs.ts";async function main() { // previous code here ... // convert the buffer to a blob const blob = new Blob([condensedImgBuffer], { type: "image/png" }); // store the blob on ipfs const imageCid = await uploadBlobToIPFS(blob, "lighthouse.png");}main();
Now that we have generated and stored our image, we can register the image as IP on Story. First, let’s set up our config. In a utils.ts file, add the following code:
When registering your image on Story, you can attach License Terms to the IP. These are real, legally binding terms enforced on-chain by the Licensing Module, disputable by the Dispute Module, and in the worst case, able to be enforced off-chain in court through traditional means.
Let’s say we want to monetize our image such that every time someone wants to use it (on merch, advertisement, or whatever) they have to pay an initial minting fee of 10 $WIP. Additionally, every time they earn revenue on derivative work, they owe 5% revenue back as royalty.
main.ts
import fs from "fs";import axios from "axios";import FormData from "form-data";import { uploadBlobToIPFS, uploadJSONToIPFS } from "./uploadToIpfs.ts";import { client, account } from "./utils";import { createHash } from "crypto";import { LicenseTerms, WIP_TOKEN_ADDRESS } from "@story-protocol/core-sdk";import { zeroAddress, parseEther } from "viem";async function main() { // previous code here ... const commercialRemixTerms: LicenseTerms = { transferable: true, royaltyPolicy: "0xBe54FB168b3c982b7AaE60dB6CF75Bd8447b390E", // RoyaltyPolicyLAP address from https://docs.story.foundation/developers/deployed-smart-contracts defaultMintingFee: parseEther("1"), // 1 $WIP expiration: BigInt(0), commercialUse: true, commercialAttribution: true, // must give us attribution commercializerChecker: zeroAddress, commercializerCheckerData: zeroAddress, commercialRevShare: 5, // can claim 50% of derivative revenue commercialRevCeiling: BigInt(0), derivativesAllowed: true, derivativesAttribution: true, derivativesApproval: false, derivativesReciprocal: true, derivativeRevCeiling: BigInt(0), currency: WIP_TOKEN_ADDRESS, uri: "", };}main();
Next we will mint an NFT, register it as an IP Asset, set License Terms on the IP, and then set both NFT & IP metadata.
Luckily, we can use the mintAndRegisterIp function to mint an NFT and register it as an IP Asset in the same transaction.
This function needs an SPG NFT Contract to mint from. For simplicity, you can use a public collection we have created for you on Aeneid testnet: 0xc32A8a0FF3beDDDa58393d022aF433e78739FAbc.
Using the public collection we provide for you is fine, but when you do this for real, you should make your own NFT Collection for your IPs. You can do this in 2 ways:
Deploy a contract that implements the ISPGNFT interface, or use the SDK’s createNFTCollection function (shown below) to do it for you. This will give you your own SPG NFT Collection that only you can mint from.
Create a custom ERC-721 NFT collection on your own and use the register function - providing an nftContract and tokenId - instead of using the mintAndRegisterIp function. See a working code example here. This is helpful if you already have a custom NFT contract that has your own custom logic, or if your IPs themselves are NFTs.