Sha256 digest string in json/schema

Hi,

I have a param struct that has a property that is a sha256 digest. I copied this code from CIS2:

/// Sha256 digest
pub type Sha256 = [u8; 32];

and then the property in my schema struct is:

hash: Option,

How can I represent this hash property in my parameter json if let’s say the hash string is below?

323633923f068c3dcabecc98b525aff88134265bcd5f998281099d2f3818853d

“hash”: { “Some”: [?] }. What should be in the “?”, can it be a string? So far, I’m only allowed to put in an array that is of course [u8;32]. I wonder if there is a way for me to represent the hash as string in the json file and then just put in some custom deserialise code somewhere for this struct? Any hints on how the deserialise code will look like?

Are you using the latest version of cargo-concordium?

I believe so. I built it from source about a week ago.

Thanks.

So the SchemaType for [u8; 32] means you have to supply it as a list of 32 bytes.

However if instead you use the type HashSha2256 defined in concordium-std then you should be able to supply it as a hex string.

ah great! I will use HashSha256 then. Thanks abizjak!

Replied too soon. Any reason though why the Cics2 code example code uses the

type Sha256Digest = [u8; 32];

for the hash content of the struct MetadataUrl in cics2 lib code here?

I think it’s only legacy reasons. At the time it was written we did not have hex in schemas, and we did not update it when we added them.

It would be better to use the sha type. Would you contribute a PR to concordium-rust-smart-contracts that changes this?

That explains it. :slight_smile: Thanks. I would love to contribute. I will create PR soon for this.

@abizjak I’ve just deployed the change and tested and got below error. Any hint as to what’s causing the schema error? My unit test within the contract seems to work with a hash.

Error: Could not decode parameters from file ‘mint.json’ as JSON:
Expected length is 536870912, but actual length is 32.
In “323633923f068c3dcabecc98b525aff88134265bcd5f998281099d2f3818853d”.
In [
“323633923f068c3dcabecc98b525aff88134265bcd5f998281099d2f3818853d”,
“07b2575ee81aba42c97a9089ca99cd1a52b67ea2e8dea8253088f2a01a7fa750”
].
In field ‘tokens’.
In {
“batch_meta”: {
“None”:
},
“number”: 1,
“tokens”: [
“323633923f068c3dcabecc98b525aff88134265bcd5f998281099d2f3818853d”,
“07b2575ee81aba42c97a9089ca99cd1a52b67ea2e8dea8253088f2a01a7fa750”
]
}.

This is the json input parameter:

{
“number”: 1,
“batch_meta”: { “None”: },
“tokens”: [“323633923f068c3dcabecc98b525aff88134265bcd5f998281099d2f3818853d”, “07b2575ee81aba42c97a9089ca99cd1a52b67ea2e8dea8253088f2a01a7fa750”]
}

Can you show the relevant part of the contract?

Hi @abizjak,
I DM’ed you. Thanks.
Br shaun

Following the discussion in private we discovered there is a bug in concordium-client.

This PR fixes it https://github.com/Concordium/concordium-client/pull/165

We expect to release a new version early next week. In the meantime you can build your own version and you should be able to interact with your contract.

1 Like

Thanks for the help @abizjak

1 Like