Let’s Do Some Zeppelin, and Some Ganache and Truffles…

Have you heard about Open Zeppelin? Well, it’s not the latest tribute band which plays classic Led Zeppelin tracks, but an open-source…

Source [here]

Let’s Do Some Zeppelin, and Some Ganache and Truffles…

Have you heard about Open Zeppelin? Well, it’s not the latest tribute band which plays classic Led Zeppelin tracks, but an open-source library that supports a wide range of functions that integrate into smart contracts in Ethereum:

With this, we get a number of solidity code integrations that enhance smart contracts:

The integration is fairly simple, and where we just pick the required solidity file integration (after reviewing it, of course). In the following we integrate with Base64 and Strings:

import "@openzeppelin/contracts/utils/Base64.sol";                             import "@openzeppelin/contracts/utils/Strings.sol";

So, let’s make a smart contract that integrates this code:

There are certain standard hash functions that integrate into Solidity. These include keccak256() and sha256():

function getKeccak256(string memory str) public pure returns(bytes32){
 return keccak256(abi.encodePacked(str));
}
function getSha256(string memory str) public  pure  returns (bytes32) {
 return sha256(abi.encodePacked(str));
}

We can then integrate a Base64 and Hex string conversion from the OpenZepplin code:

function getBase64(string memory str) public pure returns(string memory){
 return Base64.encode(abi.encodePacked(str));
}
function getStringHex(uint256 str) public pure returns(string memory){
 return Strings.toHexString(str);
}
function getString(uint256 str) public pure returns(string memory){
 return Strings.toString(str);
}

We can then create our smart contract in Remix, and compile the contract:

Now we can start our local blockchain with Ganache:

And then deploy our smart contact:

We then see that this has cost one of the accounts some amount of gas:

And then we see we have a contract:

Now we can go ahead and test the contract:

In this case we see that the Base64 string for “hello” is “aGVsbG8=”, and that the Keccak-256 hash for “hello” is “0x1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8”. You can test this [here]:

And [here] for Base64:

Now, we know it is working, we can deploy it to the Ropsten test network. For this we change the deployment to “Injected Web3” and link to the test network and integrate our Metamask wallet:

We need our wallet, as we will need to pay some gas for the smart contract to be deployed. Once deployed, it should not require any gas, as we are not changing the state of the contact. We now deploy to the test network, and wait for it to be mined:

Our contract will then be deployed:

But, before we can get this contact accepted, we need to flatten it with Truffle. I will cover this in another tutorial.

Conclusions

Smart contracts can have many risks, and we need trusted and reviewable code that integrates into them. The usage of Open Zeppelin is a great step forward in creating a more trust worthy digital world.