ipld / serde_ipld_dagjson

IPLD DAG-JSON support for Serde.
Apache License 2.0
3 stars 2 forks source link

Non-canonical encoding #8

Closed wellcaffeinated closed 5 months ago

wellcaffeinated commented 5 months ago

Hey there. I've been using the ipld/dag-cbor library to encode my data.

I just tried using this crate to encode the same data and noticed that it encodes it in the order the struct is declared in.

Here's a test you can run:

#[test]
  fn test_canonical(){
    use serde::{Serialize, Deserialize};
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct First {
      a: u32,
      b: u32,
    }
    #[derive(Serialize, Deserialize, PartialEq, Debug)]
    struct Second {
      b: u32,
      a: u32,
    }

    let first = First { a: 1, b: 2 };
    let second = Second { a: 1, b: 2 };

    let first_bytes = DagCborCodec::encode_to_vec(&first).unwrap();
    let second_bytes = DagCborCodec::encode_to_vec(&second).unwrap();

    assert_eq!(first_bytes, second_bytes);

  }

it fails with:

assertion `left == right` failed
  left: [162, 97, 97, 1, 97, 98, 2]
 right: [162, 97, 98, 2, 97, 97, 1]
wellcaffeinated commented 5 months ago

Sorry wrong crate