The Certificate class provides functionality to create, sign, and verify certificates. It implements the CertificateData interface.

The properties of those class are immutable, except signature, because properties are part of the signature computation. The signature is used of extract and match the signer. The fact the properties are immutable assure is not possible to create an object tampering properties and carry on the legitimate signature and signer address of the object before tampering to make tampered content to result in a validated certificate.

CertificateData

Implements

Constructors

  • Returns a new instance of this class assuring the formal validity of the arguments used to build the object.

    Parameters

    • purpose: string

      The purpose of the certificate.

    • payload: {
          content: string;
          type: string;
      }

      The payload containing type and content.

      • content: string

        The content of the payload.

      • type: string

        The type of the payload.

    • domain: string

      The domain associated with the certificate.

    • timestamp: number

      The time at which the certificate is created; must be a positive safe integer.

    • signer: string

      The signer of the certificate; must be a valid address.

    • Optionalsignature: string

      The signature of the certificate; optional parameter.

    Returns Certificate

    If timestamp is not a positive safe integer.

    If signer is not a valid address.

    If signature is invalid.

    The signer address is represented lowercase and 0x prefixed.

Properties

domain: string

Return the description of the context of validity of this certificate.

payload: {
    content: string;
    type: string;
}

Returns the content of the certificate.

Type declaration

  • Readonlycontent: string

    Return the content serialized as a string.

  • Readonlytype: string

    Return the description of the type of content.

purpose: string

Return the intended use or context of the certificate.

signature?: string

Return the signature computed evaluating the properties of this object and the private key of the signer.

The signature is a lowercase hexadecimal expression prefixed with 0x.

signer: string

Return the address of the entity signed the certificate, as a lowercase hexadecimal expression prefixed by 0x.

Normalized lowercase prefixed expression is needed because the content of this property is part of the {@signature } computation: certificates made from checksum case address of the signer should result valid as the certificate made from the same signer address not checksum case.

timestamp: number

The value expressed as of milliseconds elapsed since the epoch, when the certificate was issued.

The value is a natural number in the safe integer range of JS number type.

Methods

  • Encodes the current certificate instance into a Uint8Array representation.

    Returns Uint8Array

    The encoded Uint8Array representation of the current certificate instance.

    This method normalizes the content by:

    • Sorting the properties in ascending alphabetic order.
    • Delimiting key/value properties with " when serialized as JSON before encoding as bytes.
    • Ignoring any not meaningful blank characters.
    • Using the UTF-8 normalization form for canonical composition for byte encoding.
  • Signs the current object using a given private key.

    The signature is computed encoding this object according the following normalization rules:

    • the signature property is ignored, because its value is the result of this method.
    • the properties are sorted in ascending alphabetic order;
    • the key/value properties are delimited with " when serialized as JSON before to be encoded as bytes;
    • any not meaningful blank characters are ignored;
    • the JSON representation of this object is byte encoded using the UTF-8 normalization form for canonical composition.

    Parameters

    • privateKey: Uint8Array

      The private key used for signing.

    Returns this

    The current instance after signing.

    • If a hash error occurs.
    • If the private key is not a valid 32-byte private key.

    Security auditable method, depends on

    • encode
    • verify
  • Encodes a given object into a Uint8Array representation applying the following operation to normalize the content:

    • the properties are sorted in ascending alphabetic order;
    • the key/value properties are delimited with " when serialized as JSON before to be encoded as bytes;
    • any not meaningful blank characters are ignored;
    • the JSON representation of this object is byte encoded using the UTF-8 normalization form for canonical composition.

    Parameters

    • object: unknown

      The input object to be encoded.

    Returns Uint8Array

    The encoded Uint8Array representation of the input object.