Resources

People often ask me "How did you learn how to hack?" The answer: by reading. This page is a collection of the blog posts and other articles that I have accumulated over the years of my journey. Enjoy!

Commit-Reveal scheme in Solidity- 1232

Srinivas Joshi - CoinmonksPosted 2 Years Ago
  • A commit-reveal scheme is a mechanism to have a secret value on chain without actually disclosing it until it's necessary. This is useful since everything on the blockchain is public.
  • The commit is a user setting a hash with unique values to them. This hash is generated from a secret value offline, which will be provided later.
  • The reveal is a user specifying their secret value. Of course, we check that the offline hash matches the contract generated hash to ensure that the value is the proper one. At this point, a decision can be made on whatever values we are operating on.
  • Why is this useful? Frontrunning can be used if a user submits a transaction where they will make a bunch of money from a secret. Additionally, secrets on the blockchain are public.
  • To do this correctly, there is a commit window and a reveal window. These should never overlap; otherwise, frontrunning will be possible. Additionally, the hash should not just be based upon the value but the address making the call; this is to prevent simple replay attacks.
  • The rest of this article is a code implementation of how this works. Overall, interesting scheme to work around a blockchain limitation.