Blockchain Commons Known Values TypeScript Library - v1.0.0-alpha.13
    Preparing search index...

    Class KnownValue

    Implements

    • CborTaggedEncodable
    • CborTaggedDecodable<KnownValue>
    • DigestProvider
    Index

    Constructors

    • Creates a new KnownValue with the given numeric value and optional name.

      Parameters

      • value: KnownValueInput

        The numeric value (number or bigint). Numbers are converted to bigint internally.

      • OptionalassignedName: string

        Optional human-readable name for the value

      Returns KnownValue

      const knownValue = new KnownValue(42);
      console.log(knownValue.value()); // 42

      const namedValue = new KnownValue(1, 'isA');
      console.log(namedValue.name()); // "isA"

      // Using bigint for large values
      const largeValue = new KnownValue(9007199254740993n);

    Methods

    • Returns the numeric value of the KnownValue.

      This is the raw unsigned integer that identifies the concept. Returns a number for backward compatibility. For values > MAX_SAFE_INTEGER, use valueBigInt().

      Returns number

      import { IS_A, NOTE } from '@bcts/known-values';
      console.log(IS_A.value()); // 1
      console.log(NOTE.value()); // 4
    • Returns the numeric value of the KnownValue as a bigint.

      Use this for values that may exceed Number.MAX_SAFE_INTEGER (2^53-1).

      Returns bigint

      const largeValue = new KnownValue(9007199254740993n);
      console.log(largeValue.valueBigInt()); // 9007199254740993n
    • Returns the assigned name of the KnownValue, if one exists.

      Returns string | undefined

      const namedValue = new KnownValue(1, 'isA');
      console.log(namedValue.assignedName()); // "isA"

      const unnamedValue = new KnownValue(42);
      console.log(unnamedValue.assignedName()); // undefined
    • Returns a human-readable name for the KnownValue.

      If the KnownValue has an assigned name, that name is returned. Otherwise, the string representation of the numeric value is returned.

      Returns string

      const namedValue = new KnownValue(1, 'isA');
      console.log(namedValue.name()); // "isA"

      const unnamedValue = new KnownValue(42);
      console.log(unnamedValue.name()); // "42"
    • Compares this KnownValue with another for equality. Equality is based solely on the numeric value, ignoring the name.

      Parameters

      Returns boolean

      true if the values are equal

      const kv1 = new KnownValue(1, 'isA');
      const kv2 = new KnownValue(1, 'different');
      console.log(kv1.equals(kv2)); // true (same value, different name)
    • Hash code based on the numeric value. Useful for using KnownValue in hash-based collections.

      Returns number

    • Returns the cryptographic digest of this KnownValue.

      The digest is computed from the tagged CBOR encoding of the value, providing a unique content-addressable identifier.

      This is used for Envelope integration where KnownValues are hashed for tree construction.

      Returns Digest

      A Digest of the tagged CBOR encoding

      const kv = new KnownValue(1, "isA");
      const digest = kv.digest();
      console.log(digest.hex()); // SHA-256 hash of the CBOR encoding
    • String representation of the KnownValue.

      If a name is assigned, the name is displayed. Otherwise, the numeric value is displayed.

      Returns string

    • Returns the CBOR tags associated with KnownValue.

      The primary tag is TAG_KNOWN_VALUE (201).

      Returns Tag[]

      Array containing the KnownValue tag

    • Returns the untagged CBOR encoding of this KnownValue.

      The untagged representation is simply the unsigned integer value.

      Returns Cbor

      CBOR representation of the value (unsigned integer)

    • Returns the tagged CBOR encoding of this KnownValue.

      This wraps the unsigned integer value with tag 201.

      Returns Cbor

      Tagged CBOR representation

      const kv = new KnownValue(1, 'isA');
      const tagged = kv.taggedCbor();
      console.log(tagged.toHex()); // "d8c901" (tag 201, value 1)
    • Returns the tagged CBOR encoding as binary data.

      Returns Uint8Array

      Binary CBOR representation

      const kv = new KnownValue(1, 'isA');
      const bytes = kv.toCborData();
      // bytes is Uint8Array containing the CBOR encoding
    • Alias for toCborData() to match the dcbor interface.

      Returns Uint8Array

    • Creates a KnownValue from untagged CBOR (an unsigned integer). Instance method for interface compliance.

      Parameters

      • cborValue: Cbor

        The CBOR value (must be an unsigned integer)

      Returns KnownValue

      A new KnownValue

      If the CBOR is not an unsigned integer

    • Creates a KnownValue from tagged CBOR (tag 201). Instance method for interface compliance.

      Parameters

      • cborValue: Cbor

        The tagged CBOR value

      Returns KnownValue

      A new KnownValue

      If the CBOR is not properly tagged or contains invalid data

    • Creates a KnownValue from untagged CBOR (an unsigned integer).

      Parameters

      • cborValue: Cbor

        The CBOR value (must be an unsigned integer)

      Returns KnownValue

      A new KnownValue

      If the CBOR is not an unsigned integer

      const cborValue = cbor(42);
      const kv = KnownValue.fromUntaggedCbor(cborValue);
      console.log(kv.value()); // 42
    • Creates a KnownValue from tagged CBOR (tag 201).

      Parameters

      • cborValue: Cbor

        The tagged CBOR value

      Returns KnownValue

      A new KnownValue

      If the CBOR is not properly tagged or contains invalid data

      const kv = KnownValue.fromTaggedCbor(taggedCborValue);
      
    • Creates a KnownValue from binary CBOR data.

      Parameters

      • data: Uint8Array

        Binary CBOR data (must be a tagged KnownValue)

      Returns KnownValue

      A new KnownValue

      If the data cannot be decoded or is not a valid KnownValue

      const bytes = new Uint8Array([0xd8, 0xc9, 0x01]); // tag 201, value 1
      const kv = KnownValue.fromCborData(bytes);
      console.log(kv.value()); // 1
    • Creates a KnownValue from a CBOR value, automatically detecting whether it's tagged or untagged.

      Parameters

      • cborValue: Cbor

        The CBOR value (tagged or untagged)

      Returns KnownValue

      A new KnownValue

      If the CBOR cannot be converted to a KnownValue

      // Works with both tagged and untagged
      const kv1 = KnownValue.fromCbor(cbor(42));
      const kv2 = KnownValue.fromCbor(taggedCborValue);