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

    Interface CborTaggedEncodable

    Interface for types that can be encoded to CBOR with a specific tag.

    This interface extends CborTagged to provide methods for encoding a value with its associated tag. Types that implement this interface define how they should be represented in CBOR format, both with and without their tag.

    // Define a Date type
    class Date implements CborTaggedEncodable {
    constructor(private timestamp: number) {}

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

    untaggedCbor(): Cbor {
    // Date content is represented as a number
    return cbor(this.timestamp);
    }

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

    taggedCborData(): Uint8Array {
    return cborData(this.taggedCbor());
    }
    }

    // Create a date and encode it
    const date = new Date(1609459200);

    // Get the untagged CBOR (just the timestamp)
    const untagged = date.untaggedCbor();

    // Get the tagged CBOR (with tag 1)
    const tagged = date.taggedCbor();

    // Get binary representation
    const data = date.taggedCborData();
    interface CborTaggedEncodable {
        untaggedCbor(): Cbor;
        taggedCbor(): Cbor;
        taggedCborData?(): Uint8Array;
        cborTags(): Tag[];
    }

    Hierarchy (View Summary)

    Implemented by

    Index

    Methods

    • Returns the untagged CBOR encoding of this instance.

      This method defines how the value itself (without its tag) should be represented in CBOR format.

      Returns Cbor

      Untagged CBOR representation

    • 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[]