> For the complete documentation index, see [llms.txt](https://docs.teranode.group/nchain-event/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.teranode.group/nchain-event/linked-records/tutorials/collection-integrity.md).

# Collection Integrity

<figure><img src="/files/e0hzPavOkrggCVMlpVA8" alt="" width="563"><figcaption></figcaption></figure>

A business wants to be assured about the data integrity (accuracy and authenticity) of a collection of items for a product or service that may be used anywhere, such as a motor vehicle. To achieve that, the business wants to easily verify the collection of items.

This is part of Data Integrity for a collection. It is an extension of the Data Integrity use case described [here](/nchain-event/independent-records/tutorials/data-integrity.md). The solution includes these steps for each item:

* Next item registration – items are kept hidden but registered and linked on the blockchain
* Nth item verification – items and links are compared with the blockchain to determine validity

Please be aware that verification always requires the original item.

We will use this class wrapper around the Linked Record REST API (described [here](/nchain-event/linked-records/api-workflow.md)):

```typescript
Class Event(aUrl, aApiKey) {

  upsert(record, salt, location) // register a fingerprint of record on the blockchain
    if location = 0
      location = create(record, salt) // create link location
    else
      location = update(record, salt, location) // update link location
    return location

  verify(record, time, previousLoc, location, nextLoc) // return match/mismatch/error

}
```

## **Next Item Registration**

Whenever an item is added to the collection in the application database, this procedure is performed to register a fingerprint of the item on the blockchain, and record all collection data:

<pre class="language-typescript"><code class="lang-typescript">aRecord = "details of the next item of the collection to register"

aSalt = time.now()

previousLocation = either 0 or collection[count-1].location

nextLocation = event.upsert(aRecord, aSalt, previousLocation) // register fingerprint

// persist aSalt, nextLocation in collection[count]

<strong>// increment and persist count
</strong></code></pre>

## **Nth Item Verification**

At any arbitrary time later, the business can verify the Nth item (from 0 to count-1) with this procedure:

```typescript
  nthRecord = "get the Nth item of the collection to verify"

  nthSalt = collection[Nth].salt

  previousLoc = either 0 or collection[Nth - 1].location
  
  nthLocation = collection[Nth].location

  nextLoc = either 0 or collection[Nth + 1].location
  
  result = event.verify(nthRecord, nthSalt, previousLoc, nthLocation, nextLoc)

  if result = match // nthRecord and links are verified as valid
  if result = mismatch // nthRecord or links are verified as invalid
  if result = error // try again later
```

"nthRecord" must contain identical fields to those that were originally registered to the blockchain.

Handling the different possible responses is described in detail in the Data Integrity use case [here](/nchain-event/independent-records/tutorials/data-integrity.md).&#x20;

The actual syntax is shown in [Verify Link Record](/nchain-event/linked-records/api-workflow/verify-link-record-+.md) below.

The business can employ iteration to verify some or all of the collection of items.&#x20;

This example ignores issues like multiple collections (requires collections\[x]\[y]) and updating individual items.

### **Benefits**

* Provides high confidence of the validity of a verified collection for the business.
* Provides high confidence to auditors/users who verify the original collection of its validity.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.teranode.group/nchain-event/linked-records/tutorials/collection-integrity.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
