Skip to main content

CBORUtilities

CBORUtilities

Solidity library built for decoding CBOR data.

parseField

function parseField(bytes encoding, uint256 cursor) internal view returns (enum CBORSpec.MajorType majorType, uint8 shortCount, uint256 start, uint256 end, uint256 next)

Intelligently parses supported CBOR-encoded types.

NameTypeDescription
encodingbytesthe dynamic bytes array
cursoruint256position where type starts (in bytes)
NameTypeDescription
majorTypeenum CBORSpec.MajorTypethe type of the data sliced
shortCountuint8the corresponding shortCount for the data
startuint256position where the data starts (in bytes)
enduint256position where the data ends (in bytes)
nextuint256position to find the next field (in bytes)

extractValue

function extractValue(bytes encoding, enum CBORSpec.MajorType majorType, uint8 shortCount, uint256 start, uint256 end) internal view returns (bytes value)

Extracts the data from CBOR-encoded type.

NameTypeDescription
encodingbytesthe dynamic bytes array to slice from
majorTypeenum CBORSpec.MajorTypethe correspondnig data type being used
shortCountuint8
startuint256position where type starts (in bytes)
enduint256position where the type ends (in bytes)
NameTypeDescription
valuebytesa cloned dynamic bytes array with the data value

parseFieldEncoding

function parseFieldEncoding(bytes1 fieldEncoding) internal view returns (enum CBORSpec.MajorType majorType, uint8 shortCount)

Parses a CBOR byte into major type and short count. See https://en.wikipedia.org/wiki/CBOR for reference.

NameTypeDescription
fieldEncodingbytes1the field to encode
NameTypeDescription
majorTypeenum CBORSpec.MajorTypecorresponding data type (see RFC8949 section 3.2)
shortCountuint8corresponding short count (see RFC8949 section 3)

scanIndefiniteItems

function scanIndefiniteItems(bytes encoding, uint256 cursor, uint256 maxItems) internal view returns (uint256 totalItems, uint256 endCursor)

If data structures are nested, this will be a recursive function.

Counts encoded items until a BREAK or the end of the bytes.

NameTypeDescription
encodingbytesthe encoded bytes array
cursoruint256where to start scanning
maxItemsuint256once this number of items is reached, return. Set 0 for infinite
NameTypeDescription
totalItemsuint256total items found in encoding
endCursoruint256cursor position after scanning (non-inclusive)