HashMap should remember key order #66

Open
opened 2025-09-06 10:48:06 +00:00 by doesnm · 1 comment

While trying make one thing with HashMap and Json i noticed that HashMap are not alphabetely sorted, but it's required to signing json
With this code:

HashMap *h;
    HashMap *old_verify_keys;
    HashMap *signatures;
    HashMap *verify_keys;
    Stream *s;
    s = StreamStdout();
    h = HashMapCreate();
    old_verify_keys = HashMapCreate();
    signatures = HashMapCreate();
    verify_keys = HashMapCreate();
    HashMapSet(h,"old_verify_keys",JsonValueObject(old_verify_keys));
    HashMapSet(h,"server_name",JsonValueString("doesnm.cc"));
    HashMapSet(h,"signatures",JsonValueObject(signatures));
    HashMapSet(h,"valid_until_ts",JsonValueInteger(0));
    HashMapSet(h,"verify_keys",JsonValueObject(verify_keys));
    JsonEncode(h,s,JSON_DEFAULT);
    HashMapFree(old_verify_keys);
    HashMapFree(signatures);
    HashMapFree(verify_keys);
    HashMapFree(h);
    Free(public_key_b64);

I have this output

{
   "verify_keys": {
   },
   "old_verify_keys": {
   },
   "server_name": "doesnm.cc",
   "signatures": {
   },
   "valid_until_ts": 0

Position of keys are not same as code

While trying make one thing with HashMap and Json i noticed that HashMap are not alphabetely sorted, but it's required to [signing json](https://spec.matrix.org/v1.15/appendices/#signing-json) With this code: ``` HashMap *h; HashMap *old_verify_keys; HashMap *signatures; HashMap *verify_keys; Stream *s; s = StreamStdout(); h = HashMapCreate(); old_verify_keys = HashMapCreate(); signatures = HashMapCreate(); verify_keys = HashMapCreate(); HashMapSet(h,"old_verify_keys",JsonValueObject(old_verify_keys)); HashMapSet(h,"server_name",JsonValueString("doesnm.cc")); HashMapSet(h,"signatures",JsonValueObject(signatures)); HashMapSet(h,"valid_until_ts",JsonValueInteger(0)); HashMapSet(h,"verify_keys",JsonValueObject(verify_keys)); JsonEncode(h,s,JSON_DEFAULT); HashMapFree(old_verify_keys); HashMapFree(signatures); HashMapFree(verify_keys); HashMapFree(h); Free(public_key_b64); ``` I have this output ``` { "verify_keys": { }, "old_verify_keys": { }, "server_name": "doesnm.cc", "signatures": { }, "valid_until_ts": 0 ``` Position of keys are not same as code
Owner

Note that for the purposes of signing JSON in compliance with the Matrix specification, CanonicalJsonEncode() shall be used. If there is an implementation error in CanonicalJsonEncode(), please open an issue on the Telodendria repository.

I'll keep this issue open because it may be a nice feature to have in Cytoplasm, but this issue should already be solved in Telodendria.

Note that for the purposes of signing JSON in compliance with the Matrix specification, [`CanonicalJsonEncode()`](https://git.telodendria.org/Telodendria/Telodendria/src/branch/master/src/CanonicalJson.c#L91) shall be used. If there is an implementation error in `CanonicalJsonEncode()`, please open an issue on the Telodendria repository. I'll keep this issue open because it may be a nice feature to have in Cytoplasm, but this issue should already be solved in Telodendria.
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: Telodendria/Cytoplasm#66
No description provided.