@aptos-labs/ts-sdk - v7.1.0
    Preparing search index...

    Class Secp256k1PrivateKey

    Represents a Secp256k1 ECDSA private key, providing functionality to create, sign messages, derive public keys, and serialize/deserialize the key.

    Hierarchy (View Summary)

    Implements

    Index

    Implementation - BCS

    • Serializes a Serializable value to its BCS representation. This function is the TypeScript SDK equivalent of bcs::to_bytes in Move.

      Returns Uint8Array

      the BCS representation of the Serializable instance as a byte buffer.

    • Converts the BCS-serialized bytes of a value into a Hex instance. This function provides a Hex representation of the BCS-serialized data for easier handling and manipulation.

      Returns Hex

      A Hex instance with the BCS-serialized bytes loaded into its underlying Uint8Array.

    Implementation - Serialization

    LENGTH: number = 32

    Length of Secp256k1 ecdsa private key

    • Overwrites the underlying private-key byte buffer with random bytes and then zeros. After calling this method the key can no longer sign or derive a public key.

      SECURITY: This is a best-effort window-narrowing tool, NOT a true zeroization guarantee. In JavaScript, four classes of copies cannot be reached from user code and so survive clear():

      1. JS string copies. Any value previously produced by toString(), toHexString(), or bcsToHex().toString() is an immutable string in the heap. The language provides no API to overwrite string memory; it is reclaimed only when GC collects it.
      2. noble-curves internals. The sign path inside @noble/curves expands the private key into scalar BigInt field elements, which are also immutable. Even if noble explicitly zeroed its own byte copies after use, the BigInt intermediates persist.
      3. JIT register / stack residue. The engine may have held key bytes in CPU registers or on the engine stack during a sign call. There is no JS-visible way to scrub those.
      4. GC-relocated copies. Generational GCs (V8, JSC) copy live objects between heap regions during minor/major collections. The Uint8Array we zeroed may have stale copies sitting in survivor space until the next cycle reclaims them.

      This method zeros the SDK's own Uint8Array (the most reachable copy), but downstream consumers should treat it as a hardening signal, not a guarantee. If you need real key-material hygiene, prefer a WASM-backed secp256k1 implementation that exposes explicit memzero, or hardware-backed keys (HSM / TPM / secure enclave). Note that crypto.subtle does NOT support secp256k1 on any major runtime, so non-extractable WebCrypto keys are not available for this curve.

      To minimize the size of the unreachable-copy set, avoid calling toString() / toHexString() on private keys at all in long-lived processes — the byte form is what gets cleared.

      Returns void

    • Returns whether the private key has been cleared from memory.

      Returns boolean

      true if the key has been cleared, false otherwise

    • Sign the given message with the private key. This function generates a cryptographic signature for the provided message, ensuring the signature is canonical and non-malleable.

      Parameters

      • message: HexInput

        A message in HexInput format to be signed.

      Returns Secp256k1Signature

      Signature - The generated signature for the provided message.

      The polymorphic message: HexInput input is ambiguous — a bare even-length string of hex characters (e.g., "cafe") is signed as the 2 bytes [0xCA, 0xFE], not 4 UTF-8 text bytes. Use signBytes for Uint8Array input or signText for string input; both are unambiguous. See convertSigningMessage for the full legacy rule.

      Error if the private key has been cleared from memory.

    • Sign exactly the bytes of message. The input is interpreted as raw bytes regardless of what they encode. Pair with Secp256k1PublicKey.verifyBytes.

      The message is SHA3-256 hashed before signing (matching the Aptos-side Secp256k1 signing convention), and the produced signature is in canonical low-S form for malleability resistance.

      Parameters

      • message: Uint8Array

        The exact bytes to sign.

      Returns Secp256k1Signature

      The generated signature for the provided bytes.

      Error if the private key has been cleared from memory.

    • Get the private key as a string representation.

      SECURITY: This produces an immutable JS string containing the key material in hex. Strings cannot be zeroed by clear() (see the clear() JSDoc for the four classes of unreachable copies). Avoid calling this method on long-lived Secp256k1PrivateKey instances in processes where memory hygiene matters; prefer toUint8Array(), which returns a clearable Uint8Array.

      Returns string

      string representation of the private key

      Error if the private key has been cleared from memory.

    • Get the private key in bytes (Uint8Array).

      Returns Uint8Array

      Uint8Array representation of the private key

      Error if the private key has been cleared from memory.

    • Derives a private key from a mnemonic seed phrase using a specified BIP44 path.

      Parameters

      • path: string

        The BIP44 path to derive the key from.

      • mnemonics: string

        The mnemonic seed phrase used for key generation.

      Returns Secp256k1PrivateKey

      The generated private key.

      Error if the provided path is not a valid BIP44 path.

    Methods

    • Get the private key as a AIP-80 compliant hex string.

      Read about AIP-80

      SECURITY: Same caveat as toString() — produces an immutable JS string containing the key material; cannot be zeroed by clear().

      Returns string

      AIP-80 compliant string representation of the private key.

      Error if the private key has been cleared from memory.

    • Get the private key as a hex string with the 0x prefix.

      SECURITY: Same caveat as toString() — the returned string is an immutable JS heap allocation that clear() cannot zero.

      Returns string

      string representation of the private key.

      Error if the private key has been cleared from memory.