Create an MD5 Solidity Interface
Learn how to create an MD5 Solidity Interface
The first step is defining the interface that will wrap the precompile implementation and that other contracts and users will interact with. In addition to declaring the way users can interact with our MD5 precompile, defining the MD5 interface will also allow us to utilize a generator script. A precompile consists of many files, and generating boilerplate Go files will make implementing the precompile much easier.
SHA-256 Precompile Interface
Before defining the MD5 interface, we will first look at the interface of the very similar SHA-256 precompile. This reference implementation is included in the repository we have created earlier.
ISHA256 contains a single function hashWithSHA256
. hashWithSHA256
takes in a value of type string, which is the value which is to be hashed, and outputs a 32-byte hash.
Creating the Solidity Interface For The MD5 Precompile
Now it's your turn to define a precompile interface!
Create the interface for the MD5 hash function. Start by going into the same directory where ISHA256.sol
lives (contracts/contracts/interfaces/
) and create a new file named IMD5.sol
. Note that:
→ MD5 returns a 16-byte hash instead of a 32-byte hash
→ Make sure to name all parameters and return values
Generate the ABI
Now that we have an interface of our precompile, let's create an ABI of our Solidity interface. Open the integrated VS Code terminal (control + `), change to the subdirectory /contracts directory.
Run the command to compile to solidity interface to the ABI:
Now rename the file:
Now, you should have a file called IMD5.abi
in the folder /contracts/abis
with the following content: