What would be a way to assign a code word inside a contract (such as a promotional / coupon style code word) so that you could let people know "send this message/code word to this contract address and it will issue out a token".

The use case is during certain times of the week (say between 1pm and 2pm on Wednesday) I'd like to issue a code word, which friends would then send this code word to the contract, and it will issue out a token for them.

Double points for help with creating a timing regulation within the contract that's editable at a later date (eg I can set this week's code word to be valid between X and Y time on Wednesday).

Thank you.


This is pretty easy, and I'm going to take a different approach to it because there is a trust aspect here, you are inserting the code word into the contract which means there is a hidden value here that you can reveal later.

So what you do is have accounts send guesses by hashing their account with the code word they are trying to redeem.

At a later point you insert the code word, if their account+codeword hash is valid based on that code word than they get the token. You probably need to use a redemption pattern rather than iterating through guesses to avoid block gas limits. This gives you a lot of flexibility because you won't be able to observe the code word on chain until it is inserted, at which point it will be too late to use.


I can't see how you would do this for more than one occurrence.

The first one could be a long string where you store the hash value inside the contract.

The downside is, once Alice has sent the string and got the token, Bob can observe Alice's transaction and the fact that it issued the token, hence send exactly the same string.

You could, of course, store a number of single use hashed strings in the contract and give each to a different person.


One possibility would be to distribute the code word, then derive from it an ECDSA private key, by hashing the code word. Then have each person sign a message along with a random sing-use salt, and verify the message in the contract using ECRECOVER. This would allow a single code word to be used more than once without revealing any information about the code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.