The Semaphore identity is essentially an EdDSA public/private key pair. The EdDSA implementation in this library uses Baby Jubjub for public key generation and Poseidon for signatures. In addition, the commitment, i.e. the hash of the public key, is used to represent Semaphore identities in groups, adding an additional layer of privacy and security. The private key of the identity can be exported as a base64 string.

Constructors

  • Initializes the class attributes based on a given private key, which must be text or a buffer. If the private key is not passed as a parameter, a random private key will be generated. The EdDSAPoseidon class is used to generate the secret scalar and the public key. Additionally, the constructor computes a commitment of the public key using a hash function (Poseidon).

    Parameters

    • Optional privateKey: string | Buffer | Uint8Array

      The private key used to derive the public key (hexadecimal or string).

    Returns Identity

    Example

    // Generates an identity.
    const { privateKey, publicKey, commitment } = new Identity("private-key")

    Example

    // Generates an identity with a random private key.
    const { privateKey, publicKey, commitment } = new Identity()

Properties

_commitment: bigint
_privateKey: string | Buffer | Uint8Array
_publicKey: Point<bigint>
_secretScalar: bigint

Accessors

  • get commitment(): bigint
  • Returns the commitment hash of the public key.

    Returns bigint

    The commitment as a string.

  • get privateKey(): string | Buffer | Uint8Array
  • Returns the private key.

    Returns string | Buffer | Uint8Array

    The private key as a buffer or text.

  • get publicKey(): Point<bigint>
  • Returns the public key as a Baby Jubjub Point.

    Returns Point<bigint>

    The public key as a point.

  • get secretScalar(): bigint
  • Returns the secret scalar.

    Returns bigint

    The secret scalar as a string.

Methods

  • Returns the private key encoded as a base64 string.

    Returns string

    The private key as a base64 string.

  • Generates a signature for a given message using the private key. This method demonstrates how to sign a message and could be used for authentication or data integrity.

    Parameters

    • message: BigNumberish

      The message to be signed.

    Returns Signature<bigint>

    A Signature object containing the signature components.

    Example

    const identity = new Identity()
    const signature = identity.signMessage("message")
  • Returns a Semaphore identity based on a private key encoded as a base64 string. The private key will be converted to a buffer, regardless of its original type.

    Parameters

    • privateKey: string

      The private key as a base64 string.

    Returns Identity

    The Semaphore identity.

  • Verifies a signature against a given message and public key. This static method allows for the verification of signatures without needing an instance of the Identity class. It's useful for cases where you only have the public key, the message and a signature, and need to verify if they match.

    Parameters

    • message: BigNumberish

      The message that was signed.

    • signature: Signature

      The signature to verify.

    • publicKey: Point

      The public key to use for verification.

    Returns boolean

    A boolean indicating whether the signature is valid.

    Example

    const identity = new Identity()
    const signature = identity.signMessage("message")
    Identity.verifySignature("message", signature, identity.publicKey)