Using the nit module to create asset commits is free, but there will still be a gas fee associated with the transaction. To ensure that the transaction can be completed, it is important to have Mainnet NUM or Testnet NUM in the wallet being used.
Before starting, make sure nit is installed in your working environment. Here are the basic steps to commit via the nit module.
Prepare nit config
Generate AssetTree
Create commit
Prepare nit config
In order to commit via nit module, you need to complete the following steps:
Follow Infura document and acquire YOUR_INFURA_PROJECT_ID andYOUR_INFURA_PROJECT_SECRET
Create a wallet and set up YOUR_PRIVATE_KEY in order to commit and create transactions. This wallet will appear as the committer in the Commit.
Make sure there are Mainnet NUM in the wallet
[Option] Make sure there are Testnet NUM in the wallet
constconfig= {"defaultNetwork":"jade","provider":"bafkreigrt5tepycewppysdwcjccdkdvvc2ztelqv64idgautq52g3vfh4i","infura": {"projectId":YOUR_INFURA_PROJECT_ID,"projectSecret":YOUR_INFURA_PROJECT_SECRET },"network": {"jade": {"url":"https://mainnetrpc.num.network","chainId":10507,"accounts": [YOUR_PRIVATE_KEY ],"contract":"0x7EC2F14ABE8b0Ea2F657bBb62b6fEfDe161c9001","explorerBaseUrl":"https://mainnet.num.network/tx" },"snow": {"url":"https://testnetrpc.num.network","chainId":10508,"accounts": [YOUR_PRIVATE_KEY ],"contract":"0x02eFA51b583d03342687b585417e5A62cd8273a4","explorerBaseUrl":"https://testnet.num.network/tx" }, }};//Set defaultNetwork to snow if testnet is enabledif(testnet ===true){config.defaultNetwork ="snow"}
Create Commit
The following example is an example to create a commit for the asset on Numbers Mainnet using nit library. Before you start, follow the AssetTree spec to prepare the information for your asset and put them in the inputData.
import ipfs from"@numbersprotocol/nit/lib/ipfs.js";import nit from"@numbersprotocol/nit/lib/nit.js";asyncfunctionmain(inputData, config) {constblockchainInfo=awaitnit.loadBlockchain(config);console.log(blockchainInfo)//create the basic assetTreeawaitipfs.initInfura(config.infura.projectId,config.infura.projectSecret);let assetTree =awaitnit.createAssetTreeInitialRegister(inputData.assetCid,inputData.assetSha256,inputData.encodingFormat,inputData.assetTimestampCreated,inputData.assetCreator, );//Set asset abstractif (inputData.abstract) {assetTree.abstract =inputData.abstract; }//Set asset headlineif (inputData.headline) {assetTree.headline =inputData.headline; }//check license and set it as null if no license is assignedif (inputData.licenseName) {assetTree.license.name =inputData.licenseName; } else {assetTree.license.name =null; }if (inputData.licenseDocument) {assetTree.license.document =inputData.licenseDocument; } else {assetTree.license.document =null; }//if there are custom fields, add to assetTreeif (inputData.custom !==null) { assetTree =awaitnit.updateAssetTree(assetTree,JSON.parse(inputData.custom)); }let author;if (inputData.author) { author =steps.trigger.event.body.author; } else { author =blockchainInfo.signer.address; }//Initialize nitconstcommit=awaitnit.createCommitInitialRegister(blockchainInfo.signer, assetTree, author,config.provider );constcommitResult=awaitnit.commit(inputData.assetCid,JSON.stringify(commit), blockchainInfo);return({"txHash":commitResult.hash,"assetCid":assetTree.assetCid,"assetTreeCid":commit.assetTreeCid,"explorer":config.network[config.defaultNetwork].explorerBaseUrl })}
Please replace the "YOUR_PRIVATE_KEY" with your own private key in the config. The following is the package.json used by this example:
If your asset has been minted as NFTs and you wish to include NFT records in the AssetTree, please follow the specifications outlined in the nftRecord documentation to prepare the nftRecord file. Then, add the following code to your AssetTree preparation process.