The Secp256k1 class provides cryptographic utilities for the SECP256K1 elliptic curve, including compressing and inflating public keys, generating private keys, and validating message hashes and private keys.

Constructors

Properties

SIGNATURE_LENGTH = 65

Represents the fixed length of the cryptographic signature. The value is set to 65, which is the size in bytes required for a 520-bit signature.

SIGNATURE_LENGTH

Methods

  • Compresses an uncompressed public key.

    Parameters

    • publicKey: Uint8Array

      The uncompressed public key to be compressed.

    Returns Uint8Array

    • The compressed public key.

    Secp256k1.inflatePublicKey

  • Derives the public key from a given private key.

    Parameters

    • privateKey: Uint8Array

      The private key in Uint8Array format. Must be a valid 32-byte secp256k1 private key.

    • OptionalisCompressed: boolean = true

      Indicates whether the derived public key should be in compressed format.

    Returns Uint8Array

    The derived public key in Uint8Array format.

    Throws an error if the provided private key is not valid.

    Security auditable method, depends on

  • Generates a new Secp256k1 private key using a secure random number generator.

    Returns Promise<Uint8Array>

    A promise that resolves to a Uint8Array representing the generated private key. This encoded private key is suitable for cryptographic operations.

    Throws an error if private key generation fails if a secure random number generator is not provided by the hosting operating system.

    Security auditable method, depends on

  • Checks whether the provided hash is a valid message hash.

    Parameters

    • hash: Uint8Array

      The hash to be validated.

    Returns boolean

    true if the hash is 32 bytes long, otherwise false.

  • Generates a random sequence of bytes. If an error occurs during generation using nc_secp256k1, {@link global.crypto is used as fall back togenerate the random sequence.

    Parameters

    • OptionalbytesLength: number = 32

      Optional. The number of random bytes to generate, 32 by default.

    Returns Uint8Array

    • A Uint8Array containing the random bytes.

    Security auditable method, depends on

  • Recovers the public key associated with the message hash from the given signature.

    Parameters

    • messageHash: Uint8Array

      The 32-byte message hash to be verified.

    • sig: Uint8Array

      The 65-byte signature used for recovery, consisting of the compact signature and recovery byte.

    Returns Uint8Array

    The recovered public key in its raw bytes form.

    If the provided message hash is invalid.

    If the provided signature is not 65 bytes or contains an invalid recovery value.

    Security auditable method, depends on

    Secp256k1.isValidMessageHash

  • Signs a given message hash using the provided private key.

    Parameters

    • messageHash: Uint8Array

      A 32-byte message hash that needs to be signed.

    • privateKey: Uint8Array

      A 32-byte private key used for signing the message hash.

    Returns Uint8Array

    The signature of the message hash consisting of the r, s, and recovery values.

    InvalidSecp256k1MessageHash if the message hash is not a valid 32-byte hash.

    InvalidSecp256k1PrivateKey if the private key is not a valid 32-byte private key.

    Security auditable method, depends on

    • Secp256k1.isValidMessageHash
    • Secp256k1.isValidPrivateKey