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

    Interface CborTaggedDecodable<T>

    Interface for types that can be decoded from CBOR with a specific tag.

    This interface extends CborTagged to provide methods for decoding tagged CBOR data into TypeScript types. It handles verification that the CBOR data has the expected tag(s) and provides utilities for both tagged and untagged decoding.

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

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

    fromUntaggedCbor(cbor: Cbor): Date {
    // Convert the untagged CBOR to a number
    if (cbor.type !== MajorType.Unsigned) {
    throw new Error('Wrong type');
    }
    const timestamp = typeof cbor.value === 'bigint' ? Number(cbor.value) : cbor.value;
    return new Date(timestamp);
    }

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

    const tags = this.cborTags();
    const tagValues = tags.map(t => t.value);
    if (!tagValues.includes(cbor.tag as number)) {
    throw new Error(`Wrong tag: expected ${tagValues[0]}, got ${cbor.tag}`);
    }

    return this.fromUntaggedCbor(cbor.value);
    }

    static fromTaggedCborData(data: Uint8Array): Date {
    const cbor = decodeCbor(data);
    return new Date(0).fromTaggedCbor(cbor);
    }

    static fromUntaggedCborData(data: Uint8Array): Date {
    const cbor = decodeCbor(data);
    return new Date(0).fromUntaggedCbor(cbor);
    }
    }

    // Create tagged CBOR data
    const taggedCbor = {
    isCbor: true,
    type: MajorType.Tagged,
    tag: 1,
    value: cbor(1609459200)
    };

    // Decode using the interface
    const date = new Date(0).fromTaggedCbor(taggedCbor);
    assert(date.timestamp === 1609459200);
    interface CborTaggedDecodable<T> {
        fromUntaggedCbor(cbor: Cbor): T;
        fromTaggedCbor(cbor: Cbor): T;
        fromTaggedCborData?(data: Uint8Array): T;
        fromUntaggedCborData?(data: Uint8Array): T;
        cborTags(): Tag[];
    }

    Type Parameters

    • T

    Hierarchy (View Summary)

    Implemented by

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