Replacing the solution hash with an access key

Replacing our solution hash

So far in this tutorial, the user sends the plaintext solution to the crossword puzzle smart contract, where it’s hashed and compared with the known answer.

This works, but we might want to be more careful and avoid sending the plaintext solution.

Why?

Blockchains rely on many computers processing transactions. When you send a transaction to the blockchain, it doesn’t immediately get processed. In some Layer 1 blockchains it can take minutes or longer. On NEAR transactions settle within a couple seconds, but nonetheless there’s a small period of waiting time.

When we previously sent the crossword puzzle solution in plain text (via the parameter solution to submit_solution) it means it’s visible to everyone before it gets processed.

At the time of this writing, there haven’t been outstanding incidents of validators “front-running” transactions, but it’s something to be aware of. Front-running is when a validator sees a transaction that might be profitable and does it themselves.

There have been several incidents of this and it continues to be an issue.

Tweet talking about a puzzle where tens of thousands of dollars were taken because of a frontrun attack
Real-life example of a puzzle being front-run.
Read Anish Agnihotri’s thread

How?

We’re doing to do something unique — and frankly unusual — with our crossword puzzle. We’re going to use function-call access keys in a new way.

Our crossword puzzle smart contract will add a function-call access key to itself. The private key is derived from the solution, used as a seed phrase.

So when we add a new puzzle, we’ll use the AddKey Action to add a limited, function-call access key can that only call the submit_solution method.

The first user to solve the puzzle will essentially “discover” the private key and call that method. Think of it like a safe that contains a function-call access key.

A small safe with a padlock containing words to a seed phrase, and you can see through the safe, showing it holds a function-call access key. Art created by soulless.near.
Open the safe using answers to the puzzle, revealing the function-call access key.
Art by soulless.near
Generate comment with AI 2 nL
Scroll to Top