#Conversion/Comparison between hash and BytesN<32>

9 messages · Page 1 of 1 (latest)

smoky ether
#

For a proof of concept I'm toying with, I want to compare a pre-defined hash (for example 01b4b211108994a6cd3eb2530b294b237303005453c0f4a4233923147a6ecb97 ) against a BytesN<32> value.

The rough idea is to have a contract with an upgrade function. The function won't require auth, but instead checks if the supplied hash argument matches (one of) the pre-defined values. Think treasure-hunt/puzzle style.

Now, in soroban-cli you can use the hash as shown above, and it will be converted to BytesN<32> in the process of invoking a contract.
But I can't work out how to do this myself, in order to end up with 2 BytesN<32> values to compare.

Can anyone give me some pointers?

scarlet condor
smoky ether
#

Awesome, thanks. I'll tinker around with that

twin aurora
#

Think treasure-hunt/puzzle style.
FWIW any hardcoded value is trivial to extract, so I hope that's the intent of the puzzle 🙂

as for your question, why not use BytesN::from_array?

smoky ether
smoky ether
#

With BytesN::from_array I can't get it to take my hash as 32 bytes, I keep getting it in a 64 length array.
I've got it functional now by using bytes!, then try_into() to get to
let hash_as_bytes_n: BytesN<32> = bytes!(&e, 0x01b4b211108994a6cd3eb2530b294b237303005453c0f4a4233923147a6ecb97).try_into().expect("bytes to have length 32"); So I've made some progress and have a lot to learn 🙂
Thanks for the input!

twin aurora
#

With BytesN::from_array I can't get it to take my hash as 32 bytes,
that's a bit concerning... interestingly, bytes! macro uses Bytes::from_array behind the scenes

smoky ether
#

90+% chance of user error instead of concerning thing s

twin aurora
#

macro solution is not any worse than from_array btw - it's just that you can't generate the array contents dynamically