ERC1155 Support
A quick step-by-step overview of distributing digital content via ERC1155
What is ERC-1155?
ERC-1155 is a token which combines non-fungible and fungible elements. The ERC-721 standard only allows 1 owner/holder of an NFT to exist, however ERC-1155 standards allow a token to have multiple 'holders', being much more efficient when the same piece of digital content is published to multiple 'holders'.
With ERC-1155 when a content creator or publisher wishes to deliver one digital content to 100 people, they would mint ERC-1155 with a token quantity set at 100, only require minting once.
1. Install the VWBL SDK
// Install via yarn
yarn add vwbl-sdk
// Install via npm
npm install vwbl-sdk
2. Import the VWBL SDK
Import the VWBLERC1155 class and type definition.
const {
VWBLERC1155,
ManageKeyType,
UploadContentType,
UploadMetadataType,
} = require("vwbl-sdk");
3. Create Web3 or Ethers instance
The popular libraries that interact with evm smart contract are web3.js and ethers.js. VWBL SDK supports both libraries.
Using web3.js in Browser env:
// ※There are some ways to connect with MetaMask. In this time, use @metamask/detect-provider as one example.
import detectEthereumProvider from "@metamask/detect-provider";
import Web3 from "web3";
const provider = await detectEthereumProvider();
const web3 = new Web3(provider);
const Web3 = require("web3");
const Provider = require("@truffle/hdwallet-provider");
const privateKey = "walletPrivateKeyString"
const blockchainRPCURL = "https://polygon-rpc.com"
const provider = new Provider(privateKey, blockchainRPCURL);
const web3 = new Web3(provider);
Using Ethers.js in Browser/NodeJs env:
// ※Web3 modal enables connect various wallet such as metamask, coinbase wallet.
import Web3Modal from "web3modal";
import { ethers } from "ethers";
const web3Modal = new Web3Modal({ cacheProvider: true });
const provider = await web3Modal.connect();
const ethProvider = new ethers.providers.Web3Provider(provider);
const ethSigner = ethProvider.getSigner();
import { ethers } from "ethers";
const privateKey = "walletPrivateKeyString"
const ethProvider = new ethers.providers.JsonRpcProvider('https://polygon-rpc.com');
const ethSigner = new ethers.Wallet(privateKey, ethProvider);
4. Instantiate the VWBL SDK
VWBL SDK provides VWBLERC1155 class for web3.js / ethers.js
Using web3.js in Browser env:
import {
ManageKeyType,
UploadContentType,
UploadMetadataType,
VWBLERC1155
} from 'vwbl-sdk';
const vwbl = new VWBLERC1155({
web3, // web3 instance
contractAddress: "0x...", // VWBL erc1155's contract address
manageKeyType: ManageKeyType.VWBL_NETWORK_SERVER, // how to manage key
uploadContentType: UploadCotentType.S3, // where to upload content
uploadMetadataType: UploadMetadataType.S3, // where to upload metadata
awsConfig: {
region: "ap-northeast-1",
idPoolId: "ap-northeast-1:...",
cloudFrontUrl: "https://xxx.cloudfront.net",
bucketName: {
metadata: "vwbl-metadata",
content: "vwbl-content",
}
}
});
import {
ManageKeyType,
UploadContentType,
UploadMetadataType,
VWBLERC1155
} from 'vwbl-sdk';
const vwbl = new VWBLERC1155({
web3, // web3 instance
contractAddress: "0x...", // VWBL erc1155's contract address
manageKeyType: ManageKeyType.VWBL_NETWORK_SERVER, // how to manage key
uploadContentType: UploadContentType.IPFS, // where to upload content
uploadMetadataType: UploadMetadataType.IPFS, // where to upload metadata
ipfsConfig: {
apiKey: "yourPinataApiKey", // pinata API Key
apiSecret: "yourPinataSecretApiKey", // pinata SecretAPI Key
}
});
Using ethers.js in Broswer/NodeJs env:
import {
ManageKeyType,
UploadContentType,
UploadMetadataType,
VWBLERC1155
} from 'vwbl-sdk';
const vwbl = new VWBLERC1155({
ethersProvider: ethProvider, // ethers.js provider instance
ethersSigner: ethSigner, // ethers.js signer instance
contractAddress: "0x...", // VWBL erc1155's contract address
manageKeyType: ManageKeyType.VWBL_NETWORK_SERVER, // how to manage key
uploadContentType: UploadCotentType.S3, // where to upload content
uploadMetadataType: UploadMetadataType.S3, // where to upload metadata
awsConfig: {
region: "ap-northeast-1",
idPoolId: "ap-northeast-1:...",
cloudFrontUrl: "https://xxx.cloudfront.net",
bucketName: {
metadata: "vwbl-metadata",
content: "vwbl-content",
}
}
});
import {
ManageKeyType,
UploadContentType,
UploadMetadataType,
VWBLERC1155
} from 'vwbl-sdk';
const vwbl = new VWBLERC1155({
ethersProvider: ethProvider, // ethers.js provider instance
ethersSigner: ethSigner, // ethers.js signer instance
contractAddress: "0x...", // VWBL erc1155's contract address
manageKeyType: ManageKeyType.VWBL_NETWORK_SERVER, // how to manage key
uploadContentType: UploadCotentType.IPFS, // where to upload content
uploadMetadataType: UploadMetadataType.IPFS, // where to upload metadata
ipfsConfig: {
apiKey: "yourPinataApiKey", // pinata API Key
apiSecret: "yourPinataSecretApiKey", // pinata SecretAPI Key
}
});
5. Encryption and Distributing digital contents
When minting a VWBL ERC1155, managedCreateToken
will need to be executed. When this method is executed, the following happens: An ERC1155 is minted, key pairs are generated, encryption and uploading of the encrypted data, uploading metadata, and the sending the key to the VWBL Network. VWBL support type of preview image is File
and digital content is File
/ File[]
.
Also, it is necessary to set the decryption key to the VWBL Network in minting a VWBL ERC1155. Since the signature is required when communicating with the VWBL Network, be sure to execute the sign function before executing the managedCreateToken
function.
// make sure to sign before creating tokens
await vwbl.sign()
// create token, which metadata is stored on AWS.
const tokenId = await vwbl.managedCreateToken(
"vwblERC1155", // The ERC1155 name
"ERC1155 owner can view content", // The ERC1155 description
amount, // The amount of ERC1155 tokens to be minted
file, // The data that only ERC1155 owner can view
thumbnail, // The ERC1155 preview image
royaltiesPercentage, // The percentage of ERC1155 royalty percentage
);
// make sure to sign before creating tokens
await vwbl.sign()
// create token, which metadata is stored on IPFS.
const tokenId = await vwbl.managedCreateTokenForIPFS(
"vwblERC1155", // The ERC1155 name
"ERC1155 owner can view content", // The ERC1155 description
amount, // The amount of ERC1155 tokens to be minted
file, // The data that only ERC1155 owner can view
thumbnail, // The ERC1155 preview image
royaltiesPercentage, // The percentage of ERC1155 royalty percentage
);
6. Decryption encrypted content
This method can executed only by the ERC1155 owner or minter. When this method is executed, the following happens: Downloading of the metadata & encrypted content, retrieval of the key, and decryption of encrypted content.
Also, it is necessary to get the decryption key from the VWBL Network in extracting VWBL ERC1155's metadata. Since the signature is required when communicating with the VWBL Network, be sure to execute the sign
function before executing the extractMetadata
function.
// make sure to sign before creating tokens
await vwbl.sign();
// This method can execute by only ERC1155 owner
const {
id, // tokenId
name, // metadata name
description, // metadata description
image, // metadata image
mimeType, // metadata mime type
fileName, // file name
ownData, // decrypted content
} = await vwbl.extractMetadata(tokenId);
If you are using VWBL SDK (v0.1.6 or later), it is possible to extract token's metadata from multiple contracts by setting data-collector address to constructor props and adding specific contract address in params of extractMetadata
.
const vwbl = new VWBLERC1155({
web3, // web3 instance
contractAddress: "0x...", // VWBL ERC1155's contract address
manageKeyType: ManageKeyType.VWBL_NETWORK_SERVER, // how to manage key
uploadContentType: UploadContentType.IPFS, // where to upload content
uploadMetadataType: UploadMetadataType.IPFS, // where to upload metadata
ipfsConfig: {
apiKey: "yourPinataApiKey", // pinata API Key
apiKey: "yourPinataApiKey", // pinata SecretAPI Key
},
dataCollectorAddress: "0x...", // check "Endpoint for VWBL"
});
// make sure to sign before creating tokens
await vwbl.sign();
// This method can execute by only ERC1155 owner
const {
id, // tokenId
name, // metadata name
description, // metadata description
image, // metadata image
mimeType, // metadata mime type
fileName, // file name
ownData, // decrypted content
} = await vwbl.extractMetadata(tokenId, contractAddress);