Class BitcoinMessage

Drop-in replacement class for bitcoinjs-message.

Constructors

Methods

  • Computes the "Magic Hash" of a message as defined in the Bitcoin message signing standard.

    The function prefixes the message with specific bytes ("\x18Bitcoin Signed Message:\n") and the variable-length encoded message length, then performs a double SHA-256 hash (hash256) on the result. This specific hashing mechanism prevents the signature from being used as a valid transaction signature on the Bitcoin network.

    Parameters

    • message: string | Buffer

      The input message to be hashed (string or Buffer).

    Returns Buffer

    Buffer A 32-byte Buffer containing the double SHA-256 hash of the prefixed message.

  • Signs a message with full BIP-137 support, compatible with Legacy (P2PKH), Nested Segwit (P2SH-P2WPKH), and Native Segwit (P2WPKH) addresses.

    This method produces a compact signature using the secp256k1 elliptic curve. It implements deterministic signatures (RFC 6979) by default but supports additional entropy via options. The resulting signature includes a specific header byte that encodes the recovery ID and the key/address type, allowing for public key recovery during verification.

    The process involves:

    1. Hashing the message with the Bitcoin magic prefix (double SHA-256).
    2. Signing the hash deterministically (or with extra entropy).
    3. Calculating the recovery ID (0-3) ensuring the public key matches.
    4. Constructing the BIP-137 header byte based on the address type and compression.
    5. Returning the concatenated signature buffer [header + r + s].

    Parameters

    • message: string | Buffer

      The message string or buffer to be signed.

    • privateKey: Buffer

      The 32-byte private key buffer used for signing.

    • compressed: boolean

      Boolean indicating if the corresponding public key is compressed.

    • Optional options: SignOptions

      Optional parameters including 'segwitType' ('p2sh(p2wpkh)' or 'p2wpkh') and 'extraEntropy'.

    Returns Buffer

    A Buffer containing the 65-byte BIP-137 signature.

  • Converts a Bitcoin address string into its corresponding output script buffer, attempting to detect the network automatically.

    Since bitcoinjs-lib enforces strict network validation (defaulting to Mainnet), this helper iterates through Mainnet, Testnet, and Regtest networks to successfully parse the address. This allows the verifier to handle addresses from different networks transparently without requiring explicit network configuration from the caller.

    Parameters

    • address: string

      The Bitcoin address string to convert.

    Returns Buffer

    Buffer | null The output script Buffer if the address is valid on any supported network, or null if invalid.

  • Verifies a signed Bitcoin message against a provided address.

    This method validates signatures adhering to the BIP-137 standard. It supports automatic detection of the address type (Legacy, Nested Segwit, or Native Segwit) and the network (Mainnet, Testnet, Regtest) by analyzing the signature header and the provided address format.

    The verification steps are:

    1. Parse the signature header to determine the recovery ID and expected address type.
    2. Recover the public key from the signature and message hash.
    3. Convert the provided address into a network-agnostic output script.
    4. Derive the expected output script from the recovered public key based on the detected type.
    5. Compare the derived script with the target address script.

    Parameters

    • message: string | Buffer

      The message that was signed (string or Buffer).

    • address: string

      The Bitcoin address (Legacy, Segwit, or Bech32) that supposedly signed the message.

    • signatureBase64: string

      The Base64 encoded signature string.

    Returns boolean

    boolean Returns true if the signature is valid for the given message and address, false otherwise.

    Throws

    Error if unexpected error is encountered (e.g., unexpected object being passed as an message)