BC-DCBOR TypeScript Library - v1.0.0-alpha.13
    Preparing search index...

    Interface CborTaggedCodable<T>

    Interface for types that can be both encoded to and decoded from CBOR with a specific tag.

    This interface is automatically implemented for any type that implements both CborTaggedEncodable and CborTaggedDecodable. It serves as a convenience marker to indicate full-tagged CBOR serialization support.

    // Define a Date type
    class Date implements CborTaggedCodable<Date> {
    constructor(public timestamp: number) {}

    cborTags(): Tag[] {
    return [createTag(1, 'date')]; // Standard date tag
    }

    // Implement encoding to tagged CBOR
    untaggedCbor(): Cbor {
    return cbor(this.timestamp);
    }

    taggedCbor(): Cbor {
    const tags = this.cborTags();
    return {
    isCbor: true,
    type: MajorType.Tagged,
    tag: tags[0].value,
    value: this.untaggedCbor()
    };
    }

    // Implement decoding from tagged CBOR
    fromUntaggedCbor(cbor: Cbor): Date {
    const timestamp = cbor.value as number;
    return new Date(timestamp);
    }

    fromTaggedCbor(cbor: Cbor): Date {
    if (cbor.type !== MajorType.Tagged) {
    throw new Error('Wrong type');
    }
    return this.fromUntaggedCbor(cbor.value);
    }
    }

    // The CborTaggedCodable interface is automatically implemented
    // Create a date and demonstrate round-trip conversion
    const original = new Date(1609459200);
    const cborValue = original.taggedCbor();
    const roundtrip = new Date(0).fromTaggedCbor(cborValue);
    assert(original.timestamp === roundtrip.timestamp);
    interface CborTaggedCodable<T> {
        fromUntaggedCbor(cbor: Cbor): T;
        fromTaggedCbor(cbor: Cbor): T;
        fromTaggedCborData?(data: Uint8Array): T;
        fromUntaggedCborData?(data: Uint8Array): T;
        untaggedCbor(): Cbor;
        taggedCbor(): Cbor;
        taggedCborData?(): Uint8Array;
        cborTags(): Tag[];
    }

    Type Parameters

    • T

    Hierarchy (View Summary)

    Index

    Methods

    • Creates an instance of this type by decoding it from untagged CBOR.

      This method defines how to interpret the CBOR content (without considering the tag) and convert it to the implementing type.

      Parameters

      • cbor: Cbor

        Untagged CBOR value

      Returns T

      Decoded instance

      Error if the CBOR value cannot be decoded

    • Creates an instance of this type by decoding it from tagged CBOR.

      This method first verifies that the CBOR value has one of the expected tags (as defined by cborTags()), then delegates to fromUntaggedCbor() to decode the content.

      For backward compatibility, this method accepts any tag from the cborTags() array, not just the first one. This allows new versions of types to still accept data tagged with older/alternative tag values.

      In most cases, you don't need to override this method.

      Parameters

      • cbor: Cbor

        Tagged CBOR value

      Returns T

      Decoded instance

      Error if the CBOR value has the wrong tag or cannot be decoded

    • Creates an instance of this type by decoding it from binary encoded tagged CBOR.

      This is a convenience method that first parses the binary data into a CBOR value, then uses fromTaggedCbor() to decode it.

      Parameters

      • data: Uint8Array

        Binary CBOR data

      Returns T

      Decoded instance

      Error if the data cannot be parsed or decoded

    • Creates an instance of this type by decoding it from binary encoded untagged CBOR.

      This is a convenience method that first parses the binary data into a CBOR value, then uses fromUntaggedCbor() to decode it.

      Parameters

      • data: Uint8Array

        Binary CBOR data

      Returns T

      Decoded instance

      Error if the data cannot be parsed or decoded

    • Returns the tagged CBOR encoding of this instance.

      This method wraps the result of untaggedCbor() with the first tag from cborTags(), which is considered the "preferred" tag for the type.

      Even if a type supports multiple tags for backward-compatible decoding via cborTags(), only the first (preferred) tag is used for encoding. This ensures consistency in newly created data while maintaining the ability to read older formats.

      In most cases, you don't need to override this method.

      Returns Cbor

      Tagged CBOR representation

    • Returns the tagged value in CBOR binary representation.

      This is a convenience method that converts the result of taggedCbor() to binary format.

      Returns Uint8Array

      Binary CBOR representation

    • Returns the CBOR tags associated with this type.

      This method should return an array of tags in order of preference:

      • The first tag in the array is the "preferred" tag and will be used when encoding values of this type via CborTaggedEncodable.taggedCbor().

      • All tags in the array are considered equivalent for decoding. When CborTaggedDecodable.fromTaggedCbor() is called, any tag in this array will be accepted as valid for this type.

      This design enables backward compatibility: you can introduce a new tag (placed first in the array) while still supporting older tags for decoding.

      For standard CBOR tags, you can use predefined tag constants from the tags module, or create custom tags with createTag().

      Returns Tag[]