livery.strongroom.cbor¶
livery.strongroom.cbor ¶
Deterministic CBOR: one value, one encoding, one name.
RFC 8949's core deterministic encoding requirements over a restricted
subset, so that everything the store and the fabric hash has exactly
one byte sequence. encode produces it; decode accepts it and
refuses every other encoding of the same value. The subset, the rules
and the vectors are in the spec beside this package, spec/cbor.md.
Reach for livery.strongroom.cbor.encode and livery.strongroom.cbor.decode.
Classes:
| Name | Description |
|---|---|
CodecError |
A value with no deterministic encoding, or bytes that are not one. |
Functions:
| Name | Description |
|---|---|
decode |
Decode data, refusing anything that is not a deterministic encoding. |
encode |
Encode value deterministically. |
Attributes:
| Name | Type | Description |
|---|---|---|
MAX_DEPTH |
Containers may nest this deep and no deeper. |
|
Value |
TypeAlias
|
A value the codec encodes: the subset, nested the same way. |
Value
module-attribute
¶
Value: TypeAlias = 'bool | int | float | bytes | str | list[Any] | dict[str, Any] | None'
A value the codec encodes: the subset, nested the same way.
CodecError ¶
Bases: ValueError
A value with no deterministic encoding, or bytes that are not one.
The message names the rule: the phrase a vector's refusal quotes.
decode ¶
Decode data, refusing anything that is not a deterministic encoding.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
exactly one encoded item. |
required |
Returns:
| Type | Description |
|---|---|
Value
|
The value. |
Raises:
| Type | Description |
|---|---|
CodecError
|
when the bytes are not the deterministic encoding of a value in the subset: a non-shortest argument, an indefinite length, a tag, a simple value the subset lacks, a float not in its shortest form or a NaN other than the canonical one, a map key that is not text, keys out of order or repeated, text that is not valid UTF-8, nesting past the bound, truncated input, or bytes after the item. |
Source code in packages/strongroom/src/livery/strongroom/cbor/_codec.py
encode ¶
Encode value deterministically.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Value
|
an integer within 64 bits, a float, bytes, text, a list, a dict with text keys, a boolean, or None, nested at most livery.strongroom.cbor.MAX_DEPTH deep. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
The one encoding of value. |
Raises:
| Type | Description |
|---|---|
CodecError
|
when value is outside the subset: an integer past 64 bits, a map key that is not text, text that is not valid UTF-8, a type the subset lacks, or nesting past the bound. |