I’m reading the Internet Computer docs regarding how to retrieve a Representation-indepent hashing of structured data. I’m a bit unclear on what the second step is saying to do. What does it mean to ‘sort concatenations from low to high’? is it saying to sort the concatenations based on the byte lengths? so the concatenation with the smallest byte length should appear first in the sorting, followed by the concatenation with the next smallest byte length and so on?
It’s saying to sort the items alphabetically in ascending order. In case it’s helpful, you can read an implementation of it here: response-verification/packages/ic-representation-independent-hash/src/representation_independent_hash.rs at main · dfinity/response-verification · GitHub
Ok. just so that i know I’m interpreting you correctly:
if my data object is structured like this:
type content = {
nonce: [Nat8];
ingress_expiry: Nat64;
sender: Principal;
canister_id: Principal;
method_name: Text;
arg: [Nat8];
}
the hashes should be sorted precisely in the following order:
[
H(H("arg") + H([Nat8])),
H(H("canister_id") + H(Principal) ),
H(H("ingress_expiry") + H(Nat64) ),
H(H("method_name") + H(Text)),
H(H("nonce") + H([Nat8])),
H(H("sender") + H(Principal))
];
and then I concatenate all six of the resulting Hashes in this array,
then hash that concatenation,
Motoko here: GitHub - skilesare/RepIndyHash.mo: Representationally Independent Hashes for Value Types
It’s the concatenated hashes that are sorted, not the original values.
And you don’t hash the individual concatenations, but the concatenation of the concatenations (sry😅).
So it would be something like this:
H([
H("arg") + H([Nat8]),
H("canister_id") + H(Principal),
H("ingress_expiry") + H(Nat64),
H("method_name") + H(Text),
H("nonce") + H([Nat8]),
H("sender") + H(Principal)
].sort().concat());
this is way neater than what i came up with. I’m gonna use it to clean up the code I wrote.
@NathanosDev. Where I’m getting confused is when it comes to what it means to “sort()
” the concatenated hashes alphabetically. Does that mean I take the Text
or String
representation of the concatenations of the hashes and order them alphabetically? I’ve been introduced to a lot of binary protocols over the last couple of days. I’m not sure if there is some bitwise definition of sorting alphabetically that I’m missing.
Feel free to add some tests. The more tests we have the more sure we can be the implementation is correct.
Hi @Jesse, It is the sorted bits. As in 0s and 1s. In the Dart agent, I convert the bytes into a bitstring - a string of the bits - for the sort.
@levi thank you for sharing this. In Motoko, would i be able to convert each byte array of the respective concatenations to a Blob
and then use the Blob.greater()
function to sort the concatenations?
@Jesse You’re welcome. Looks like that will do it, Blob.compare
might also be useful. I am not familiar with motoko so there might be some nuances with those functions that I don’t know. There is a sample calculation of the representation-independent-hash for a request-id in the interface-spec which you can use to test your code: https://internetcomputer.org/docs/current/references/ic-interface-spec/#request-id.