hildjj / node-cbor

Encode and decode CBOR documents, with both easy mode, streaming mode, and SAX-style evented mode.
MIT License
358 stars 73 forks source link

Ensure maps are sorted #26

Closed dignifiedquire closed 7 years ago

dignifiedquire commented 7 years ago

According to https://tools.ietf.org/html/rfc7049#section-3.9

The keys in every map must be sorted lowest value to highest. Sorting is performed on the bytes of the representation of the key data items without paying attention to the 3/5 bit splitting for major types. (Note that this rule allows maps that have keys of different types, even though that is probably a bad practice that could lead to errors in some canonicalization implementations.) The sorting rules are:

  *  If two keys have different lengths, the shorter one sorts
     earlier;

  *  If two keys have the same length, the one with the lower value
     in (byte-wise) lexical order sorts earlier.

all keys in maps need to be sorted. Looking at https://github.com/hildjj/node-cbor/blob/master/src/encoder.coffee#L238-L260 it does not look like this is currently happening.

Ref https://github.com/ipld/js-ipld/issues/19

hildjj commented 7 years ago

Add support for canonical ordering on serialization. Waiting on #25, which is getting close.

hildjj commented 7 years ago

Just released version 3.0.0, contains canonicalization, but you have to turn it on.