đ¨ī¸Register Data On-Chain
This tutorial teaches you how to set up a basic React app to connect to the Concordium blockchain for on-chain data registration without smart contracts.
This tutorial will implement a basic React application that can connect to the Concordium blockchain. It will not involve any smart contract development or interaction and can run without a node.
Concordium offers a special transaction type to store data on-chain without the need to code a smart contract. An example of its simplest use case is ensuring the integrity of data. For instance, you can store the hash of data on-chain and verify it later to ensure it hasn't been tampered with.
Step 1: Setting up the React Project
Create a React project: Set up a working directory for your dApp and create an empty React project by running the following command in your terminal:
Your project will be created as follows:
When you run the application, you will see the template application interface as shown below:
Note: You're starting from scratch with an empty React application that has been bootstrapped from a React template and will be using Material-UI.
Installation of Dependencies: Once the project is created, install the necessary dependencies by running the command below:
Note: This command installs all the specified packages. It adds dependencies for some React components from Material-UI and necessary libraries from Concordium Web SDK and Concordium Web Wallet Helper.
Once the command runs successfully, it will create a "package.json" file that includes all dependencies:
Step 2: Creating Header Component
Create Header Component: Create a file named "Header.tsx" containing a button and handle the connect()
function to the web wallet. Then, paste the following code to define the Header component:
Step 3: Creating Register Component
Create Register Component: Create a file named "Register.tsx" in the "src" directory that will take input from the user with a text box, calculate its SHA-256, and store it on-chain with a simple and special type of transaction called "registerData."
To do this, paste the following code to define the Register component:
There are two important parts of the code to be careful about:
The connection with the wallet
The transaction parameters
To ensure these are provided, add a control that checks if the wallet is connected successfully.
Then, you need to pass the data as a parameter to the AccountTransactionType.RegisterData transaction and convert it to SHA256. Keep the returning transaction hash (txnHash) to verify the transactionâs state and its details:
Step 4: Integrating Components in App
Integrate Components in App: Open the App.tsx
file in the src
directory and paste the following code to integrate the Header and Register components:
Step 5: Testing the Application
Testing the Application: Once everything is set up, run the application. You should see the application interface similar to the one below:
Connecting to the Wallet: Click the "Connect" button to connect to the wallet. After connecting, the interface should change to indicate that you are connected, as shown below:
Registering Data: Fill in the input field with desired data and click the "REGISTER DATA" button. The application will print the TxnHash value for tracking on the block explorer. You can click on it to verify, as demonstrated below:
Verification on Block Explorer: Upon clicking the TxnHash, you'll be redirected to the dashboard to view the specific block details, confirming the data registration:
It will redirect you to the dashboard, to that block in particular:
Verification with SHA-256 Calculator: Compare the SHA-256 value of the stored data with an online SHA-256 calculator to ensure data integrity, as illustrated below:
Conclusion: Reflecting on the Implementation
As expected, those two values are matched! Take a step back and consider what you've implemented with this dApp. Essentially, you've stored proof of data on-chain, enabling anyone interested in verifying the integrity of your data to do so by examining this decentralized network. It is now stored on-chain, and you cannot change it. The source code can be found in this GitHub repository.
Last updated