Skip to content Skip to footer

Workshop Task 4

Task 4: Precompiles

The pre- compiles are inside solidity. 

Solidity is a standardized language you have you know usual calls arithmetics control sentences statements and so on functions  and but then you also have some you know extended functionality for example in ethereum have this Sha 56 hashing which is built in  the solidity  language you have the elective curve  verifications  and so on  What sapphire does is we have some additional  pre- compiles available I mentioned the gas padding  because you want to avoid gas attacks for this matter  then we have the randomized the random key random number generator which generates random numbers or secret keys on chain  this is because we support the confidentiality  and we we can also do some  known symmetric encryption like the x25519 or the Deoxys  envelope  decryption and so on  

Task 4: Precompiles in Sapphire

For a full list visit our docs,  there’s a nice list of precompiles there and how to use it. 

It’s already the the npm package  offers the solidity contract pre- compiles  you import it and then  you can use it in your  Solidity.. okay so let’s see it 

If I go to back end  I do pmpm install minus D oasis protocol slash sapphire contracts okay and then inside the contract I do import protocol s contracts and then for example  I think it’s inside the yeah the sapphire contract sapphire so contract sapphire so okay  so what can we do with it let’s see

 Suppose we want to implement the onchain captcha so there are some spammers and the the

rose token price is so low that it doesn’t prevent spammers from spamming your contract but what you can do is onchain captcha because as I mentioned we have the random onchain randomness support so  this is pretty awesome but let’s simplify captcha so the only thing we want to do is add two numbers together and if they match then you’re allowed to post the message and if they don’t match well you revert the transaction.  For this I we will use  something called the captcha seed and we will combine this captcha seed which is confidential with the number of already  submitted messages so each message you post there will be a new captcha generated for you and you will need to manually compute the result of that captcha and submit it along your message okay let’s see captchaSeed then let’s have a message counter as well and then when you when you deploy the contract  you would want constructor you would want to initialize  this captchaSeed

Sapphire offers this random bytes precompile which basically produces any number of bytes. I will use the 32 bytes here and with no additional entropy provided just an empty string in our case  and then when a user wants to set message he first needs to receive the captcha question or the captcha riddle and then submit the answer  along the set message function. To compute the captcha riddle we will have this function  let’s say computeCaptcha the challenge  okay public View and it will return two numbers  which you will need to add 

The first one is let’s say something like this A and B numbers and then we will have something like hm a big chunk of this which will then hash and obtain the actual numbers ranging from zero to  or something like that so we will have for the entropy we will use our captchaSeed we will use our message counter okay and we will use something more unique because this is the same inside the same call  A is zero B is one and now we get bunch of random bytes here  and we just need to transform it to our unsigned integer inside some range right so something like this.

This computes the the captcha  and now in the set message we want the a correct answer to be provided so we want to have something like and then what’s the code for this this  I would say so you first want to compute captcha use the same logic used for the generation of

Captcha and then  if a + b equals  captcha then this is fine 

If this doesn’t hold then we will revert right with some message — wrong captcha okay  cool so this is the captcha check and then  one more thing we have this message counter here  so we will need to if if we set the message we need to also increase the counter so message counter

Plus+ hope this works 

Let’s try to deploy it. We use the same test net with the same key wow okay it actually works now the problem is that if I try to call the set message with this set message  I have a problem Because I don’t have the hardhat task to provide the captcha yet. Let’s add this captcha support to our set message as well so the only thing here is we have the address the message and then we also have this captcha and we just you know add it as a  parameter set message args.captcha message captcha this is fine but  we also need a task for obtaining the captcha. The captcha task will connect to our contract and it will call this compute captcha and print it on screen  so if I do not, the set message but captcha task. Okay so the captcha question is a + b  this is  so ifI call set message and if I type in it should work

It seems it works but let’s check

If the message was really stored  so just call message  the message is confirmation Hello World yay it works! 

This is a very nice example of how you can use the onchain random number generator for solving the captcha problem on chain without using any intermediaries or Bridges or web two servers whatsoever.  This concludes  our hands-on workshop. I would just give take five more minutes  and maybe if there are any questions otherwise I would like to give you some more ideas where to look  afterwards.