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
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";
async function main(inputData, config) {
const blockchainInfo = await nit.loadBlockchain(config);
console.log(blockchainInfo)
//create the basic assetTree
await ipfs.initInfura(config.infura.projectId, config.infura.projectSecret);
let assetTree = await nit.createAssetTreeInitialRegister(
inputData.assetCid,
inputData.assetSha256,
inputData.encodingFormat,
inputData.assetTimestampCreated,
inputData.assetCreator,
);
//Set asset abstract
if (inputData.abstract) {
assetTree.abstract = inputData.abstract;
}
//Set asset headline
if (inputData.headline) {
assetTree.headline = inputData.headline;
}
//check license and set it as null if no license is assigned
if (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 assetTree
if (inputData.custom !== null) {
assetTree = await nit.updateAssetTree(assetTree, JSON.parse(inputData.custom));
}
let author;
if (inputData.author) {
author = steps.trigger.event.body.author;
} else {
author = blockchainInfo.signer.address;
}
//Initialize nit
const commit = await nit.createCommitInitialRegister(
blockchainInfo.signer,
assetTree,
author,
config.provider
);
const commitResult = await nit.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.