Registering PPE documents in Blockchain

Marcus Poli
Aug 11, 2018 · 4 min read

There are documents that need be authenticate to be accept. Authentications need signatures to confirm that they were made on the date indicated, and some documents have other people who serve as witnesses to avoid doubts about veracity. However, there are some documents that do not receive treatment to avoid bureaucratizing daily life.
A good example is the delivery of PPE (personal protective equipment).
Sometimes they are delivered to employees and are not used and when an accident occurs lawyers accused companies of not providing them to employees. Okay, there are signed documents that certify the delivery of PPE and training certificates for use. But, who ensures that they was not signed to decommit the company’s liability after the accident? Witnesses may be influenced to lie and usually are people working for the same company.

Blockchain has a characteristic of being immutable and being processed by anyone in the world, is independent of financial interests and totally safe against fraud. Besides that, has a date that will be effective in the first validation of the miner. In other words, this date was not generated by company machines, so it may be useful to authenticate when the event was performed.

Encryption can also help us in this exercise because has the SHA-256 algorithm that generate a unique hash that identify if the document has not changed after being sent to Blockchain.
In order to maintain better control, we can create a field that will store the user who has certified this document on the network.

Ok, let’s hands dirty.
Our Solidity code would look like this:

pragma solidity ^0.4.24;
contract PPE {
struct tipo {
uint date;
string owner;
}
mapping(bytes32 => tipo) private store;
constructor() public{
}
function register(bytes32 p_sha2, string p_owner) public check(p_sha2) {
store[p_sha2].date = now;
store[p_sha2].owner = p_owner;
}
function query(bytes32 p_sha2) public constant returns (uint,string) {
return (store[p_sha2].date, store[p_sha2].owner);
}
function exists(bytes32 p_sha2) public constant returns (uint){
return (store[p_sha2].date);
}
modifier check(bytes32 p_sha2){
require (exists(p_sha2) == 0,”Operation not allowed. Document registered in other date”);
_;
}
}

Our application use HTML + JavaScript + Metamask plugin.

Source code here

If you don’t worry about the financial costs, apply it to Ethereum network and it’ll works. But, if you want to know how much you can save compressing, continue reading.

Let’s fix the same Solidity code applying some minor changes, see:

pragma solidity ^0.4.24;
contract PPEII {
struct tipo {
uint date;
bytes32 owner;
}
mapping(bytes32 => tipo) private store;
constructor() public{
}
function register(bytes32 p_sha2, bytes32 p_owner) chk_exists(p_sha2) public {
store[p_sha2].date = now;
store[p_sha2].owner = p_owner;
}
function query(bytes32 p_sha2) public constant returns (uint date,bytes32 owner) {
return (store[p_sha2].date, store[p_sha2].owner);
}
function exists(bytes32 p_sha2) public constant returns (uint date){
return (store[p_sha2].date);
}
modifier chk_exists(bytes32 p_sha2){
require(exists(p_sha2) == 0,”Operation not allowed. Document registered in other date”);
_;
}
}

If you compare the old code and new, we were only replace String to bytes32.

Now, let’s change HTML.

Source code here

Here we add library polidatacompressor.js

<script src=”http://polidatacompressor.com/PoliDataCompressor.js"></script>

A simple function to convert String to Bytes32

function str_to_bytes32hash(p_str1) {

var tmp = “”;

var resp = “”;

tmp = web3.fromAscii(p_str1);

for (var x = 0; x < 66; x++) {

if (tmp.length <= x) {

resp = resp + “0”;

}

else {

resp = resp + tmp.substr(x, 1);

}

}

return resp;

}

We pass second parameter compressed (Tcomp is a simple function responsible for compress String limited by list).

var myByteName = '';

myByteName = str_to_bytes32hash(bytearray_to_string(Tcomp($(".myName").val(), list)));

Finally, we receive second parameter decompressing (Tdecomp).

Tdecomp(string_to_bytearray(remove_chrzero(web3.toAscii(res[1]))), list)

Let’s see how much we save.
Publishing contracts:

Original link X Compressing link

Inserting records:

Link1 Link2 Link3 Link4 Link5 Link6

The economy varies depending on the size of the compressed field value. If the String is smaller, the tendency is to have a lower GAS reduction.

This exercise have only one field to compress, if we have a form with more fields, we would probably have a much larger GAS economy.

Thank you for reading.

Marcus Poli

Written by

I’m Brazilian. I’ve interest in technologies matters. I wrote one fiction book that talk about drones - KDP 2017. Currently I’ve developing PoliDataCompressor.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade