Substrate based 2D barcode scanner Pallet in Rust

Table of contents
Reading Time: 2 minutes

As we know, Rust is one of the best programming languages for blockchain due to its highly-capable mechanism of handling mutable states. The fast, memory safe, and exclusively concurrent nature of this blockchain coding language makes it most suitable for developing real-world blockchains.

I was looking for a Rust based blockchain framework and I found Substrate, which is developed by Parity technology and built in Rust. It provides a template of code on which you can build your own blockchain.

Substrate comes with everything you need to build your blockchain. Use Substrate’s pallets to easily create what you want, or craft your own custom logic. Either way, Substrate makes building a blockchain far faster, easier, and safer than ever before.

For more details, you can go to https://substrate.dev/. In this blog, I would explain a 2D barcode scanner substrate pallet. This pallet is based on a template that is included with the Substrate node template. You can refer to these steps, if you want to use for your runtime module.

Every popular brand has fake manufacturers selling a counterfeited item at cheaper rates. Even the company experts may not be able to distinguish between fake ones and real ones. What if the original manufacturer has embedded a 2D barcode on the product which is tied to a blockchain system.
You can scan the 2D barcode using your smartphone, and your smartphone will tell you whether the product is fake or not.

This blog explains the first step of creating A Fake Product Identification System. We would cover only:

1) Add a product in chain and associate with a barcode
2) Verify a barcode

You can find the source code here substrate-barcode-scanner-pallet.

1) In Cargo.toml, you will find all dependencies.

2) We have defined all main components in lib.rs.

3) We have added a ManufactureOrigin that can add product-barcode information in the chain.

type ManufactureOrigin: EnsureOrigin<Self::Origin, Success = Self::AccountId>;

4) In decl_storage!, we have defined a storage map. We will be using barcode as key and product information as its associated value.

trait Store for Module<T: Trait> as SubstrateBarcodeScanner {
        ProductInformation get(fn product_information): map hasher(blake2_128_concat) T::Hash =>
        Product<T::AccountId, T::Hash>;
    }

5) Declared a runtime event: ProductInformationStored

6) Defined a custom DispatchErrors BarcodeAlreadyExists by using the decl_error! macro.

7) In decl_module!, we have added logic to add product information and verify barcode, if someone wants to check whether a product is fake or not.

As I mentioned above, this is the first step of A Fake Product Identification System. We will extend it with other features like barcode scanning, expiry checking etc.

Thanks for reading the blog!!!

If you want to read more content like this?  Subscribe Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe Rust Times Newsletter: https://bit.ly/2Vdlld7

Video: Learn more and enhance your knowledge and skills of Rust Language by subscribing Rust Times newsletter and receive updates bi-weekly. https://bit.ly/2Vdlld7

Template: For more such template updates, subscribe Rust Times Newsletter: https://bit.ly/2Vdlld7

Written by 

Ayush is the Sr. Lead Consultant @ Knoldus Software LLP. In his 10 years of experience he has become a developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality.