Commit via Nit module

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.

  1. Prepare nit config

  2. Generate AssetTree

  3. 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 and YOUR_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

const config = {
    "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 enabled
if(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";

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:

{
    "dependencies": {
        "@numbersprotocol/nit": "^1.2.6"
    }
}

Commit with NFT Records

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.

//Update NFT related data
if (inputData.nftChainID !== null) {
    const nftRecord = {
        "network": inputData.nftChainID,
        "contractAddress": inputData.nftContractAddress,
        "tokenId": inputData.nftTokenID,
    };
    const nftRecordBytes = Buffer.from(JSON.stringify([nftRecord], null, 2));
    assetTree.nftRecord = await ipfs.infuraIpfsAddBytes(nftRecordBytes);
}

Last updated