Implement the Precompile
Learn how to implement the Precompile.
Now, we'll implement the logic of the precompile in Go. We'll hash our string using the MD5 algorithm.
SHA-256 Precompile Implementation
Before defining the logic of our MD5 precompile, let's look at the logic of the function hashWithSHA256
(located in sha256/contract.go
), which computes the SHA-256 hash of a string:
As you can see, we're performing the following steps:
- Line 1: Importing the sha256 function from the crypto library (at the top of the Go file)
- Line 15: Unpacking the input to the variable inputStruct. It doesn't make sense that the variable has Struct in its name, but you will see why it is done like this when we have multiple inputs in a later example
- Line 24: Calling the sha256 function and assign its result to the output variable
- Line 26: Packing the output into a byte array
- Line 32: Returning the packed output, the remaining gas and nil, since no error has occurred
Implementing the MD5 Precompile in contract.go
Go ahead and implement the md5/contract.go
for the MD5 precompile. You should only have to write a few lines of code. If you're unsure which function to use, the following documentation might help: Go Documentation Crypto/md5