1

I'd like to generate an NFT within a contract and give it a randomly occurring attribute.

As I search, it seems that the only fully-secure way to do this is to use an Oracle or build some sort of system where a number is contributed via a third party.

But as this is not a financial use and mostly for fun, I'm ok if the baker goes out of their way to try and steal a fancy NFT (this also seems extremely unlikely anyway). So because I am willing to give up some security, I'm wondering about what my options are for generating a random-ish number, or at least a "hard" to predict random number.

It seems that my options are something like: Create a hash based on some value that is not super easy to control, such as: hash(time.now() + sender address + block hash)

time.now() and the sender address is possible to get, but that would mean someone could generate a ton of addresses and has time into the future to predict success - I'd need to introduce some additional value.

I like including the sender address in the hash as it would make it more apparent that the caller may be cheating if the call comes from a brand new address and not one that is already established.

So my specific questions: Is there a way to get the next block hash within a smart contract? Or some other way to get a value that isn't known at the time the contract is called?

If I can get the next block hash and use that value, can the contract called view the value and cancel the call before it is committed to the block or would the call complete?

Thanks!

1 Answer 1

3

So my specific questions: Is there a way to get the next block hash within a smart contract?

No, there is no instruction in Michelson to query a block hash.

Or some other way to get a value that isn't known at the time the contract is called?

You could use the CREATE_CONTRACT instruction that is usually meant to originate a new contract, it returns the address at which the contract should be originated and an origination operation. If you drop the operation it should not cost you so much gas. The address you get is reasonably hard to predict in advance or to manipulate.

Create a hash based on some value that is not super easy to control, such as: hash(time.now() + sender address + block hash)

Putting several data sources makes your hash harder to predict but easier to manipulate.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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