Skip to content

Better support for lists #224

@theeldermillenial

Description

@theeldermillenial

Things to check first

  • I have searched the existing issues and didn't find my feature already requested there

Feature description

The decoding mechanism doesn't have a way of differentiating between an indefinite array and a fixed length array such that a round trip decode and encode could be performed without loss of information.

Ideally fixed length arrays are given a different class type than variable length arrays. For example, a fixed length array may be decoded as a tuple or deque while an indefinite array could be assigned the type list.

Use case

In pycardano, cbor is hashed. If an array of length two is defined as an indefinite array (/x9f) rather than an array of length 2, decoding then encoding yields a different cbor result, which gives a different hash. This is problematic when verifying cbor contents.

Since cbor2 does not distinguish between fixed/indefinite arrays, pycardano creates a custom encoder that is used to create an indefinite array when requested (even if the array is smaller than 30 values). However, there is no analogous functionality with decoding, and trying to subclass CBORDecoder will not be straightforward.

Thus, the ideal implementation would be to differentiate between these two encodings by using different classes. One mechanism could be as described, or alternatively dummy list classes could be created to distinguish between the two (e.g. FixedArray and IndefiniteArray, both of which are just lists).

I am happy to implement this in any way @agronholm or any other maintainer would like. Just point me in the right direction. The goal is round trip reproduction of cbor regardless of how the array is encoded.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions