Read commits via nit module

Using nit module to access commits is free. Before starting, make sure nit is installed in your working environment. The process to read asset commits using nit module is simple:

  1. Create nit config

  2. Retrieve blocks

  3. Retrieve commits from each block

Example

The following is an example to read asset commits on a specified blockchain network using nit library.

const ethers = require("ethers");
const nit = require("@numbersprotocol/nit");

// Declare configuration object for connecting to blockchain network.
// This should match the blockchain network you used during the commit process
const config = {
  "defaultNetwork": "jade",
  "network": {
    "jade": {
      "url": "https://mainnetrpc.num.network",
      "chainId": 10507,
      "accounts": [
        "<private-key>"
      ],
      "contract": "0x7EC2F14ABE8b0Ea2F657bBb62b6fEfDe161c9001",
      "explorerBaseUrl": "https://mainnet.num.network/tx"
    }
  }
};

// Declare variable for asset Nid
const assetNid = "bafybeigvqgqzob4754cqqic3wmagjak6kwhcadmhnyhs76ladkqejmc6ie";

// Function to retrieve block numbers for asset NID
async function getBlock(assetNid, blockchainInfo) {
  // Use Nit library to get block numbers
  const blocks = await nit.getCommitBlockNumbers(assetNid, blockchainInfo);
  return { "blocks": blocks };
}

// Function to retrieve commit data for asset NID
async function getCommitsFromNid(assetNid, blockchainInfo) {
  // Get block numbers for asset Nid
  const blocks = await getBlock(assetNid, blockchainInfo);
  let commits = []
  // Loop through block numbers
  for(let i = 0; i < blocks.blocks.length; i++){
    // Create filter to retrieve "Commit" event logs
    let filter = await blockchainInfo.contract.filters.Commit(null, assetNid);
    // Declare ABI for "Commit" event
    const abi = [
      "event Commit(address indexed recorder, string indexed assetNid, string commitData)"
    ];
    // Set filter to retrieve event logs from specific block
    filter.fromBlock = blocks.blocks[i];
    filter.toBlock = blocks.blocks[i];
    // Use provider to get event logs
    const eventLogs = await blockchainInfo.provider.getLogs(filter);
    // Only one event log because restricting block range to a specific block number
    const eventLog = eventLogs[0];

    // Use ethers library to parse event log
    const commitEventInterface = new ethers.utils.Interface(abi);
 
    console.log(`\nBlock number: ${(eventLog.blockNumber)}`);
    console.log(`${blockchainInfo.explorerBaseUrl}/${(eventLog.transactionHash)}`);

    // Parse commit data from event log
    const commitEvent = await commitEventInterface.parseLog(eventLog);
    const commitData = JSON.parse(commitEvent.args[2])
    
    // Add transaction ID to the commitData
    commitData["transaction"] = eventLog.transactionHash;
    commits.push(commitData)
  }
  // Return commit data array
  return commits
}

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": {
        "ethers": "^5.5.4",
        "@numbersprotocol/nit": "^1.2.4"
    }
}

Sample results

[
  {
    "assetTreeCid": "bafkreibyy3oe3fyucn4sxfiphzdga5caznvm3vmxralkyli3mx74wkbsfm",
    "assetTreeSha256": "38c6dc4d971413792b950f3e46607440cb6acdd5978816ac2d1b65ffcb28322b",
    "assetTreeSignature": "0x0254ad5c79450372e65ed6a84c7952349e4b40ffb1026b45e2f0a19047b98a6a51f3209e34c6a5c14eecfe55801bc1de66ffba739145a4e9375581c0d7b6db2c1b",
    "author": "0x8212099e5aF75e555A3E63da77a99CcC9527aCC1",
    "committer": "0x51130dB91B91377A24d6Ebeb2a5fC02748b53ce1",
    "provider": "bafkreido4zu743f6isb5wninfkedvbirj2ngb5fkivrpdijh2xtd3s6rnu",
    "timestampCreated": 1673977414,
    "action": "bafkreiavifzn7ntlb6p2lr55k3oezo6pofwvknecukc5auhng4miowcte4",
    "actionResult": "https://bafkreibyy3oe3fyucn4sxfiphzdga5caznvm3vmxralkyli3mx74wkbsfm.ipfs.dweb.link",
    "transaction": "0xb9fa815047817b2c0fbfb50056914e00ce66feb98eef224142799fae12a2ec0d",
    "abstract": "Action action-initial-registration."
  },
 ...
  {
    "assetTreeCid": "bafkreihlg25bvhx2amwlihj5fqfiuq7o2z2v7an2x6oksqsn66aotcpo5e",
    "assetTreeSha256": "eb36ba1a9efa032cb41d3d2c0a8a43eed6755f81babf9ca9424df780e989eee9",
    "assetTreeSignature": "0x90f90885ec6dacab229fe910204ab91fd5c161b7c3fd99047e288712e184de2607bdbf35c230f85133847d587ccb3178f09ff94d5e427c6396b5ffaf4298f1871b",
    "author": "0x8212099e5aF75e555A3E63da77a99CcC9527aCC1",
    "committer": "0x51130dB91B91377A24d6Ebeb2a5fC02748b53ce1",
    "provider": "bafkreigrt5tepycewppysdwcjccdkdvvc2ztelqv64idgautq52g3vfh4i",
    "timestampCreated": 1674056166,
    "action": "bafkreicptxn6f752c4pvb6gqwro7s7wb336idkzr6wmolkifj3aafhvwii",
    "actionResult": "https://bafkreihlg25bvhx2amwlihj5fqfiuq7o2z2v7an2x6oksqsn66aotcpo5e.ipfs.dweb.link",
    "transaction": "0xfe16ba196fbce5c60be3244b020633c3913d3338ae7b353b58f356f6e87f78a5",
    "abstract": "Action action-initial-registration."
  }
]

Last updated